> 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; } CPP program for the baby counter (part 2)"> > 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; } CPP program for the baby counter (part 2)">
Download presentation
Presentation is loading. Please wait.
Published byNeil Terry Modified over 9 years ago
1
3/6/2016Chung-Chih Li @ ISU IT 2791 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
2
3/6/2016Chung-Chih Li @ ISU IT 2792 #include 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"; CPP program for the baby counter (part 1) cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
3
3/6/2016Chung-Chih Li @ ISU IT 2793 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; } CPP program for the baby counter (part 2)
4
3/6/2016Chung-Chih Li @ ISU IT 2794 #include 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"; Directives These two statements use the include directive to ask the compiler to provide necessary accesses to the two standard libraries. 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;
5
3/6/2016Chung-Chih Li @ ISU IT 2795 #include 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"; Comments
6
3/6/2016Chung-Chih Li @ ISU IT 2796 #include 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"; Identifiers
7
3/6/2016Chung-Chih Li @ ISU IT 2797 #include 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"; Variables and Types
8
3/6/2016Chung-Chih Li @ ISU IT 2798 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; Literals (constants)
9
3/6/2016Chung-Chih Li @ ISU IT 2799 #include 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"; Value Initialization
10
3/6/2016Chung-Chih Li @ ISU IT 27910 short s; int i; long l; float f; double d; long double u; char c; bool b; cout << "sizeof(short) :\t\t " << sizeof(short) << " sizeof(s) : " << sizeof(s) << endl; 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
11
3/6/2016Chung-Chih Li @ ISU IT 27911 total_item = a + b; total_item = a; a = a; amount = (price_1*a*(1-discount)+price_2*b*(1-Discount)) *(1+taxRate); Assignment I a + 1 = a L-value = R-value
12
3/6/2016Chung-Chih Li @ ISU IT 27912 a += 2; a /= 2; a %= 2; a *= a;........ total_item = a + b; cout << (a = 2*3); a = b = 3; a = (b = 3); Assignment II a = a+b = 3; ? a = 3; a *= a*= 2; cout << a; 36 (a = b) = 3; a = 3; a = a * ( a *=2); cout << a; a = 3; a = a * ( a = a*2); cout << a; ? ?
13
3/6/2016Chung-Chih Li @ ISU IT 27913 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 a=b=c=d a=(b=(c=d)) left-to-right
14
3/6/2016Chung-Chih Li @ ISU IT 27914 Increment and Decrement i++; ++i; i = i+1; i--; --i; i = i-1; n = (i++); n = (++i); n = i; i = i+1; n = i; i=0; s = (++(++i))++; cout << i << endl; cout << s << endl; i=0; i = i+1; s = i; i = i+1; cout...
15
3/6/2016Chung-Chih Li @ ISU IT 27915 Type Coercion (Type Casting) Input the price for 10% discount: 200 Input the quantity: 5 Input the price for 20% discount: 500 Input the quantity: 4 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: ";
16
3/6/2016Chung-Chih Li @ ISU IT 27916 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
17
3/6/2016Chung-Chih Li @ ISU IT 27917 Type Casting, (Explicit) int score_1=1, score_2=2; int no_test=2; double average;..... average = (score_1 + score_2 )/ no_test; 1 average = (score_1 + score_2 )/ ((double)no_test); 1.5 average = (double)(score_1 + score_2 )/no_test; average = (double)((score_1 + score_2 )/no_test); 1 average =(score_1 + score_2 + 0.0)/no_test; 1.5 older form...... (.....)/ double(no_test);...... (.....)/ static_cast (no_test); new notation for C++ standard
18
3/6/2016Chung-Chih Li @ ISU IT 27918 More on Type Casting int i; i = 1/2.0; double a; a = static_cast (1/2.0); double p=2.99; const double tax=0.085; double total = p*(1+tax); cout << total; total = double(int(p*(1+tax)*100))/100; 3.24415 3.24 total = (int(p*(1+tax)*100))/100; 3 a = 0.0 a = 1/2.0; a = 0.5
19
3/6/2016Chung-Chih Li @ ISU IT 27919 #include using namespace std; cout, the Class cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); scientific notation: 1.2293e+009 fixed point: 1229312345.67 12.00 ref: page 490
20
3/6/2016Chung-Chih Li @ ISU IT 27920 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? Some location in the main memory (e.g. 004787f4 ) 59 5 9d
21
3/6/2016Chung-Chih Li @ ISU IT 27921 Evaluation order in a << statement cout << "a" << (cout << "B") << endl; cout << (cout << "a") << "B" << endl; i=0; cout << i++ << i++ << i++ << endl; 2323 1212 0101 0 i 210 Ba a 004787f4 Ba004787f4 004787f4B a004787f4B
22
3/6/2016Chung-Chih Li @ ISU IT 27922 The extraction operator: >> cin >> x: Extract the value in the input stream to variable x. 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.
23
3/6/2016Chung-Chih Li @ ISU IT 27923 The extraction operator: >> int i,j,k; cout > i; cout > j; cout > 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
24
3/6/2016Chung-Chih Li @ ISU IT 27924 Escape Sequences 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: 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 \\ \” \’
25
3/6/2016Chung-Chih Li @ ISU IT 27925 More on Escape Sequences 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: cout << x,y;cout << x y; exception: cout << " Dennis " " Li " " ! " ;
26
3/6/2016Chung-Chih Li @ ISU IT 27926 Nasty C++ Programs a = (++n) + (i++); OK! n = 0; a = n + (++n); n = 0; a = (++n) + n; n = 0; a = (++n) + (++n); a=0+1 a=1+1 a=1+0 2222 4 KISS principle: Keep It Simple, Stupid!! ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.