c++primer plus第五章編程練習__編程

來源:互聯網
上載者:User

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;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.