C++ Primer Plus (第6版)习题之循环和关系表达式

习题5.1

#include<iostream>
using namespace std;

int main()
{
	int num1, num2;
	int sum = 0;
	cout << "Please enter two integers:";
	cin >> num1 >> num2;
	for (int i = num1; i <= num2; i++)
		sum += i;
	cout << "The sum of all integers between " << num1 << " and " << num2 << " is " << sum << endl;
	return 0;
}

执行结果

Please enter two integers: 1 100
The sum of all integers between 1 and 100 is 5050

习题5.2

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

const int ArSize = 16;
int main()
{
	array<long double, ArSize>factorials;
	factorials[1] = factorials[0] = 1LL;
	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;
}

执行结果

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3.26288e+06
11! = 3.99168e+07
12! = 4.79002e+08
13! = 6.22705e+09
14! = 8.71783e+10
15! = 1.30767e+12

习题5.3

#include<iostream>
using namespace std;

int main()
{
	int num;
	int sum = 0;
	cin >> num;
	while (num != 0)
	{
		sum += num;
		cout << "sum = " << sum <<endl;
		cout << "Continue to input an integer:";
		cin >> num;
	}
	cout << "Input 0 or error input!\n";
}

执行结果

10
sum = 10
Continue to input an integer:50
sum = 60
Continue to input an integer:70
sum = 130
Continue to input an integer:-20
sum = 110
Continue to input an integer:0
Input 0 or error input!

习题5.4


#include<iostream>
using namespace std;
const float Danli = 0.1f;
const float Fuli = 0.05f;
const float Amount = 100.0f;
const float EPSINON = 0.001f;        //涉及到浮点值的比较,计算机只保证精度所以自己定义一个范围
int main()							//借此来判断	
{
	float Daphne = Amount, Cleo = Amount;
	float compare;
	int years = 0;
	do
	{
		Daphne += Amount * Danli;
		Cleo = Cleo * Fuli + Cleo;
		years++;
		compare = Cleo - Daphne;              //计算差值
	} while (compare < EPSINON);                //比较范围就行了,如果直接比较,程序会崩溃
	cout << "Cleocount :" << Cleo << endl;
	cout << "Daphnecount: " << Daphne << endl;
	cout << "After " << years << " years,Cleo's count will past over Daphne.\n";
	return 0;
}

习题5.5

#include<iostream>
#include<string>

using namespace std;
const int Month_Num = 12;
const string Month[] = { "January","February","March","April","May","June","July","August","September","October","November","December" };

int main()
{
	int Book_Sale[Month_Num];
	int Sale_Sum = 0;
	for (int i = 0; i < Month_Num; i++)
	{
		cout << "Input in " << Month[i] << " <<C++ For Fools>>salemount:";
		cin >> Book_Sale[i];
		Sale_Sum += Book_Sale[i];	
	}
	cout << "This year,<<C++ For Fools>>sellmount is:" << Sale_Sum << endl;
	return 0;
}

习题5.6

#include<iostream>
#include<string>

using namespace std;
const int Month_Num = 12;
const int Years = 3;
const string Month[] = { "January","February","March","April","May","June","July","August","September","October","November","December" };

int main()
{
	int Book_Sale[Years][Month_Num];
	int Year_Sum[Years] = { 0 }, Sale_Sum = 0;
	for (int j = 0; j < Years; j++)
	{
		for (int i = 0; i < Month_Num; i++)
		{
			cout << "Enter " << Month[i] <<" of the year "<< j+1 << " <<C++ For Fools>> salemount:";
			cin >> Book_Sale[j][i];
			Year_Sum[j] += Book_Sale[j][i];
		}
		cout << "The sales in year " << j+1 << " : " << Year_Sum[j]<< endl;
		Sale_Sum += Year_Sum[j];
	
	}

	cout << "This "<< Years<<" years,<<C++ For Fools>> salemount is:" << Sale_Sum << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值