C++Primer 第六版中文答案(第六章)

6.11.1

# include <iostream>
# include<cctype>

int main()
{
	using namespace std;
	cout<<"Enter text for analysis, and type @ to terminate input.\n ";
	char ch;
	cin.get(ch);
	while(ch!='@')
	{
		if(isdigit(ch))
		{
			cout<<"please input a new letter.";
			cin.get(ch);
		}
		else
		{
			if(islower(ch))
			{
				ch=toupper(ch);
			}
			else 
			{
				ch=tolower(ch);
			}
			cout<<ch;
			cin.get(ch);
		}
	}
	return 0;
}


 

6.11.2

# include <iostream>
#include <array>
const int Max=10;
int main()
{
	using namespace std;
	array<double,Max> ad;
	cout<<"Please enter the donations.\n";
	cout<<"you may enter up to "<<Max<<" donations<1 to terminate>."<<endl;
	cout<<"donation #1: ";
	int i=0;
	while(i<Max&&cin>>ad[i])
	{
		if(++i<Max)
			cout<<"donations #"<<i+1<<": ";
	}
	//calculate average
	double total =0.0;
	for(int j=0;j<i;j++)
		total+=ad[j];
	double average;
	average=total/i;
	//calculate the number higher than average
	int count=0;
	for(int k=0;k<i;k++)
	{
		if(ad[k]>average)
			count++;
	}
	cout<<average<<"= the average of donations"<<endl;
	cout<<" the donations up the average is "<<count;
	return 0;
}


6.11.3

# include <iostream>
using namespace std;
void showmenu();
int main()
{
	showmenu();
	cout<<"Please enter a c,p,t,or g: ";
	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<<"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 tree.\n";
                  break;
	}
	return 0;
}

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


6.11.4

# include <iostream>
using namespace std;
const int strsize=20;
struct bop
{
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};

int main()
{
	cout<<"Bnevolent Order of Programmers Report\n"
		  "a. display by name     b.display by title\n"
		  "c.display by bopname   d.display by prefenrences\n"
		  "q.quit\n";
	bop number[5]=
	{
		{"Wimp Macho","English Teacher","DEMON",0},
		{"Raki Rhodes","Junior Programmer","LILY",1},
		{"Celia Laiter","Movie star","MIPS",2},
		{"Hoppy Hipman","Analyst Trainee","BUSH",1},
		{"Pat Hand","doctor","LOOPY",2}
	};
	cout<<"Enter your choice: ";
	char ch;
	while(cin>>ch&&ch!='q')
	{
		switch(ch)
		{
		case 'a':
			for(int i=0;i<5;i++)
				cout<<number[i].fullname<<endl;
			break;
		case 'b':
			for(int i=0;i<5;i++)
				cout<<number[i].title<<endl;
			break;
		case 'c':
			for(int i=0;i<5;i++)
				cout<<number[i].bopname<<endl;
			break;
		case 'd':
			for(int i=0;i<5;i++)
			{
				if(0==number[i].preference)
					cout<<number[i].fullname<<endl;
				else if(1==number[i].preference)
					cout<<number[i].title<<endl;
				else if(2==number[i].preference)
					cout<<number[i].bopname<<endl;
			}
			break;
		default : cout<<"That's not choice.\n";
		}
		cout<<"Next Choice: ";
	}
	cout<<"Bye!\n";
	return 0;
}

6.11.5

# include <iostream>
using namespace std;


int main()
{
	double income,revenue;
	cout<<"Please input your income: ";
	while(cin>>income&&income>=0)
	{
		if(income<=5000)
			revenue=0;
		else if(income<=15000)
			revenue=(income-5000)*0.1;
		else if(income<=35000)
			revenue=(income-15000)*0.15+(15000-5000)*0.1;
		else 
			revenue=(income-35000)*0.2+(35000-15000)*0.15+(15000-5000)*0.1;
		cout<<"your revenue is "<<revenue<<endl;
		cout<<"Please input your income: ";
	}
	cout<<"Bye.\n";
	return 0;
}

6.11.6

# include <iostream>
#include <string>
const double distinguish=10000;
using namespace std;

struct patron{
	string name;
	double fund;
};

int main()
{
	int number;
	int count1=0,count2=0;
	cout<<"The number of patrons: ";
	cin>>number;
	cin.get();
	patron *ppatron=new patron[number];
	for(int i=0;i<number;i++)
	{
		cout<<"Please input the name of patrons: ";
		getline(cin,ppatron[i].name);
		cout<<"Please input the fund of patrons: ";
		cin>>ppatron[i].fund;
		cin.get();
	}

	cout<<"Grand Patrons: \n";
	for(int i=0;i<number;i++)
	{
		if(ppatron[i].fund>distinguish)
		{
			cout<<ppatron[i].name<<endl;
			count1++;
		}
	}
	if(0==count1)
		cout<<"none\n";

    cout<<"Patrons: \n";
	for(int i=0;i<number;i++)
	{
		if(ppatron[i].fund<=distinguish)
		{
			cout<<ppatron[i].name<<endl;
			count2++;
		}
	}
	if(count2==0)
		cout<<"none\n";
	delete []ppatron;
	return 0;
}


6.11.7

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

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


6.11.8

# include <iostream>
# include <fstream>
# include<cstdlib>  //support for exit
using namespace std;

int main()
{
	char ch;
	int sum=0;
	ifstream inFile;
	inFile.open("hehe.txt"); //文件的路径:相对路径和绝对路径都可以
	//判断文件是否打开
	if(!inFile.is_open())
	{
		cout<<"Could not op the file "<<endl;
		cout<<"Program terminatinf.\n";
		exit(EXIT_FAILURE);
	}
	inFile>>ch;
	while(inFile.good())
	{
		sum++;
		inFile>>ch;
	}
	if(inFile.eof())
		cout<<"End of file reached.\n";
	else if(inFile.fail())
		cout<<"Input terminated by data mismatch.\n";
	else 
		cout<<"Input terminated for unknown reasons.\n";
	cout<<"共有"<<sum<<"在这个文件中";
	return 0;
}

6.11.9

# include <iostream>
# include <fstream>
# include<string>
# include<cstdlib>  //support for exit
using namespace std;

struct patron{
	char name[20];
	double fund;
};

int main()
{
	
	int num;
	int count1=0;
	int count2=0;
	ifstream fin;
	char filename[50];
	cout<<"The name of data file: ";
	cin>>filename;
	fin.open(filename);
	if(!fin.is_open())
	{
		cout<<"Couldn't open the file"<<filename<<endl;
		cout<<"Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	fin>>num;
	fin.get();
	patron *ppatron=new patron[num];
	for(int i=0;i<num;i++)
	{
		fin.getline(ppatron[i].name,20);
		fin>>ppatron[i].fund;
		fin.get();
	}
	cout<<"Grand Patron:\n";
	for(int i=0;i<num;i++)
	{
		if(ppatron[i].fund>=10000)
		{
			cout<<ppatron[i].name<<endl;
			count1++;
		}
	}
	if(0==count1)
		cout<<"none\n";

	cout<<"Patrons: \n";
	for(int i=0;i<num;i++)
	{
		if(ppatron[i].fund<10000)
		{
			cout<<ppatron[i].name<<endl;
			count2++;
		}
	}
	if(count2==0)
		cout<<"none\n";
	delete []ppatron;
	return 0;
}




 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值