c++ primer 5.9 编程练习

#include <iostream>
int main()
{
	using namespace std;
	int a, b, sum;
	cout << "Enter the first number: ";
	cin >> a;
	cout << "Enter the second number: ";
	cin >> b;
	sum = 0;
	for(int i = a, j = b; i <= j; i++)
		sum += i;
	cout << "Sum between " << a << " to " << b
		 << " is " << sum << endl;
	system("pause");
	return 0;
}
#include <iostream>
#include <array>
const int ArSize = 101;
int main()
{
	using namespace std;
	array<long double, ArSize> factorials = {1,1};
	for (int i = 2; i < ArSize; i++)
		factorials[i] = i * factorials[i-1];
	for (int i= 0; i < ArSize; i++)
		std::cout << i << "! = " << factorials[i] << std::endl;
	system("pause");
	return 0;
}

#include <iostream>
int main()
{
	using namespace std;
	int a, sum;
	sum = 0;
	do
	{
		cout << "Enter a number: ";
		cin >> a;
		sum += a;
		cout << "Total sum is " << sum << endl;
	}while(a != 0);
	system("pause");
	return 0;
}
#include <iostream>
int main()
{
	using namespace std;
	double Daphnc = 100, Clco = 100;
	int year = 0;
	do
	{
		year++;
		Daphnc = 100 + year * 10;
		Clco = Clco * 1.05;
	}while(Daphnc >= Clco);
	cout << "After " << year << " year "
		<< "Clco beyond Daphn." << endl;
	system("pause");
	return 0;
}
#include <iostream>
#include <string>
int main()
{
	using namespace std;
	string month[12] = {"January","February","March","April","May","June","July","August","September",
		"October","November","December"};
	int sold[12], sum = 0;
	for (int i = 0; i < 12; i++)
	{
		cout << "Enter the " << month[i] << " month sold:";
		cin >> sold[i];
		sum += sold[i];
	}
	cout << "The anuual sale is " << sum << endl;
	system("pause");
	return 0;
}
#include <iostream>
#include <string>
int main()
{
	using namespace std;
	string month[12] = {"January","February","March","April","May","June","July","August","September",
		"October","November","December"};
	string year[3] = {"first", "second", "third"};
	int sold[3][12], sum = 0;
	for (int j = 0; j < 3; j++)
	{
		cout << "The " << year[j] << " data:\n";
		for (int i = 0; i < 12; i++)
		{
			cout << "Enter the " << month[i] << " month sold:";
			cin >> sold[j][i];
			sum += sold[j][i];
		}
	}
	cout << "The three years sales are " << sum << endl;
	system("pause");
	return 0;
}
#include <iostream>
#include <string>
using namespace std;
struct car
{
	string manufature;
	int year;
};
int main()
{
	int number;
	cout << "How many cars do you wish to catalog? ";
	cin >> number;
	car * data = new car[number];
	for (int i = 0; i < 2;i++)
	{
		cout << "Car #" << i + 1 << ":\n";
		cout << "Please enter the make: ";
		cin.get();
		getline(cin, data[i].manufature);
		cout << "Please enter the year made:";
		cin >> data[i].year;
	}
	cout << "Here is your collection:\n";
	for (int i = 0; i < 2; i++)
	{
		cout << data[i].year << " " << data[i].manufature << endl;
	}
	system("pause");
	return 0;
#include <iostream>
#include <cstring>
int main()
{
	using namespace std;
	char words[100];
	int count = 0;
	cout << "Enter words (to stop, type the word done):\n";
	do
	{
		cin >> words;
		count++;
	}while(strcmp(words, "done"));
	cout << "Your entered a total of " << count - 1 << " words.\n";
	system("pause");
	return 0;
}
#include <iostream>
#include <string>
#include <cstring>
int main()
{
	using namespace std;
	string words;
	int count = 0;
	cout << "Enter words (to stop, type the word done):\n";
	do
	{
		cin >> words;
		count++;
	}while(words != "done");
	cout << "Your entered a total of " << count - 1 << " words.\n";
	system("pause");
	return 0;
}
#include <iostream>
int main()
{
	using namespace std;
	int number;
	cout << "Enter number of rows: ";
	cin >> number;
	for (int t = 0; t < number; t++)
	{
		for(int i =number - t - 1; i > 0; i--)
			cout <<".";
		for(int j = 0 ;j <= t;j++)
			cout << "*";
		cout << endl;
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值