C++PrimerPlus第六中文版第五章答案

5.9.1
#include <iostream>
using namespace std;

int main()
{
	cout<<"Please enter two intergers: ";
	int num1,num2;
	cin>>num1>>num2;
	getchar();
	int sum=0;
	for(int temp=num1;temp<=num2;temp++)
	{
		sum+=temp;
	}
	cout<<"The sum from "<<num1<<" to "<<num2<<" is "<<sum<<endl;
	getchar();
	return 0;
}
5.9.2

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

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

5.9.3

#include <iostream>
using namespace std;

int main()
{
	cout<<"please enter an interger: "<<endl;
	int sum=0,num;
	while((cin>>num)&&num!=0)   //确保输入为整数且不为零
	{
		sum+=num;
		cout<<"So far, the sum is "<<sum<<endl;
		cout<<"please input a new interger: "<<endl;
	}
	return 0;
}

5.9.4

#include <iostream>
using namespace std;

int main()
{
	double sum1,sum2;
	sum1=sum2=0.0;
	int year=0;
	while(sum2<=sum1)
	{
		year++;
		sum1=sum1+10;
		sum2=(100+sum2)*0.05+sum2;
	}
	cout<<" Through "<<year<<" year, "<<"The investment value of Celo surpass the Daphne."<<endl;
	cout<<"Celo earn "<<sum1<<endl;
	cout<<"Daphne earn "<<sum2<<endl;
	getchar();
	return 0;
}

5.9.5

#include <iostream>
using namespace std;
const int MONTH=12;

int main()
{
	const char* months[12]={
		"January",
		"February",
		"March",
		"April",
		"May",
		"June",
		"July",
		"Augest",
		"September",
		"October",
		"Novembrt",
		"December"
	};
	int sales[MONTH],sum=0;
	for(int i=0;i<MONTH;i++)
	{
		cout<<"Input the number of FOOLS selled in "<<months[i];
		cin>>sales[i];
		sum+=sales[i];
	}
	cout<<"The sale number of FOOLS in this year is "<<sum<<endl;
	getchar();
	getchar();
	return 0; 
}

5.9.6

#include <iostream>
using namespace std;
const int MONTH=12;

int main()
{
	const char* months[12]={
		"January",
		"February",
		"March",
		"April",
		"May",
		"June",
		"July",
		"Augest",
		"September",
		"October",
		"Novembrt",
		"December"
	};
	const char* year[3]={"第一年","第二年","第三年"};
	int sales[3][MONTH],sum=0,year_sale[3];
	for(int i=0;i<3;i++)
	{
		int temp=0;
		cout<<year[i]<<"每个月的销售量:"<<endl;
	    for(int j=0;j<MONTH;j++)
	  {
		cout<<"Input the number of FOOLS selled in "<<months[j];
		cin>>sales[i][j];
		temp+=sales[i][j];
	   }
		year_sale[i]=temp;
		sum+=year_sale[i];
	}
	for(int i=0;i<3;i++)
	{
		cout<<year[i]<<"的销量为"<<year_sale[i]<<endl;
	}
	cout<<"总销量为"<<sum<<endl;
	getchar();
	getchar();
	return 0; 
}


5.9.7

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

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

5.9.8

#include <iostream>

using namespace std;

int main()
{
	char word[20];
	int sum=0;
	cout<<"Enter words(to stop, type the woed done): ";
	cin>>word;
	while(strcmp(word,"done"))
	{
		sum++;
		cin>>word;
	}
	cout<<"your entered a total of "<<sum<<" words.";
	return 0;
}

5.9.9

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

int main()
{
	string word;
	int sum=0;
	cout<<"Enter words(to stop, type the woed done): ";
	cin>>word;
	while(word!="done")
	{
		sum++;
		cin>>word;
	}
	cout<<"your entered a total of "<<sum<<" words."<<endl;
	return 0;
}

5.9.10

#include <iostream>
using namespace std;

int main()
{
	cout<<"Enter number od rows:";
	int num;
	cin>>num;
	cin.get();
	for(int i=0;i<num;i++)
	{
		for(int j=num-i;j>1;j--)
		{
			cout<<".";
		}
		for(int k=0;k<=i;k++)
			cout<<"*";
		cout<<endl;
	}
	getchar();
	return 0;
}


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值