C++ Primer Plus章节编程练习(第六章)
第六章 分支语句和逻辑运算符
1、编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字母转换为小写,将小写字符转换为大写。
1 #include<iostream>
2 #include<cctype>
3 using namespace std;
4 int main(){
5 char ch;
6 cin >> ch;
7 while(ch != '@'){
8 if(islower(ch))
9 cout << char(ch - 32);
10 else if(isupper(ch))
11 cout << char(ch + 32);
12 else if(isdigit(ch) == false)
13 cout << ch;
14 cin >> ch;
15 }
16 }
2、编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类 array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
1 #include<iostream>
2 using namespace std;
3 const int SIZE = 10;
4 int main(){
5 double donation[SIZE];
6 int i = 0;
7 int above = 0;
8 int count = 0;
9 double sum = 0;
10 double average;
11 while(i < SIZE && (cin >> donation[i])){
12 sum += donation[i];
13 i++;
14 }
15 average = sum / i;
16 for(int j = 0; j < i; j++){
17 if(donation[j] > average)
18 above++;
19 }
20 cout << "Average: " << average <<endl;
21 cout << "Above average: " << above << endl;
22 }
cin>>donation[i]返回一个布尔类型,用来判断输入是否有效
3、编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。该程序的运行情况如下:
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 a tree.
1 #include<iostream>
2 using namespace std;
3 int main(){
4 char ch;
5 cout << "Please enter one of the following choices:" << endl
6 << "c) carnivore\t\t" << "p) pianist" << endl
7 << "t) tree \t\t" << "g) game" << endl
8 << "f" << endl;
9 paris:
10 cin >> ch;
11 switch(ch){
12 case 'C':
13 case 'c': cout << "you choice c" << endl;
14 break;
15 case 'P':
16 case 'p': cout << "you choice p" << endl;
17 break;
18 case 'T':
19 case 't': cout << "you choice t" << endl;
20 break;
21 case 'G':
22 case 'g': cout << "you choice g" << endl;
23 break;
24 default : cout << "Please enter a c, p, t, or g:";
25 goto paris;
26 }
27 return 0;
28 }
4、加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
//Benevolent Order of Programmer name structure
struct bop{
char fullname[strsize]; // real name
char title[strsize]; // job title
char bopname[strsize]; // secret BOP name
int preference; // 0 = fulname, 1 = title, 2 = bopname
}
该程序创建了一个由上述结构组成的小型数组,并将其初始化为适合的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name b.display by title
c.display by bopname d.diaplay by preference
q.quit
注意,“diaplay by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。
1 #include<iostream>
2 const int strsize = 20;
3 using namespace std;
4 struct bop{
5 char fullname[strsize];
6 char title[strsize];
7 char bopname[strsize];
8 int preference;
9 };
10 int main(){
11 bop info[5] = {{"Wimp Macho", "W", "Wimp", 0}, {"Raki Rhodes", "R", "Raki", 1}, {"Celia Laiter", "C", "Celia", 2}, {"Hoppy Hipman", "H", "Hoppy", 0}, {"Pat Hand", "P", "Pat", 1}};
12 char ch;
13 cout << "Benevolent Order of Programmers Report" << endl
14 << "a. display by name \t" << "b.display by title" << endl
15 << "c.display by bopname\t" << "d.diaplay by preference" << endl
16 << "q.quit" << endl;
17 cout << "Enter your choice: ";
18 cin >> ch;
19 while(ch != 'q'){
20 switch(ch){
21 case 'a' : for(int i=0; i<5; i++)
22 {
23 cout << info[i].fullname << endl;
24 }
25 break;
26 case 'b' : for(int i=0; i<5; i++)
27 {
28 cout << info[i].title << endl;
29 }
30 break;
31 case 'c' : for(int i=0; i<5; i++)
32 {
33 cout << info[i].bopname << endl;
34 }
35 break;
36 case 'd' : for(int i=0; i<5; i++)
37 {
38 if(info[i].preference == 0)
39 cout << info[i].fullname << endl;
40 else if(info[i].preference == 1)
41 cout << info[i].title << endl;
42 else
43 cout << info[i].bopname << endl;
44 }
45 break;
46
47 }
48 cout << "Next choice: ";
49 cin >> ch;
50 }
51 return 0;
52 }
5、在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~15000 tvarps:10%
15001~35000 tvarps:15%
35000 tvarps以上:20%
例如,收入为38000tvarps时,所得税为5000 x 0.00 + 10000 x 0.10 + 20000 x 0.15 + 3000 x 0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非零数字时,循环将结束。
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 double income; 5 double tax; 6 cout << "Enter your income: "; 7 while(cin >> income){ 8 if(income <= 5000.0) 9 tax = 0.0; 10 else if(income > 5000.0 && income <= 15000) 11 tax = (income - 5000.0) * 0.10; 12 else if(income > 15000 && income <= 35000) 13 tax = 1000 + (income - 15000) * 0.15; 14 else 15 tax = 4000 + (income - 35000) * 0.20; 16 17 cout << "Your tax: " << tax << endl; 18 } 19 return 0; 20 }
6、编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐款者数目,然后要求用户输入每一个捐款者的姓名和款项。这些信息被存储在一个动态分配的结构数组中。每个结构有两个成员:用来存储姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要的捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 struct person{ 5 string name; 6 double donation; 7 }; 8 int main(){ 9 int n; 10 cout << "How many donors ?" << endl; 11 bool isgrand = false; 12 bool isoridinary = false; 13 cin >> n; 14 person *per = new person[n]; 15 for(int i = 0; i < n; i++){ 16 cin.ignore(); 17 cout << "Name: "; 18 getline(cin, per[i].name); 19 cout << "Donation: "; 20 cin >> per[i].donation; 21 } 22 cout << endl << "Grand Patrons:" << endl; 23 for(int i = 0; i < n; i++){ 24 if(per[i].donation >= 10000){ 25 cout << per[i].name << "\t" << per[i].donation << endl; 26 isgrand = true; 27 } 28 } 29 if(!isgrand) 30 cout << "none" << endl; 31 cout << "Patrons:" << endl; 32 for(int i = 0; i < n; i++){ 33 if(per[i].donation < 10000){ 34 cout << per[i].name << "\t" << per[i].donation << endl; 35 isoridinary = true; 36 } 37 } 38 if(!isoridinary) 39 cout << "none" << endl; 40 return 0; 41 }
7、编写一个程序,它每次读取一个单词,直到用户输入q。然后,该程序指出有多少个单词以元音打头,有多少单词以辅音打头,还有多少单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words (q to quit):
The 12 awesome oxen ambled
quietly across 15 meters od lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others
1 #include<iostream> 2 #include<cstring> 3 #include<cctype> 4 using namespace std; 5 int main(){ 6 int vowels = 0; 7 int consonants = 0; 8 int others = 0; 9 cout << "Enter words (q to quit):" << endl; 10 char ch[20]; 11 cin >> ch; 12 while(strcmp(ch, "q")){ 13 if(isalpha(ch[0])){ 14 switch(ch[0]){ 15 case 'a': 16 case 'A': 17 case 'e': 18 case 'E': 19 case 'i': 20 case 'I': 21 case 'o': 22 case 'O': 23 case 'u': 24 case 'U': vowels++; 25 break; 26 default : consonants++; 27 break; 28 } 29 } 30 else 31 others++; 32 cin >> ch; 33 } 34 cout << vowels << " words beginning with vowels" << endl; 35 cout << consonants << " words beginning with consonants" << endl; 36 cout << others << " others" << endl; 37 return 0; 38 }
8、编写一个程序,它打开一个文本文件,逐个地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
1 #include<iostream> 2 #include<fstream> 3 #include<cstdlib> 4 const int SIZE = 50; 5 using namespace std; 6 int main(){ 7 int count = 0; 8 char ch; 9 char fileName[SIZE]; 10 ifstream inFile; 11 cout << "Enter name of the data file: "; 12 cin.getline(fileName, SIZE); 13 inFile.open(fileName); 14 if(!inFile.is_open()){ 15 cout << "Could not open the file " << fileName << endl; 16 cout << "Program terminating.\n"; 17 exit(EXIT_FAILURE); 18 } 19 while(inFile >> ch >> ch && inFile.good()) 20 count++; 21 cout << count << endl; 22 inFile.close(); 23 return 0; 24 }
9、完成编程练习6,但从文件中读取所需要的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。
1 #include<iostream> 2 #include<fstream> 3 #include<cstdlib> 4 const int SIZE = 30; 5 using namespace std; 6 struct person{ 7 string name; 8 double donation; 9 }; 10 int main(){ 11 int n; 12 char fileName[SIZE]; 13 ifstream inFile; 14 char name[SIZE]; 15 double money; 16 cout << "Enter name of the data file: "; 17 cin.getline(fileName, SIZE); 18 inFile.open(fileName); 19 if(!inFile.is_open()){ 20 cout << "Could not open the file " << fileName << endl; 21 cout << "Program terminating.\n"; 22 exit(EXIT_FAILURE); 23 } 24 inFile >> n; 25 inFile.ignore(); 26 cout << n <<" persons" << endl; 27 for(int i=0; i<n; i++){ 28 inFile.getline(name, SIZE); 29 inFile >> money; 30 inFile.ignore(); 31 cout << "Name: " << name <<endl; 32 cout << "Donation: " << money << endl; 33 } 34 inFile.close(); 35 return 0; 36 37 }