This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
notes:projects:cpp [2010/03/15 22:31] – mcooper6 | notes:projects:cpp [2010/03/22 17:58] (current) – mcooper6 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~DISCUSSION|C++ Demonstration Page Discussion~~ | ||
+ | <WRAP left 40%> | ||
+ | ~~TOC~~ | ||
+ | </ | ||
+ | // | ||
+ | // | ||
+ | <WRAP centeralign 90% bigger> | ||
+ | <WRAP bigger fgred> | ||
+ | <WRAP muchbigger> | ||
+ | A Practical Demonstration | ||
+ | </ | ||
+ | <WRAP info right bgwhite 50%> | ||
+ | This is the documentation page for the CSCS 2850 C++ Demonstration Code. These demonstrations explore the some of the finer points of C++ programming while using practical, workable examples to illustrate the language. | ||
+ | </ | ||
+ | <WRAP warning right bgwhite 50%> | ||
+ | For the Reader to consider: The programs in this demonstration begin near the culmination of the first month of a college C++ coarse, and lead to more advanced techniques that are addressed at the end of said coarse, or not at all. So, consider that, if you're reading this article as one of the first C++ resources you've picked up, you're about 1 month behind. | ||
+ | local bookstore that will catch you up in no time at all. [[http:// | ||
+ | </ | ||
+ | <WRAP clear></ | ||
+ | |||
+ | =====The Problem Statement ===== | ||
+ | This is a practical demonstration of the C++ and, by extension, C programming language. | ||
+ | |||
+ | Write a menu driven program that will allow a user to convert between the following values: | ||
+ | * Degrees Fahrenheit to degrees Celcius | ||
+ | * Degrees Celcius to degrees Fahrenheit | ||
+ | * Inches to centimeters | ||
+ | * Centimeters to inches | ||
+ | * Pounds to Kilograms | ||
+ | * Kilograms to pounds | ||
+ | |||
+ | ====The Logic==== | ||
+ | In keeping up with the nature of academic exploration, | ||
+ | |||
+ | ===Step 1=== | ||
+ | * Show the user all the possible options, request that they make a choice, and wait. | ||
+ | |||
+ | This demonstration is a command line program, and in the interest of keeping it simple, we'll just write a quick number based menu ... 1 for this, 2 for that ... so on. | ||
+ | |||
+ | In psuedocode, it looks something like this: | ||
+ | < | ||
+ | print " 1. Fahrenheit to Celsius" | ||
+ | print " 2. Celsius to Fahrenheit" | ||
+ | print " 3. Inches to centimeter" | ||
+ | print " 4. Centimeters to inches" | ||
+ | print " 5. Pounds to kilograms" | ||
+ | print " 6. Kilograms to pounds" | ||
+ | print " 0. Exit" | ||
+ | </ | ||
+ | |||
+ | ===Step 2=== | ||
+ | *Get the number the user entered. | ||
+ | This line is pretty straight forward in psuedocode: | ||
+ | < | ||
+ | num option | ||
+ | print " | ||
+ | get option | ||
+ | </ | ||
+ | The '' | ||
+ | |||
+ | ===Step 3=== | ||
+ | *Test the number the user entered against the options that we presented. | ||
+ | |||
+ | In psuedocode, it looks like this: | ||
+ | |||
+ | < | ||
+ | if (option == 1) then | ||
+ | |||
+ | //fahr to cels | ||
+ | |||
+ | else if (option == 2) then | ||
+ | |||
+ | //cels to fahr | ||
+ | |||
+ | else if (option == 3) then | ||
+ | |||
+ | // inches to centimeters | ||
+ | |||
+ | else if (option == 4) | ||
+ | |||
+ | // | ||
+ | |||
+ | else if (option == 5) then | ||
+ | |||
+ | // pounds to kilos | ||
+ | |||
+ | else if (option == 6) then | ||
+ | |||
+ | // kilos to pounds | ||
+ | |||
+ | end if | ||
+ | </ | ||
+ | |||
+ | The benefit of this method is that there' | ||
+ | |||
+ | ===Step 4=== | ||
+ | *Depending on the user's choice, perform the calculation. | ||
+ | |||
+ | Each of our conversions has different logic. | ||
+ | |||
+ | **Fahrenheit to Celsius ... and back again:** | ||
+ | <WRAP indent> | ||
+ | We know that 100 degrees Celsius is equal to 212 degrees Fahrenheit and 0 degrees Celsius is equal to 32 degrees Fahrenheit, | ||
+ | |||
+ | So, the final conversion is < | ||
+ | It looks like this in psuedocode: | ||
+ | </ | ||
+ | < | ||
+ | //fahr to Celsius | ||
+ | num f; //temp in Fahrenheit | ||
+ | num c; //temp in Celsius | ||
+ | |||
+ | print "Enter the temperature in Fahrenheit: " | ||
+ | get f | ||
+ | |||
+ | c=(5.0/ | ||
+ | |||
+ | print f, " degrees Fahrenheit is ", c, " degrees Celsius." | ||
+ | </ | ||
+ | < | ||
+ | //cels to fahr | ||
+ | |||
+ | num f | ||
+ | num c | ||
+ | |||
+ | print "Enter a temperature in Celsius: " | ||
+ | get c; | ||
+ | |||
+ | f=(9.0/ | ||
+ | |||
+ | print c, " degrees Celsius is ", f, " degrees Fahrenheit." | ||
+ | </ | ||
+ | |||
+ | **Inches to centimeters and the other way around:** | ||
+ | <WRAP indent> | ||
+ | This one's a little easier. | ||
+ | |||
+ | Here it is, in psuedocode: | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | // inches to centimeters | ||
+ | const num IN_CONV = 2.54 | ||
+ | num inches | ||
+ | |||
+ | print " | ||
+ | get inches | ||
+ | |||
+ | num cent = inches*IN_CONV | ||
+ | |||
+ | print inches, " inches | ||
+ | </ | ||
+ | < | ||
+ | // | ||
+ | |||
+ | const num IN_CONV = 2.54 | ||
+ | num cent(0); | ||
+ | print " | ||
+ | |||
+ | get cent; | ||
+ | |||
+ | num inches = cent/ | ||
+ | |||
+ | print cent, " centimeters | ||
+ | </ | ||
+ | <WRAP indent> | ||
+ | The psuedocode above introduces the concepts of constants; variables that maintain a constant value and cannot be changed within the body of code. | ||
+ | </ | ||
+ | \\ | ||
+ | **Pounds to kilos and the inverse** | ||
+ | |||
+ | <WRAP indent> | ||
+ | There are 2.2 kilograms in one pound, so again, if given pounds, simply divide by 2.2 to get kilograms, and likewise, if given kilograms, multiply by 2.2 to get pounds. | ||
+ | |||
+ | The psuedocode snippet: | ||
+ | </ | ||
+ | < | ||
+ | // pounds to kilos | ||
+ | |||
+ | num p //pounds | ||
+ | num k //kilos | ||
+ | |||
+ | print " Please enter a weight in pounds: " | ||
+ | get p | ||
+ | |||
+ | k = p/2.2; | ||
+ | |||
+ | print p, " pounds is equal to ", k, " kilograms."; | ||
+ | </ | ||
+ | < | ||
+ | // kilos to pounds | ||
+ | |||
+ | num p //pounds | ||
+ | num k //kilos | ||
+ | |||
+ | print " Please enter a weight in kilograms: "; | ||
+ | get k; | ||
+ | |||
+ | p = k*2.2; | ||
+ | |||
+ | print k, " kilograms is equal to ", p, " pounds."; | ||
+ | </ | ||
+ | |||
+ | ===Step 5=== | ||
+ | * Loop back to the beginning and allow the user one more shot at it. | ||
+ | |||
+ | There are several looping structures that could be used to solve this common task. However, in this case the '' | ||
+ | |||
+ | Like this: | ||
+ | |||
+ | < | ||
+ | int input=0 | ||
+ | boolean condition=false | ||
+ | do | ||
+ | |||
+ | print " | ||
+ | get input | ||
+ | |||
+ | if (input == 1) | ||
+ | condition=true | ||
+ | |||
+ | while(condition) | ||
+ | </ | ||
+ | |||
+ | The above loop will execute at least once, prompting the user for an input. | ||
+ | |||
+ | The final psuedocode solution looks like this: | ||
+ | |||
+ | < | ||
+ | start | ||
+ | num option; | ||
+ | |||
+ | do{ | ||
+ | print " 1. Fahrenheit to Celcius" | ||
+ | print " 2. Celcius to Fahrenheit" | ||
+ | print " 3. Inches to centimeter" | ||
+ | print " 4. Centimeters to inches" | ||
+ | print " 5. Pounds to kilograms" | ||
+ | print " 6. Kilograms to pounds" | ||
+ | print " 0. Exit" | ||
+ | |||
+ | print << " Choose an option: " | ||
+ | get option; | ||
+ | |||
+ | if (option == 1) then | ||
+ | |||
+ | //fahr to celc | ||
+ | |||
+ | num f; //temp in Fahrenheit | ||
+ | num c; //temp in Celcius | ||
+ | |||
+ | print "Enter the temerature in Fahrenheit: " | ||
+ | get f | ||
+ | |||
+ | c=(5.0/ | ||
+ | |||
+ | print f, " degrees Fahrenheit is ", c, " degrees Celcius." | ||
+ | |||
+ | else if (option == 2) then | ||
+ | |||
+ | //celc to fahr | ||
+ | |||
+ | num f | ||
+ | num c | ||
+ | |||
+ | print "Enter a temperature in Celcius: " | ||
+ | get c; | ||
+ | |||
+ | f=(9.0/ | ||
+ | |||
+ | print c, " degrees celcius is ", f, " degrees fahrenheit." | ||
+ | |||
+ | else if (option == 3) then | ||
+ | |||
+ | // inches to centimeters | ||
+ | const num IN_CONV = 2.54 | ||
+ | num inches | ||
+ | |||
+ | print " | ||
+ | get inches | ||
+ | |||
+ | num cent = inches*IN_CONV | ||
+ | |||
+ | print inches, " inches | ||
+ | |||
+ | else if (option == 4) | ||
+ | // | ||
+ | |||
+ | const num IN_CONV = 2.54 | ||
+ | num cent(0); | ||
+ | print " | ||
+ | |||
+ | get cent; | ||
+ | |||
+ | num inches = cent/ | ||
+ | |||
+ | print cent, " centimeters | ||
+ | |||
+ | else if (option == 5) then | ||
+ | // pounds to kilos | ||
+ | |||
+ | num p //pounds | ||
+ | num k //kilos | ||
+ | |||
+ | print " Please enter a weight in pounds: " | ||
+ | get p | ||
+ | |||
+ | k = p/2.2; | ||
+ | |||
+ | print p, " pounds is equal to ", k, " kilograms."; | ||
+ | |||
+ | else if (option == 6) then | ||
+ | // kilos to pounds | ||
+ | |||
+ | num p //pounds | ||
+ | num k //kilos | ||
+ | |||
+ | print " Please enter a weight in kilograms: "; | ||
+ | get k; | ||
+ | |||
+ | p = k*2.2; | ||
+ | |||
+ | print k, " kilograms is equal to ", p, " pounds."; | ||
+ | | ||
+ | |||
+ | }while (option> | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====Version 1: if...else===== | ||
+ | Solving the problem statement above in C++ is very similar to solving it in psuedocode. | ||
+ | |||
+ | At this point, you can head over to the [[notes: | ||
+ | |||
+ | ====Comments==== | ||
+ | The first lines in our demonstration program are comments. | ||
+ | |||
+ | < | ||
+ | /* | ||
+ | |||
+ | everything between "/ | ||
+ | |||
+ | */ | ||
+ | |||
+ | //this is a single line comment | ||
+ | |||
+ | a=x+b //end of line comment | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====Preprocessor Directives==== | ||
+ | The lines beginning with a hash (#) are not actually program statements. | ||
+ | |||
+ | <code c> | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | </ | ||
+ | |||
+ | \\ | ||
+ | \\ | ||
+ | * Namespaces | ||
+ | * int main() | ||
+ | * Variables and datatypes | ||
+ | * Standard Input/ | ||
+ | * Looping and Conditional Sturctures | ||
+ | * .cc file extension | ||
+ | * Tips & Tricks | ||
+ | |||
+ | |||
+ | ====Compiling the code.==== | ||
+ | Once you get the code to your shell, simply compile it using your favorite C++ compiler. | ||
+ | |||
+ | <cli> | ||
+ | :~$ g++ -o converter converter.cc | ||
+ | </ | ||
+ | |||
+ | Once the code is compiled, you can run the program using the command ./ | ||
+ | |||
+ | <cli> | ||
+ | :~$ ./converter | ||
+ | </ | ||
+ | |||
+ | =====Version 2: Modular===== | ||
+ | Compiling the code | ||
+ | * Directory Structures | ||
+ | * Header Files | ||
+ | * Functions | ||
+ | * Switch statements | ||
+ | * Tips & Tricks | ||
+ | |||
+ | =====Version 3: Classes===== | ||
+ | Compiling the code | ||
+ | * Custom data types | ||
+ | * Classes | ||
+ | * Return Values | ||
+ | * Arguments | ||
+ | * Tips & Tricks | ||
+ | =====Version 4: Make===== | ||
+ | * Using Make | ||
+ | * Tips & Tricks | ||
+ | |||
+ | =====Version 5: GUI with C++===== | ||
+ | *GUI With C++ | ||
+ | *Necessary libraries | ||
+ | *portability?? | ||
+ | *Tips and Tricks | ||
+ | |||
+ | =====C++ Demonstration Code===== | ||
+ | The C Demonstration Code is located [[notes: | ||
+ | |||
+ | Download the code using the links found at the download page. While you can copy and paste the code directly from the wiki text, this is method is discouraged as typos in each edit may not have passed through a compiler. | ||
+ | |||
+ | If you're downloading the code to a Windows machine, you'll want to open it in using something like wordpad. | ||
+ | |||
+ | *[[notes: |