C ++ Primer Plus 第六版 第六章编程练习答案

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

#include<iostream>
#include<cctype>
void main()
{
	using namespace std;
	char ch;
	while((ch=cin.get())&&ch!='@')
	{	
		if(isdigit(ch))
			continue;
		if(isupper(ch))
			cout<<char(tolower(ch));
			else if(islower(ch))
				cout<<char(toupper(ch));
			else cout<<ch;
			
	}
	cout<<endl;
}


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

#include<iostream>
void main()
{
	using namespace std;
	double donation[10];
	int i=0,j,temp,count;
	double sum;
	while(i<10 && (cin>>temp))
	{
		donation[i]=temp;
		i++;
	}
	
	for(j=0,sum=0;j<i;j++)
		sum+=donation[j];
	sum/=(i);
	for(j=0,count=0;j<i;j++)
		if(donation[j]>sum)
			count++;
	cout<<"Average is "<<sum<<endl<<"Greater than average number of "<<count<<endl;
}


3. 编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:

#include<iostream>
void main()
{
	using namespace std;
    char letter;
	cout << "Please enter one of the following choise:"<<endl;
	cout << "c) carnivore" << "      " << "p) pianist" << endl;
    cout << "t) tree " << "          " << "g) game" << endl;
	cin>>letter;
	while(letter!='c'&&letter!='p'&&letter!='t'&&letter!='g')
	{
		cout << "please enter a c , p , t or g: ";
		cin>>letter;
	}
	switch(letter)
	{
		case 'c':cout<<"carnivore";break;
		case 'p':cout<<"pianist";break;
		case 't':cout<<"A maple is a tree.";break;
		case 'g':cout<<"tree";break;
	}
	cout<<endl;
}


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

5000 tvarps”:不收税

5001~15000 tvarps:10%

15001~35000 tvarps:15%

35000以上: 20%

例如,收入为38000 tvarps时,所得税为5000*0.00 + 10000*0.10+20000*0.15+3000*0.20,即4600tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。

#include <iostream>

int main()
{
	using namespace std;
	double revenue;
	cout<<"Please enter your revenue"<<endl;
	while ( cin >> revenue )
	{
		if ( revenue < 0.0 ) break;
		if ( revenue <= 5000.0 )
			cout << "0" << "tvarps"<<endl;
		else if ( revenue >= 5001.0 && revenue <= 15000.0 )
			cout << (( revenue - 5000.0 ) * 0.1 )<<"tvarps"<< endl;
		else if ( revenue >= 15001.0 && revenue <= 35000.0 )
			cout << (10000.0 * 0.1 + ( revenue - 15000.0 ) * 0.15) << "tvarps"<<endl;
		else if ( revenue > 35000 )
			cout << (10000.0 * 0.1 + 20000.0 * 0.15 + ( revenue - 35000.0 ) * 0.2) <<"tvarps"<< endl;
	}
	return 0;
}



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

#include <iostream>
#include<string>
using namespace std;
struct donation
{
	string name;
	double money;
};

int main()
{
	int num;
	cin >> num;
	donation *don = new donation[num];
	int i, count = 0;
	for ( i = 0; i < num; i++ )
	{
		cout << "name :";
		cin >> don[i].name ;
		cout << "money :";
		cin >> don[i].money;
	}
	cout << "Grand Patrons :" << endl;
	for ( i = 0; i < num; i++ )
	{
		if ( don[i].money > 10000 )
		{
			cout << don[i].name << '\t' << don[i].money << endl;
			count++;
		}
	}
	if ( count == 0 )
		cout << "none\n\n";

	cout << "Patrons :" << endl;
	count = 0;
	for ( i = 0; i < num; i++ )
	{
		if ( don[i].money <= 10000 )
		{
			cout << don[i].name << '\t' << don[i].money << endl;
			count++;
		}
	}
	if ( count == 0 )
		cout << "none\n\n";
	return 0;
	delete []don;
}


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

whitehack写的,摘抄在这里,有些换行错了,少了头文件,都改了过来,我自己写的已经不行了,写了一下午,最后啥都没写出来

#include <iostream>
#include<cstring>
#include <cctype>
using namespace std;
int main()
{
    int YuanYin=0;
    int FuYin=0;
    int Others=0;
    char str[80];                           //还是不会怎么用 STRING...........
    bool end=false;                             //..放一个BOOL变量控制循环...
    cout <<"Enter words (q to exit):\n";
    while (cin>>str  && false == end )          //
    {
//      cout <<str<<endl;
        if (isalpha(str[0])>0)
        {
            switch (str[0])
            {
            case 'a':
            case 'o':
            case 'e':
            case 'i':
            case 'u':
            case 'A':
            case 'O':
            case 'E':
            case 'I':
            case 'U':
                ++YuanYin;
                break;
            case 'q':
                if (strlen(str)==1)
                {
                    end=true;           //读取到单独的Q
                    break;
                }
            default:

                ++FuYin;
            }
            if (end)
                break;  //读取到 单独的 Q 跳出循环
            continue;       //否着继续下一次循环
        }
        ++Others;           //非字母
    }
    cout << YuanYin <<" words beginning with vowel\n";
    cout <<FuYin <<" words beginning with consonants\n";
    cout <<Others <<" others";
        return 0;
}

好了,休息了一天参照了下上面大佬的和别人的,写出来了

if版本:

#include <iostream>
#include<string>
#include<cctype>
#include<cstring>
int main()
{

	using namespace std;
	string word;
	int c1 = 0, c2 = 0, c3 = 0;
	while ( cin >> word && word != "q" )
	{

		if ( isalpha ( word[0] ) )
		{

			if ( isupper ( 0 ) )
				word[0] = char ( tolower ( word[0] ) );

			if ( word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u' )
				c1++;


			else
				c2++;
		}
		else c3++;


	}
	cout << c1 << " words beginning with vowel\n";
	cout << c2 << " words beginning with consonants\n";
	cout << c3 << " others";

	return 0;
}

switch版本:

#include <iostream>
#include<string>
#include<cctype>
#include<cstring>
int main()
{

	using namespace std;
	string word;
	int c1 = 0, c2 = 0, c3 = 0;
	while ( cin >> word && word != "q" )
	{

		if ( isalpha ( word[0] ) )
		{

			if ( isupper ( word[0] ) )
				word[0] = char ( tolower ( word[0] ) );

			switch ( word[0] )
			{
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				c1++;
				break;
			default:
				c2++;
				break;

			}
		}
		else c3++;


	}
	cout << c1 << " words beginning with vowel\n";
	cout << c2 << " words beginning with consonants\n";
	cout << c3 << " others";

	return 0;
}



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

#include<iostream>
#include<fstream>
#include<cstdlib>


int main()
{
	using namespace std;
	ifstream infile ( "C:\\Users\\Administrator\\Desktop\\file.txt", ios::in );
	if ( !infile )
	{
		cout << "Failed to open the file" << endl;
		exit ( EXIT_FAILURE );
	}
	int n = 0;
	char read;
	while ( !infile.eof() )
	{
		infile >> read;
		n++;
	}
	cout << "This file has " << n << " characters" << endl;
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值