C++ Primer Plus 第六版 第六章编程练习答案参考(仅仅用作学习交流,若有错误,请联系作者,谢谢)

第一题:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
	char ch;
	cin.get(ch);
	while(ch!='@')
	{
	    if(isalpha(ch))
	    {
	    	if(isupper(ch))
	    		cout<<(char) tolower(ch);
	    	else
	    		cout<<(char) toupper(ch);
		}
		cin.get(ch);
	} 
	return 0;
}

第二题:

#include <iostream>
#include <cctype>
using namespace std;
int main()
{
	double donation[10];
	double average;
	double sum=0;
	int i=0,count=0;
	cout<<"number #1: ";
	while(i<10&&cin>>donation[i])
	{
		sum+=donation[i];
		if(++i<10)
			cout<<"number #"<<i+1<<": ";
	} 
	average=sum/i;
	for(i=0;i<10;i++)
	{
		if(donation[i]>average)
			count++;
	}
	cout<<"In the array, there are "<<count<<" numbers more than the average!"<<endl;
	return 0; 
} 

第三题:

#include <iostream>
using namespace std;
int main()
{
	cout<<"Please enter one of the following choice:"<<endl;
	cout<<"c)carnivore\t\tp)pianist"<<endl;
	cout<<"t)tree\t\t        g)game"<<endl;
	cout<<"Please enter a c, p, t, or g:";
	char ch;
	while(cin>>ch)
	{
	switch(ch)
	{
		case 'c':
		case 'C':
				cout<<"A lion is a  carnivore."<<endl;
				cout<<"Please enter a c, p, t, or g:";
				break;
		case 'p':
		case 'P':
				cout<<"LangLang is a pianist."<<endl;
				cout<<"Please enter a c, p, t, or g:";
				break;
		case 't':
		case 'T':
				cout<<"A maple is a tree."<<endl;
				cout<<"Please enter a c, p, t, or g:";
				break;
		case 'g':
		case 'G':
				cout<<"LoL is a game."<<endl;
				cout<<"Please enter a c, p, t, or g:";
				break;
		case 'f'://f or F to quit 
		case 'F':
				return 0;
		default:
				cout<<"Please enter a c, p, t, or g:";
				break;
	}
	}
	return 0;
}

第四题:

#include <iostream>
using namespace std;
const int strsize=15;
int main()
{
    struct bop{
    	char fullname[strsize];
    	char title[strsize];
    	char bopname[strsize];
    	int preference;
    	};
	bop arr[3]={
		{"My World","Game","myworld",0},
		{"Math","book","highmath",1},
		{"Huawei","phone","transport",2}
		};
	char choice;
	int i;
	cout<<"Benevolent Order of Programmers Report"<<endl;
	cout<<"a.display by name\t"<<"b.dispalu by title"<<endl;
	cout<<"c.display by bopname\t"<<"d.display by preference"<<endl;
	cout<<"q.quit"<<endl;
	cout<<"Enter your choice: ";
	while(cin>>choice&&choice!='q')
	{
		switch(choice){
			case 'a':	cout<<arr[0].fullname<<endl;
						cout<<arr[1].fullname<<endl;
						cout<<arr[2].fullname<<endl;
						cout<<"Next choice: ";
			            break;
			case 'b':	cout<<arr[0].title<<endl;
						cout<<arr[1].title<<endl;
						cout<<arr[2].title<<endl;
						cout<<"Next choice: ";
						break;
		    case 'c':    cout<<arr[0].bopname<<endl;
		    			cout<<arr[1].bopname<<endl;
						cout<<arr[2].bopname<<endl;
						cout<<"Next choice: ";
						break;
			case 'd':    
						for(i=0;i<3;i++)
						{
							if(arr[i].preference==0)
								cout<<arr[i].fullname<<endl;
					        else if(arr[i].preference==1)
					        	cout<<arr[i].title<<endl;
					        else
					        	cout<<arr[i].bopname<<endl;;
						}
						cout<<"Next choice: ";
						break;
			default:    cout<<"Please enter a,b,c,d or q!"<<endl;
						cout<<"Next choice: ";
						break;
						
		}
	}
	cout<<"Bye!"<<endl;
	return 0;
}

第五题:

#include <iostream>
using namespace std;
int main()
{
	cout<<"Please Enter Your Salary: ";
	double salary;
	cin>>salary;
	double tax;
	if(salary<=5000)
		tax=0;
	else if(salary>5000&&salary<=15000)
		tax=0.1*(salary-5000);
	else if(salary>15000&&salary<=35000)
		tax=10000*0.1+(salary-15000)*0.15;
	else
		tax=10000*0.1+20000*0.15+(salary-35000)*0.2;
	cout<<"Your Tax is: "<<tax<<endl;
	return 0;
}

第六题:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    struct man{
    	string name;
    	double money;
    };
	int num;
	cout<<"Please enter the num: ";
	cin>>num;
	man arr[num];
	int i;
	for(i=0;i<num;i++)
	{
		cout<<"Enter the name: ";
		cin.get();
		getline(cin,arr[i].name);
		cout<<"Enter the money: ";
		cin>>arr[i].money;
	}
	int test=0;
	cout<<"Grand Patrons"<<endl;
	for(i=0;i<num;i++)
	{
		if(arr[i].money>10000)
		{
			cout<<arr[i].name<<endl;
		    test++;	
		}
	} 
	if(test==0)
		cout<<"none"<<endl;
	else
		test=0;
	cout<<endl;
	cout<<"Patrons"<<endl;	
		for(i=0;i<num;i++)
	{
		if(arr[i].money<=10000)
		{
			cout<<arr[i].name<<endl;
		    test++;	
		}
	}
	if(test==0)
		cout<<"none"<<endl;
	
	return 0;
}

第七题:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
	cout<<"Enter words (q to quit) :"<<endl;
	string word;
	int count1=0;
	int count2=0;
	int count3=0;
	while(cin>>word)
	{   
	    if(word.size()==1&&word[0]=='q')
	    	break;
		if(isalpha(word[0]))
	    {
	    	if(word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u'||word[0]=='A'||word[0]=='E'||word[0]=='I'||word[0]=='O'||word[0]=='U')
	    		count1++;
	    	else
	    		count2++;
	    }
	    else
	    	count3++;
	}
	cout<<count1<<" words beginning with vowels"<<endl;
	cout<<count2<<" words beginning with consonants"<<endl;
	cout<<count3<<" others"<<endl;
	return 0;
}

第八题:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
int main()
{
	ifstream file;
	file.open("test.txt");
	if(!file.is_open())
	{
		exit(EXIT_FAILURE);
	}
	char c;
	int count=0;
	while(!file.eof())
	{
		file>>c;
		count++;
	}
	cout<<"Here is "<<count-1<<" characters"<<endl;
	file.close();
	return 0;
}

第九题:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
    struct man{
    	string name;
    	double money;
    };
	ifstream file;
	file.open("test1.txt");
	if(!file.is_open())
	{
		exit(EXIT_FAILURE);
	}
	int num;
	file>>num;
	man arr[num];
	int i;
	cout<<num<<endl;
	for(i=0;i<num;i++)
	{   
	    file.get();/* 这里用file.gget()*/ 
		getline(file,arr[i].name);
		file>>arr[i].money;
	}
	int test=0;
	cout<<"Grand Patrons"<<endl;
	for(i=0;i<num;i++)
	{
		if(arr[i].money>10000)
		{
			cout<<arr[i].name<<endl;
		    test++;	
		}
	} 
	if(test==0)
		cout<<"none"<<endl;
	else
		test=0;
	cout<<endl;
	cout<<"Patrons"<<endl;	
		for(i=0;i<num;i++)
	{
		if(arr[i].money<=10000)
		{
			cout<<arr[i].name<<endl;
		    test++;	
		}
	}
	if(test==0)
		cout<<"none"<<endl;
	file.close();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值