C++primer plus 第六章程式設計練習
阿新 • • 發佈:2019-01-13
本人用code::block 編寫,如需參考,善用Ctrl+shift+C 和 Ctrl + shift + X 快捷鍵
如有任何錯誤或疑問,歡迎留言
//6.1 #include <iostream> #include <cctype> #include <string> #include <fstream> #include <cstdlib> using namespace std; const int strsize = 128; struct bop { char fullname[strsize]; char title[strsize]; char bopname[strsize]; int preference; }; //6.1 int P6_1(void) { using namespace std; char ch; while ((ch = cin.get()) != '@') { if(isdigit(ch)) continue; else if(islower(ch)) cout << (char)toupper(ch) ; else if (isupper(ch)) cout << (char)tolower(ch) ; } return 0; } //6.2 int P6_2(void) { double donation[10]; double sum = 0, ave; int count = 0, clarger = 0; while (count<10 && cin >> donation[count]) { sum += donation[count]; ++count; } ave = sum / count; cout << "You have entered " << count << " numbers.\n"; cout << "sum = " << sum << endl; cout << "average = " << ave << endl; for ( int i =0; i<count; i++ ) { if(donation[i]>ave) ++clarger; } cout << "There are " << clarger << " number(s) lager than average value.\n"; return 0; } //6.3 int P6_3(void) { char ch; cout << "Please enter one of the following choices: \n"; cout << "c) carnivore" "p) pianist\n"; cout << "t) tree g) game\n"; cout << "Please enter a c, p, t or g: "; while (cin.get(ch)) { cin.get(); switch(ch) { case 'c' : cout << "Tiger is a carnivore.\n"; break; case 'p' : cout << "Mozart is a pianist.\n"; break; case 't' : cout << "A maple is a tree.\n"; break; case 'g' : cout << "GTA is a game.\n"; break; } cout << "Please enter a c, p, t or g: "; } return 0; } //6.4 void P6_4() { bop datbop[5] = { {"Wimp Macho", "123", "zhangsan", 2}, {"Raki Rhodes", "456","lisi", 1}, {"Celia Laiter", "789", "wangwu", 0}, {"Hoppy Hipman", "234", "zhaoliu", 2}, {"Pat Hand", "567", "chenqi", 1}, }; int i = 0; int exit = 0; cout << "Benevolent Order of Programmers Report\n"; cout << "a. display by name b. display by title\n"; cout << "c. displea by bopname d. display by preference\nq. quit\n"; char ch; cout << "Enter your choice: "; while(cin.get(ch)) { cin.get(); switch(ch) { case 'a': for(i=0; i<5; i++) cout << datbop[i].fullname << endl; break; case 'b': for(i=0; i<5; i++) cout << datbop[i].title << endl; break; case 'c': for(i=0; i<5; i++) cout << datbop[i].bopname << endl; break; case 'd': for(i=0; i<5; i++) { if (datbop[i].preference==0) cout << datbop[i].fullname << endl; else if (datbop[i].preference==1) cout << datbop[i].title << endl; else if (datbop[i].preference==2) cout << datbop[i].bopname << endl; } break; case 'q': exit = 1; break; } if (exit == 1) { exit = 0; break; } cout << "Next choice: "; } cout << "Bye!\n"; } //6.5 void P6_5(void) { int tvarp; cout << "Enter your tvarps: "; while( (cin>>tvarp) && tvarp>=0) { if(tvarp <= 5000) cout << "No tax!\n"; else if(tvarp>5000 && tvarp <=15000) cout << "Your tax is " << (tvarp-5000)*0.1 << endl; else if(tvarp>15000 && tvarp <=35000) cout << "Your tax is " << 1000+(tvarp-15000)*0.15 << endl; else if(tvarp>35000) cout << "Your tax is " << 4000+(tvarp-35000)*0.2 << endl; cout << "Please enter your tvarps (enter a negative value or none value to end): "; } cout << "Bye!"; } struct donate { string name; double money; }; //6.6 void P6_6(void) { donate *dolist = new donate [5]; int i = 0, gra = 0; while (i<5 && cin) { cout << "Enter your name: "; getline(cin, (dolist+i)->name); cout << "Enter your money: "; cin >> (dolist+i)->money; cin.get(); i++; } cout << "Grand Patrons:\n"; for (i=0; i<5; ++i) { if(dolist[i].money>=10000) { cout << dolist[i].name << ": " << dolist[i].money << endl; gra++; } } if (gra == 0) cout << "none!\n"; gra = 0; cout << "Patrons: \n"; for (i=0; i<5; ++i) if(dolist[i].money<10000) { cout << dolist[i].name << ": " << dolist[i].money << endl; gra++; } if (gra == 0) cout << "none!\n"; return; } //6.7 void P6_7() { string word; int vowels = 0; int consonants = 0; int others = 0; while(cin >> word) { if(word.length() == 1 && word[0]=='q') break; if( isalpha(word[0]) ) { if (word[0]=='a'||word[0]=='A'|| word[0]=='e'||word[0]=='E'|| word[0]=='i'||word[0]=='I'|| word[0]=='o'||word[0]=='O'|| word[0]=='u'||word[0]=='U') vowels++; else consonants++; } else others++; } cout << vowels << " words beginning with vowels\n"; cout << consonants << " words beginning with consonants\n"; cout << others << " others"; return; } //6.8 void P6_8() { ifstream fin; fin.open("practice6.8.txt"); if (!fin.is_open()) { cout << "Can not open the file.\n"; cout << "Program terminated\n"; exit(EXIT_FAILURE); } char ch; int counter = 0; while(fin.get(ch)) { counter++; fin.get(ch); } fin.close(); cout << counter << " characters inputed."; return; } //6.9 void P6_9(void) { fstream fin; fin.open("practice6.9.txt"); int donate_size; fin >> donate_size; fin.get(); donate *dolist = new donate [donate_size]; int i = 0, gra = 0; while (i<donate_size && fin) { getline(fin, (dolist+i)->name); fin >> (dolist+i)->money; fin.get(); i++; } cout << "Grand Patrons:\n"; for (i=0; i<donate_size; ++i) { if(dolist[i].money>=10000) { cout << dolist[i].name << ": " << dolist[i].money << endl; gra++; } } if (gra == 0) cout << "none!\n"; gra = 0; cout << "Patrons: \n"; for (i=0; i<donate_size; ++i) if(dolist[i].money<10000) { cout << dolist[i].name << ": " << dolist[i].money << endl; gra++; } if (gra == 0) cout << "none!\n"; return; } int main() { P6_9(); return 0; }