C++ Primer Plus课后习题第六章

1.

#include<iostream>
#include<cctype>
using namespace std;


int main()
{
cout << "Enter text for analysis, and type @ to terminate input." << endl;
char ch;
cin.get(ch);
while (ch != '@')
{
if (isupper(ch))
{
ch = tolower(ch);
}
else
{
ch = toupper(ch);
}
if (isdigit(ch) == false)
{
cout << ch;
}
cin.get(ch);
}
cout << endl;
return 0;

}

2.

#include<iostream>
using namespace std;
const int d_size = 10;


int main()
{
double donation[d_size];
double sum = 0;
double average;
int num1 = 0;
int num2 = 0;
for (int i = 0; i < d_size; i++)
{
cout << "Enter one number of donations: ";
cin >> donation[i];
if (cin.fail())
break;
sum += donation[i];
num1 ++;
}
if (sum == 0)
cout << "No donation!" << endl;
else
{
average = sum / num1;
for (int i = 0; i < num1; i++)
{
if (donation[i]>average)
num2 ++;
}
cout << "Average: " << average << endl;
cout << "The number that exceed the average is: " << num2 << endl;
}
return 0;

}

3.

#include<iostream>
using namespace std;
void menu();


int main()
{
menu();
cout << "Please enter a c, p, t, g: ";
char ch;
while ((ch = cin.get()) != 'c' && ch != 'p' && ch != 't' && ch != 'g')
{
cout << "Please enter a c, p, t, g: ";
cin.ignore();
}
switch (ch)
{
case('c') : cout << "A maple is a carnivore\n";
break;
case('p') : cout << "A maple is a pianist\n";
break;
case('t') : cout << "A maple is a tree.\n";
break;
case('g') : cout << "A maple is a game\n";
break;
}
return 0;
}


void menu()
{
cout << "Please enter one of the following choices: \n";
cout << "c) carnivore           p) pianist\n";
cout << "t) tree                g) game\n";

}

4.

#include<iostream>
using namespace std;
const int strsize = 20;
void menu();


struct bop{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int perference;
};


int main()
{
menu();
bop member[3] = 
{
{ "Wimp Macho", "stu", "bobbob", 0 },
{ "Raki Rhodes", "stu", "jane", 1 },
{ "Celia Laiter", "stu", "dandan", 2 }
};
cout << "Enter your choice: ";
char ch;
while ((ch = cin.get()) == 'a' || ch == 'b' || ch == 'c' || ch == 'd')
{
switch (ch)
{
case 'a':cout << member[0].fullname << endl;
cout << member[1].fullname << endl;
cout << member[2].fullname << endl;
break;
case 'b':cout << member[0].title << endl;
cout << member[1].title << endl;
cout << member[2].title << endl;
break;
case 'c':cout << member[0].bopname << endl;
cout << member[1].bopname << endl;
cout << member[2].bopname << endl;
break;
case 'd':cout << member[0].fullname << endl;
cout << member[1].title << endl;
cout << member[2].bopname << endl;
break;
}
cout << "Next choice: ";
cin.get();
}
if (ch == 'q')
cout << "Bye!\n";
else
cout << "It's not a choice\n";
return 0;
}


void menu()
{
cout << "Benevolent Order of Programmers Report\n";
cout << "a. diaplay by name       b. display by title\n";
cout << "c. display by bopname    d. display by preference\n";
cout << "q. quit\n";

}

5.

#include<iostream>
using namespace std;

int main()
{
int salary;
double tax = 0;
cout << "Please enter the salary, and type negative number or nonumeric character to terminate: ";
while (1)
{
cin >> salary;
if (cin.fail() || salary < 0)
break;
if (salary > 35000)
tax = 4000 + (salary - 35000) * 0.2;
else if (salary > 15000)
tax = 1000 + (salary - 15000) * 0.15;
else if (salary > 5000)
tax = (salary - 5000) * 0.1;
else
tax = 0;
cout << tax << endl;
cout << "next salary: ";
}
return 0;

}

6.

#include<iostream>
#include<string>
using namespace std;


struct patrons
{
string name;
double money;
};
int main()
{
int number;
cout << "Enter the number of patrons: ";
cin >> number;
patrons *pat = new patrons[number];
for (int i = 0; i < number; i++)
{
string tempname;
double tempmoney;
cout << "第" << i + 1 << "个姓名: ";
cin >> tempname;
cout << "捐款数:";
cin >> tempmoney;
pat[i].name = tempname;
pat[i].money = tempmoney;
}
int grand_number = 0;
int ordinary_number = 0;
cout << "Grand Patrons: \n";
for (int i = 0; i < number; i++)
{
if (pat[i].money > 10000)
{
grand_number++;
cout << "name: " << pat[i].name << "\tmoney: " << pat[i].money << endl;
}
}
if (grand_number == 0) cout << "none\n";
cout << "Patrons: \n";
for (int i = 0; i < number; i++)
{
if (pat[i].money <= 10000)
{
ordinary_number++;
cout << "name: " << pat[i].name << "\tmoney: " << pat[i].money << endl;
}
}
if (ordinary_number == 0) cout << "none\n";
return 0;

}

7.

#include<iostream>
#include<cstring>
using namespace std;


int main()
{
cout << "Enter words (q to quit)\n";
char word[20];
int vowels = 0;
int consonants = 0;
int others = 0;
while (cin >> word)
{
if (word[0] == 'q' && strlen(word) == 1)
break;
if (isalpha(word[0]))
{
switch (word[0])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
vowels++;
break;
default:
consonants++;
}
}
else
others++;
}
cout << vowels << " words beginning with vowels\n";
cout << consonants << " words beginning with consonants\n";
cout << others << " others\n";
return 0;

}

8.

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
const int SIZE = 60;


int main()
{
char filename[SIZE];
ifstream infile;
cout << "Enter name of file: ";
cin.getline(filename, SIZE);
infile.open(filename);
if (!infile.is_open())
{
cout << "Couldn't open the file " << filename << endl;
cout << "Program terminating. \n";
exit(EXIT_FAILURE);
}
char ch;
infile.get(ch);
int number = 0;
while (infile.good())
{
number++;
infile.get(ch);
}
if (infile.eof())
cout << "End of file reached.\n";
cout << "There are " << number << " characters in the file.\n";
infile.close();
return 0;

}

9.

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;


struct patrons
{
string name;
double money;
};


int main()
{
ifstream infile;
infile.open("text.txt");
if (!infile.is_open())
{
cout << "Couldn't open the file " << endl;
cout << "Program terminating. \n";
exit(EXIT_FAILURE);
}
int number;
infile >> number;
patrons *pat = new patrons[number];
int i = 0;
while (infile.good())
{
getline(infile, pat[i].name);
(infile >> pat[i].money).get();
i++;
}
int grand_number = 0;
int ordinary_number = 0;
cout << "Grand Patrons: \n";
for (int i = 0; i < number; i++)
{
if (pat[i].money > 10000)
{
grand_number++;
cout << "name: " << pat[i].name << "\tmoney: " << pat[i].money << endl;
}
}
if (grand_number == 0) cout << "none\n";
cout << "Patrons: \n";
for (int i = 0; i < number; i++)
{
if (pat[i].money <= 10000)
{
ordinary_number++;
cout << "name: " << pat[i].name << "\tmoney: " << pat[i].money << endl;
}
}
if (ordinary_number == 0) cout << "none\n";
infile.close();
return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值