C++PrimerPlus 第六章 分支语句和逻辑运算符(编程练习含答案)
1、编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char ch;
while (cin.get(ch) && ch != '@') {
if (!isdigit(ch))
if (islower(ch))
cout << char(toupper(ch));
else if (isupper(ch))
cout << char(tolower(ch));
else
cout << ch;
}
return 0;
}
2、编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include<iostream>
#include<array>
using namespace std;
int main()
{
const int SIZE = 10;
array<double, SIZE> arr;
int num = 0, sum = 0;
while (num < SIZE && cin >> arr[num]) {
cout << "#" << num + 1 << ":";
sum += arr[num];
num++;
}
int average = sum / num;
int aveMoreNum = 0;
for (int i = 0; i < num; i++) {
if (arr[i] > average)
aveMoreNum++;
}
cout << "The average value is: " << average << endl;
cout << "There are " << aveMoreNum << " double value large than average value." << endl;
if(!cin)
{
cin.clear();
cin.get();
}
return 0;
}
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
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout << "Please enter one of the following choices:\nc) carnivore p) pianist\nt) tree g) game" << endl;
char ch;
cout << "Please enter a c, p, t, or g: ";
cin >> ch;
while ((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g')) {
cout << "Please enter a c, p, t, or g: ";
cin >> ch;
}
string str;
switch (ch) {
case 'c':str = "carnivore"; break;
case 'p':str = "pianist"; break;
case 't':str = "tree"; break;
case 'g':str = "game"; break;
}
cout << "A maple is a " << str << "." << endl;
return 0;
}
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 = fullname, 1 = title, 2 = bopname
};
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name b. display by title
c. display by bopname d. display by preference
d. quit
注意, “display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:
Benevolent Order of Programmers Report
a. display by name b. display by title
c. display by bopname d. display by preference
d. 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!
#include<iostream>
using namespace std;
const unsigned int strsize = 128;
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void DisplayByName(const struct bop* arrBOP, unsigned int size) {
for (int i = 0; i < size; i++) {
cout << arrBOP[i].fullname << endl;
}
}
void DisplayByTitle(const struct bop* arrBOP, unsigned int size) {
for (int i = 0; i < size; i++) {
cout << arrBOP[i].title << endl;
}
}
void DisplayByBopname(const struct bop* arrBOP, unsigned int size) {
for (int i = 0; i < size; i++) {
cout << arrBOP[i].bopname << endl;
}
}
void DisplayByPreference(const struct bop* arrBOP, unsigned int size) {
for (int i = 0; i < size; i++) {
if (arrBOP[i].preference == 0)
cout << arrBOP[i].fullname << endl;
else if (arrBOP[i].preference == 1)
cout << arrBOP[i].title << endl;
else
cout << arrBOP[i].bopname << endl;
}
}
int main()
{
bop* arrBOP = new bop[strsize];
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name \t b. display by title" << endl;
cout << "c. display by bopname \t d. display by preference" << endl;
cout << "q. quit" << endl;
cout << "Enter your choice: ";
char choice = 0;
while (cin >> choice) {
if (choice == 'q') {
break;
}
if (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd') {
cout << "Please enter a, b, c, d, q:";
continue;
}
switch (choice) {
case 'a':
DisplayByName(arrBOP, strsize);
break;
case 'b':
DisplayByTitle(arrBOP, strsize);
break;
case 'c':
DisplayByBopname(arrBOP, strsize);
break;
case 'd':
DisplayByPreference(arrBOP, strsize);
break;
}
cout << "Next choice: ";
}
cout << "Bye!" << endl;
return 0;
}
5、在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000tvarps:不收税
5001~15000tvarps:10%
15001~35000tvarps:15%
35001tvarps以上:20%
例如,收入为38000tvarps时,所得税为5000x0.00+10000x0.10+20000x0.15+3000x0.20,即4600tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或数字时,循环将结束。
#include<iostream>
using namespace std;
int main()
{
double money;
cout << "Please enter your income: ";
while (cin >> money && money >= 0) {
double tex = 0;
if (money > 0 && money <= 5000) {
tex = 0;
}
else if (money > 5000 && money <= 15000) {
tex = (money - 5000) * 0.1;
}
else if (money > 15000 && money <= 35000) {
tex = 10000 * 0.1 + (money - 15000) * 0.15;
}
else {
tex = 10000 * 0.1 + 20000 * 0.15 + (money - 35000) * 0.2;
}
cout << "The tex you should pay is: " << tex << endl;
cout << "Please enter your income: ";
}
return 0;
}
6、编写一个程序,记录捐助给“维护合法权利团队”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来存储姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
#include<iostream>
#include<string>
using namespace std;
struct Person {
string name = "";
double money = 0;
};
int main()
{
cout << "Enter the number of Person: ";
int numPerson = 0;
cin >> numPerson;
Person* arrPerson = new Person[numPerson];
for (int i = 0; i < numPerson; i++) {
cout << "#" << i + 1 << " Person: " << endl;
cout << "Name: ";
cin >> arrPerson[i].name;
cout << "Money: ";
cin >> arrPerson[i].money;
}
cout << "Grand Patrons" << endl;
int numGrandPatrons = 0;
for (int i = 0; i < numPerson; i++) {
if (arrPerson[i].money > 10000) {
cout << arrPerson[i].name << " " << arrPerson[i].money << endl;
numGrandPatrons++;
}
}
if (numGrandPatrons == 0)
cout << "NONE" << endl;
cout << "Patrons" << endl;
int numPatrons = 0;
for (int i = 0; i < numPerson; i++) {
if (arrPerson[i].money <= 10000) {
cout << arrPerson[i].name << " " << arrPerson[i].money << endl;
numPatrons++;
}
}
if(numPatrons == 0)
cout << "NONE" << endl;
delete[] arrPerson;
return 0;
}
7、编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter words (q to quit): " << endl;
string str;
int numVow = 0, numCons = 0, numOther = 0;
while (cin >> str) {
if (str == "q")
break;
if (isalpha(str[0]))
{
switch(str[0])
{
case 'a':case 'A':
case 'o':case 'O':
case 'e':case 'E':
case 'i':case 'I':
case 'u':case 'U':
numVow++;
break;
default:
numCons++;
break;
}
}
else {
numOther++;
}
}
cout << numVow << " words beginning with vowels" << endl;
cout << numCons << " words beginning with consonants" << endl;
cout << numOther << " others" << endl;
return 0;
}
8、编写一个程序,它打开一个文本文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifs;
ifs.open("words.txt");
if (!ifs)
{
cout << "文件未打开!!!" << endl;
exit(0);
}
char ch;
int num = 0;
while (ifs >> ch)
num++;
cout << "文件中的字符数:" << num << endl;
return 0;
}
9、完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct Person {
string name = "";
double money = 0;
};
int main()
{
int numPerson = 0;
string filename;
ifstream ifs;
cout << "Enter the file name: ";
getline(cin, filename);
ifs.open(filename.c_str());
ifs >> numPerson;
ifs.get();
Person* arrPerson = new Person[numPerson];
for (int i = 0; i < numPerson; i++) {
getline(ifs, arrPerson[i].name);
ifs >> arrPerson[i].money;
ifs.get();
}
cout << "Grand Patrons" << endl;
int numGrandPatrons = 0;
for (int i = 0; i < numPerson; i++) {
if (arrPerson[i].money > 10000) {
cout << arrPerson[i].name << " " << arrPerson[i].money << endl;
numGrandPatrons++;
}
}
if (numGrandPatrons == 0)
cout << "NONE" << endl;
cout << "Patrons" << endl;
int numPatrons = 0;
for (int i = 0; i < numPerson; i++) {
if (arrPerson[i].money <= 10000) {
cout << arrPerson[i].name << " " << arrPerson[i].money << endl;
numPatrons++;
}
}
if (numPatrons == 0)
cout << "NONE" << endl;
delete[] arrPerson;
return 0;
}