c++ primer plus第五章编程答案

//编程练习1
#include <iostream>
using namespace std;
int main()
{
    int num1,num2;
    cout << "Please input the first number: ";
    cin >> num1;
    cout << "Please input the second number: ";
    cin >> num2;
    int sum =0;
        if(num1<num2)
        {
            for (num1;num1<=num2;num1++)
            {
               sum += num1;
            }
        }
        else
        {
            for (num2;num2<=num1;num2++)
            {
               sum += num2;
            }
        }
          cout << "total is: " << sum << endl;
          
    return 0;
    }

// 编程练习2
#include <iostream>
#include <array>
const int ArSize = 101;      // example of external declaration
int main()
{
    using namespace std;
    array <long double , ArSize > factorials;
    factorials[1] = factorials[0] = 1.0;
    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;
}
// 编程练习3(while循环)
#include <iostream>
int main()
{
    using namespace std;
    int num;
    cin >> num ;
    int sum = 0;
    while(num!= 0)//有初始化入口、更新条件、终止
    {
        sum += num;
        cout << "Sum = " << sum << endl;
        cin >> num ;
    }
    return 0;
}
// 编程练习4
#include <iostream>
int main()
{
    using namespace std;
    float Daphne,Cleo;
    Daphne = Cleo = 100;
    int i=0;
    while(Cleo <= Daphne)
    {
        Daphne = Daphne + 0.1 * 100;
        Cleo = Cleo + 0.05 * Cleo ;
        i++;
    }
    cout << "When " << i << " years goes "<<"Cleo has "
        << Cleo << " dollars ," << "Daphne has "               <<  Daphne <<" dollars." <<endl;
    return 0;
}
// 编程练习5
#include <iostream>
#include <string>
const int Num = 12;
int main()
{
    using namespace std;
    string month[Num] =
    {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };
    int sole[Num];
    int sum = 0;
    for (int i=0;i<Num;i++)
    {
        cout << month[i] << " : ";
        cin >> sole[i];
        sum += sole[i];
    }
    cout << "The whole years had sale: " << sum 
    << " books.\n";
    return 0;
}
// 编程练习6
#include <iostream>
const int YEAR = 3;
const int MOUTH = 12;
int main()
{
    using namespace std;
    string month[MOUTH] =
    {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };
     int sole[YEAR][MOUTH];
    int sum[ YEAR] = {0};
    for (int i=0;i<YEAR;i++)
    {
        cout << i+1 <<" year : ";
        for(int j=0;j<MOUTH;j++)
        {
            cout << month[j] << " : ";
            cin >> sole[i][j];
            cout << "\t";
             sum[i] += sole[i][j];
        }
        cout << "the year's whole sole:" << sum[i]                  << endl;
    }
    cout << "The there years had sale: "
     << sum[0]+sum[1]+sum[2] << " books.\n";
    return 0;
}

// 编程练习7,用数组或者string。注意使用string声明变量时,要用命名空间std。
#include <iostream>
#include <string>
//const int SIZE =20;
using namespace std;
struct Car
{
    //char Name[SIZE];//std::sting
    string Name;//std::sting
    int Year;
};
int main()
{
    cout << "How many cars do you wish to catalog? ";
    int num;
    (cin >> num).get();
    Car *ps = new Car[num];
    cout << "Car#1:\n" <<"Please enter the make: ";
    getline(cin,ps->Name);
//    cin.get(ps->Name,20);
    cout << "Please enter the year made: ";
    (cin >> ps->Year).get();
     cout << "Car#2:\n" <<"Please enter the make: ";
     getline(cin,(ps+1)->Name);
    //cin.get((ps+1)->Name,20);
    cout << "Please enter the year made: ";
    cin >> (ps+1)->Year;
    cout << "Here is your collection:\n";
    cout << ps->Year << " " << ps->Name << endl;
    cout << (ps+1)->Year << " " << (ps+1)->Name << endl;
    delete [] ps;
    return 0;
}

// 编程练习8
#include <iostream>
#include <cstring>
const int SIZE = 20;
int main()
{
    using namespace std;
    cout << "Enter words (to stop , type the word done):\n";
    char word[SIZE];//每次读取一个单词,用cin
    cin >> word;
    int i = 0;
   while( strcmp(word,"done") )//strcmp()比较两个字符串,相等返回0
   {
       i++;
       cin >> word;
   }
   cout << "You enter a total of " << i << " words\n";
    return 0;
}
   
   // 编程练习9
#include <iostream>
#include <string>
//const int SIZE = 20;
int main()
{
    using namespace std;
    cout << "Enter words (to stop , type the word done):\n";
    string word;
    cin >> word;//cin同样可以用在string
    int i = 0;
   while( word != "done" )//string类可以用关系运算符。
   {
       i++;
       cin >> word;
   }
   cout << "You enter a total of " << i << " words\n";
   return 0;
}

// 编程练习10(要先思考逻辑关系,思考以什么格式输出)
#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter number of rows: ";
    int num;
    cin >> num;
    for (int i = 0; i < num ;i++)
    {
       for (int j=0;j<num-1-i;j++)
           cout << '.';
           for(int k = 0;k<=i;k++)
            cout << '*';
       cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值