├── README.md ├── C02 ├── BackToFront.cpp ├── square.cpp ├── VectorFloat.cpp ├── number.cpp ├── one.cpp ├── key.cpp └── OnceALine.cpp ├── print.h ├── zlz_workpath └── example.cpp ├── .gitignore ├── ex3.5.cpp ├── exercise.cpp ├── if-else.cpp ├── StringArray.cpp ├── primeNumber.cpp ├── .gitattributes ├── print.cpp ├── exStruct.cpp ├── StructArray.cpp └── menu.cpp /README.md: -------------------------------------------------------------------------------- 1 | Exercise 2 | ================= 3 | 4 | C++ exercise 5 | -------------------------------------------------------------------------------- /C02/BackToFront.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/reku-push/C-Exercise/HEAD/C02/BackToFront.cpp -------------------------------------------------------------------------------- /print.h: -------------------------------------------------------------------------------- 1 | void function(); 2 | char function1(const char op1); 3 | int function2(const int op2); 4 | float function3(const float op3); -------------------------------------------------------------------------------- /zlz_workpath/example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | cout << "This is an example!" << endl; 7 | return 0; 8 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | 9 | # Compiled Static libraries 10 | *.lai 11 | *.la 12 | *.a 13 | -------------------------------------------------------------------------------- /ex3.5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int exercise() 5 | { 6 | int x = 1, y = 2, z = 3; 7 | int A, A1; 8 | A = x + y - 2 / 2 + z; 9 | A1 = x + (y - 2) / (2 + z); 10 | cout << A << " " << A1 << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /exercise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "print.h" 3 | 4 | int main() 5 | { 6 | char op1 = 'a'; 7 | int op2 = 10; 8 | float op3 = 2.5; 9 | function(); 10 | function1(op1); 11 | function2(op2); 12 | function3(op3); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /C02/square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int square() 5 | { 6 | float r,s; 7 | cout << "Enter the radius of a circle:" << endl; 8 | cin >> r; 9 | s = 3.14 * r * r; 10 | cout << "The sqaure of the circle is " << s << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /if-else.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int ifelse() 5 | { 6 | int i; 7 | cout << "type a number and 'Enter'" << endl; 8 | cin >> i; 9 | i > 5 ? cout << " i > 5 " << endl : i < 5 ? cout << "i < 5" << endl : 10 | cout << "i = 5" << endl; 11 | return 0; 12 | } -------------------------------------------------------------------------------- /C02/VectorFloat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | vector nums; 8 | float num; 9 | cout << "Enter ten numbers:" << endl; 10 | for (int i = 0; i <= 9; i++) 11 | { 12 | cin >> num; 13 | nums.push_back(num); 14 | } 15 | for (int i = 0; i <= 9; i++) 16 | cout << nums[i] << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /StringArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int StringArray() 6 | { 7 | string s[10]; 8 | cout << "Enter ten strings:" << endl; 9 | for (int i = 0; i < 10;i++) 10 | { 11 | cin >> s[i]; 12 | } 13 | for (int i = 0;i < 10;i++) 14 | { 15 | cout << "s[" << i << "] = " << s[i] << endl; 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /C02/number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int WordNumber() 8 | { 9 | vector words; 10 | ifstream in ("exp2.3.cpp"); 11 | string word; 12 | int i = 0; 13 | while (in >> word) 14 | { 15 | words.push_back(word); 16 | i++; 17 | } 18 | cout << "The number of words is " << i << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /C02/one.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int one() 8 | { 9 | vector v; 10 | ifstream in("string.cpp"); 11 | string line; 12 | string s1 = ""; 13 | while(getline(in, line)) 14 | v.push_back(line); // Add the line to the end 15 | // Add line numbers: 16 | for(int i = 0; i < v.size(); i++) 17 | s1 += v[i]; 18 | cout << s1 << endl; 19 | return 0; 20 | } -------------------------------------------------------------------------------- /C02/key.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int key() 8 | { 9 | vector words; 10 | ifstream in("special.cpp"); 11 | string word, key; 12 | int j = 0; 13 | cout << "Enter the word you want:" << endl; 14 | cin >> key; 15 | while (in >> word) 16 | words.push_back (word); 17 | for (int i = 0; i < words.size(); i++) 18 | { 19 | if (words[i] == word) 20 | j++; 21 | } 22 | cout << "The number of the word \"" << key << "\" is " << j << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /primeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int primeNumber() 6 | { 7 | int n; 8 | float k; 9 | cout << "Enter a number: " << endl; 10 | cin >> n; 11 | for (int i = 2; i < n + 1; i++) 12 | { 13 | k = sqrt((float)i); 14 | if (i == 2) 15 | cout << 2 << endl; 16 | for (int j = 2; j < k + 1; j++) 17 | { 18 | if (i % j == 0) 19 | break; 20 | if (j > k) 21 | cout << i << endl; 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /C02/OnceALine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int OnceALine() 8 | { 9 | vector v; 10 | ifstream in("OnceALine.cpp"); 11 | string line; 12 | char input; 13 | int i = 0; 14 | while(getline(in, line)) 15 | v.push_back(line); 16 | while(i < v.size()) 17 | { 18 | cout << i << ":" << v[i] << endl; 19 | do 20 | { 21 | cout << "press ENTER to display next line.." << endl; 22 | input = getchar(); 23 | }while(input != '\n'); 24 | i++; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /print.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void function() 5 | { 6 | cout << "This function is void" << endl; 7 | } 8 | 9 | char function1(const char op1) 10 | { 11 | cout << "This function is char. The parameter is " 12 | << op1 << endl; 13 | return op1; 14 | } 15 | 16 | int function2(const int op2) 17 | { 18 | cout << "This function is int. The parameter is " 19 | << op2 << endl; 20 | return op2; 21 | } 22 | 23 | float function3(const float op3) 24 | { 25 | cout << "This function is float. The parameter is " 26 | << op3 << endl; 27 | return op3; 28 | } -------------------------------------------------------------------------------- /exStruct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef struct 6 | { 7 | string s1; 8 | string s2; 9 | int i; 10 | }Exercise; 11 | 12 | int main() 13 | { 14 | Exercise e; 15 | Exercise* ep = &e; 16 | e.s1 = "a"; 17 | e.s2 = "b"; 18 | e.i = 1; 19 | cout << "e.s1 = " << e.s1 << " " 20 | << "e.s2 = " << e.s2 << " " 21 | << "e.i = " << e.i << endl; 22 | ep->s1 = "c"; 23 | ep->s2 = "d"; 24 | ep->i = 2; 25 | cout << "e.s1 = " << e.s1 << " " 26 | << "e.s2 = " << e.s2 << " " 27 | << "e.i = " << e.i << endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /StructArray.cpp: -------------------------------------------------------------------------------- 1 | //: C03:StructArray.cpp 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // An array of struct 7 | #include 8 | using namespace std; 9 | 10 | typedef struct { 11 | int i, j, k; 12 | } ThreeDpoint; 13 | 14 | int main() { 15 | ThreeDpoint p[10]; 16 | cout << "sizeof(ThreeDpoint) = "<< sizeof(ThreeDpoint) << endl; 17 | for(int i = 0; i < 10; i++) { 18 | p[i].i = i + 1; 19 | p[i].j = i + 2; 20 | p[i].k = i + 3; 21 | } 22 | for(int i = 0; i < 10; i++) 23 | { 24 | cout << "&p[" << i << "].i = " 25 | << (long)&p[i].i << endl; 26 | cout << "&p[" << i << "].j = " 27 | << (long)&p[i].j << endl; 28 | cout << "&p[" << i << "].k = " 29 | << (long)&p[i].k << endl; 30 | } 31 | } ///:~ 32 | -------------------------------------------------------------------------------- /menu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | char c; 6 | bool quit = false; 7 | while(quit == false) { 8 | cout << "MAIN MENU:" << endl; 9 | cout << "l: left, r: right, q: quit -> "; 10 | cin >> c; 11 | switch(c) 12 | { 13 | case 'l' : cout << "LEFT MENU:" << endl; 14 | cout << "select a or b:"; 15 | cin >> c; 16 | switch(c) 17 | { 18 | case 'a' : cout << "you chose 'a'" << endl; 19 | continue; 20 | case 'b' : cout << "you chose 'b'" << endl; 21 | continue; 22 | default : cout << "you didn't chose a or b" << endl; 23 | continue; 24 | } 25 | case 'r' : cout << "RIGHT MENU:" << endl; 26 | cout << "select c or d:"; 27 | cin >> c; 28 | switch(c) 29 | { 30 | case 'c' : cout << "you chose 'c'" << endl; 31 | continue; 32 | case 'd' : cout << "you chose 'd'" << endl; 33 | continue; 34 | default : cout << "you didn't chose c or d" << endl; 35 | continue; 36 | } 37 | case 'q' : cout << "quitting menu" << endl; 38 | quit = true; 39 | break; 40 | default : cout << "Please use l,r or q!" << endl; 41 | } 42 | } 43 | } 44 | 45 | --------------------------------------------------------------------------------