《C++ Primer Plus》第六章课后题

复习题

1.请看下面两个计算空格和换行符数目的代码片段,第二种格式比第一种格式好在哪里呢?

// Version 1
while (cin.get (ch) )

{

        if (ch == ' ')
                spaces++;
        if (ch == '\n')
                newlines++;

}

// Version 2
while(cin.get (ch))
{

        if (ch == ' 1)
                spaces++;
        else if (ch == '\n')
                newlines++;

}

这两个版本将给出相同的答案,但if else版本的效率更高。例如,考虑当ch为空格时的情况。版本1对空格加1,然后看它是否为换行符。这将浪费时间,因为程序已经知道ch为空格,因此它不是换行符。在这种情况下,版本2将不会查看字符是否为换行符。

2. 在程序清单6.2中,用ch+1替换++ch将发生什么情况呢?

++ch和ch+1得到的数值相同。但++ch的类型为char,将作为字符打印,而ch+1是int类型(因为将char和int相加),将作为数字打印。

3. 请认真考虑下面的程序:

#include <iostream>
using namespace std;
int main()

{

        

        char ch;
        int ct1, ct2;

        ct1 = ct2 = 0;
        while ((ch = cin.get () ) != '$')

        {

                cout << ch;
                ct1++;
                if (ch = '$')
                        ct2++;
                cout << ch;

        }

        cout << "ctl = " << ct1 << ", ct2 = " << ct2 << "\n";
        return 0;

}

假设输入如下(请在每行末尾按回车键):
Hi!
Send $10 or $20 now!
则输出将是什么(还记得吗,输入被缓冲)?

由于程序使用ch='$',而不是ch == '$',因此输入和输出将如下:
Hi!
H$İ$!$
$Send $10 or $20 now!
S$e$n$d$ $ct1 = 9, ct2 = 9

在第二次打印前,每个字符都被转换为$字符。另外,表达式ch=$的值为$字符的编码,因此它是非0
值,因而为true;所以每次ct2将被加1。

4. 创建表示下述条件的逻辑表达式:

a. weight 大于或等于115,但小于125。
b. ch为q或Q。
c. x为偶数,但不是26。
d. x为偶数,但不是26的倍数。
e. donation 为1000-2000或guest为1。
f. ch 是小写字母或大写字母(假设小写字母是依次编码的,大写字母也是依次编码的,但在大小写字母间编码不是连续的)。

a. weight >= 115 && weight < 125
b. ch == 'q' | | ch == 'Q'
c. x % 2 == 0 && X != 26
d. x % 2 == 0 && ! (x % 26 == 0)
e. donation >= 1000 && donation <= 2000 || guest == 1
f. (ch >= 'a' && ch <= 'z') | | (ch >= 'A' && ch <= 'Z')

5. 在英语中,“I will not not speak(我不会不说)”的意思与“I will speak(我要说)”相同。在C++中, !! x是否与x相同呢?

不一定。例如,如果x为10,则!x为0, !! x为1。然而,如果x为bool变量,则 !!x为x。

6. 创建一个条件表达式,其值为变量的绝对值。也是说,如果变量x为正,则表达式的值为x;但如果x为负,则表达式的值为-x——这是一个正值。

(x < 0) ? -x : x或(x >= 0)? x : -x

7. 用switch改写下面的代码片段:

if (ch == 'A')
a_grade++;
else if (ch == 'B')
b_grade++;
else if (ch == 'C')
c_grade++;
else if (ch == 'D')
d_grade++;
else
f_grade++;

switch(ch)
{
        case 'A':a_grade++;break;
        case 'B':b_grade++;break;
        case 'C':c_grade++;break;
        case 'D':d_grade++;break;
        default:f_grade;break
}

8.对于程序清单6.10,与使用数字相比,使用字符(如a和c)表示菜单选项和case标签有何优点呢?(提示:想想用户输入q和输入5的情况。)

如果使用整数标签,且用户输入了非整数(如q),则程序将因为整数输入不能处理字符而挂起。但是,如果使用字符标签,而用户输入了整数(如5),则字符输入将5作为字符处理。然后,switch语句的default 部分将提示输入另一个字符。

9. 请看下面的代码片段:

int line = 0;
char ch;
while (cin.get (ch) )

{

        if (ch == 'Q')
        break;
        if (ch != '\n')
        continue;

        line++;

}

请重写该代码片段,不要使用break和continue语句。

int line = 0;
char ch;
while (cin.get (ch) && ch != 'Q')
{
        if (ch == '\n')
        line++;
}

编程练习

1. 编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char ch;
    while ((ch = cin.get()) != '@')
    {
        if (!isdigit(ch))
        {
            if (islower(ch))
            {
                cout << char(ch + 'A' - 'a');
            }
            else if (isupper(ch))
            {
                cout << char(ch - 'A' + 'a');
            }
        }
    }
    return 0;
}

2. 编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类 array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

#include <iostream>
using namespace std;
int main()
{
    double donation[10];
    int i=0;
    cout<<"请输入数组第"<<i+1<<"个元素:";
    while(cin>>donation[i])
    {
        i++;
        if(i==10) break;
        cout<<"请输入数组第"<<i+1<<"个元素:";
    }
    double sum=0.0;
    double avg=0.0;
    int count=0;
    for(int j=0;j<i;j++)
    {
        sum+=donation[j];
    }
    avg=sum/i;
    for(int j=0;j<i;j++)
    {
        if(donation[j]>avg) count++;
    }
    cout<<"数组的平均值为"<<avg<<",其中有"<<count<<"个大于平均值";
    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>
using namespace std;
int main()
{
    cout<<"Please enter one of the following choices:"<<endl;
    cout<<"c) carnivore          p) pianist"<<endl;
    cout<<"t) tree              g) game"<<endl;
    char ch;
    cin>>ch;
    while(ch!='c'&&ch!='p'&&ch!='t'&&ch!='g')
    {
        cout<<"Please enter a c, p, t, or g:";
        cin>>ch;
    }
    switch(ch)
    {
        case 'c':cout<<"It's a carnivore."<<endl;break;
        case 'p':cout<<"It's a pianist."<<endl;break;
        case 't':cout<<"It's a tree."<<endl;break;
        case 'g':cout<<"It's a game."<<endl;break;
    }
    return 0;
}

4.加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:

// Benevolent Order of Programmers 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
q. 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
q. 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>
#include <cstring>
using namespace std;
const int strsize = 50;
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
};
int main()
{
    bop bops[5];
    strcpy(bops[0].fullname,"Wimp Macho");strcpy(bops[0].title,"0 pro");
	strcpy(bops[0].bopname,"name 0");bops[0].preference=0;
	strcpy(bops[1].fullname,"Raki Rhodes");strcpy(bops[1].title,"1 pro");
	strcpy(bops[1].bopname,"name 1");bops[1].preference=1;
	strcpy(bops[2].fullname,"Celia Laiter");strcpy(bops[2].title,"2 pro");
	strcpy(bops[2].bopname,"name 2");bops[2].preference=2;
	strcpy(bops[3].fullname,"Hoppy Hipman");strcpy(bops[3].title,"3 pro");
	strcpy(bops[3].bopname,"name 3");bops[3].preference=0;
	strcpy(bops[4].fullname,"Pat Hand");strcpy(bops[4].title,"4 pro");
	strcpy(bops[4].bopname,"name 4");bops[4].preference=1;
    cout<<"Benevolent Order of Programmers Report"<<endl;
    cout<<"a. display by name		b. display by title"<<endl;
    cout<<"c. display by bopname  	d. display by preference"<<endl;
    cout<<"q. quit\nEnter your choice:";
    char ch;
    cin>>ch;
    while(1)
    {
    	while(ch!='a'&&ch!='b'&&ch!='c'&&ch!='d'&&ch!='q')
    {
    	cout<<"Wrong choice!Next choice:";
    	cin>>ch;
	}
	switch(ch)
	{
		case 'a':
			for(int i=0;i<5;i++) cout<<bops[i].fullname<<endl;
			break;
		case 'b':
			for(int i=0;i<5;i++) cout<<bops[i].title<<endl;
			break;
		case 'c':
			for(int i=0;i<5;i++) cout<<bops[i].bopname<<endl;
			break;
		case 'd':
			for(int i=0;i<5;i++)
			{
				if(bops[i].preference==0) cout<<bops[i].fullname<<endl;
				else if(bops[i].preference==1) cout<<bops[i].title<<endl;
				else if(bops[i].preference==2) cout<<bops[i].bopname<<endl;
			}
			break;
		case 'q':cout<<"Bye!";return 0;
	}
	cout<<"Next choice:";
	cin>>ch;
	}
	return 0;
}

5. 在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:

5000 tvarps:不收税
5001~15000 tvarps: 10%
15001~35000 tvarps: 15%
35000 tvarps 以上:20%
例如,收入为38000tvarps时,所得税为5000×0.00+10000×0.10+20000×0.15+3000×0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。

#include <iostream>

using namespace std;

int main()
{
    double x;
    cout<<"请输入收入:";
    cin>>x;
    while(x>=0&&!cin.fail())
    {
    	if(x>=0&&x<=5000)
    	cout<<"所得税为0"<<endl;
    	else if(x>=5001&&x<=15000)
    	cout<<"所得税为"<<(x-5000)*0.1<<endl;
		else if(x>=15001&&x<=35000)
		cout<<"所得税为"<<1000+(x-15000)*0.15<<endl;
		else if(x>=35001)
		cout<<"所得税为"<<1000+3000+(x-35000)*0.2<<endl;
		cout<<"请继续输入:";
		cin>>x; 
	}
	cout<<"程序结束";
	return 0;
}

6. 编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。

#include <iostream>
using namespace std;

struct person
{
	string s;
	double d;
};
int main()
{
	int n;
	cout<<"请输入捐款人数:";
	cin>>n;
	cout<<"接下来"<<n<<"行请分别输入姓名和捐款额:"<<endl;
	person* p=new person[n];
	for(int i=0;i<n;i++) cin>>p[i].s>>p[i].d;
	int count=0;
	cout<<"重要捐款人名单为:";
	for(int i=0;i<n;i++)
	{
		if(p[i].d>10000) 
		{
			cout<<p[i].s<<" ";
			count++;
		}
	}
	if(count==0) cout<<"none";
	cout<<endl;
	count=0;
	cout<<"Patrons名单为:";
	for(int i=0;i<n;i++)
	{
		if(p[i].d<=10000) 
		{
			cout<<p[i].s<<" ";
			count++;
		}
	}
	if(count==0) cout<<"none";
	cout<<endl;
	delete[] p;
	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>
using namespace std;

int main()
{
    cout<<"Enter words (q to quit):"<<endl;
    int c_vo=0;
    int c_con=0;
    int oth=0;
    string s;
    while(cin>>s)
    {
        if(s=="q") break;
        else if(!isalpha(s[0]))
        {
            oth++;
        }
        else 
        {
            switch(s[0])
            {
                case 'a':case 'A':case 'e':case 'E':case 'i':case 'I':case 'o':case 'O':case 'u':case 'U':
                c_vo++;break;
                default:
                c_con++;break;
            }
        }
    }
    cout<<c_vo<<" words beginning with vowels"<<endl;
    cout<<c_con<<" words beginning with consonants"<<endl;
    cout<<oth<<" others";
    return 0;
}

8. 编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。

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

int main()
{
    ifstream fp;
    fp.open("a.txt");
    int count=0;
    char ch;
    while(fp.get(ch))
    {
        cout<<ch;
        count++;
    }
    fp.close();
    cout<<"总共"<<count<<"个字符";
    return 0;
}

9. 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:

4

Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000

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

struct person
{
	string s;
	double d;
};
int main()
{
    ifstream fp;
    fp.open("a.txt");
	int n;
    fp>>n;
	fp.get();
	cout<<"捐款人数:"<<n<<endl;
	person* p=new person[n];
	for(int i=0;i<n;i++) 
    {
        getline(fp , p[i].s);
        fp>>p[i].d;
		fp.get();
    }
	int count=0;
	cout<<"重要捐款人名单为:";
	for(int i=0;i<n;i++)
	{
		if(p[i].d>10000) 
		{
			cout<<p[i].s<<" ";
			count++;
		}
	}
	if(count==0) cout<<"none";
	cout<<endl;
	count=0;
	cout<<"Patrons名单为:";
	for(int i=0;i<n;i++)
	{
		if(p[i].d<=10000) 
		{
			cout<<p[i].s<<" ";
			count++;
		}
	}
	if(count==0) cout<<"none";
	cout<<endl;
    fp.close();
	delete[] p;
	return 0;
}
  • 19
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值