C++ premier plus 第六版 编程练习解答(第五章)

1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间所有整数的和。

#include <iostream>
int main()
{
	using namespace std;
	int max,min,sum;
	sum = 0;
	cout << "MAX: ";
	cin >> max;
	cout << "MIN: ";
	cin >> min;

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

	cout << "The result is: " << sum << endl;
	return 0;
}

2.使用array对象和long double重新编写程序清单5.4,并计算100!的值。

#include <iostream>
#include <array>

const int Arsize = 100;

int main()
{
	using namespace std;
	array<long double,Arsize> factorials;
	factorials[1] = factorials[0] = 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;
}

3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。

#include <iostream>
int main()
{
	using namespace std;
	int i,sum;
	sum = 0;
	cout << "Please enter the numbers: " <<endl;
	cin >> i;
	while (i != 0)
	{
		sum = sum + i;
		cin >> i;
	}
	cout << "The result is: " << sum << endl;
}

4.请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。

#include <iostream>
int main()
{
	using namespace std;
	float Cleo,Daphne;
	int year;
	Cleo = 100;
	Daphne = 100;
	year = 0;

	while (Daphne <= Cleo)
	{
		year ++;
		Cleo = 100 + 10 * year;
		Daphne = 1.05 * Daphne;
	}

	cout << "After " << year << " years, Cleo have " << Cleo << ", and Daphne have " << Daphne << endl;
	return 0;
}

5.编写程序,输入每个人月的销售量并计算总销售量。

#include <iostream>
#include <string>

int main()
{
	using namespace std;
	const string month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
	int sum,books[12];
	sum = 0;
	cout << "Please enter the number of books: " << endl;

	for (int i = 0; i < 12; i ++)
	{
		cout << month[i] << ": ";
		cin >> books[i];
		sum = sum + books[i];
	}

	cout << "The total number of books is: " << sum << endl;
	return 0;
}

6.完成练习5,但这一次使用一个二维数组来存储输入——3年中每个月的销售量。程序将报告每年销售量及三年总销售量。

#include <iostream>
#include <string>

int main()
{
	using namespace std;
	const string month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
	int books[3][12],result[3] = {0,0,0};
	int sum = 0;
	cout << "Please enter the number of books:" << endl;
	for (int j = 0; j < 3; j ++)
	{
		cout << "The " << j + 1 << " year: " << endl;
		for (int i = 0; i < 12; i ++)
		{
			cout << month[i] << ": ";
			cin >> books[j][i];
			sum = sum + books[j][i];
			result[j] = result[j] +books[j][i];
		}

	}
	for (int i = 0; i < 3; i ++)
		cout << "The total number for the " << i + 1 << " year is: " << result[i] << endl;
	cout << "All together is: " << sum << " books." << endl;
	return 0;
}

7.设计一个car结构,储存有关汽车的信息

#include <iostream>
#include <string>

using namespace std;

struct car
{
	string company;
	int year;
};

int main()
{
	int num;
	cout << "How many cars do you wish to catalog? ";
	cin >> num;
	car* test = new car[num];
	for (int i = 0; i < num; i ++)
	{
		cout << "Car #" << i + 1 << ":" << endl
			 << "Please enter the make: ";
		getline(cin,test[i].company);
		cout << "Please enter the year made: ";
		cin >> test[i].year;
	}

	cout << "Here is your collection:" << endl;
	for (int i = 0; i < num; i ++)
		cout << test[i].year << " " << test[i].company << endl;
	return 0;
}

8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,知道用户输入done为止。随后该程序指出用户输入了多少个单词(不包括done在内)。

#include <iostream>
#include <cstring>

int main()
{
	using namespace std;
	char ch[20];
	int num;
	num = 0;
	cout << "Enter words (to stop, type the word done): " << endl;
	cin >> ch;
	while (strcmp(ch,"done"))
	{
		num ++;
		cin >> ch;
	}
	cout << "You enter a total of " << num << " words." << endl;
	return 0;
}

9.编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。

#include <iostream>
#include <string>

int main()
{
	using namespace std;
	string ch;
	int num;
	num = 0;
	cout << "Enter words (to stop, type the word done): " << endl;
	cin >> ch;
	while (ch != "done")
	{
		num ++;
		cin >> ch;
	}
	cout << "You enter a total of " << num << " words." << endl;
	return 0;
}

10.嵌套循环,输入行数输出制定阵列。

#include <iostream>
int main()
{
	using namespace std;
	int row;
	cin >> row;
	for (int i = 0; i < row; i ++)
	{
		for (int j = 0; j < row - i - 1; j++)
			cout << ".";
		for (int j = 0; j <= i; j ++)
			cout << "*";
		cout << endl;
	}
	return 0;
}
发布了17 篇原创文章 · 获赞 10 · 访问量 415

猜你喜欢

转载自blog.csdn.net/acslsr/article/details/104041395