1 / 26

Spec of the program outputs:

Spec of the program outputs:. What's your name? Dennis Hi! Dennis! Input the price for 10% discount: 200 Input the quantity: 5 Input the price for 20% discount: 500 Input the quantity: 4 Total items: 9 Amount: 2500.00

Download Presentation

Spec of the program outputs:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Spec of the program outputs: What's your name? Dennis Hi! Dennis! Input the price for 10% discount: 200 Input the quantity: 5 Input the price for 20% discount: 500 Input the quantity: 4 Total items: 9 Amount: 2500.00 Tax rate: 8.5% Total amount: 2712.50 Press any key to continue Chung-Chih Li @ ISU ITK 279

  2. CPP program for the baby counter (part 1) #include <iostream> #include <string> using namespace std; void main() { string name; int itemNo = 0; int a,b; int total_item; float price_1, price_2; const float taxRate = 0.085; float discount = 0.1, Discount (0.2); double amount; cout << "What's your name? "; cin >> name; cout << "\nHi! " << name << "!\n\n"; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); Chung-Chih Li @ ISU ITK 279

  3. CPP program for the baby counter (part 2) cout << "\t Input the price for "; cout << (int) (discount*100) << "% discount: "; cin >> price_1; cout << "\t Input the quantity: "; cin >> a; cout << "\t Input the price for "; cout << (int) (Discount*100) << "% discount: "; cin >> price_2; cout << "\t Input the quantity: "; cin >> b; total_item = a + b; cout << "\n\n\t Total items:\t" << total_item << endl; amount = price_1*a*(1-discount)+price_2*b*(1-Discount); cout << "\t Amount:\t" << amount << endl; cout << "\t Tax rate: \t" << taxRate << endl; amount = (price_1*a*(1-discount)+price_2*b*(1-Discount)) *(1+taxRate); cout << "\t Total amount: \t" << amount << endl << endl; } Chung-Chih Li @ ISU ITK 279

  4. Directives These two statements use the include directive to ask the compiler to provide necessary accesses to the twostandard libraries. #include <iostream> #include <string> using namespace std; /* This is a program that can do something .. By Chung-Chih Li */ void main() { string name; // string is a class int itemNo = 0; int a,b; int total_item; float price_1, price_2; const float taxRate = 0.085; float discount = 0.1, Discount = 0.2; double amount; cout << "What's your name? "; cin >> name; cout << "\nHi! " << name << "!\n\n"; The directive using asks the compiler to use the definitions in namespace std. alternatively: (for this program) using std::cin; using std::cout; using std::endl; using std::ios; using std::string; Chung-Chih Li @ ISU ITK 279

  5. Comments #include <iostream> #include <string> using namespace std; /*This is a program that can do something .. By Chung-Chih Li */ void main() { string name; //string is a class int itemNo = 0; int a,b; int total_item; float price_1, price_2; const float taxRate = 0.085; float discount = 0.1, Discount = 0.2; double amount; cout << "What's your name? "; cin >> name; cout << "\nHi! " << name << "!\n\n"; Chung-Chih Li @ ISU ITK 279

  6. Identifiers #include <iostream> #include <string> using namespace std; void main() { string name; int itemNo = 0; int a,b; int total_item; float price_1, price_2; const float taxRate = 0.085; float discount = 0.1, Discount (0.2); double amount; cout << "What's your name? "; cin >> name; cout << "\nHi! " << name << "!\n\n"; Chung-Chih Li @ ISU ITK 279

  7. Variables and Types #include <iostream> #include <string> using namespace std; void main() { stringname; intitemNo = 0; inta,b; inttotal_item; floatprice_1, price_2; constfloattaxRate = 0.085; floatdiscount = 0.1, Discount (0.2); doubleamount; cout << "What's your name? "; cin >> name; cout << "\nHi! " << name << "!\n\n"; Chung-Chih Li @ ISU ITK 279

  8. Literals (constants) string name; int itemNo = 0; int a,b; int total_item; float price_1, price_2; const float taxRate = 0.085; float discount = 0.1, Discount (0.2); double amount; cout << "What's your name? "; cin >> name; cout << "\nHi! " << name << "!\n\n"; cout << "\t Input the price for "; cout << (int) (discount*100) << "% discount: "; cin >> price_1; cout << "\t Input the quantity: "; cin >> a; Chung-Chih Li @ ISU ITK 279

  9. Value Initialization #include <iostream> #include <string> using namespace std; void main() { string name; int itemNo = 0; int a,b; int total_item; float price_1, price_2; const float taxRate = 0.085; float discount = 0.1, Discount (0.2); double amount; cout << "What's your name? "; cin >> name; cout << "\nHi! " << name << "!\n\n"; Chung-Chih Li @ ISU ITK 279

  10. Simple Types sizeof(short) : 2 sizeof(s) : 2 sizeof(int) : 4 sizeof(i) : 4 sizeof(long) : 4 sizeof(l) : 4 sizeof(double) : 8 sizeof(d) : 8 sizeof(long double) : 8 sizeof(u) : 8 sizeof(char) : 1 sizeof(c) : 1 sizeof(bool) : 1 sizeof(b) : 1 Press any key to continue short s; int i; long l; float f; double d; longdouble u; char c; bool b; cout << "sizeof(short) :\t\t " << sizeof(short) << " sizeof(s) : " << sizeof(s) << endl; Chung-Chih Li @ ISU ITK 279

  11. Assignment I total_item = a + b; total_item = a; a = a; amount = (price_1*a*(1-discount)+price_2*b*(1-Discount)) *(1+taxRate); L-value = R-value a + 1 = a Chung-Chih Li @ ISU ITK 279

  12. Assignment II a = 3; a *= a*= 2; cout << a; a += 2; a /= 2; a %= 2; a *= a; ........ total_item = a + b; cout << (a = 2*3); a = b = 3; a = (b = 3); ? a = 3; a = a * ( a *=2); cout << a; ? a = 3; a = a * ( a = a*2); cout << a; ? a = a+b = 3; 36 (a = b) = 3; Chung-Chih Li @ ISU ITK 279

  13. Arithmetic operators and their precedence rules + - * / % amount = (price_1*a*(1-discount)+price_2*b*(1-Discount))* (1+taxRate); precedence rules: + - % * / > a-b-c-d ((a-b)-c)-d left-to-right a=b=c=d a=(b=(c=d)) Chung-Chih Li @ ISU ITK 279

  14. Increment and Decrement i = i+1; i = i-1; i++; ++i; i--; --i; n = i; i = i+1; n = (i++); n = (++i); i = i+1; n = i; i=0; i = i+1; i = i+1; s = i; i = i+1; cout... i=0; s = (++(++i))++; cout << i << endl; cout << s << endl; Chung-Chih Li @ ISU ITK 279

  15. Type Coercion (Type Casting) float discount = 0.1, Discount (0.2); cout << "\t Input the price for "; cout << (int) (discount*100) << "% discount: "; ...... cout << "\t Input the price for "; cout << (int) (Discount*100) << "% discount: "; Input the price for 10% discount: 200 Input the quantity: 5 Input the price for 20% discount: 500 Input the quantity: 4 Chung-Chih Li @ ISU ITK 279

  16. Type Coercion (Type Casting), (Implicit) double score_1=1, score_2=2; int no_test=2; double average; ..... average = (score_1 + score_2 )/ no_test; int i=1, j=2, k=3; double r=1, s=2, t=3; double a,b,c,d,e; ... a = r/t + s/t; // 0.33333... + 0.66666... b = i/k + j/k; // 1/3 + 2/3  0 + 0 c = (i+j)/k; // (1+2) / 3  3/3 d = i/t + j/k; // 0.33333... + 0 e = (i+r)/k; // (1+1.0)/3  2.0/3  0.66666 Chung-Chih Li @ ISU ITK 279

  17. Type Casting, (Explicit) int score_1=1, score_2=2; int no_test=2; double average; ..... average = (score_1 + score_2 )/ no_test; older form 1 average = (score_1 + score_2 )/ ((double)no_test); 1.5 average = (double)(score_1 + score_2 )/no_test; 1 average = (double)((score_1 + score_2 )/no_test); 1.5 average =(score_1 + score_2 + 0.0)/no_test; ...... (.....)/ double(no_test); ...... (.....)/ static_cast<double>(no_test); new notation for C++ standard Chung-Chih Li @ ISU ITK 279

  18. More on Type Casting int i; i = 1/2.0; double a; a = 1/2.0; a = 0.5 a = static_cast<int>(1/2.0); a = 0.0 double p=2.99; const double tax=0.085; double total = p*(1+tax); cout << total; 3.24415 total = double(int(p*(1+tax)*100))/100; 3.24 total = (int(p*(1+tax)*100))/100; 3 Chung-Chih Li @ ISU ITK 279

  19. cout, the Class #include <iostream> #include <string> using namespace std; ref: page 490 cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); fixed point: 1229312345.67 12.00 scientific notation: 1.2293e+009 Chung-Chih Li @ ISU ITK 279

  20. The insertion operator:<< Standard Console I/O: cin and cout cout << x; Insert the value of x to the standard output stream. cout << x,y; cout << x << y; x=5; y=9; cout << x << y << “ “ << x << “ “ << y << “d“; 59 5 9d The printing will take place at the end of the cout statement. After printing, the output stream will become empty again. Why we need a output stream? 59 5 9d Some location in the main memory (e.g. 004787f4) Chung-Chih Li @ ISU ITK 279

  21. Evaluation order in a << statement i=0; cout << i++ << i++ << i++ << endl; 210 2 3 1 2 0 1 0 i cout << (cout << "a") << "B" << endl; a B 004787f4B 004787f4 a a004787f4B cout << "a" << (cout << "B") << endl; Ba004787f4 Chung-Chih Li @ ISU ITK 279

  22. The extraction operator:>> cin >> x: Extract the value in the input stream to variable x. cin >> x,y; cin >> x >> y; cin >> x >> y; cin >> name; 5 9.6 Dennis • Values in the input stream are separated by blank space or returns, or some proper delimiters. • If there are more variables than values in the stream, the computer will halt and wait for more inputs. Chung-Chih Li @ ISU ITK 279

  23. The extraction operator:>> int i,j,k; cout << "Input 3 numbers, 1st:"; cin >> i; cout << “2nd:"; cin >> j; cout << “3rd:"; cin >> k; cout << "The three numbers are:" << i << " " << j << " " << k << "\n"; Input 3 numbers, 1st:10 2nd:200 3rd:3000 The three numbers are:10 200 3000 Input 3 numbers, 1st:1 2 3 2nd:3rd:The three numbers are:1 2 3 Input 3 numbers, 1st:2 2nd:4.3 3rd:The three numbers are:2 4 -858993460 Chung-Chih Li @ ISU ITK 279

  24. Escape Sequences In a string that to be printed, C++ considers a character that immediately follows \ as a special symbol that “escapes” from its regular C++ meaning.. \n \t \\ \” \’ cout << "What's your name? "; cin >> name; cout << "\nHi! \"" << name << "\"!\n\n"; cout << "\t Input the price for "; What's your name? Dennis Hi! "Dennis"! Input the price for 10% discount: Chung-Chih Li @ ISU ITK 279

  25. More on Escape Sequences cout << x,y; cout << x y; exception: cout << "Dennis ""Li ""!"; cout << "What's your name? "; cin >> name; cout << "\nHi! ""<< name << ""!\n\n"; cout << "\t Input the price for "; What's your name? Dennis Hi! << name << ! Input the price for 10% discount: Chung-Chih Li @ ISU ITK 279

  26. Nasty C++ Programs OK! a = (++n) + (i++); n = 0; a = n + (++n); n = 0; a = (++n) + n; n = 0; a = (++n) + (++n); a=0+1 a=1+1 a=1+1 a=1+0 2 2 ? 4 KISS principle: Keep It Simple, Stupid!! Chung-Chih Li @ ISU ITK 279

More Related