C++ primer plus 第五章笔记和答案

本文提供了C++ Primer Plus第五章的编程练习详细解答,涵盖了关键的编程概念和技巧。
摘要由CSDN通过智能技术生成

三、编程练习答案

#include<iostream>
using namespace std;
int main()
{
	int x;
	int y;
	int sum;
	cout << "Please enter two number: ";
	cin >> x>> y;
	int z = x;
	for (sum = 0; x <= y; x++)
		sum += x;
	cout << "The sum of " << z << " to " << y << " is: " << sum << endl;
	return 0;
}
#include<iostream>
#include<array>
using namespace std;
const int Arsize = 100;
int main()
{
	array<long double, Arsize + 1>factorials;    //array 用法:array<类型;元素个数>数组名
	factorials[0] = factorials[1] = 1.0;
	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;
	return 0;
}
#include<iostream>
using namespace std;
int main()
{
	int x;
	int sum=0;
	cout << "Please enter the number: ";
	cin >> x;
	while (x!=0)
	{
		sum += x;
		cout << "Untill now, the sum is: " << sum << endl;
		cin >> x;
	}
	return 0;
}
#include<iostream>
using namespace std;
int main()
{
	int x;
	double SumDa=100.0;
	double SumCl=100.0;
	double InCl;
	for (x = 1; SumDa >= SumCl; x++)
	{
		SumDa = 100.0 + 0.1 * 100.0 * x;
		InCl = 0.05 * SumCl;
		SumCl += InCl;
	}
	cout << x << " years later, Cleo have :" << SumCl << "money, and Daphne have "
		<< SumDa << " money. " << endl;
	return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string month[12] =
	{"January","February","March","April","May","June","July",
		"August","Septempber","October","November","December"};
	int sel[12];
	int sum=0;
	int i = 0;
	cout << "Enter the sales of book each month: ";
	while (i<12)
	{
		cout << month[i] << " sell : ";
		cin >> sel[i];
		sum += sel[i];
		i += 1;
	}
	cout << "The sum is: " << sum;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string month[12] =
	{"January","February","March","April","May","June","July",
		"August","Septempber","October","November","December"};
	int maxtemps[3][12];
	int totalsum = 0;
	int sum[3] = { 0};
	cout << "Enter the sales of book each month: ";
	for (int year = 0; year < 3; year++)
	{
		for (int mon = 0; mon < 12; mon++)
		{
			cout << "Enter " << year + 1 << " year's " << month[mon]<<": ";
			cin >> maxtemps[year][mon];
			sum[year] += maxtemps[year][mon];
		}
		totalsum += sum[year];
	}
	cout << "The sales of first year is " << sum[0] << endl;
	cout << "The sales of second year is " << sum[1] << endl;
	cout << "The sales of third year is " << sum[2] << endl;
	cout << "The total sales is " << totalsum<< endl;
	return 0;
}
#include<iostream>
#include<string>
using namespace std;
struct car
{
	string company;
	int year;
};
int main()
{
	int i;
	cout << "How many cars do you wish to enter  : ";
	cin >> i;
	cin.get();
	//使用get函数简单的暂停屏幕直到按回车键,且不需要储存字符

	struct  car* p1 = new struct car[i];
	for (int x = 0; x < i; x++)
	{
		cout << "Car # " << x+1 << " : " << endl;
		cout << "Please enter the make: ";
		getline(cin, p1[x].company);
		cout << "Please enter the year made: ";
		cin >> p1[x].year;
		cin.get();
	}
	cout << "Here is your collection: " << endl;
	for (int x = 0; x < i; x++)
	{
		cout << p1[x].year << " " << p1[x].company << endl;
	}
	return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int n = 0;
	char input[128];
	cout << "Enter words (to stop, type the word done) : " << endl;
	while (cin>>input)
	{
		if (strcmp(input, "done"))
		{
			n++;
		}
		else
		{
			break;
		}
	}
	cout << "You entered a total of " << n << " words. " << endl;
	return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n = 0;
	string input;
	cout << "Enter words (to stop, type the word done) : " << endl;
	while (cin>>input)
	{
		if (input != "done")
		{
			n++;
		}
		else
		{
			break;
		}
	}
	cout << "You entered a total of " << n << " words. " << endl;
	return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int row ;
	cout << "Enter number of rows: ";
	cin >> row;
	for (int i = 1; i <= row; i++)
	{
		for (int j = i; j < row; j++)
		{
			cout << ".";
		}
		for (int j = 0; j < i; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值