> name; cout << "\nHi! " << name << "!\n\n"; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); Chung-Chih ISU IT 279 5/7/2019"> > name; cout << "\nHi! " << name << "!\n\n"; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); Chung-Chih ISU IT 279 5/7/2019">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spec of the program outputs:

Similar presentations


Presentation on theme: "Spec of the program outputs:"— 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: Tax rate: % Total amount: Press any key to continue Chung-Chih ISU IT 279 5/7/2019

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 ISU IT 279 5/7/2019

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 << (int) (Discount*100) << "% discount: "; cin >> price_2; 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 ISU IT 279 5/7/2019

4 alternatively: (for this program)
These two statements use the include directive to ask the compiler to provide necessary accesses to the two standard libraries. Directives #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 ISU IT 279 5/7/2019

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 ISU IT 279 5/7/2019

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 ISU IT 279 5/7/2019

7 Variables and Types #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 ISU IT 279 5/7/2019

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 ISU IT 279 5/7/2019

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 ISU IT 279 5/7/2019

10 Simple Types short s; int i; long l; float f; double d; long double u;
sizeof(short) : sizeof(s) : 2 sizeof(int) : sizeof(i) : 4 sizeof(long) : sizeof(l) : 4 sizeof(double) : sizeof(d) : 8 sizeof(long double) : 8 sizeof(u) : 8 sizeof(char) : sizeof(c) : 1 sizeof(bool) : sizeof(b) : 1 Press any key to continue 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; Chung-Chih ISU IT 279 5/7/2019

11 a + 1 = a L-value = R-value Assignment I total_item = a + b;
amount = (price_1*a*(1-discount)+price_2*b*(1-Discount)) *(1+taxRate); L-value = R-value a + 1 = a Chung-Chih ISU IT 279 5/7/2019

12 ? ? ? 36 Assignment II a = a+b = 3; (a = b) = 3; 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 ISU IT 279 5/7/2019

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 ISU IT 279 5/7/2019

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; s = i; cout... i=0; s = (++(++i))++; cout << i << endl; cout << s << endl; Chung-Chih ISU IT 279 5/7/2019

15 Type Coercion (Type Casting)
float discount = 0.1, Discount (0.2); cout << "\t Input the price for "; cout << (int) (discount*100) << "% discount: "; ...... 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 ISU IT 279 5/7/2019

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; // b = i/k + j/k; // 1/3 + 2/3  c = (i+j)/k; // (1+2) / 3  3/3 d = i/t + j/k; // e = (i+r)/k; // (1+1.0)/3  2.0/3  Chung-Chih ISU IT 279 5/7/2019

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); average =(score_1 + score_ )/no_test; 1.5 (.....)/ double(no_test); (.....)/ static_cast<double>(no_test); new notation for C++ standard Chung-Chih ISU IT 279 5/7/2019

18 More on Type Casting a = 0.5 a = 0.0 3.24415 3.24 3
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; total = double(int(p*(1+tax)*100))/100; 3.24 total = (int(p*(1+tax)*100))/100; 3 Chung-Chih ISU IT 279 5/7/2019

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

20 The insertion operator: <<
Standard Console I/O: cin and cout The insertion operator: << 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 f4) Chung-Chih ISU IT 279 5/7/2019

21 Evaluation order in a << statement
cout << i++ << i++ << i++ << endl; 210 2 3 1 2 1 0 i cout << (cout << "a") << "B" << endl; a B 004787f4B 004787f4 a a004787f4B cout << "a" << (cout << "B") << endl; Ba004787f4 Chung-Chih ISU IT 279 5/7/2019

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; 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 ISU IT 279 5/7/2019

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: 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: Chung-Chih ISU IT 279 5/7/2019

24 \n \t \\ \” \’ 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 ISU IT 279 5/7/2019

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 ISU IT 279 5/7/2019

26 ? Nasty C++ Programs 4 KISS principle: Keep It Simple, Stupid!! OK!
a = (++n) + (i++); n = 0; a = n + (++n); a = (++n) + n; a = (++n) + (++n); a=0+1 a=1+1 a=1+1 a=1+0 2 ? 4 KISS principle: Keep It Simple, Stupid!! Chung-Chih ISU IT 279 5/7/2019


Download ppt "Spec of the program outputs:"

Similar presentations


Ads by Google