c++ primer plus第五章programming exercises答案

c++ primer plus第五章programming exercises答案

5.1

//programming exercise 5.1
#include <iostream>

int main()
{
	using namespace std;
	cout << "Enter two integers~" << endl;
	cout << "First enter the smaller one: ";
	int i;
	cin >> i;
	cout << "Then enter the bigger one: ";
	int j;
	cin >> j;
	int sum = 0;
	for (int a = i; a <= j; ++a)
		sum += a;
	cout << "the sum between " << i << " and " << j << " is " << sum << endl;

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

5.2

//programming exercise 5.2
#include <iostream>
#include<array>
const int ArSize = 101;
int main()
{
	using namespace std;
	//cout.setf(ios_base::fixed, ios_base::floatfield);//fixed-point
	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;

	cin.get();
	return 0;
}

5.3

//programming exercise 5.3
#include <iostream>
int main()
{
	using namespace std;
	int num;
	long sum = 0;
	cout << "Enter a integer number each time (enter 0 to quit): " << endl;
	while ((cin >> num),num)//逗号表达式
	{
		sum += num;
		cout << "Until now, the sum is " << sum << endl;
	}
	cout << "Finally, the sum of the number you inputed is " << sum << endl;

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

5.4

//programming exercise 5.4
#include <iostream>
int main()
{
	using namespace std;
	double Daphne_account = 100;
	double Cleo_account = 100;
	int years = 0;
	while (Daphne_account >= Cleo_account)
	{
		++years;
		Daphne_account += 10;
		Cleo_account = Cleo_account + Cleo_account * 0.05;
	}
	cout << years << " years later, the value of Cleo's investment to exceed"
	    <<" the value of Daphne's."
		<< endl << "Cleo's investment is $" << Daphne_account << endl
		<< "and Daphne.s investment is $" << Cleo_account << endl;
	cin.get();
	return 0;
}

5.5

//programming exercise 5.5
#include <iostream>
#include <string>
int main()
{
	using namespace std;
	string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" };
	int sales[12];
	int total_sales = 0; 
	cout << "Enter the sales of book <C++ for Fools> each month: " << endl;
	for (int i = 0; i < 12; ++i)
	{
		cout << month[i] << ": ";
		cin >> sales[i];
		total_sales += sales[i];
	}

	cout << "The total sales for the year is " << total_sales << endl;
	for (int i = 0; i < 12; ++i)
	{
		cout << month[i] << ": " << sales[i] << endl;
	}

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

5.6

//programming exercise 5.6
#include <iostream>
#include <string>
int main()
{
	using namespace std;
	string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "Septempber", "October", "November", "December" };
	int sales[3][12];
	int total_sales[3] = { 0 };
	for (int j = 0; j < 3; ++j)
	{
		cout << "Enter " << j + 1 << " year(s) sales of book <C++ for Fools> each month : " << endl;
		for (int i = 0; i < 12; ++i)
		{
			cout << month[i] << ": ";
			cin >> sales[j][i];
			total_sales[j] += sales[j][i];
		}
	}
	
	cout << "The total sales for the year is " << total_sales[0] + total_sales[1] + total_sales[2] << endl;
	for (int j = 0; j < 3; ++j)
	{
		cout << j + 1 << " year(s) total sales is " << total_sales[j] << endl;
	}
	cin.get();
	cin.get();
	return 0;
}

5.7

//programming exercise 5.7
#include <iostream>
using namespace std;
struct car
{
	char company[30];
	int years;
};
int main()
{
	cout << "How many cars do you wish to catalog?";
	int num;
	cin >> num;
	cin.get();
	car * pcar = new car[num];
	for (int i = 0; i < num; ++i)
	{
		cout << "Car #" << i+1 << ":" << endl
			<< "Please enter the make: ";
		cin.get(pcar[i].company,29).get();
		cout << "Please enter the year made: ";
		cin >> pcar[i].years;
		cin.get();

	}
	cout << "Here is your collection:" << endl;
	for (int i = 0; i < num; ++i)
	{
		cout << pcar[i].years << " " << pcar[i].company << endl;
	}
	cin.get();
	cin.get();
	return 0;
}

5.8

//programming exercise 5.8
#include <iostream>
#include <cstring>
#include <cstdlib>
int main()
{
	using namespace std;
	cout << "Enter words (to stop, type the word done): " << endl;
	char inputchar[128];
	unsigned int num_word = 0;
	for (cin >> inputchar; strcmp(inputchar, "done") != 0; )
	{
		num_word++;
		cin >> inputchar;
	}
	cout << "You entered a total of " << num_word << " words." << endl;
	system("pause");//windows的cmd命令暂停,被包含在c库stdlib.h中
	return 0;
}

5.9

//programming exercise 5.9
#include <iostream>
#include <string>
#include <cstdlib>
int main()
{
	using namespace std;
	cout << "Enter words (to stop, type the word done): " << endl;
	string inputchar;
	unsigned int num_word = 0;
	for (cin >> inputchar; inputchar !=  "done"; )
	{
		num_word++;
		cin >> inputchar;
	}
	cout << "You entered a total of " << num_word << " words." << endl;
	system("pause");
	return 0;
}

5.10

//programming exercise 5.10
#include <iostream>
int main()
{
	using namespace std;
	cout << "Enter number of rows: ";
	int num;
	cin >> num;
	for (int i = 1; i <= num; ++i)
	{
		for (int j = 1; j <= num - i; ++j)
		{
			cout << ".";
		}
		for (int j = num - i + 1; j <= num; ++j)
		{
			cout << "*";
		}
		cout << endl;
	}
	cin.get();
	cin.get();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值