《C++ Primer Plus 第六版》编程练习参考答案(第五章)

第五章

第一题:

#include <iostream>
using namespace std;

int main()
{
	int a=0;
	int b=0;
	int SUM=0;
	
	cout<<"Please enter two numbers a b(a<b):\n";
	cout<<"a = ";
	cin>>a;
	cout<<"b = ";
	cin>>b;
	
	for(int i=a;i<=b;i++)
	{
		SUM+=i;
	}
	
	cout<<"The SUM of the numbers from a to b is: "<<SUM;
}

第二题:

#include <iostream>
#include <array>
using namespace std;
const int ArSize=101;

int main()
{
	array<long double,ArSize> factorials;
	factorials[1]=factorials[0]=1;
	for(int i=2;i<ArSize;i++)
		factorials[i]=i*factorials[i-1];
	for(int i=0;i<ArSize;i++)
		cout<<i<<"!="<<factorials[i]<<endl;
}

第三题:

#include <iostream>
using namespace std;
const int MaxSize=100;

int main()
{
	double num[MaxSize]={};
	double SUM=0;
	bool stop=false;
	cout<<"Please enter a number(Enter zero to stop): ";
	
	for(int i=0;(i<MaxSize)&&(stop==false);i++)
	{
	//Initialize one number in one loop in order to identify the stop signal	
		num[i]=1;
		
		cin>>num[i];
		SUM+=num[i];
		if(num[i]!=0)
			cout<<"Up to now, the SUM of the numbers is: "<<SUM<<endl;
		else
			stop=true;
	}
	
	cout<<"Loop exit.";
}

第四题:

#include <iostream>
using namespace std;

int main()
{
	int year=0;
	double Init=100;
	double DaphneTotal=100;
	double CleoTotal=100;
	
	for(year=1;DaphneTotal>=CleoTotal;year++)
	{
		DaphneTotal+=Init*0.1;
		CleoTotal+=CleoTotal*0.05;
	}
	
	cout<<year-1
	<<" years later, the investment value of Cleo exceed that of Daphne."<<endl;
	cout<<"Cleo: "<<CleoTotal<<endl;
	cout<<"Daphne: "<<DaphneTotal<<endl;
}

第五题:

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

int main()
{
	int sales[12]={};
	int SUM=0;
	string month[12]=
	{"Jan","Fen","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
	
	cout<<"Please enter the sales of <C++ For Fools> every month."<<endl;
	for(int i=0;i<12;i++)
	{
		cout<<month[i]<<": ";
		cin>>sales[i];
		SUM+=sales[i];
	}
	
	cout<<"The sales of the whole year is: "<<SUM<<endl;
}

第六题:

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

int main()
{
	int sales[3][12]={};
	int SUM[3]={};
	string month[12]=
	{"Jan","Fen","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
	cout<<"Please enter the sales of <C++ For Fools> every month."<<endl;
	cout<<"Fisrt year: "<<endl;
	
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<12;j++)
		{
			cout<<month[j]<<": ";
			cin>>sales[i][j];
			SUM[i]+=sales[i][j];
		}
		cout<<"This year's sales is: "<<SUM[i]<<endl;
		if(i!=2)
			cout<<"Next year: "<<endl;
		else 
			cout<<"The total sales is: "<<SUM[0]+SUM[1]+SUM[2]<<endl;
	}
}

第七题:

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

int main()
{
	int num;
	struct car
	{
		string make;
		int yearmade;
	};
	
	cout<<"How many cars do wish to catalog? ";
	cin>>num;
	cin.get();	
	car *carlist=new car[num];
	
	for(int i=0;i<num;i++)
	{
		cout<<"Car #"<<i+1<<":"<<endl;
		cout<<"Please enter the make: ";
		getline(cin,carlist[i].make);
		cout<<"Please enter the year made: ";
		cin>>carlist[i].yearmade;
		cin.get(); 
	}
	
	cout<<"Here is your collection:"<<endl;
	for(int i=0;i<num;i++)
	{
		cout<<carlist[i].yearmade<<" "<<carlist[i].make<<endl;
	}
}

第八题:

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

int main()
{
	char ch[20];
	int count=0;
	
	cout<<"Enter words; enter 'done' to quit."<<endl;
	while(cin>>ch)
	{
		if(strcmp(ch,"done"))
			count++;
		else
			break;
	}
	cout<<"You entered "<<count<<" words";
}

第九题:

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

int main()
{
	string st;
	int count=0;
	
	cout<<"Enter words; enter 'done' to quit."<<endl;
	while(cin>>st)
	{
		if(st!="done")
			count++;
		else
			break;
	}
	cout<<"You entered "<<count<<" words";
}

第十题:

#include <iostream>
using namespace std;

int main()
{
	int line;
	cout<<"How many line do you want to show? ";
	cin>>line;
	
	for(int i=1;i<=line;i++)
	{
		for(int j=0;j<line-i;j++)
			cout<<".";
		for(int k=0;k<i;k++)
			cout<<"*";
		cout<<endl;
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值