【第六章】C++ Primer plus 的编程练习题(选取部分)
2018-02-08 08:25
507 查看
/*********************************** 2017年10月16日13:41:12 Athor:xiyuan255 Course:C++ Contain:review6.cpp Reference: C++ Primer plus 说明:C++ Primer plus第六章的练习题(选取部分) 【 P200 】 *************************************/ // review6.cpp -- C++ Primer plus 第六章练习题 #include <iostream> #include <string> // exsampleNo2 field #include <array> // exsampleNo2 field #include <fstream> // exsampleNo8/exsampleNo9 field file I/O #include <cstdlib> // exsampleNo8/exsampleNo9 field EXIL_FAILURE using namespace std; void exampleNo1(void); void exampleNo2(void); void exampleNo3(void); void exampleNo4(void); void exampleNo5(void); void exampleNo6(void); void exampleNo7(void); void exampleNo8(void); void exampleNo9(void); int main(void) { //exampleNo1(); // example 1 test //exampleNo2(); // example 2 test //exampleNo3(); // example 3 test //exampleNo4(); // example 4 test //exampleNo5(); // example 5 test //exampleNo6(); // example 6 test //exampleNo7(); // example 7 test //exampleNo8(); // example 8 test exampleNo9(); // example 9 test return 0; } void exampleNo1(void) { cout << "请输入数据信息: \n"; char ch; while (cin.get(ch) && ch != '@') { if (islower(ch)) { cout << (char)((ch-'a') + 'A'); } else if (isupper(ch)) { cout << (char)((ch-'A') + 'a'); } if (ch == '\n') cout << endl; } cout << "\nDone.\n"; } /** 输出结果: [[ vjhvjsJSHDvjd237647JDFKJjdfk ]] VJHVJSjshdVJDjdfkjJDFK [[ vsdghdhv273HGJSDGJ@vdnvf8487DFHJ ]] VSDGHDHVhgjsdgj Done. */ void exampleNo2(void) { const int ArSize = 10; array<double, ArSize> donstion_array; cout << "请输入最多10个数的值(用回车键区分):\n"; int input_count = 0; double donstion; while (cin >> donstion) { donstion_array[input_count++] = donstion; } int i; double sum = 0.0; for (i = 0; i < input_count; i++) { sum += donstion_array[i]; } double average = sum / input_count; int count = 0; for (i = 0; i < input_count; i++) { if (donstion_array[i] > average) count++; } cout << "这" << input_count << "个数的平均值是:" << average << ", 有" << count << "个数超过平均值。\n"; } /** 输出结果: 请输入最多10个数的值(用回车键区分): 12.5 32.4 23.5 26.4 32.5 5.6 8.952 ds 这7个数的平均值是:20.2646, 有4个数超过平均值。 */ void exampleNo3(void) { cout << "Please enter one of the following choices: \n" << "c) carnivore p) pianist\n" << "t) tree g) game\n"; char ch; cin.get(ch); while (cin.get(ch)) { switch (ch) { case 'c': cout << "A tiger.\n"; break; case 'p': cout << "Beethoven is a pianist.\n"; break; case 't': cout << "A maple is tree.\n"; break; case 'g': cout << "The league of heroes is game.\n"; break; default: if ('\n' == ch) cout << "Please enter a c, p, t, or g:"; break; } if ('a' == ch || 'c' == ch || 'p' == ch || 't' == ch || 'g' == ch) break; } } /** 输出结果: Please enter one of the following choices: c) carnivore p) pianist t) tree g) game f Please enter a c, p, t, or g:q Please enter a c, p, t, or g:t A maple is tree. */ void exampleNo4(void) { const int strsize = 20; const int SIZE = 5; // Benevolent Order of Programers name structure struct bop { char fullname[strsize]; // real name char title[strsize]; // job title char bopname[strsize]; // secret BOP name int preference; // 0 = fullname, 1 = title, 2 = bopname }; bop member[SIZE] = { {"Wimp Macho", "C Programmer", "WMCP", 0}, {"Raki Rhodes", "Junior Programmer", "RRJP", 1}, {"Celia Laiter", "JAVA Programmer", "MIPS", 2}, {"Hoppy Hipman", "Analyst Trainee", "HHAT", 1}, {"Pat Hand", "C++ Programmer", "LOOPY", 2}, }; cout << "Please enter one of the following choices:\n" << "a. display by name b. display by title\n" << "c. display by bopname d. display by preference\n" << "q. quit \n"; char choice; cout << "Enter your choice:"; while (cin.get(choice) && choice != 'q') { for (int i = 0; i < SIZE; i++) { if ('a' == choice) { cout << member[i].fullname << endl; } else if ('b' == choice) { cout << member[i].title << endl; } else if ('c' == choice) { cout << member[i].bopname << endl; } else if ('d' == choice) { if (member[i].preference == 0) { cout << member[i].fullname << endl; } else if (member[i].preference == 1) { cout << member[i].title << endl; } else if (member[i].preference == 2) { cout << member[i].bopname << endl; } } } if ('\n' == choice) cout << "Next choice:"; } cout << "Bye!\n"; } /** 输出结果: Please enter one of the following choices: a. display by name b. display by title c. display by bopname d. display by preference q. quit Enter your choice:a Wimp Macho Raki Rhodes Celia Laiter Hoppy Hipman Pat Hand Next choice:d Wimp Macho Junior Programmer MIPS Analyst Trainee LOOPY Next choice:q Bye! */ void exampleNo5(void) { cout << "请输入你的收入:"; int income; double sum_income; while (cin >> income && income > 0) { sum_income = 0.0; if (income > 35000) { sum_income += (income - 35000)*0.20; income = 35000; } if (income > 15000) { sum_income += (income - 15000)*0.15; income = 15000; } if (income > 5000) { sum_income += (income - 5000)*0.10; income = 5000; } cout << "你的所得税为:" << sum_income << " tvarps.\n"; cout << "请输入你的收入:"; } if (cin.fail()) cout << "输入类型不匹配!\n"; } /** 输出结果: 请输入你的收入:38000 你的所得税为:4600 tvarps. 请输入你的收入:f 输入类型不匹配! */ void exampleNo6(void) { struct donor_msg { string name; double money; }; cout << "Enter Donor Number: "; int Number; cin >> Number; cin.get(); donor_msg* donor = new donor_msg[Number]; for (int i = 0; i < Number; i++) { cout << "Enter the #" << i+1 << " donor name: "; getline(cin, donor[i].name); if (!donor[i].name.size()) donor[i].name = "none"; cout << "donor money: "; cin >> donor[i].money; cin.get(); } cout << "\r- Grand Patrons -\n"; for (int i = 0; i < Number; i++) { if (donor[i].money > 10000) { cout << "Patrons: " << donor[i].name << ", Money: " << donor[i].money << endl; } } cout << "\r- Patrons -\n"; for (int i = 0; i < Number; i++) { cout << "Patrons: " << donor[i].name << ", Money: " << donor[i].money << endl; } } /** 输出结果: Enter Donor Number: 5 Enter the #1 donor name: donor money: 12000 Enter the #2 donor name: zhangsan donor money: 12036 Enter the #3 donor name: lishi donor money: 9888 Enter the #4 donor name: donor money: 12456 Enter the #5 donor name: wangwu donor money: 12000 - Grand Patrons - Patrons: none, Money: 12000 Patrons: zhangsan, Money: 12036 Patrons: none, Money: 12456 Patrons: wangwu, Money: 12000 - Patrons - Patrons: none, Money: 12000 Patrons: zhangsan, Money: 12036 Patrons: lishi, Money: 9888 Patrons: none, Money: 12456 Patrons: wangwu, Money: 12000 */ int IsVowel(char ch) { int isLowercaseVowel, isUppercaseVowel; isLowercaseVowel = ('a' == ch || 'e' == ch || 'i' == ch || 'o' == ch || 'u' == ch); isUppercaseVowel = ('A' == ch || 'E' == ch || 'I' == ch || 'O' == ch || 'U' == ch); if (isLowercaseVowel || isUppercaseVowel) return 1; // 为元音字母 else return 0; // 为辅音字母 } void exampleNo7(void) { cout << "Enter words (q to quit):\n"; int vpwels = 0, consonants= 0, others = 0; // 方法一: #if 0 string str = NULL; while (cin >> str && str != "q") { //cout << "str: " << str << endl; if (isalpha(str[0])) { if (IsVowel(str[0])) { vpwels++; }else { consonants++; } } else { others++; } } #endif // 方法二: char ch[20] = {0, }; while (cin >> ch && strcmp(ch, "q")) { //cout << "ch: " << ch << endl; if (isalpha(ch[0])) { if (IsVowel(ch[0])) { vpwels++; }else { consonants++; } } else { others++; } } cout << vpwels << " words beginning with vowels\n" << consonants << " words beginning with consonants\n" << others << " others\n"; } /** 输出结果: Enter words (q to quit): The 12 awesome oxen ambled quietly across 15 meters of lawn. q 5 words beginning with vowels 4 words beginning with consonants 2 others */ void exampleNo8(void) { char filename[20]; cout << "Please enter filename for open: "; cin >> filename; ifstream inFile; inFile.open(filename); if (!inFile.is_open()) { cout << "The file open error " << "Programming terminating.\n"; exit(EXIT_FAILURE); } char value; int count = 0; while (inFile >> value) { //cout << value; count++; } if (inFile.eof()) cout << "End of file reached.\n"; else if (inFile.fail()) cout << "Input terminated by data mismach.\n"; else cout << "Input terminated for unknown reasom.\n"; if (0 == count) cout << "No data processed.\n"; else cout << "Input the " << count << " characters.\n"; inFile.close(); } /** 输出结果: Please enter filename for open: testdata.txt End of file reached. Input the 70 characters. */ void exampleNo9(void) { struct donor_msg { string name; double money; }; //cout << "Enter Donor Number: "; ifstream inFile; inFile.open("donor.txt"); if (!inFile.is_open()) { cout << "The file open error " << "Programming terminating.\n"; exit(EXIT_FAILURE); } int Number; inFile >> Number; inFile.get(); donor_msg* donor = new donor_msg[Number]; for (int i = 0; i < Number; i++) { getline(inFile, donor[i].name); if (!donor[i].name.size()) donor[i].name = "none"; cout << "Enter the #" << i+1 & ac27 lt;< " donor name: " << donor[i].name << endl; inFile >> donor[i].money; cout << "donor money: " << donor[i].money << endl; inFile.get(); } cout << "\r- Grand Patrons -\n"; for (int i = 0; i < Number; i++) { if (donor[i].money > 10000) { cout << "Patrons: " << donor[i].name << ", Money: " << donor[i].money << endl; } } cout << "\r- Patrons -\n"; for (int i = 0; i < Number; i++) { cout << "Patrons: " << donor[i].name << ", Money: " << donor[i].money << endl; } } /** 输出结果: Enter the #1 donor name: Sam Stone donor money: 2000 Enter the #2 donor name: Freida Flass donor money: 100500 Enter the #3 donor name: Tammy Tubbs donor money: 5000 Enter the #4 donor name: Rich Raptor donor money: 50000 - Grand Patrons - Patrons: Freida Flass, Money: 100500 Patrons: Rich Raptor, Money: 50000 - Patrons - Patrons: Sam Stone, Money: 2000 Patrons: Freida Flass, Money: 100500 Patrons: Tammy Tubbs, Money: 5000 Patrons: Rich Raptor, Money: 50000 */
// donor.txt 4 Sam Stone 2000 Freida Flass 100500 Tammy Tubbs 5000 Rich Raptor 50000
// testdata.txt This is test data feil: A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
相关文章推荐
- 【第五章】C++ Primer plus 的编程练习题(选取部分)
- 【第八章】C++ Primer plus 的编程练习题(选取部分)
- 【第三章】C++ Primer plus 的编程练习题(选取部分)
- 【第四章】C++ Primer plus 的编程练习题(选取部分)
- 【第二章】C++ Primer plus 的编程练习题(选取部分)
- C++ Primer Plus第六版 第六章 编程练习答案
- C++ primer plus(中文版) 第五章编程练习题
- C++ Primer Plus 第五版 第六章 编程练习 7
- C++ Primer Plus第五版 第六章 编程练习答案
- 【第十一章】C++ Primer plus 的编程练习题
- 【第九章】C++ Primer plus 的编程练习题
- c++ primer plus(第6版)中文版 第六章编程练习答案
- C++ primer plus编程第六章练习习题代码
- 【第十二章】C++ Primer plus 的编程练习题
- C++ Primer Plus(第五版)第五章编程练习题6(关于string难点哦)
- 【第十三章】C++ Primer plus 的编程练习题
- 【第十四章】C++ Primer plus 的编程练习题
- C++ Primer Plus课后编程练习——第七章 函数-C++的编程模块(部分)
- C++ Primer Plus (第六版)第六章编程练习参考答案
- 【第十五章】C++ Primer plus 的编程练习题