-
-
Save derekmma/98b769fb0f2da4b7f04e50d9edd4a7a8 to your computer and use it in GitHub Desktop.
Demo program for Week 3, 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 3 | |
Oct 15, 2021, Mingyu Derek Ma | |
*/ | |
#include <iostream> | |
#include <string> | |
#include <cctype> | |
using namespace std; | |
void example1(){ | |
// Print out positive integer 1~n inclusively | |
int n = -1; | |
// For loop | |
// Style 1 | |
for (int i = 1; i <= n; ++i){ | |
cout << i << endl; | |
} | |
// Style 2 | |
cout << "----" << endl; | |
int i = 1; | |
for (; i <= n ; ++i){ | |
cout << i << endl; | |
} | |
// Style 3 | |
cout << "----" << endl; | |
i = 1; | |
for (; i <= n ;){ | |
cout << i << endl; | |
++i; | |
} | |
// Style 4 | |
cout << "----" << endl; | |
i = 1; | |
for (;;){ | |
if (i > n) | |
break; | |
cout << i << endl; | |
++i; | |
} | |
// While loop | |
cout << "---- while loop" << endl; | |
i = 1; | |
while (i <= n){ | |
cout << i << endl; | |
++i; | |
} | |
// do...while loop | |
cout << "---- do while loop" << endl; | |
i = 1; | |
do { | |
cout << i << endl; | |
++i; | |
} while (i <= n); | |
// Test input: 5 -1 | |
} | |
void example2(){ | |
char input; | |
cout << "Enter any character: "; | |
cin >> input; | |
cout << "The character you entered is: " << input << endl; | |
cout << "Its ASCII code is: " << int(input) << endl; | |
// Arithmetic and relational operations | |
cout << "---" << endl; | |
cout << '\n' << endl; | |
if ('a' < 'b'){ | |
cout << "In true branch because char a is smaller than char b" << endl; | |
} | |
cout << 'a' - 32 << endl; | |
cout << char('a' - 32) << endl; | |
cout << 'x' + ('A' - 'a') << endl; | |
cout << char('x' + ('A' - 'a')) << endl; | |
// Some function enabled by include <cctype> | |
cout << "---" << endl; | |
if (isalpha(input)) | |
cout << "That's an alphabetic character.\n"; | |
if (isdigit(input)) | |
cout << "That's a numeric digit.\n"; | |
if (islower(input)) | |
cout << "The letter you entered is lowercase.\n"; | |
if (isupper(input)) | |
cout << "The letter you entered is uppercase.\n"; | |
if (isspace(input)) | |
cout << "That's a whitespace character.\n"; | |
cout << tolower(input) << endl; | |
cout << char(tolower(input)) << endl; | |
cout << toupper(input) << endl; | |
cout << char(toupper(input)) << endl; | |
} | |
void example3(){ | |
cout << "Enter a string: "; | |
string s = "abcdefghijklmnopq"; | |
getline(cin, s); | |
cout << "String size: " << s.size() << endl; | |
for (int i=0; i<s.size(); i++){ | |
cout << i << " " << s[i] << s.at(i) << endl; | |
if (s.at(i)=='h') { // note we are using char H rather than string H for condition | |
if (s.at(i+1)=='e') | |
cout << "We found a mention of \"he\" at index " << i << endl; | |
} | |
} | |
// Testing examples | |
// He likes to tell joke, he just told one. | |
// it will work for this input | |
// abcdefghijklmn | |
// it will work for this input | |
// abcdefgh | |
// undefined behavior since i+1 for s.at(i+1) is \0 | |
} | |
int main(){ | |
example1(); | |
// example2(); | |
// example3(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment