C++ Primer Plus (第六版)编程练习记录(chapter6)
1.编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。
/**************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
**************************************************/
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main()
{
cout << "输入字符,执行大小写转换,数字将被忽略\n";
char ch;
string str;
while ((ch = cin.get()) != '@')
{
if (isupper(ch))
{
ch = tolower(ch);
str += ch;
}
else if (islower(ch))
{
ch = toupper(ch);
str += ch;
}
}
cout << str << endl;
return 0;
}
2.编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
/**************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
**************************************************/
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "Please enter donation number"
<<"(Enter any character to exit):\n";
double donation[10];
double temp, ave, sum = 0;
int count = 0;
while (cin>>temp)
{
if (isalpha(temp))
break;
else
{
donation[count] = temp;
count++;
if (count > 10)
break;
}
sum += temp;
ave = sum / count;
}
cout << count << " numbers.\n";
cout << "Average value:" << ave << endl;
cout << "The sum:" << sum << endl;
return 0;
}
3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
/**************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
**************************************************/
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter one of the following choices:\n";
cout << "c)carnivore\t" << "p)pianist\n";
cout << "t)tree \t" << "g)game\n";
char choice;
while (cin>>choice)//判定输入是否成功
{
switch (choice)//swtich会将choice提升为int型
{
case int('c') : cout << "carnivore\n";//'c'要强制转化成int型才可以
break;
case int('p') : cout << "pianist\n";
break;
case int('t') : cout << "tree\n";
break;
case int('g') : cout << "game\n";
break;
default:cout << "Please enter a c,p,t, or g:\n";//未输入上述选项时一直循环下去,提示输入正确的选项。
}
}
return 0;
}
4.加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:
注意,“display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:
/**************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
**************************************************/
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
const int strsize = 30;
const int num = 5;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
bop pep[num] = {
{ "Wimp Mache", "BOSS", "AS", 0 },
{ "Raki Rhodes", "Junior Programmer", "MA", 1 },
{ "Celia Laiter", "Manager", "MIPS", 2 },
{ "Hoppy Hipman", "Analyst Trainee", "CL", 1 },
{ "Pat Hand", "Student", "LOOPY", 2 }
};
cout << "Benevolent Order of Progranmmers Report\n";
cout << "a.display by name \t" << "b.display by title\n";
cout << "c.display by bopname\t" << "d.display by preference\n";
cout << "q.quit\n";
cout << "Enter your choice:";
char choice;
while (cin >> choice&&(choice!='q'))
{
switch (choice)
{
case 'a':
{
for (int i = 0;i < num;i++)
cout << pep[i].fullname << endl;
break;
}
case 'b':
{
for (int i = 0;i < num;i++)
cout << pep[i].title << endl;
break;
}
case 'c':
{
for (int i = 0;i < num;i++)
cout << pep[i].bopname << endl;
break;
}
case 'd':
{
for (int i = 0;i < num;i++)
{
if (pep[i].preference == 0)
cout << pep[i].fullname << endl;
else if (pep[i].preference == 1)
cout << pep[i].title << endl;
else
cout << pep[i].bopname << endl;
}
}
default:
{
cout << "Please enter correct instruction.\n";
continue;
}
}
}
return 0;
}
5.在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps:不收税
5001~15000 tvarps:10%
15001~35000 tvarps:15%
35000 tvarps以上:20%
例如,收入为38000 tvarps时,所得税为5000×0.00 + 10000×0.10 + 20000×0.15 + 3000×0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。
/**************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
**************************************************/
#include <iostream>
int main()
{
using namespace std;
const double tax[4] = { 0,0.10,0.15,0.20 };
double tvarp, pay;
cout << "Please enter your tvarp:\n";
while (cin >> tvarp)
{
if (tvarp > 0)//采用if else 语句而不是直接在while里判断可以在else里设置合理的处理错误的机制
{
if (tvarp <= 5000)
pay = tvarp * tax[0];
else if ((tvarp > 5000) && (tvarp <= 15000))
pay = (tvarp - 5000) * tax[1];
else if ((tvarp > 15000) && (tvarp <= 35000))
pay = (tvarp - 15000) * tax[2] + 10000 * tax[1];
else
pay = (tvarp - 35000) * tax[3] + 20000 * tax[2] + 10000 * tax[1];
cout << "You should pay " << pay << " tvarps.\n";
}
else
{
cout << "Plase enter correct number.\n";
break;
}
}
return 0;
}
6.编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。
/**************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
**************************************************/
#include <iostream>
#include <string>
int main()
{
using namespace std;
struct patron
{
string name;
double money;
};
int patron_num;
int grand_num = 0;//标记重要捐款人数
int normal_num = 0;//标记普通捐款人数
cout << "Please enter the number of patrons.\n";
cin >> patron_num;
cin.get();
patron* pa = new patron[patron_num];//构造动态结构
cout << "Please enter the name and money of each patron.\n";
for (int i = 0;i < patron_num;i++)//存储输入的名字及捐款金额
cin >> pa[i].name >> pa[i].money;
cout << "The patrons below are Grand Patrons.\n";
for (int i = 0;i < patron_num; i++)
{
if (pa[i].money > 10000)
{
cout << pa[i].name << ":\t" << pa[i].money << endl;
grand_num += 1;
}
}
if (grand_num == 0)
cout << "none.\n";
cout << "The patrons below are Normal Patrons.\n";
for (int i = 0;i < patron_num; i++)
{
if (pa[i].money <= 10000)
{
cout << pa[i].name << ":\t" << pa[i].money << endl;
normal_num += 1;
}
}
if (normal_num == 0)
cout << "none.\n";
delete[] pa;//释放内存
return 0;
}
7.编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha( )来区分以字母和其他字符打头的单词,然后对于通过了isalpha( )测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
/**************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
**************************************************/
#include <iostream>
#include <string>
#include <cctype>
int main()
{
using namespace std;
cout << "Enter words (q to quit):\n";
string word;
int vowels = 0;
int consonants = 0;
int kind = 0;
while (cin >> word)//每次读取输入队列中的一个单词(或者数字,以空格隔开的)至word中
{
if (word == "q")
break;
else
{
if (isalpha(word[0]))
{
switch (word[0])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
{
vowels++;
break;
}
default:
consonants++;
}
}
else
kind++;
}
}
cout << vowels << " words beginning with vowels.\n";
cout << consonants << " words beginning with consonants.\n";
cout << kind << " others.\n";
return 0;
}
8.编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
/**************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
**************************************************/
#include <iostream>
#include <fstream>//文件输入输出必须要包括的头文件
#include <string>
#include <cstdlib>
int main()
{
using namespace std;
ifstream inFile;//指定文件
inFile.open("test.txt");//打开文件
if (!inFile.is_open())//检测文件是否打开,很关键
{
cout << "Could not open the file.\n";
exit(EXIT_FAILURE);
}
string words;
int num = 0;
inFile >> words;
while (inFile.good())
{
++num;
inFile >> words;
}
cout << num << " words included.\n";
inFile.close("test.txt");//关闭文件
return 0;
}
9.完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/2/25
* 描述:
************************************************* */
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
int main()
{
using namespace std;
struct patron
{
string name;
double money;
};
ifstream inFile;
inFile.open("test.txt");
if (!inFile.is_open())//检测文件是否打开,很关键
{
std::cout << "Could not open the file.\n";
exit(EXIT_FAILURE);
}
int patron_num;
int grand_num = 0;//标记重要捐款人数
int normal_num = 0;//标记普通捐款人数
string temp;
inFile >> patron_num;
inFile.get();//"吃掉"换行符
patron* pa = new patron[patron_num];//构造动态结构
for (int i = 0;i < patron_num;i++)//存储输入的名字及捐款金额
{
getline(inFile, pa[i].name);
inFile >> pa[i].money;
inFile.get();//"吃掉"换行符
}
inFile.close();//用完就关闭
cout << "The patrons below are Grand Patrons.\n";
for (int i = 0;i < patron_num; i++)
{
if (pa[i].money > 10000)
{
cout << pa[i].name << ":\t" << pa[i].money << endl;
grand_num += 1;
}
}
if (grand_num == 0)
cout << "none.\n";
cout << "The patrons below are Normal Patrons.\n";
for (int i = 0;i < patron_num; i++)
{
if (pa[i].money <= 10000)
{
cout << pa[i].name << ":\t" << pa[i].money << endl;
normal_num += 1;
}
}
if (normal_num == 0)
cout << "none.\n";
delete[] pa;//释放内存
return 0;
}