C++ primer Plus(第六版)_课后编程练习_第五章

练习代码为本人所写,欢迎大家学习交流

若有疑问请留言,我将尽快回复!!!!

题5.1:

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	int numb1, numb2, sum=0, temp;
	cout << "please enter two number: ";
	cin >> numb1;
	cin >> numb2;

	if (numb1 > numb2)  //使得先输入数字更大也能实现
	{
		temp = numb1;
		numb1 = numb2;
		numb2 = temp;
	}

	for (int i = numb1; i <= numb2; i++)
	{
		sum += i;
	}

	cout << "sum = " << sum;

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.2:

#include <iostream>
#include <ctime>
#include <array>
using namespace std;

int main()
{
	array<long double, 101> q;
	q[0] = q[1] = 1;
	for (int i = 2; i <= 100; i++)
	{
		q[i] =i * q[i-1];
		cout << i << "! = " << q[i] << endl;
	}

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.3:

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	long long numb, sum = 0;
	cout << "please input one number at a time, and it will show the sum of the numbers you have inputed";
	cout << endl << "Enter 0 to exit" << endl;
	cin >> numb;
	while (numb)
	{
		sum += numb;
		cout << "sum = " << sum <<endl;
		cin >> numb;
	}

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.4:

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	double profit_Daphne, profit_Cleo = 100;
	int i=0;
	do
	{
		i++;
		profit_Daphne = 10*i +100;
		profit_Cleo = profit_Cleo * 0.05 + profit_Cleo;
	} while (profit_Daphne > profit_Cleo);

	cout << "After " << i << " years, the profits of Cleo are more than Daphne's" << endl;
	cout << "the profits of Cleo: " << profit_Cleo << endl;
	cout << "the profits of Daphne: " << profit_Daphne;

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.5:

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

int main()
{
	int sell[12];
	string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", 
						"Septempber", "October", "November", "December" };
	int total_sell = 0;

	for (int i = 0; i < 12; i++)
	{
		cout << "enter the sell of " << month[i] << ": ";
		cin >> sell[i];
		total_sell += sell[i];
	}

	cout << "the total sell in this year: " << total_sell;

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.6:

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

int main()
{
	int sell[3][12];
	string month[12] = {"January", "February", "March", "April", "May", "June", "July", "August", 
						"Septempber", "October", "November", "December" };
	int total_sell_oneyear[3] = {0};
	int total_sell_allyears = 0;

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 12; j++)
		{
		cout << "enter the sell of " << month[j] << " in the " << i+1 << "th year: ";
		cin >> sell[i][j];
		total_sell_oneyear[i] += sell[i][j];
		}
		cout << "the total sell in the " << i+1 << "th year: " << total_sell_oneyear[i] << endl;
		total_sell_allyears += total_sell_oneyear[i];
	}

	cout << "the total sell of three years: " << total_sell_allyears;

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.7:

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

struct car
	{
		string manufacturer;
		int year;
	};

int main()
{
	int numb;
	cout << "How many cars do you wish to catalog? ";
	cin >> numb;

	car * ps = new car [numb];

	for (int i = 0; i < numb; i++)
	{
		cin.get();    //数字和字符串一起,一定要注意把回车给吃掉
		cout << "CAR #" << i+1 << endl << "please enter the make: ";
		getline(cin, ps[i].manufacturer);
		cout << "please enter the year made: ";
		cin >> ps[i].year;
	}

	cout << "Here is your collection" << endl;
	
	for (int i = 0; i < numb; i++)
	{
		cout << ps[i].year << "   " << ps[i].manufacturer << endl;
	}

	delete [] ps;    //记住是数组的指针删除要用[],而不是delete ps

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.8:

#include <iostream>
#include <ctime>
#include <cstring>
using namespace std;

int main()
{
	char word[20];   //输入必须小于20个字符
	int numb=-1;

	do 
	{
		cin >> word;
		numb++;
	} while(strcmp("done", word));

	cout << "there have " << numb << " words before \"done\"";

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.9:

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

int main()
{
	string word;   //随意多少字符数
	int numb=-1;

	do 
	{
		cin >> word;
		numb++;
	} while(word != "done");

	cout << "there have " << numb << " words before \"done\"";

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

题5.10:

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	int numb_Rows;

	cout << "Enter the number of rows:";
	cin >> numb_Rows;

	for (int i = 1; i <= numb_Rows; i++)
	{
		for (int j = 1; j <= (numb_Rows - i); j++)
			cout << ".";
		for (int j = 1; j <=  i; j++)
			cout << "*";
		cout << endl;
	}

	//以下是时延和回车读取,防止看不到结果
	clock_t delay = 10 * CLOCKS_PER_SEC;   
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cin.get();  
	cin.get();  
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值