1. 编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写。(提示:cctype函数系列)
// 6-1
#include
int main()
{
using namespace std;
char ch = 0;
cout << "Enter some words: " << endl;
while (cin.get(ch) && ch != '@') { //逐个字母读取
if (isdigit(ch))
continue;
else if (islower(ch))
cout << char(toupper(ch));
else if (isupper(ch))
cout << char(tolower(ch));
else cout << ch;
}
cout << endl;
cout << "Done!\n";
return 0;
}
2. 编写一个程序,最多将10个donation值读入到一个double数组中(或模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
// 6-2
#include
#include
const unsigned int NUM = 10;
int main()
{
using namespace std;
array donation;
unsigned int enter = 0;
double total_value = 0.0;
double avg = 0.0;
unsigned int large_avg = 0;
cout << "Please enter up to ten double value, Non-digital to exit: \n";
while (enter < 10 && (cin >> donation[enter])) { //遇到非数字字符时,程序将挂起
total_value += donation[enter];
enter++;
}
avg = total_value / enter;
for (int i = 0; i < enter; i++) {
if (donation[i] > avg)
large_avg++;
}
cout << "The average value is " << avg << ", and there are " << large_avg << " double value large than average value." << endl;
return 0;
}
3. 编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行相应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单的操作。
// 6-3
#include
#include
int main()
{
using namespace std;
cout << "Please enter one of the following choices: " << endl;
cout << "c) carnivore \t p) pianist" << endl;
cout << "t) tree \t g) game" << endl;
// cout.flags(ios::left);
// cout << setw(20) << "c) carnivore" << "p) pianist" << endl;
// cout << setw(20) << "t) tree" << "g) game" << endl;
char ch;
bool exit = false;
while (!exit && cin >> ch) {
switch (ch) {
case'c':
cout << "A lion is a carnivore." << endl;
exit = true;
break;
case'p':
cout << "Beethoven is a pianist." << endl;
exit = true;
break;
case't':
cout << "A maple is a tree." << endl;
exit = true;
break;
case'g':
cout << "League of heroes is a game." << endl;
exit = true;
break;
default:
cout << "Please enter a c, p, t, or g: ";
break;
}
}
return 0;
}
4. 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
// 6-4 练习switch
#include
using namespace std;
const unsigned int strsize = 128;
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void display_by_name(const struct bop* bopArray, unsigned int size) {
for (size_t i = 0; i < size; i++)
cout << bopArray[i].fullname << endl;
}
void display_by_title(const struct bop* bopArray, unsigned int size) {
for (size_t i = 0; i < size; i++)
cout << bopArray[i].title << endl;
}
void display_by_bopname(const struct bop* bopArray, unsigned int size) {
for (size_t i = 0; i < size; i++)
cout << bopArray[i].bopname << endl;
}
void display_by_preference(const struct bop* bopArray, unsigned int size) {
for (size_t i = 0; i < size; i++) {
if (bopArray[i].preference == 0)
cout << bopArray[i].fullname << endl;
else if (bopArray[i].preference == 1)
cout << bopArray[i].title << endl;
else
cout << bopArray[i].bopname << endl;
}
}
int main()
{
const struct bop bopArray[5] = {
{"AA AA", "aa aa", "AA aa", 0},
{"BB BB", "bb bb", "BB bb", 1},
{"CC CC", "cc cc", "CC cc", 2},
{"DD DD", "dd dd", "DD dd", 0},
{"EE EE", "ee ee", "EE ee", 1}
};
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;
switch (choice) {
case'a':
display_by_name(bopArray, 5);
break;
case'b':
display_by_title(bopArray, 5);
break;
case'c':
display_by_bopname(bopArray, 5);
break;
case'd':
display_by_preference(bopArray, 5);
break;
default:
// cout << "Please enter a, b, c, d, q: ";
break;
}
cout << "Next choice: ";
}
cout << "Bye!" << endl;
return 0;
}
5. 在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~15000 tvarps:10%
15001~35000 tvarps:15%
35000 tvarps以上:20%
请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。
// 6-5
#include
using namespace std;
int main()
{
cout << "Please enter your income: ";
int tvarps = 0, tax = 0;
while (cin >> tvarps) {
if (tvarps <= 5000)
tax = 0;
else if (tvarps <= 15000)
tax = (tvarps - 5000) * 0.1;
else if (tvarps <= 35000)
tax = 10000 * 0.1 + (tvarps - 15000) * 0.15;
else
tax = 10000 * 0.1 + 20000 * 0.15 + (tvarps - 35000) * 0.2;
cout << "The tax you shoule pay is " << tax << " tvarps." << endl;
cout << "Please enter your income: ";
}
return 0;
}
6. 编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被存储在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序仅显示这两种类别,不进行排序。
// 6-6
#include
#include
using namespace std;
struct Donor {
string name;
double amount;
};
int main()
{
cout << "Enter the number of donors: ";
int number;
cin >> number;
struct Donor* donorPtr = new Donor[number];
for (size_t i = 0; i < number; i++) {
cout << "Enter the name: ";
cin >> donorPtr[i].name;
cout << "Enter the amount: ";
cin >> donorPtr[i].amount;
}
cout << "Important donors are listed below: " << endl;
int num_imp = 0;
for (int i = 0; i < number; ++i) {
if (donorPtr[i].amount > 10000) {
cout << "name : " << donorPtr[i].name << " and the amount is " << donorPtr[i].amount << "." << endl;
num_imp++;
}
}
if (num_imp == 0) cout << "none" << endl;
cout << "Other donors are listed below: " << endl;
for (int i = 0; i < number; ++i) {
if (donorPtr[i].amount <= 10000) {
cout << "name : " << donorPtr[i].name << " and the amount is " << donorPtr[i].amount << "." << endl;
}
}
if (num_imp == number) cout << "none" << endl;
return 0;
}
7. 编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于此类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。
// 6-7
#include
#include
#include
using namespace std;
int main()
{
cout << "Enter words (q to quit): " << endl;
string str;
int num_vow = 0, num_cons = 0, num_others = 0;
while (cin >> str) {
if (str == "q") break;
if (isalpha(str[0])) {
switch (str[0]) {
case'a':
case'e':
case'i':
case'o':
case'u':
case'A':
case'E':
case'I':
case'O':
case'U':
num_vow++;
break;
default:
num_cons++;
break;
}
}
else num_others++;
}
cout << num_vow << " words beginning with vowels\n";
cout << num_cons << " words beginning with consonants\n";
cout << num_others << " others\n";
return 0;
}
8. 编写一个程序,它打开一个文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
// 6-8
#include
#include
#include
using namespace std;
int main()
{
string fileName;
ifstream inFile;
unsigned int num = 0;
char ch = 0;
cout << "Enter the file name: ";
getline(cin, fileName);
inFile.open(fileName.c_str());
while ((ch = inFile.get()) != EOF)
num++;
cout << "There are " << num << "characters in " << fileName << " file." << endl;
return 0;
}
9. 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。
// 6-9
#include
#include
#include
using namespace std;
struct pat_info {
string name;
double amount;
};
int main() {
unsigned int contributors = 0;
unsigned int tmp = 0;
string FileName;
ifstream inFile;
cout << "Enter the file name: ";
getline(cin, FileName);
inFile.open(FileName.c_str());
inFile >> contributors;
inFile.get();
struct pat_info* pContributors = new struct pat_info[contributors];
for (size_t i = 0; i < contributors; i++) {
getline(inFile, pContributors[i].name);
inFile >> pContributors[i].amount;
inFile.get();
}
cout << "Grand Pators: " << endl;
for (size_t i = 0; i < contributors; i++) {
if (pContributors[i].amount > 10000) {
cout << "Contributors name: " << pContributors[i].name << endl;
cout << "Contributors amount: " << pContributors[i].amount << endl;
tmp++;
}
}
if (tmp == 0) cout << "none" << endl;
tmp = 0;
cout << "Pators: " << endl;
for (size_t i = 0; i < contributors; i++) {
if (pContributors[i].amount <= 10000) {
cout << "Contributors name: " << pContributors[i].name << endl;
cout << "Contributors amount: " << pContributors[i].amount << endl;
tmp++;
}
}
if (tmp == 0) cout << "none" << endl;
return 0;
}