This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:projects:cpp_demo [2010/02/25 05:53] – mcooper6 | notes:projects:cpp_demo [2010/03/22 13:28] (current) – mcooper6 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~DISCUSSION|C++ Demo Code Discussion~~ | ||
+ | |||
+ | This is the home of the **C++ Demonstration Code**. | ||
+ | =====Version 1: if...else ===== | ||
+ | <WRAP download round 20%> | ||
+ | [[http:// | ||
+ | </ | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | * | ||
+ | *converter.cc | ||
+ | * | ||
+ | */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | // A menu driven program that allows the user to choose an option | ||
+ | // and then performs a task based on the option chosen | ||
+ | |||
+ | int main(){ | ||
+ | |||
+ | int option(0); | ||
+ | do{ | ||
+ | |||
+ | cout << " | ||
+ | cout << " 1. Fahrenheit to Celcius" | ||
+ | cout << " 2. Celcius to Fahrenheit" | ||
+ | cout << " 3. Inches to centimeter" | ||
+ | cout << " 4. Centimeters to inches" | ||
+ | cout << " 5. Pounds to kilograms" | ||
+ | cout << " 6. Kilograms to pounds" | ||
+ | cout << " 0. Exit" << " | ||
+ | |||
+ | cout << " | ||
+ | cout << " Choose an option: "; | ||
+ | cin >> option; | ||
+ | |||
+ | cout << " | ||
+ | if (option == 1){ | ||
+ | |||
+ | //100c is equal to 212f AND | ||
+ | //0 c is equal to 32f THEREFORE | ||
+ | //the ratio of fahrenheit to celcius is (212-32)/ | ||
+ | // | ||
+ | // | ||
+ | //since we're after the modifier 1.81 AND NOT 1 => use 9.0/5.0 | ||
+ | |||
+ | double f(0); //temp in Fahrenheit | ||
+ | double c(0); //temp in Celcius | ||
+ | |||
+ | //Get fahrenheit from the user | ||
+ | cout << "Enter the temerature in Fahrenheit: "; | ||
+ | cin >> f; | ||
+ | |||
+ | c=(5.0/ | ||
+ | |||
+ | cout << | ||
+ | }else if (option == 2){ | ||
+ | |||
+ | //100c is equal to 212f AND | ||
+ | //0 c is equal to 32f THEREFORE | ||
+ | //the ratio of fahrenheit to celcius is (212-32)/ | ||
+ | // | ||
+ | // | ||
+ | //since we're after the modifier 1.81 AND NOT 1 => use 9.0/5.0 | ||
+ | |||
+ | double f(0); | ||
+ | double c(0); | ||
+ | |||
+ | //get celcius from the user | ||
+ | cout << "Enter a temperature in Celcius: "; | ||
+ | cin >> c; | ||
+ | |||
+ | f=(9.0/ | ||
+ | |||
+ | cout << c << " degrees celcius is " << fixed << showpoint << setprecision(1) << f << " degrees fahrenheit.\n\n"; | ||
+ | }else if (option == 3){ | ||
+ | // inches to centimeters | ||
+ | const double IN_CONV = 2.54; | ||
+ | int inches(0); | ||
+ | cout << " | ||
+ | cin >> inches; | ||
+ | |||
+ | double cent(inches*IN_CONV); | ||
+ | |||
+ | cout << inches << " inches | ||
+ | |||
+ | }else if (option == 4){ | ||
+ | // | ||
+ | const double IN_CONV = 2.54; | ||
+ | double cent(0); | ||
+ | cout << " | ||
+ | cin >> cent; | ||
+ | |||
+ | double inches(cent/ | ||
+ | |||
+ | cout << cent << " centimeters | ||
+ | }else if (option == 5){ | ||
+ | // pounds to kilos | ||
+ | double p(0); //pounds | ||
+ | double k(0); //kilos | ||
+ | cout << " Please enter a weight in pounds: "; | ||
+ | cin >> p; | ||
+ | k = p/2.2; | ||
+ | |||
+ | cout << p << " pounds is equal to " << setprecision(2) << k << " kilograms.\n\n"; | ||
+ | |||
+ | }else if (option == 6){ | ||
+ | // kilos to pounds | ||
+ | double p(0); //pounds | ||
+ | double k(0); //kilos | ||
+ | cout << " Please enter a weight in kilograms: "; | ||
+ | cin >> k; | ||
+ | p = k*2.2; | ||
+ | |||
+ | cout << k << " kilograms is equal to " << setprecision(2) << p << " pounds.\n\n"; | ||
+ | } | ||
+ | |||
+ | }while (option> | ||
+ | |||
+ | cout << " | ||
+ | } | ||
+ | </ | ||
+ | ===== Version 2: Modular ===== | ||
+ | this modularizes the program but as long as the files are in the same directory and you use "" | ||
+ | <WRAP download round 20%> | ||
+ | [[http:// | ||
+ | </ | ||
+ | <code c> | ||
+ | /* | ||
+ | *menu.H - menu for conv.cpp file. | ||
+ | * | ||
+ | * use by: # | ||
+ | * call by: menu(); | ||
+ | */ | ||
+ | |||
+ | # | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | void menu() | ||
+ | { | ||
+ | cout << " 1. Fahrenheit to Celcius" | ||
+ | cout << " 2. Celcius to Fahrenheit" | ||
+ | cout << " 3. Inches to centimeter" | ||
+ | cout << " 4. Centimeters to inches" | ||
+ | cout << " 5. Pounds to kilograms" | ||
+ | cout << " 6. Kilograms to pounds" | ||
+ | cout << " 0. Exit" << " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <WRAP download round 20%> | ||
+ | [[http:// | ||
+ | </ | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | * | ||
+ | * functions.H - functions for the conv.cpp program | ||
+ | * | ||
+ | * use by: include" | ||
+ | * call by: function name below | ||
+ | * | ||
+ | */ | ||
+ | # | ||
+ | # | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | void fahr() | ||
+ | { | ||
+ | double f(0); //temp in Fahrenheit | ||
+ | double c(0); //temp in Celcius | ||
+ | |||
+ | //Get fahrenheit from the user | ||
+ | cout << "Enter the temerature in Fahrenheit: "; | ||
+ | cin >> f; | ||
+ | |||
+ | c=(5.0/ | ||
+ | |||
+ | cout << | ||
+ | setprecision(1) << c << " degrees Celcius.\n"; | ||
+ | } | ||
+ | void celc() | ||
+ | { | ||
+ | double f(0); | ||
+ | double c(0); | ||
+ | |||
+ | //get celcius from the user | ||
+ | cout << "Enter a temperature in Celcius: "; | ||
+ | cin >> c; | ||
+ | |||
+ | f=(9.0/ | ||
+ | |||
+ | cout << c << " degrees celcius is " << fixed << showpoint << | ||
+ | setprecision(1) << f << " degrees fahrenheit.\n\n"; | ||
+ | } | ||
+ | void cent() | ||
+ | { | ||
+ | // inches to centimeters | ||
+ | const double IN_CONV = 2.54; | ||
+ | int inches(0); | ||
+ | cout << " | ||
+ | cin >> inches; | ||
+ | |||
+ | double cent(inches*IN_CONV); | ||
+ | |||
+ | cout << inches << " inches | ||
+ | setprecision(2) << cent << " centimeters\n"; | ||
+ | } | ||
+ | void inch() | ||
+ | { | ||
+ | // | ||
+ | const double IN_CONV = 2.54; | ||
+ | double cent(0); | ||
+ | cout << " | ||
+ | cin >> cent; | ||
+ | |||
+ | double inches(cent/ | ||
+ | |||
+ | cout << cent << " centimeters | ||
+ | setprecision(2) << inches << " Inch(es)\n"; | ||
+ | } | ||
+ | void kilo() | ||
+ | { | ||
+ | // pounds to kilos | ||
+ | double p(0); //pounds | ||
+ | double k(0); //kilos | ||
+ | cout << " Please enter a weight in pounds: "; | ||
+ | cin >> p; | ||
+ | k = p/2.2; | ||
+ | |||
+ | cout << p << " pounds is equal to " << setprecision(2) << | ||
+ | k << " kilograms.\n\n"; | ||
+ | |||
+ | } | ||
+ | void pound() | ||
+ | { | ||
+ | // kilos to pounds | ||
+ | double p(0); //pounds | ||
+ | double k(0); //kilos | ||
+ | cout << " Please enter a weight in kilograms: "; | ||
+ | cin >> k; | ||
+ | p = k*2.2; | ||
+ | |||
+ | cout << k << " kilograms is equal to " << setprecision(2) << | ||
+ | p << " pounds.\n\n"; | ||
+ | } | ||
+ | void lines() | ||
+ | { | ||
+ | cout << " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <WRAP download round 20%> | ||
+ | [[http:// | ||
+ | </ | ||
+ | |||
+ | <code c> | ||
+ | /* | ||
+ | * conv.cpp - this is a menu drivent program that allows the user to choose an option | ||
+ | * and then perform a task based on the option chosen | ||
+ | * | ||
+ | * requires: menu.H, functions.H (to be in current dir) | ||
+ | * compile: g++ conv.cpp -o conv | ||
+ | * execute: ./conv | ||
+ | * | ||
+ | */ | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int option(0); | ||
+ | do{ | ||
+ | lines(); | ||
+ | | ||
+ | cout << " Choose an option: "; | ||
+ | cin >> option; | ||
+ | lines(); | ||
+ | switch(option) | ||
+ | { | ||
+ | case 1: | ||
+ | | ||
+ | break; | ||
+ | case 2: | ||
+ | celc(); | ||
+ | break; | ||
+ | case 3: | ||
+ | cent(); | ||
+ | | ||
+ | case 4: | ||
+ | inch(); | ||
+ | break; | ||
+ | case 5: | ||
+ | kilo(); | ||
+ | break; | ||
+ | case 6: | ||
+ | pound(); | ||
+ | break; | ||
+ | default: | ||
+ | break; | ||
+ | } | ||
+ | }while (option> | ||
+ | |||
+ | lines(); | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | requires: menu.H, functions.H (to be in current dir) \\ | ||
+ | instead of having to use "" | ||
+ | if you have added the files to / | ||
+ | |||
+ | =====Version 3: Classes===== | ||
+ | Version 3 is not actually the same program as versions 1, 2, 4 or 5, however it exhibits some behaviors that the final Version 3 hopes to have. | ||
+ | <code c> | ||
+ | /* | ||
+ | * classDay.cc - class for the day program. | ||
+ | */ | ||
+ | |||
+ | # | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | class dayType | ||
+ | { | ||
+ | public: | ||
+ | void setDay(); | ||
+ | string printDay(); | ||
+ | string returnDay(int value); | ||
+ | void returnNDay(); | ||
+ | void returnPDay(); | ||
+ | void dayCalc(); | ||
+ | dayType(); | ||
+ | |||
+ | private: | ||
+ | string day; | ||
+ | int dayValue; | ||
+ | }; | ||
+ | </ | ||
+ | <code c> | ||
+ | /* | ||
+ | * funcDay.cc - functions for the day program | ||
+ | * | ||
+ | * uses - classDay | ||
+ | */ | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | void dayType:: | ||
+ | { | ||
+ | string inDay; | ||
+ | |||
+ | |||
+ | cout << "Enter a day of the week: "; | ||
+ | cin >> inDay; | ||
+ | |||
+ | if (inDay == " | ||
+ | { | ||
+ | day = " | ||
+ | dayValue = 1; | ||
+ | } | ||
+ | else if (inDay == " | ||
+ | { | ||
+ | day = " | ||
+ | dayValue = 2; | ||
+ | } | ||
+ | else if (inDay == " | ||
+ | { | ||
+ | day = " | ||
+ | dayValue = 3; | ||
+ | } | ||
+ | else if (inDay == " | ||
+ | { | ||
+ | day = " | ||
+ | dayValue = 4; | ||
+ | } | ||
+ | else if (inDay == " | ||
+ | { | ||
+ | day = " | ||
+ | dayValue = 5; | ||
+ | } | ||
+ | else if (inDay == " | ||
+ | { | ||
+ | day = " | ||
+ | dayValue = 6; | ||
+ | } | ||
+ | else if (inDay == " | ||
+ | { | ||
+ | day = " | ||
+ | dayValue = 7; | ||
+ | } | ||
+ | else | ||
+ | cout << "Not a day of the week!" << endl; | ||
+ | } | ||
+ | |||
+ | string dayType:: | ||
+ | { | ||
+ | string rDay; | ||
+ | |||
+ | rDay = day; | ||
+ | return rDay; | ||
+ | } | ||
+ | |||
+ | string dayType:: | ||
+ | { | ||
+ | string rDay; | ||
+ | |||
+ | switch(value) | ||
+ | { | ||
+ | case 1: | ||
+ | rDay = " | ||
+ | break; | ||
+ | case 2: | ||
+ | rDay = " | ||
+ | break; | ||
+ | case 3: | ||
+ | rDay = " | ||
+ | break; | ||
+ | case 4: | ||
+ | rDay = " | ||
+ | break; | ||
+ | case 5: | ||
+ | rDay = " | ||
+ | break; | ||
+ | case 6: | ||
+ | rDay = " | ||
+ | break; | ||
+ | case 7: | ||
+ | rDay = " | ||
+ | default: | ||
+ | rDay = " | ||
+ | } | ||
+ | |||
+ | return rDay; | ||
+ | } | ||
+ | |||
+ | void dayType:: | ||
+ | { | ||
+ | string rDay; | ||
+ | |||
+ | if (dayValue > 6) | ||
+ | cout << "The next day is monday" | ||
+ | else | ||
+ | { | ||
+ | rDay = returnDay(dayValue + 1); | ||
+ | cout << "The next day is " << rDay << endl; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void dayType:: | ||
+ | { | ||
+ | string rDay; | ||
+ | |||
+ | if (dayValue < 2) | ||
+ | cout << "The previous day is sunday" | ||
+ | else | ||
+ | { | ||
+ | rDay = returnDay(dayValue - 1); | ||
+ | cout << "The previous day is " << rDay << endl; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void dayType:: | ||
+ | { | ||
+ | string rDay; | ||
+ | int value; | ||
+ | int newValue; | ||
+ | |||
+ | cout << "how many days do you want add? "; | ||
+ | cin >> value; | ||
+ | cout << endl; | ||
+ | |||
+ | newValue = value; | ||
+ | |||
+ | while (newValue > 7) | ||
+ | { | ||
+ | newValue -= 7; | ||
+ | } | ||
+ | |||
+ | cout << "In " << value << " days the day will be " | ||
+ | << returnDay(dayValue + newValue) << endl; | ||
+ | } | ||
+ | |||
+ | dayType:: | ||
+ | { | ||
+ | day = " | ||
+ | dayValue = 1; | ||
+ | } | ||
+ | </ | ||
+ | <code c> | ||
+ | /* | ||
+ | * day.cc - main program uses classDay and funcDay | ||
+ | * | ||
+ | * requires - classDay.cc and funcDay.cc | ||
+ | * compile - g++ day.cc -o day | ||
+ | * to run - ./day | ||
+ | */ | ||
+ | |||
+ | # | ||
+ | # | ||
+ | # | ||
+ | |||
+ | void menu(dayType curDay); | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | |||
+ | char end = ' | ||
+ | int option; | ||
+ | dayType curDay; | ||
+ | |||
+ | |||
+ | while (end != ' | ||
+ | { | ||
+ | |||
+ | menu(curDay); | ||
+ | cin >> option; | ||
+ | cout << endl; | ||
+ | |||
+ | switch (option) | ||
+ | { | ||
+ | case 1: | ||
+ | curDay.setDay(); | ||
+ | break; | ||
+ | case 2: | ||
+ | cout << curDay.printDay() << endl; | ||
+ | break; | ||
+ | case 3: | ||
+ | curDay.returnNDay(); | ||
+ | break; | ||
+ | case 4: | ||
+ | curDay.returnPDay(); | ||
+ | break; | ||
+ | case 5: | ||
+ | curDay.dayCalc(); | ||
+ | break; | ||
+ | default: | ||
+ | cout << "Not an option!" | ||
+ | } | ||
+ | |||
+ | cout << "do you want to see the menu again?(y or n): "; | ||
+ | cin >> end; | ||
+ | } | ||
+ | |||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void menu(dayType curDay) | ||
+ | { | ||
+ | |||
+ | cout << " | ||
+ | cout << " | ||
+ | cout << " | ||
+ | cout << " | ||
+ | cout << " | ||
+ | cout << " | ||
+ | cout << " | ||
+ | cout << "what would you like to do: "; | ||
+ | |||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====Version 4: Using Make===== | ||
+ | Coming soon! an example that require Makefile!! | ||
+ | |||