c++primer plus第五章编程练习

来源:互联网 发布:pyqt设计软件 编辑:程序博客网 时间:2024/05/01 11:23

1.编写一个要求用户输入两个整数的程序,giant程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和。这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.

#include <iostream>using namespace std;int main(){    int x,y;    cin>>x;    cin>>y;    int sum=0;    for(int i=x;x<=y;x++)    {        sum=sum+x;    }    cout<<sum<<endl;    return 0;}

3.编写一个要求用户输入数字的程序,每次输入后,程序都将报告到目前为止,所有输入的累积和,当用户输入0时,程序结束。

#include <iostream>int main(){    using namespace std;    int sum=0;    int num;    cout<<"Enter the number ;enter 0to quit:\n ";    cin>>num;    while(num!=0)    {        sum +=num;        cout<<sum;        cin>>num;    }    cout<<endl;    cout<<sum<<endl;    return 0;}
4.Daphne以10%的单利投资了100美元,每一年利润都是投资额的10%,即每年10美元。而Cleo以5%的复利投资了100美元,利息是当前存款(包括获得的利息)的5%,Cleo在第一年投资100美元的盈利是5%得到了105美元,下一年的盈利是105美元的5%,以此类推编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示两个人的投资价值。

#include <iostream>int main(){    using namespace std;    double daphne=100;    double cleo=100;    int count=0;    while(cleo<=daphne)    {        count++;        daphne +=10;        cleo *=1.05;    }    cout<<count<<endl;    cout<<"Daphne: "<<daphne<<endl;    cout<<"Cleo: "<<cleo<<endl;    return 0;}

5.假设要销《c++for fool》一书,请编写一个程序,输入全年中每个月的销售量,图书数量而不是销售额

,程序通过循环,使用初始化为月份字符串的char*数组,或string对象数组,逐月进行体术并肩输入的数据出、、UN除杂䘝int数组中,然后程序计算数组中各元素的总数,并报告这一年的销售情况。

#include <iostream>int main(){    using namespace std;    int sales[12];    string month[12]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "};    int sum=0;    for(int i=0;i<12;i++)    {        cout<<"Please enter the sales of"<<month[i]<<"is: ";        cin>>sales[i];        sum +=sales[i];    }    for(int i=0;i<+12;i++)    {        cout<<"The sales of  "<<month[i]<<"is: "<<sales[i]<<endl;    }    cout<<"The sales of a year is "<<sum<<endl;    return 0;}
6.完成编程练习5,但这一次使用一个二维数组来存储输入---3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。

#include <iostream>#include<string>int main(){    using namespace std;    int sales[3][12];    string month[12]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "};    int sum[3]={0,0,0};    for(int i=0;i<3;i++)    {        for(int j=0;j<12;j++)        {            cout<<"Please enter the sales of "<<month[j]<<" in "<<(i+1)<<" year is: ";            cin>>sales[i][j];            sum[i] +=sales[i][j];        }    }    for(int i=0;i<=2;i++)    {        cout<<"The sales of  "<<(i+1)<<" year is: "<<sum[i]<<endl;    }    cout<<"The sales of three year is "<<(sum[0]+sum[1]+sum[2])<<endl;    return 0;}
7.设计一个名为car的结构,用它存储下述有关汽车的信息: 生产商(存储在字符数组或string对象中的字符串)、  生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。  接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取  数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。

#include <iostream>#include<string>struct car{    char producer[20];//生产商    int pro_year;//生产年份};int main(){    using namespace std;    cout<<"How many cars do you wish to catalog?";    int count;    (cin>>count).get();    car *ca=new car[count];    for(int i=1;i<=count;i++)    {        cout<<"car #"<<i<<":"<<endl;        cout<<"Please enter the make :";        cin.getline(ca[i].producer,20);//        cin.getline(ca[i].producer,20);        cout<<"Please enter the year made: ";        (cin>>ca[i].pro_year).get();    }    cout<<"Here is your collection:"<<endl;    for(int i=1;i<=count;i++)    {        cout<<ca[i].pro_year<<" "<<ca[i].producer<<endl;    }    delete []ca;    return 0;}
8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入  了多少个单词(不包括done在内)。下面是该程序的运行情况:  Enter words (to stop, type the word done):  anteater birthday category dumpster  envy finagle geometry done for sure  You entered a toal of 7 words.  您应该在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试
#include <iostream>#include<cstring>#int main(){    using namespace std;    int count=0;    char word[20];    cout<<"Enter worsds(to stop,type the word done):";    cin>>word;    while(strcmp(word,"done"))    {        count++;        cin>>word;    }    cout<<"You entered a total of "<<count<<"words"<<endl;    return 0;}
9.编写一个满足前一个练习中描述的程序,但是用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。

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



10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行,然后程序将显是相应的函数的星号,其中第一行包括一个星号,第二行包括两个星号,以此类推,每一行包含的字符数等于用户指定的函数,在星号不够的情况下,在星号前面加上句点。

Enter number of rows: 5 

 ....*

  ...**

  ..***  

.****  

*****

#include <iostream>#include<string>int main(){    using namespace std;    int count;    cout<<"Enter number of row: ";    cin>>count;    for(int i=1;i<=count;i++)    {        for(int j=1;j<= (count-i);j++)        {            cout<<" . ";        }        for(int j=1;j<=i;j++)            cout<<" * ";        cout<<endl;    }    return 0;}




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三星s4相机故障怎么办 mate10屏幕碎了怎么办 mate9屏幕碎了怎么办 一体机屏幕碎了怎么办 华为p20后屏碎了怎么办 华为手机屏摔碎了怎么办 红米手机屏幕失灵怎么办 电池胶拉断了怎么办 屏保密码忘记了怎么办 手机卡注销了钱怎么办 信用卡号码换了怎么办 网上选牌照失效怎么办 银行卡身份证过期了怎么办 高铁忘带身份证怎么办 动车临时身份证怎么办 身份证丢了怎么办登机 儿童身份证丢了怎么办 临时身份证贷不了怎么办 16岁以下怎么办银行卡 身份证钱包丢了怎么办 社保卡同步自己怎么办 扬州市民(副卡)怎么办 北京医保存折怎么办卡 洛阳新医保卡怎么办 身份证丢了怎么办社保 南京社保卡个人怎么办 文登急用社保卡怎么办 临时医保卡丢失怎么办 外地儿童怎么办社保卡 给孩子怎么办社保卡 铁路社保卡丢失怎么办 医保社保卡遗失怎么办 更换医保卡需要怎么办 卡号记不住丢了怎么办 铁路医保卡丢失怎么办? 异地工作调动公积金怎么办 儿童医保卡丢失怎么办 单位辞职后医保怎么办 换省工作社保怎么办 档案回原籍社保怎么办 企业退休后医保怎么办