//C++ Primer Plus Edition 5//Chapter 6 Homework 1#include<iostream>#include<cctype>using namespace std;int main() { cout << "Enter text for analysis and type @" "to terminate input.\n"; char ch; cin.get(ch); while (ch != '@') { if (!isdigit(ch)) { if (islower(ch)) ch = toupper(ch); else if (isupper(ch)) ch = tolower(ch); cout << ch; } cin.get(ch); } system("pause"); return 0;}//错误代码，判断如果是数字执行continue的话就会循环判断字符是不是等于@，第一个字符就无限循环。//else同样。//while (ch != '@') {// if (isdigit(ch))// continue;// else if (isalpha(ch))// {// cout << ch << endl;// if (islower(ch))// ch = toupper(ch);// else if (isupper(ch))// ch = tolower(ch);// cout << ch;// }// else// continue;//}

//C++ Primer Plus Edition 5//Chapter 6 Homework 2#include<iostream>#include<cctype>const int SIZE = 10;using namespace std;int main() { double donation[10]; double average; int countbig = 0; int count=0; double sum = 0.0; cout << "Enter 10 numbers.If you want to quit, just input @:" << endl; for (int i = 0; i < SIZE; i++) { cin >> donation[i]; if (cin.fail()) break; else { sum += donation[i]; ++count; } } average = sum / count; cout << "The average number is: " << average << endl; for (int i = 0; i < count; i++) { if (donation[i] > average) ++countbig; } cout << countbig << " numbers bigger than average.\n"; system("pause"); return 0;}

//C++ Primer Plus Edition 5//Chapter 6 Homework 3#include<iostream>#include<cctype>using namespace std;void showmenu();int main() { showmenu(); char choice; cin.get(choice); while ((choice != 'c') && (choice != 'p') && (choice != 't') && (choice != 'g')) { cout << "Please enter a c,p,t,g: "; //cin.get(choice); cin.ignore(); } switch (choice) { case 'c': cout << "This is carnivore's answer.\n"; break; case 'p': cout << "This is pianist's answer.\n"; break; case 't': cout << "A maple is a tree.\n"; break; case 'g': cout << "This is game's answer.\n"; break; default: break; } system("pause"); return 0;}void showmenu() { cout << "Please enter one of the following choices.\n" "c)carnivore p)pianist\n" "t)tree g)game\n";}

//C++ Primer Plus Edition 5//Chapter 6 Homework 4#include<iostream>#include<cctype>using namespace std;//函数void showmenu();void dis_by_name();void dis_by_title();void dis_by_bop();void dis_by_pre();//定义常量const int strsize = 30;const int NUM = 5;//定义结构体struct bop { char fullname[strsize]; char title[strsize]; char bopname[strsize]; int preference;//0=fullname,1=title,2=bopname};bop people[5] = { { "Wimp Macho", "BOSS", "WM", 0 }, { "Raki Rhodes", "Manager", "Junior Programmer", 2 }, { "Celia Laiter", "MIPS", "CL", 1 }, { "Hoppy Hipman", "Analyst Trainee", "AT", 1 }, { "Pat Hand", "Student", "LOOPY", 2 }};char ch;//开始主函数int main() { showmenu(); cin >> ch; while ((ch != 'a') && (ch != 'b') && (ch != 'c') && (ch != 'd') && (ch != 'q')) { showmenu(); //cin.get(choice); cin.ignore(); } while (ch != 'q') { switch (ch) { case 'a': dis_by_name(); break; case 'b': dis_by_title(); break; case 'c': dis_by_bop(); break; case 'd': dis_by_pre(); break; default: break; } cout << "Next choice:" << endl; cin >> ch; while ((ch != 'a') && (ch != 'b') && (ch != 'c') && (ch != 'd') && (ch != 'q')) { showmenu(); //cin.get(choice); cin.ignore(); } } cout << "Bye!" << endl; system("pause"); return 0;}void showmenu() { 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)game\n";}void dis_by_name() { for (int i = 0; i < NUM; ++i) { cout << people[i].fullname << endl; }}void dis_by_title() { for (int i = 0; i < NUM; ++i) { cout << people[i].title << endl; }}void dis_by_bop() { for (int i = 0; i < NUM; ++i) { cout << people[i].bopname << endl; }}void dis_by_pre() { for (int i = 0; i < NUM; ++i) { if(people[i].preference==0) cout << people[i].fullname << endl; else if(people[i].preference==1) cout<< people[i].title << endl; else cout << people[i].bopname << endl; }}

//C++ Primer Plus Edition 5//Chapter 6 Homework 5#include<iostream>const double level1 = 0.1;const double level2 = 0.15;const double level3 = 0.2;using namespace std;int main() { double tvarp; double cus; cout << "Please input your money: "; cin >> tvarp; while (cin.good() && (tvarp > 0)) { if (tvarp <= 5000) cus = 0.0; else if (tvarp > 5000 && tvarp <= 15000) cus = (tvarp - 5000)*level1; else if (tvarp > 15000 && tvarp <= 35000) cus = 10000 * level1 + (tvarp - 15000)*level2; else if (tvarp > 35000) cus = 10000 * level1 + 20000 * level2 + (tvarp - 35000)*level3; cout << "您需要交税" << cus << " tvarp.\n"; cout << "Please input your money: "; cin >> tvarp; } cout << "Bye\n"; system("pause"); return 0;}

//C++ Primer Plus Edition 5//Chapter 6 Homework 6#include<iostream>#include<cctype>#include<string>using namespace std;struct donation{ string name; double money;};int main() { int num; int count = 0; cout << "Please input the number of donationer: "; cin >> num; cin.get();//吃掉换行符 donation *donaer = new donation[num]; for (int i = 0; i < num; i++) { cout << "Enter the name: "; getline(cin, donaer[i].name); cout << "Enter money: "; cin >> donaer[i].money; cin.get();//吃掉换行符 } cout << "****************************" << endl; cout << "Grand Patrons" << endl; cout << "****************************" << endl; for (int i = 0; i < num; i++) { if (donaer[i].money > 10000) { ++count; cout << "Name: " << donaer[i].name << endl; cout << "Donate Money: " << donaer[i].money << endl; cout << endl; } } if (count == 0) { cout << "None" << endl; } count = 0; cout << "****************************" << endl; cout << "Patrons" << endl; cout << "****************************" << endl; for (int i = 0; i < num; i++) { if (donaer[i].money <= 10000) { ++count; cout << "Name: " << donaer[i].name << endl; cout << "Donate Money: " << donaer[i].money << endl; cout << endl; } } if (count == 0) { cout << "None" << endl; } delete[] donaer; system("pause"); return 0;}

//C++ Primer Plus Edition 5//Chapter 6 Homework 7#include<iostream>#include<cctype>#include<string>using namespace std;int main() { string word; char ch; int yuan = 0; int fu = 0; int others = 0; cin >> word; while (word != "q") { ch = word[0]; if (isalpha(ch)) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') ++yuan; else ++fu; } else ++others; cin >> word; } cout << yuan << " words beginning with vowels." << endl; cout << fu << " words beginning with consonants." << endl; cout << others << " others." << endl; system("pause"); return 0;}

//C++ Primer Plus Edition 5//Chapter 6 Homework 8#include<iostream>#include<fstream>#include<cstdlib>using namespace std;const int MAXSIZE = 50;int main() { char filename[MAXSIZE]; ifstream inFile; cout << "Please enter the file's name: "; cin.getline(filename, MAXSIZE); inFile.open(filename); if (!inFile.is_open()) { cout << "Open this file error."<<endl; exit(EXIT_FAILURE); } char ch; int count = 0; inFile >> ch; while (inFile.good()) { ++count; inFile >> ch; } if (inFile.eof()) { cout << "Have reached end of this file.\n"; } else if (inFile.fail()) { cout << "Input terminated by data mismatch.\n"; } else cout << "unknown reason to stop.\n"; if (count == 0) { cout << "No data in this file.\n"; } else { cout << "There are " << count << " characters in this file.\n"; } inFile.close(); system("pause"); return 0;}

//C++ Primer Plus Edition 5//Chapter 6 Homework 9#include<iostream>#include<cctype>#include<string>#include<fstream>using namespace std;const int MAXSIZE = 50;struct donation{ string name; double money;};int main() { char filename[MAXSIZE]; ifstream inFile; cout << "Please enter the file's name: "; cin.getline(filename, MAXSIZE); inFile.open(filename); if (!inFile.is_open()) { cout << "Open this file error." << endl; exit(EXIT_FAILURE); } int num; int count = 0; inFile >> num; inFile.get();//吃掉换行符 donation *donaer = new donation[num]; for (int i = 0; i < num; i++) { getline(inFile, donaer[i].name); inFile >> donaer[i].money; inFile.get();//吃掉换行符 } cout << "****************************" << endl; cout << "Grand Patrons" << endl; cout << "****************************" << endl; for (int i = 0; i < num; i++) { if (donaer[i].money > 10000) { ++count; cout << "Name: " << donaer[i].name << endl; cout << "Donate Money: " << donaer[i].money << endl; cout << endl; } } if (count == 0) { cout << "None" << endl; } count = 0; cout << "****************************" << endl; cout << "Patrons" << endl; cout << "****************************" << endl; for (int i = 0; i < num; i++) { if (donaer[i].money <= 10000) { ++count; cout << "Name: " << donaer[i].name << endl; cout << "Donate Money: " << donaer[i].money << endl; cout << endl; } } if (count == 0) { cout << "None" << endl; } delete[] donaer; inFile.close(); system("pause"); return 0;}