C++ Primer Plus 第六版第五章循环和关系表达式编程练习答案

1.

int main()
{
	int n, m, sum = 0;
	cout << "输入较小的数: ";
	cin >> n;
	cout << "输入较大的数: ";
	cin >> m;
	if (m >= n)
	{
		for (int i = n; i <= m; ++i)
			sum += i;
		cout << n << "到" << m << "之间的所有整数和是: " << sum << endl;
	}
	else
	{
		for (int i = m; i <= n; ++i)
			sum += i;
		cout << m << "到" << n << "之间的所有整数和是: " << sum << endl;
	}
	return 0;
}

2.

#include 
#include
using namespace std;
const int ArSize = 100;
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 < 16; i++)
		cout << i << "! = " << factorials[i] << endl;
	cout << "100! = " << factorials[99] << endl;

	return 0;
}

3.

#include
using namespace std;
int main()
{
	int value, sum = 0;
	while (cin >> value && value)
	{
		sum += value;
		cout << "到目前为止,所有输入的累积和是: " << sum << endl;
	}
	cout << "程序结束" << endl;

	return 0;
}

4.

#include
using namespace std;
int main()
{
	int value = 100, year;
	double Interest = 0, Interest1 = 0;

	for (year = 1; Interest >= Interest1; ++year)
	{
		Interest += 10;
		Interest1 += value * 0.05;
		value += Interest1;
	}
	cout << year - 1 << "年后" << "Cleo的投资价值才能超过Daphne的投资价值\n";
	cout << "此时,Cleo的总利息是:" << Interest1 << endl;
	cout << "Daphne的总利息是: " << Interest << endl;

}

5.

#include
#include
using namespace std;
int main()
{
	string months[] = { "一月", "二月", "三月", "四月", "五月", "六月",
					   "七月", "八月", "九月", "十月", "十一月", "十二月" };
	int data[12], sum = 0;
	for (int i = 0; i < 12; ++i)
	{
		cout << "请输入" << months[i] << "的图书销售量: ";
		cin >> data[i];
		sum += data[i];
	}
	cout << "这一年的销售量是: " << sum << endl;

}

6.

#include
#include
using namespace std;
int main()
{
	string months[] = { "一月", "二月", "三月", "四月", "五月", "六月",
					   "七月", "八月", "九月", "十月", "十一月", "十二月" };
	int data[3][12], sum = 0, sum1 = 0;

	for (int i = 1; i <= 3; ++i)
	{
		cout << "现在输入第" << i << "年的销售量" << endl;
		sum = 0;
		for (int j = 0; j < 12; ++j)
		{
			cout << "请输入" << months[j] << "的图书销售量: ";
			while (!(cin >> data[i][j]) || data[i][j] < 0)
			{
				cin.clear();//重置错误输入标记,程序才能继续读取输入
				while (cin.get() != '\n')//读取行尾之前的所有输入,删除一行的错误输入
					continue; 
				cout << "输入错误,请重新输入: ";
			}
			sum += data[i][j];
		}
		cout << "这一年的销售量是: " << sum << endl;
		sum1 += sum;
	}
	cout << "这三年的销售量是: " << sum1 << endl;

	return 0;
}

8.

#include
#include
using namespace std;

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

9.

#include
#include
using namespace std;

int main()
{
	string words;
	int n = 0;

	cout << "Enter words (to stop, type the word done):" << endl;
	while (cin >> words && words != "done")
		++n;
	cout << "You entered a total of " << n << " words." << endl;

	return 0;
}

10.

#include
using namespace std;
int main()
{
	int rows;

	cout << "Enter number of rows: ";
	cin >> rows;
	for (int i = 1; i <= rows; ++i)
	{
		for (int j = rows - i; j > 0; --j)
			cout << ".";
		for (int k = 0; k < i; ++k)
			cout << "*";
		cout << endl;
	}

	return 0;
}

你可能感兴趣的:(c++,primer,plus,C++,Primer,Plus,第六版,第五章,编程练习,答案)