Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout << "Input the price: $"; cin >> p;

Similar presentations


Presentation on theme: "Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout << "Input the price: $"; cin >> p;"— Presentation transcript:

1 Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout << "Input the price: $"; cin >> p; return p; }

2 More on cin and cout double buy_two() {double p1, p2; cout << "Input two prices: $"; cin >> p1 >> p2; cout << "You have input " << p1 << " and " << p2 << endl; cout << "After Tax, there are $" << p1+tax(p1, 0.075) << " and $" << p2+tax(p2, 0.075) << endl; return p1+p2; } int main() { const double tax_rate=0.075; double sum=0; sum = buy_two(); cout << "Total: $" << sum+tax(sum, tax_rate) << "\n"; return 0; } We can’t use tax_rate here All elements to be inserted must be ready before any of them sent to the standard out-stream (the screen). They will be evaluated from left to right. Input two prices: $30 70 You have input 30 and 70 After Tax, there are $32.25 and $75.25 Total: $107.5 Press any key to continue

3 Assignment operator = a = 12+1; The assignment also return a value, which is the value of the expression in the right hand side, i.e. 12+1 = is right-associative. a = 12+ buy_one(); a = b = c = d = 2; is same as a = ( b = ( c = (d=2))); d = c – b – a – a; is same as d = (((c – b)– a)– a); a = b + c = d – 2; is not allowed are valid a = b + (c = d) – 2; a = b = c = d + 2; cout << (a=b+3) << endl;

4 A little trick of using assignment sum = buy_one(); cout << "After Tax: $" << (sum = sum + tax(sum, tax_rate)) << endl; cout << "Variable sum = " << sum << endl; cout << "Total after Tax: $" << (sum = sum) + tax(sum=buy_one(), tax_rate) + sum << endl; cout << "Variable sum = " << sum << endl; Input the price: $100 After Tax: $107.5 Variable sum = 107.5 Input the price: $200 Total after Tax: $322.5 Variable sum = 200 Press any key to continue The outer pair of () is needed

5 More tricks of using assignment sum=0; cout << "After Tax: $" << (sum = sum + tax(sum=buy_one(), tax_rate)) << "\n\n"; sum=0; cout << "After Tax: $" << (sum = (sum+0) + tax(sum=buy_one(), tax_rate)) << "\n\n"; sum=0; cout << "After Tax: $" << (sum = tax(sum=buy_one(), tax_rate) + sum) << "\n\n"; sum=0; cout << "After Tax: $" << (sum = tax(sum=buy_one(), tax_rate) + (sum+0)) << "\n\n"; Input the price: $10 After Tax: $10.75 Input the price: $10 After Tax: $0.75 Input the price: $10 After Tax: $10.75 Input the price: $10 After Tax: $10.75 Press any key to continue

6 Escape sequence cout << "To print \\, you need two \\" << endl; cout << "\\n \n starts a new line" << endl; cout << "\\t \t horizontal tab" << endl; cout << "\\\" print \"" << endl; cout << "\\\' print \'" << endl ; To print \, you need two \ \n starts a new line \t horizontal tab \" print " \' print ' Press any key to continue In a string, the letter follows backslash \ is to control the effect of the output. E.g. \n Originally, they are designed to control the line-printer.

7 The Syntax of if and if/else statements if ( condition ) { statement list; } if ( condition ) { statement list; } else { statement list; } Indentation indicates that the statements in the statement list are at the level next to the if/else statement. Reserved words Logical expression. In C++, 0 is false, any non-zero values will be considered as true.

8 Example of if/else statement cout << "How many items do you want to buy? "; cin >> a; if (a = = 1) { sum = buy_one(); sum = tax(sum,tax_rate)+sum; cout << "You need to pay $" << sum; } else { sum = buy_two(); sum = tax(sum,tax_rate)+sum; cout << "You need to pay $" << sum; } cout << endl; How many items do you want to buy? 1 Input the price: $20 You need to pay $21.5 Press any key to continue How many items do you want to buy? 2 Input two prices: $100 20 You have input 100 and 20 After Tax, there are $107.5 and $21.5 You need to pay $129 Press any key to continue How many items do you want to buy? 3 Input two prices: $100 20 You have input 100 and 20 After Tax, there are $107.5 and $21.5 You need to pay $129 Press any key to continue

9 Nested if/else statement if ( condition 1 ) { if ( condition 2 ) { statement list; } else { statement list; }; statement list; } else { statement list; } Indentation indicates the level of statements.

10 Example of nested if/else statement cout << "How many items do you want to buy? "; cin >> a; if (a = = 1) { sum = buy_one(); sum = tax(sum,tax_rate)+sum; cout << "You need to pay $" << sum; } else { if (a = = 2) { sum = buy_two(); sum = tax(sum,tax_rate)+sum; cout << "You need to pay $" << sum; } else { cout << "At most two items!!"; } How many items do you want to buy? 4 At most two items!! Press any key to continue

11 Another form of Nested if/else statement (Cascaded) if (condition_1) statement_1; else if (condition_2) statement_2; else if (condition_3) statement_3; else if (condition_4) statement_4; else if (condition_5) statement_5; else if (condition_6) statement_6; if (condition_1) if (condition_2) if (condition_3) if (condition_4) if (condition_5) if (condition_6) statement_1; else statement_2; if (condition_1 && condition_2 && condition_3 && condition_4 && condition_5 && condition_6 && ) statement_1; else statement_2;

12 Example of cascaded if/else statement cout << "Input an integer as the day of the week:"; cin >> i; if (i = = 1) cout << "\n Sunday"; else if (i = = 2) cout << "\n Monday"; else if (i = = 3) cout << "\n Tuesday"; else if (i = = 4) cout << "\n Wednesday"; else if (i = = 5) cout << "\n Thursday"; else if (i = = 6) cout << "\n Friday"; else if (i = = 7) cout << "\n Saturday"; Input an integer as the day of the week:3 Tuesday Input an integer as the day of the week:4 Wednesday Input an integer as the day of the week:7 Saturday Input an integer as the day of the week:4 Wednesday

13 switch vs. cascaded if/else if (i == 1) statement_1; else if (i == 2) statement_2; else if (i == 3) statement_3; else if (i == 4) statement_4; else if (i == 5) statement_5; else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; break; case 3: statement_3; break; case 4: statement_4; break; case 5: statement_5; break; case 6: statement_6; break; }

14 Example of switch cout << "Input an integer as the day of the week:"; cin >> i; switch (i) { case 1 :cout << "\n Sunday"; break; case 2 : cout << "\n Monday"; break; case 3 : cout << "\n Tuesday"; break; case 4 : cout << "\n Wednesday"; break; case 5 : cout << "\n Thursday"; break; case 6 : cout << "\n Friday"; break; case 7 : cout << "\n Saturday"; break; }

15 Function that returns a string string WeekDay(int day) { if (0 == day) { return "Sunday"; } else if (1 == day) { return "Monday"; } else if (2 == day) { return "Tuesday"; } else if (3 == day) { return "Wednesday"; } … }


Download ppt "Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout << "Input the price: $"; cin >> p;"

Similar presentations


Ads by Google