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

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

5.1

//programming exercise 5.1
#include 

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

你可能感兴趣的:(c++编程练习)