C++ Primer Plus第五章课后编程答案

1.

#include <iostream>
#include <string>
#include <cstring>
#include <array>

int main()
{
	using namespace std;
	int l, m, sum=0;
	cout << "small: ";
	cin >> l;
	cout << "big: ";
	cin >> m;
	for (; l <= m; l++)
	{
		sum += l;
	}
	cout << "sum: " << sum;
	cin.get();
	cin.get();
	return 0;
}

2.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 101;
int main()
{
	using namespace std;
	array<long double, ArSize> arr;
	arr[1] = arr[0] = 1LL;
	for (int i=2; i < ArSize; i++)
		arr[i] = i*arr[i - 1];
	for (int i = 0; i < ArSize; i++)
		cout << i << "! = " << arr[i] << endl;
	cin.get();
	return 0;
}

3.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 101;
int main()
{
	using namespace std;
	int n, sum=0;
	cout << "Enter a number: ";
	do
	{
		cin >> n;
		sum += n;
		cout << "Sum: " << sum << endl;
		cout << "Enter a number: ";
	} while (n != 0);
	cin.get();
	return 0;
}

4.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 101;
int main()
{
	using namespace std;
	double Daphne = 100, Cleo = 100;
	int i;
	for (i = 0; Cleo <= Daphne; i++)
	{
		Cleo += Cleo*0.05;
		Daphne += 100 * 0.1;
	}
	cout << "Daphne: " << Daphne << endl;
	cout << "Cleo: " << Cleo << endl;
	cout << "year: " << i << endl;
	cin.get();
	return 0;
}

5.
#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 12;
int main()
{
	using namespace std;
	const char * month[ArSize] =
	{
		"January: ",
		"February: ",
		"March: ",
		"Apirl: ",
		"May: ",
		"June: ",
		"July: ",
		"August: ",
		"September: ",
		"October: ",
		"November: ",
		"December: "
	};
	int sell[ArSize],sum=0;
	int i;
	for (i = 0; i<ArSize ; i++)
	{
		cout << month[i];
		cin >> sell[i];
		sum += sell[i];
	}
	cout << "Sum: " << sum << endl;
	cin.get();
	cin.get();
	return 0;
}

6.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 12;
const int Years = 3;
const int Months = 12;
int main()
{
	using namespace std;
	int sell[Years][Months]=
	{
		{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
		{ 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
		{ 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
	};
	const char * month[ArSize] =
	{
		"January: ",
		"February: ",
		"March: ",
		"Apirl: ",
		"May: ",
		"June: ",
		"July: ",
		"August: ",
		"September: ",
		"October: ",
		"November: ",
		"December: "
	};
	int sum=0;
	int i;
	for (i = 0; i<Months ; i++)
	{
		cout << month[i];
		for (int j = 0; j < Years; j++)
		{
			cout << sell[j][i] << "\t";
			sum += sell[j][i];
		}
		cout << endl;		
	}
	cout << "Sum: " << sum << endl;
	cin.get();
	cin.get();
	return 0;
}

7.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 12;
struct car
{
	std::string make;
	unsigned int year;
};
int main()
{
	using namespace std;
	int sum=0;
	int i,n;
	cout << "How many cars do you wish to catalog? ";
	cin >> n;
	cin.get();
	car* pz = new car[n];
	for (int j = 0; j < n; j++)
	{
		cout << "Car #" << j << ":\nPlease enter the make: ";
		getline(cin,pz[j].make);
		cout << "Please enter the year made: ";
		cin >> pz[j].year;
		cin.get();
	}
	cout << "Here is your collection:\n";
	cout << pz[0].year << " " << pz[0].make << endl;
	cout << pz[1].year << " " << pz[1].make << endl;
	cin.get();
	cin.get();
	return 0;
}

8.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 20;

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

9.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 20;

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

10.

#include <iostream>
#include <string>
#include <cstring>
#include <array>
const int ArSize = 20;

int main()
{
	using namespace std;
	int n;
	cout << "Enter rows :"<< endl;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n-i-1; j++)
		{
			cout << ".";
		}
		for (int j=n-i-1; j < n; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
	cin.get();
	cin.get();
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值