复习题
//6.10 //1 两个版本将给出相同的答案,但是if - else效率更高,因为第一个版本在检测到空格后再次判断是否为换行符 而第二个版本不会检测,因此不会浪费时间 //2 ++ch和ch + 1得到的数值相同,但是++ch的类型为char,而ch + 1的类型为int,一个输出字符一个输出数字 //3 漏了一个等号 Hi! H$i$!$ $Send $10 or $20 now! S$e$n$d$ $ct1 = 0, ct2 = 9 会读取换行符,因此输出\n后再在第二行输出一个$ //4 wieght >= 125 && weight < 125; ch == 'q' || ch == 'Q'; !(x % 2) && x != 26; !(x % 2) && x % 26; guest == 1 || donation >= 1000 && donatin <= 2000; (ch <= 'z' && ch >= 'a') || (ch <= 'Z' && ch >= 'A'); //5 不一样,x为int和x为bool //6 (x > 0) ? x : -x; //7 switch (ch) { case 'A':a_grade++; break; case 'B':b_grade++; break; case 'C':c_grade++; break; case 'D':d_grade++; break; default:f_grade++; } //8 如果使用整数标签,当用户输入了非整数时,程序会因为整数输入不能处理字符而挂起 但是如果使用字符标签,用户输入整数时可以把整数当作字符处理,执行default部分 //9 int line = 0; char ch; while (cin.get(ch) && ch!= 'Q'); { if (ch == '\n') line++; }
编程练习
//第六章编程练习 #include <iostream> #include<cctype> #include<iomanip> //使用setw(),控制输出格式,practice 3 #include<string> #include<fstream> using namespace std; //practice 1 void p6_1() { cout << "Enter text for output,and @ to terminate input.\n"; char ch = 0; while ((ch = cin.get()) != '@') { if (isdigit(ch)) continue; else if (islower(ch)) cout << (char)toupper(ch); else if (isupper(ch)) cout << (char)tolower(ch); else cout << ch; } cout << "Done!"<<endl; } //practice 2 void p6_2() { double name[10]; int num = 0; double average = 0; double sum = 0; cout << "Please enter most 10 numbers:\n"; int enter = 0; while (enter<10 && cin >> name[enter]) { sum += name[enter]; enter++; } average = sum / enter; for (int i = 0; i < enter; i++) if (name[i] > average) num++; if (sum == 0) cout << "There is no input effective!" << endl; else cout << "There are " << num << " numbers bigger than the average of them : " << average << endl; } //practice 3 void p6_3() { cout << "Please enter one of the following choices:\n"; cout.flags(ios::left); //定义setw为左对齐 cout << setw(20) << "c) carnivore" << "p) pianist\n"; cout << setw(20) << "t) tree" << "g) game\n"; char ch = 0; bool exit = 0; while (cin >> ch && !exit) //不成功需要重新输入,因此需要变量查看是否已输入正确的字符 { switch (ch) { case 'c': cout << "one\n"; exit = 1; break; case 'p': cout << "two\n"; exit = 1; break; case 't': cout << "A maple is a tree.\n"; exit = 1; break; case 'g': cout << "four\n"; exit = 1; break; default:cout << "Please enter a c, p, t, or g: "; } } } //practice 4 const unsigned int Arsize = 50; struct bop //定义结构 { char fullname[Arsize]; char title[Arsize]; char bopname[Arsize]; int preference; }; void p6_4() { cout << "Benevolent Order of Programmers Reports\n"; cout << left << setw(30) << "a. display by name" << "b. display by title\n"; ; //定义setw为左对齐 cout << left << setw(30) << "c. display by bopname" << "d. display by preference\n"; cout << "q. quit\n"; const struct bop boparray[5] = //初始化 { {"Wimp Macho", "one title", "one bopname", 0}, {"Raki Rhodes", "Junior Programmer", "two bopname", 1}, {"Celia Laiter", "three title", "MIPS", 2}, {"Hoppy Hipman", "Analyst Trainee", "four bopname", 1}, {"Pat Hand", "five title", "LOOPY", 2} }; char ch = 0; cout << "Enter your choice: "; int exit = 0; while (cin>>ch) { switch (ch) { case'a': for (int i = 0; i < 5; i++) cout << boparray[i].fullname << endl; break; case'b': for (int i = 0; i < 5; i++) cout << boparray[i].title << endl; break; case'c': for (int i = 0; i < 5; i++) cout << boparray[i].bopname << endl; break; case'd': for (int i = 0; i < 5; i++) if (boparray[i].preference == 0) cout << boparray[i].fullname << endl; else if (boparray[i].preference == 1) cout << boparray[i].title << endl; else if (boparray[i].preference == 2) cout << boparray[i].bopname << endl; break; case'q': exit = 1; cout << "Bye!" << endl; } if (exit) break; cout << "Next choice: "; } } //practice 5 void p6_5() { cout << "Enter your income(by tvarp): "; double income = 0; double tax = 0; while (cin >> income && income > 0) { if (income <= 5000) tax = 0; else if (income <= 15000) tax = (income - 5000) * 0.1; else if (income <= 35000) tax = 10000 * 0.1 + (income - 15000) * 0.15; else tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000) * 0.2; cout << "Your personal income tax is: " << tax << endl; } cout << "Exit!\n"; } //practice 6 void p6_6() { struct juanxian { string name; double num = 0; }; int people = 0; cout << "Enter the number of people who has donated: "; cin >> people; //cin读取数据后要用cin.get()读取换行符才能使用getline() cin.get(); struct juanxian* pd = new struct juanxian[people]; cout << "Enter the name and the number of donation: \n"; for (int i = 0; i < people; i++) { cout << "Enter the name of " << i + 1 << " contributor: "; getline(cin,(pd+i)->name); //三种写法:pd[i].name (*(pd+i)).name (pd+1)->name cout << "Enter the amount of donation: "; cin >> pd[i].num; cin.get(); } int one = 0; int two = 0; cout << "Grand Patrons:\n"; for (int i = 0; i < people; i++) { if (pd[i].num > 10000) { cout << pd[i].name << " : " << pd[i].num << endl; one++; } } if (one == 0) cout << "None!\n"; cout << "Patrons:\n"; for (int i = 0; i < people; i++) { if (pd[i].num <= 10000) { cout << pd[i].name << " : " << pd[i].num << endl; two++; } } if (two == 0) cout << "None!\n"; delete[]pd; } //practice 7 void p6_7() { cout << "Enter words (q to quit): \n"; string word; int yuan = 0; int fu = 0; int none = 0; while (cin >> word) { if (word.length() == 1 && word[0] == 'q') break; if (isalpha(word[0])) { switch (word[0]) { case'a': case'o': case'i': case'e': case'u':yuan++; break; default:fu++; }; } else none++; } cout << yuan << " words beginning with vowels\n"; cout << fu << " words beginning with consonan\n"; cout << none << " others\n"; } //practice 8 void p6_8() { ifstream infile; infile.open("practice.txt"); char ch; int num = 0; while((ch = infile.get()) != EOF) num++; cout << "There are " << num << " characters in file!\n"; } //practice 9 void p6_9() { ifstream infile; infile.open("juankuan.txt"); struct juanxian { string name; double num = 0; }; int people = 0; infile >> people; //cin读取数据后要用cin.get()读取换行符才能使用getline() infile.get(); juanxian* pd = new juanxian[people]; for (int i = 0; i < people; i++) { getline(infile, (pd + i)->name); //三种写法:pd[i].name (*(pd+i)).name (pd+1)->name infile >> pd[i].num; infile.get(); } int one = 0; int two = 0; cout << "Grand Patrons:\n"; for (int i = 0; i < people; i++) { if (pd[i].num > 10000) { cout << pd[i].name << " : " << pd[i].num << endl; one++; } } if (one == 0) cout << "None!\n"; cout << "Patrons:\n"; for (int i = 0; i < people; i++) { if (pd[i].num <= 10000) { cout << pd[i].name << " : " << pd[i].num << endl; two++; } } if (two == 0) cout << "None!\n"; delete[]pd; } int main() { p6_9(); return 0; }