C++ Primer Plus 第五章编程练习答案参考

话不多说直接上代码:
1.

#include<iostream>
using namespace std;

int main()
{
	int a,b, sum=0;
	cout << "Please enter two numbers:" << endl;
	cin >> a >> b;
	if(a>b)
	{
		int t = a;
		a = b;
		b = t;
	}
	for(int i=a; i<=b; i++)
	{
		sum +=i;
	}
	cout << "All integers between them add up to:" << sum << endl;
	
	return 0;
 } 
#include<iostream>
#include<array>
using namespace std;

const int ArSize = 16;
int main()
{
	array<long long, ArSize> factorials;
	factorials[0] = factorials[1] = 1LL;
	for(int i=2; i<ArSize; i++)
	{
		factorials[i] = i*factorials[i-1];
	}
	
	for(int x:factorials)//基于范围的for循环 
	{
		cout << x << endl;
	}
}
#include<iostream>
using namespace std;

int main()
{
	int n;
	int sum=0;
	cin >> n;
	while(n != 0)
	{
		sum += n;
		cout << sum << endl;
		cin >> n;
	}
	
	return 0;
}
#include<iostream>
using namespace std;

const double Daphne = 0.1;
const double Cleo = 0.05;

int main()
{
	int year = 1;
	double d_Interest = 100*Daphne;//利息 
	double c_Interest = 100*Cleo;
	double d_Amount = d_Interest + 100;
	double c_Amount = c_Interest + 100;
	while(d_Amount > c_Amount)
	{
		++year;
		d_Amount += d_Interest;
		c_Interest = c_Amount*Cleo;
		c_Amount += c_Amount*Cleo + c_Interest;
	}
	cout << "It would take " << year << 
	" years for Cleo's investment value to surpass Daphne's." << endl
	<< "Cleo's investment is: " << c_Amount << endl
	<< "Daphne's investment is: " << d_Amount << endl; 
	
	return 0;
}
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string month[] = 
	{"January","February","March","April","May","June","July","August","September","October","November","December"};
	int sales[13];
	int sum=0;
	for(int i=0; i<12; i++)
	{
		cout << "Please enter the sales for " << month[i] << endl;
		cin >> sales[i];
		sum += sales[i];
	}
	for(int i=0; i<12; i++)
	{
		cout << month[i] << " sales: " << sales[i] << endl;
	}
	cout << "Total sales this year are: " << sum;
}
#include<iostream>
#include<string> 
using namespace std;

int main()
{
	string month[] = 
	{"January","February","March","April","May","June","July","August","September","October","November","December"};
	string years[] = {"First", "Second", "Third"};
	
	int sales[3][13];
	int sum[3];
	int total=0, year=0;
	for(int j=0; j<3; j++)
	{
		for(int i=0; i<12; i++)
		{
			cout << "Please enter the sales for " 
			<< month[i] << " of the " << years[j] << "year."<< endl;
			cin >> sales[j][i];
			sum[j] += sales[j][i];
		}
		++year;
	}

	for(int i=0; i<3; i++)
	{
		cout << years[i] << " year sales :" << sum[i] << endl;
		total += sum[i];
	}
	cout << "Total sales for the three years are: " << total;
}
#include<iostream>
#include<string>
using namespace std;

struct Car
{
	string producer;
	int year;
};

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

int main()
{
	char ch[20];
	int letters=0;
	cout << "Enter words(to stop, type the word done)" << endl;
	while(cin >> ch)
	{
//		比较参数1,2(1>2:返回正数	1<2:返回负数	1==2:返回零)
		if(strcmp(ch, "done")!=0)//if(strcmp(ch, "done"))
		{
			++letters;
		}
		else
		{
			break;
		}
	}
	cout << "You entered a total of " << letters << " words.";
}
#include<iostream>
#include<string>
using namespace std;

int main()
{
	string word;
	int n_words;
	cout << "Enter words(to stop, type the word done):" << endl;
	while(cin >> word)
	{
		if(word == "done")
		{
			break;
		}
		++n_words;
	}
	cout << "You entered a total of " << n_words << " words." << endl;
	return 0;
 } 
#include<iostream>
using namespace std;

int main()
{
	int n;
	cout << "Enter number of row: " ;
	cin >> n;
	for(int i=0; i<n; i++)
	{
		for(int j=1; j<n-i; j++)
		{
			cout << "." ;
		}
		for(int k=0; k<=i; k++)
		{
			cout << "*" ;
		}
		cout << endl;
	} 
	
	return 0;
}

以上都是自己琢磨着写的,非标准答案,欢迎指正。有不懂的地方也欢迎来问噢~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值