Created
October 8, 2021 22:18
-
-
Save derekmma/3107690c518a4efbe7336a5f30df17a0 to your computer and use it in GitHub Desktop.
Demo program for Week 2, CS31 Fall 2021 Discussion 1C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Demo program for CS31 Discussion 1C Week 2 | |
Oct 8, 2021, Mingyu Derek Ma | |
*/ | |
#include <string> | |
#include <iostream> | |
using namespace std; | |
void example1(){ | |
/* | |
Example 1: cin only takes numeric part | |
*/ | |
// cin only takes numeric part | |
int num1, num2; | |
cin >> num1; | |
cout << num1 << endl; | |
// if input is " 12 ", the num is 12 | |
cin >> num2; | |
cout << num2 << endl; | |
// if input is " 12 34 567 ", the num is 12 | |
} | |
void example2(){ | |
/* | |
Example 2: use getline() to get string input | |
*/ | |
// 1- Use getline() to get string input | |
cout << "Input a string: "; | |
string stringInput; | |
getline(cin, stringInput); // reads whole line (with spaces) into a string variable | |
// cin >> stringInput; // WRONG - doesn't read spaces | |
cout << "The string input is " << stringInput << endl; | |
} | |
void example3(){ | |
/* | |
Example: use getline() right after cin | |
*/ | |
// Wrong version | |
string stringInput = ""; | |
cout << "Input a double: "; // example input: 2.5, ' 11 22 33' | |
double x; | |
cin >> x; | |
cout << "Input a string: "; | |
getline(cin, stringInput); // only takes newline and stop | |
cout << x << endl; | |
cout << stringInput << endl; | |
// Right version | |
stringInput = ""; | |
cout << "Input a double: "; | |
cin >> x; | |
cin.ignore(10000, '\n'); // add this line! | |
cout << "Input a string: "; | |
getline(cin, stringInput); | |
cout << x << endl; | |
cout << stringInput << endl; | |
} | |
void example4(){ | |
/* | |
Example: arithmetic operators | |
*/ | |
int x; | |
x = 11 * 2 + 3; | |
cout << x << endl; | |
x = 3 + 11 * 2; | |
cout << x << endl; | |
x = 11 * (2 + 3); | |
cout << x << endl; | |
// Data types in arithmetic operation | |
int a; | |
double b; | |
a = 10/4; | |
b = 10/4; | |
cout << a << endl; | |
cout << b << endl; | |
} | |
void example5(){ | |
/* | |
Example: component assignment and | |
incremental operators | |
*/ | |
int i = 5; | |
i += 200; | |
cout << i << endl; | |
i -= 200; | |
cout << i << endl; | |
i *= 100; | |
cout << i << endl; | |
i /= 20; | |
cout << i << endl; | |
i %= 20; | |
cout << i << endl; | |
i++; | |
cout << i << endl; | |
i--; | |
cout << i << endl; | |
int j; | |
j = ++i; | |
cout << i << ' ' << j << endl; // 6 6 | |
j = i++; | |
cout << i << ' ' << j << endl; // 7 6 | |
int a=5, b, c; | |
b = ++a; // a is 6, b is also 6 | |
cout << "a is " << a << ", b is " << b << ", c is " << c << endl; | |
c = b++; // c is 6, b becomes 7 | |
cout << "a is " << a << ", b is " << b << ", c is " << c << endl; | |
} | |
void example6(){ | |
/* | |
Example: If else statements | |
*/ | |
int a = 61; | |
int gold_criteria = 80; | |
if(a < gold_criteria) | |
cout << "a does not get gold award." << endl; | |
else | |
cout << "Congratulations! a get gold award!" << endl; | |
cout << "--------" << endl; | |
// Wrong version: lead to compile error | |
// if(a != criteria) | |
// cout << "a does not meet criteria." << endl; | |
// cout << "Work harder" << endl; | |
// else | |
// cout << "Congratulations!" << endl; | |
// Right version | |
if(a < gold_criteria){ | |
cout << "a does not get gold award." << endl; | |
cout << "Work harder" << endl; | |
} else | |
cout << "Congratulations! a get gold award!" << endl; | |
cout << "--------" << endl; | |
// Define variables inside block | |
if(a < gold_criteria){ | |
cout << "a does not get gold award." << endl; | |
int silver_criteria = 60; | |
if(a >= silver_criteria) | |
cout << "You get silver award!" << endl; | |
} else | |
cout << "Congratulations!" << endl; | |
// Compile error: silver_criteria is not defined. | |
// cout << "Criteria for silver award is " << silver_criteria << " score."; | |
} | |
void example7(){ | |
/* | |
Example: Scopes in if else statements, especially | |
when there is only if statements | |
*/ | |
int a = 61; | |
int gold_criteria = 80; | |
int silver_criteria = 60; | |
// Wrong version | |
if(a > silver_criteria) | |
if(a > gold_criteria) | |
cout << "Congratulations! a get gold award!" << endl; | |
else // this else condition correspond to a > gold_criteria condition | |
cout << "Sorry, a does not get any award." << endl; | |
// Right version 1 | |
cout << "--------" << endl; | |
if(a > silver_criteria) | |
{ | |
if(a > gold_criteria) | |
cout << "Congratulations! a get gold award!" << endl; | |
} | |
else // this else condition correspond to a > silver_criteria condition | |
cout << "Sorry, a does not get any award." << endl; | |
// Right version 2 | |
cout << "--------" << endl; | |
if(a > silver_criteria) | |
if(a > gold_criteria) | |
cout << "Congratulations! a get gold award!" << endl; | |
else ; | |
else // this else condition correspond to a > silver_criteria condition | |
cout << "Sorry, a does not get any award." << endl; | |
} | |
void example8(){ | |
/* | |
Example: else if | |
*/ | |
int a = 61; | |
int gold_criteria = 80; | |
int silver_criteria = 60; | |
int bronze_criteria = 40; | |
if(a > gold_criteria) | |
cout << "Congratulations! a get the gold award!" << endl; | |
else if(a > silver_criteria) | |
cout << "Congratulations! a get the silver award!" << endl; | |
else if(a > bronze_criteria) | |
cout << "Congratulations! a get the bronze award!" << endl; | |
else | |
cout << "Sorry, a does not get any award." << endl; | |
} | |
void example9(){ | |
/* | |
Example: Take care of the boolean expression | |
many other things can return boolean value | |
*/ | |
// If the condition is integer 0, then it's treated as False if you need a T/F value | |
// If the condition is non-zero integer, then it's treated as True if you need a T/F value | |
if (1) | |
cout << "We are in the True branch of the if statement" << endl; | |
else | |
cout << "We are in the False branch of the if statement" << endl; | |
if (0) | |
cout << "We are in the True branch of the if statement" << endl; | |
else | |
cout << "We are in the False branch of the if statement" << endl; | |
if (5) | |
cout << "We are in the True branch of the if statement" << endl; | |
else | |
cout << "We are in the False branch of the if statement" << endl; | |
int a = 666; | |
// Assignment statement means evaluate expression, then put result in variable and produce new value of variable. So a=333 when produce 333 if it's a condition | |
if (a = 333) | |
cout << "We are in the True branch of the if statement" << endl; | |
else | |
cout << "We are in the False branch of the if statement" << endl; | |
if (a = 0) | |
cout << "We are in the True branch of the if statement" << endl; | |
else | |
cout << "We are in the False branch of the if statement" << endl; | |
if ("hello world") | |
cout << "We are in the True branch of the if statement" << endl; | |
else | |
cout << "We are in the False branch of the if statement" << endl; | |
} | |
void example10(){ | |
/* | |
Another example of setting scope for if statements | |
*/ | |
int a = 4, b = 4; | |
if (a == 4) | |
if (a == b) | |
a++; | |
if (a != b) | |
a++; | |
cout << a << endl; // a is 6, b is 4 | |
a = 4; | |
if (a == 4) { | |
if (a == b) | |
a++; | |
} | |
if (a != b) | |
a++; | |
cout << a << endl; | |
a = 4; | |
if (a == 4) { | |
if (a == b) | |
a++; | |
if (a != b) | |
a++; | |
} | |
cout << a << endl; | |
} | |
int main(){ | |
example1(); | |
// example2(); | |
// example3(); | |
// example4(); | |
// example5(); | |
// example6(); | |
// example7(); | |
// example8(); | |
// example9(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment