【C++】C++ primer plus 编程练习—第5章

//5-1

#include<iostream>
using namespace std;

int main()
{
	int a, b;
	int sum = 0;
	cout << "Please enter two integers: ";
	cin >> a >> b;
	for (int i = a;i <= b; i++)
		sum += i;
	cout << "The sum of " << a << " and " << b << " is " << sum << endl;

	return 0;
}

//5-2

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

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

	return 0;
}

//5-3

#include<iostream>
using namespace std;

int main()
{
	int num;
	int sum = 0;
	cout << "please enter an number: ";
	cin >> num;
	while (num != 0)
	{
		sum += num;
		cout << "the sum of all numbers is " << sum << endl;
		cout << "please enter again: ";
		cin >> num;
	}
	cout << "bye";
	return 0;
}

//5-4

#include <iostream>
const double R1=0.1;
const double R2=0.05;
const double MONEY=100.0;
using namespace std;

int main()
{
   double money1,money2;
   int count=0;
    money1=money2=MONEY;
   while(money2<=money1)
   {
       money1+=MONEY*R1;
       money2=money2*(1+R2);
       count++;
       cout<<"IN year#"<<count<<" money1: "<<money1<<" and money2: "<<money2<<endl;
   }
   cout<<"\nafter "<<count<<" years "<<"cleo's money > daphne's money "<<endl;
   cout<<"cleo: "<<money2<<" and daphne: "<<money1<<endl;
   
    return 0;
}

//5-5

#include <iostream>
using namespace std;

int main()
{
   char * month[12]={
       "January","February","March",
       "April","May","June",
       "July","August","September",
       "October","November","December"};
    int sale[12];
    int sum=0;
    cout<<"enter the sales of 12 months"<<endl;
   for(int i=0;i<12;i++)
   {
       cout<<month[i]<<":";
       cin>>sale[i];
       sum+=sale[i];
   }
   cout<<"the sales of all year is "<<sum<<endl;
   return 0;
}

//5-6

#include <iostream>
using namespace std;
const int YEAR=3;
const int MONTH=12;

int main()
{
    int sale[YEAR][MONTH]={0};
    int sum[YEAR]={0,0,0};
    cout<<"Please enter sales of three years."<<endl;
   for(int i=0;i<YEAR;i++)
   {
       cout<<"year#"<<i+1<<": ";
      for(int j=0;j<MONTH;j++)
      {
          cin>>sale[i][j];
          sum[i]+=sale[i][j];
      }
      cout<<"the sales of year#"<<i+1<<" is "<<sum[i]<<endl;
   }
   cout<<"the sales of all year is "<<sum[0]+sum[1]+sum[2]<<endl;
   return 0;
}

//5-7

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

struct car
{
    string make;
    int year;
};

int main()
{
    int num;
    cout << "How many cars do you wish to catalog? ";
    cin >> num;
    cin.get();
    car* pt = new car[num];
    for (int i = 0; i < num; i++)
    {
        cout << "Car #" << i + 1 << ":" << endl;
        cout << "please enter the make: ";
        getline(cin, pt[i].make);
        cout << "please enter the year made: ";
        cin >> pt[i].year;
        cin.get();
    }
    cout << "Here is your collection:" << endl;
    for (int i = 0; i < num; i++)
    {
        cout << pt[i].year << " " << pt[i].make<<endl;
    }

    return 0;
}

//5-8

#include <iostream>
#include<cstring>
const int Wsize = 80;
using namespace std;

int main()
{
    char word[Wsize];
    int count = 0;
    cout << "Enter words (to stop,tpye the word done)" << endl;
    cin >> word;
    while (strcmp(word, "done"))
    {
        count++;
        cin >> word;
    }
    cout << "You entered a total of " << count << " word." <<endl;

    return 0;
}

//5-9

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

int main()
{
    string word;
    int count = 0;
    cout << "Enter words (to stop,tpye the word done)" << endl;
    cin >> word;
    while (word!="done")
    {
        count++;
        cin >> word;
    }
    cout << "You entered a total of " << count << " word." <<endl;

    return 0;
}

//5-10

#include <iostream>
using namespace std;

int main()
{
    int row;
    cout << "Enter number of rows: ";
    cin >> row;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j <( row-(i+1)); j++)
            cout << ". ";
        for (int k = 0; k < (i + 1); k++)
            cout << "* ";
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值