C++ Primer Plus 第六版(中文版)课后编程题----第五章

5.1

<span style="font-size:18px;">#include <iostream>
using namespace std;

void swapNum(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

int main()
{
	int min, max, sum = 0;
	cout << "Please enter two int number: ";
	cin >> min >> max;
	if (min > max)
		swapNum(min, max);

	for (int i = min; i <= max; ++i)
		sum += i;

	cout << "The sum of " << min << " and " << max << " is " << sum << ".\n";

	cin.get();
	cin.get();
	return 0;
}</span>


5.2

<span style="font-size:18px;">#include <iostream>
#include <array>
using namespace std;

const int Arrsize = 101;

int main()
{
	array<long double, Arrsize> arr;
	arr[0] = 1;
	for (int i = 1; i < Arrsize; ++i)
		arr[i] = i*arr[i - 1];

	for (int i = 0; i < Arrsize; ++i)
		cout << i << "! = " << arr[i] << endl;

	cin.get();
	return 0;
}</span>


5.3

<span style="font-size:18px;">#include <iostream>
using namespace std;

int main()
{
	int num, sum = 0;
	cout << "Enter numbers: " << endl;
	cin >> num;
	if (!num)	//若第一个输入为 0,也报告其和,并结束程序。
		cout << "sum = " << sum << endl;
	else
	{
		while (num)
		{
			sum += num;
			cout << "sum = " << sum << endl;
			cin >> num;
		}
	}
	
	cin.get();
	cin.get();
	return 0;
}</span>


5.4

<span style="font-size:18px;">#include <iostream>
using namespace std;

const int ini = 100;	//原始存款
const double b_D = 0.10;//Daphne的利率
const double b_C = 0.05;//Cleo的利率

int main()
{
	int year = 1;
	double m_D = ini;
	double m_C = ini;
	while (m_C <= m_D)
	{
		++year;
		m_D += ini*b_D;
		m_C += m_C*b_C;
	}

	cout << "After " << year << " years, the Cleo's money is more than Daphne's." << endl;
	cout << "Cleo's money is: " << m_C << endl;
	cout << "Daphne's money is: " << m_D << endl;

	cin.get();
	return 0;
}</span>


5.5

<span style="font-size:18px;">#include <iostream>
#include <string>
using namespace std;

const int ArrSize = 12;

int main()
{
	string months[ArrSize] = { "January", "February", "March", "April", "May", "June", 
		"July", "August", "September", "October", "November", "December" };
	int sales[ArrSize];
	int sum = 0;

	for (int i = 0; i < ArrSize; ++i)
	{
		cout << months[i] << "'s sales volume is: ";
		cin >> sales[i];
		sum += sales[i];
	}

	cout << "The sales volume in this year is: " << sum << endl;

	cin.get();
	cin.get();
	return 0;
}</span>


5.6

<span style="font-size:18px;">#include <iostream>
using namespace std;

const int row = 3;
const int vol = 12;

int main()
{
	int arr[row][vol];
	int sum = 0, sum_temp;

	for (int i = 0; i < row; ++i)
	{
		sum_temp = 0;
		cout << "Enter the " << i + 1 << " year's sales volume:" << endl;
		for (int j = 0; j < vol; ++j)
		{
			cout << "The " << j + 1 << " month's sale volume: ";
			cin >> arr[i][j];
			sum_temp += arr[i][j];
		}
		cout << "The " << i + 1 << " year's sale volume is: " << sum_temp << endl;
		sum += sum_temp;
	}
	cout << "The total sale volume of three years is: " << sum << endl;

	cin.get();
	cin.get();
	return 0;
}</span>


5.7

<span style="font-size:18px;">#include <iostream>
#include <string>
using namespace std;

struct car
{
	string Manufacturer;
	int year;
};

int main()
{
	int n;
	cout << "How many cars do you wish to catalog? ";
	(cin >> n).get();
	car *cars = new car[n];

	for (int i = 0; i < n; ++i)
	{
		cout << "Car #" << i + 1 << ":\n";
		cout << "Please enter the make: ";
		getline(cin, cars[i].Manufacturer);
		cout << "Please enter the year made: ";
		(cin >> cars[i].year).get();
	}
	cout << "Here is your collection: \n";
	for (int i = 0; i < n; ++i)
		cout << cars[i].year << "\t" << cars[i].Manufacturer << endl;

	delete []cars;

	cin.get();
	return 0;
}</span>


5.8

如果不用 char str[50] 的话怎么做?我试过 char *str,但是提示指针未赋初值;也试过 char* str = new char; delete []str; 会出现中断(查了一下说是指针指向的地址已被修改,所以会出错)。

<span style="font-size:18px;">#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char str[50];
	int num = 0;
	cout << "Enter words (to stop, type word done):" << endl;
	cin >> str;
	while (strcmp(str, "done"))
	{
		++num;
		cin >> str;
	}
	cout << "you entered a total of " << num << " words." << endl;

	system("pause");
	return 0;
}</span>


5.9

<span style="font-size:18px;">#include <iostream>
#include <string>
using namespace std;

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

	system("pause");
	return 0;
}</span>


5.10

#include <iostream>
using namespace std;

int main()
{
	int n;
	cout << "Please enter a number: ";
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n - i - 1; ++j)
			cout << ".";
		for (int k = 0; k <= i; ++k)
			cout << "*";
		cout << endl;
	}

	cin.get();
	cin.get();
	return 0;
}
















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值