C++ Primer Plus (第六版)第五章编程练习参考答案

本文提供了一系列C++编程练习题,包括求整数区间和、累加输入数字、计算存款增长等,通过这些练习帮助读者加深对C++语言的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习一:

#include<iostream>

using namespace std;

int main()
{
    int i,j;
    int sum=0;

    cout<<"Enter two integers: ";
    cin>>i>>j;

    for(int n=i; n<=j; ++n)
    {
        sum+=n;
    }

    cout<<i<<"——"<<j<<"之间所有整数之和为: "<<sum<<endl;
    return 0;
}

练习二:软件不可实现!!!

练习三:

#include<iostream>

using namespace std;

int main()
{
    int i;
    int sum=0;

    cout<<"Enter a figure:";
    cin>>i;

    while(i!=0)
    {
        sum+=i;
        cout<<"输入累计和为: "<<sum<<endl;
        cout<<"Enter a figure:";
        cin>>i;
    }

    return 0;
}

练习四:

#include<iostream>

using namespace std;

int main()
{
    int i=0;
    double interest1=0;
    double interest2=0; //利息
    double cd=100;  //当前存款

    while(interest2<=interest1)
    {
        interest1+=10;
        cd=((0.05*cd)+cd);
        interest2=cd-100;
        ++i;
    }

    cout<<"经过"<<i<<"年可以超过!!!"<<endl;


    return 0;
}

练习五:

#include<iostream>

using namespace std;

int main()
{
    int i,j;
    int sum=0;
    const int months=12;
    int month[12];
    const char * remind[12]=
    {
        "Please enter the january sales: ",
        "Please enter the February sales: ",
        "Please enter the March sales: ",
        "Please enter the April sales: ",
        "Please enter the May sales: ",
        "Please enter the June sales: ",
        "Please enter the July sales: ",
        "Please enter the August sales: ",
        "Please enter the September sales: ",
        "Please enter the October sales: ",
        "Please enter the November sales: ",
        "Please enter the December sales: "
    };

    for(i=0, j=0; j<months; ++i,++j)
    {
        cout<<remind[i];
        cin>>month[j];
        sum+=month[i];
    }

    cout<<"The year sale number is: "<<sum<<endl;

    return 0;
}

练习六:

#include<iostream>

using namespace std;

int main()
{
    int i,j,n;
    int Sum=0;  //三年总销量
    const years=3;
    const int months=12;
    int month[years][months];   //那一年那一月
    int sum[years]={0,0,0,};    //每一年的总销量,必须进行初始化
    const char * remind[12]=
    {
        "Please enter the january sales: ",
        "Please enter the February sales: ",
        "Please enter the March sales: ",
        "Please enter the April sales: ",
        "Please enter the May sales: ",
        "Please enter the June sales: ",
        "Please enter the July sales: ",
        "Please enter the August sales: ",
        "Please enter the September sales: ",
        "Please enter the October sales: ",
        "Please enter the November sales: ",
        "Please enter the December sales: "
    };

    for(i=0; i<years; ++i)  //对三年进行循环
    {
        cout<<"Please enter 第"<<i+1<<"的年销售量: "<<endl;
        for(n=0,j=0; n<months; ++n,++j) //一年十二月循环
        {
        cout<<remind[j];    //提醒那一月
        cin>>month[i][n];
        sum[i]+=month[i][n];
        }
    }

    Sum=(sum[0]+sum[1]+sum[2]);

    cout<<"The year sale number is: "<<Sum<<endl;

    return 0;
}

练习七:

#include<iostream>
#include<string>

using namespace std;

struct car
{
    string producter;
    int year;
};

int main()
{
    int i;
    cout<<"How many cars do you wish to catalog? ";
    (cin>>i).get();

    car * p=new car[i];

    for(int j=0; j<i; ++j)
    {
        cout<<"Car #"<<j+1<<endl;
        cout<<"Please enter the make: ";
        getline(cin,p[j].producter);
        //cin>>p[j].producter;
        cout<<"Please enter the year made: ";
        (cin>>p[j].year).get();
    }

    cout<<"here is your collection:"<<endl;

    for(int n=0; n<i; ++n)
    {
        cout<<p[n].year<<"   "<<p[n].producter<<endl;
    }

    delete [] p;

    return 0;
}

练习八:

include<iostream>
#include<cstring>

using namespace std;

int main()
{
    char word[20];
    int i=0;

    cout<<"Enter words (to stop, type the word done): ";
    cin>>word;

    while(strcmp(word,"done"))  //进行比较
    {
        cin>>word;
        ++i;
    }

    cout<<"You entered a total of "<<i<<" words!"<<endl;


    return 0;
}

练习九:

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string word;
    int i=0;

    cout<<"Enter words (to stop, type the word done): ";

    cin>>word;

    while(word != "done")
    {
        cin>>word;
        ++i;
    }

    cout<<"your entered a total of "<<i<<" words"<<endl;


    return 0;
}

练习十:

#include<iostream>

using namespace std;

int main()
{
    int size;

    cout<<"Enter number of rows: ";
    cin>>size;

    for(int j=0; j<size; ++j)   //打印多少行
    {
        for(int i=1; i<(size-j); ++i)//每一行应打印多少个"."
        {
            cout<<".";
        }
        for(int t=0; t<(j+1); ++t)  //每一行应打印多少个"*"
        {
            cout<<"*";
        }
        cout<<endl; //进行换行
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值