您的位置:首页 > 编程语言 > C语言/C++

C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 6-10

2014-02-17 19:05 651 查看
5.6#include <iostream>#include <string>using namespace std;int main(){ const int Month=12; const int Year=3; const string month[Month] =   //can also write with char*  { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; const char* year[Year]  //can be delete and don't forget to do some necessary change  { "first", "second", "third" }; long sum[Year]={0},total_sum=0; int sale_volume[Year][Month]; for(int j=0;j<3;++j) {    for(int i=0;i<12;++i)    {     cout<<"The "<<year[j]<<" year's sale volume of "<<month[i]<<" is: ";    cin>>sale_volume[j][i];    sum[j]=sum[j]+sale_volume[j][i];    };    cout<<"the "<<year[j]<<" year's sale volume of 《C++ For Fools》 this year is: "<<sum[j]<<" ."<<endl;    total_sum=total_sum+sum[j]; }     cout<<endl<<endl    <<"the total sale volume of 《C++ For Fools》 this year is: "<<total_sum<<" ."<<endl; cin.get(); return 0;
5.7#include <iostream> #include <string>using namespace std;int main(){ struct Car { string maker;  //char maker[20] int year; }; int number,i=0; cout<<"How many cars do you wish to catalog?"<<endl; cin>>number; cin.get(); Car* car_catalog=new Car[number]; for(i;i<number;++i) { cout<<"Car #"<<i+1<<" :\n"; cout<<"Please enter the maker: "; getline(cin,car_catalog[i].maker);   //and repalaced the line by cin.getline(car_catalog[i].maker,20); cout<<"Please enter the year made: "; cin>>car_catalog[i].year; cin.get(); }; cout<<endl<<"Here is your collection: "<<endl; for(i=0;i<number;++i) cout<<car_catalog[i].year<<" "<<car_catalog[i].maker<<endl; return 0; }
5.8#include <iostream>#include <cstring>using namespace std;int main(){ char word[20]; cout<<"Enter words (to stop,type the word done):"<<endl; cin>>word; int i=0; for(i;strcmp(word,"done");++i)        cin>>word;            cout<<"You entered a total of "<<i<<" words.";    return 0; }
5.9#include <iostream>#include <string>using namespace std;int main(){ string word; cout<<"Enter words (to stop,type the word done):"<<endl; cin>>word; int i=0; for(i;word!="done";++i)        cin>>word;            cout<<"You entered a total of "<<i<<" words.";    return 0; }
5.10.1用for 写成#include <iostream>using namespace std;int main(){ const char Fill='.'; const char Print='*'; int row; cout<<"Enter the number of rows: "; cin>>row; for(int i=0;i<row;++i)  //count the row { for(int j=0;j<row-i-1;++j)  //print the " . "    cout<<Fill; for(int k=0;k<i+1;++k)   //print "*"        cout<<Print; cout<<endl;    } cin.get(); return 0;}
5.10.2
#include <iostream>using namespace std;int main(){ const char Fill='.'; const char Print='*'; int row; cout<<"Enter the number of rows: "; cin>>row; int i=0; while(i<row)  //count the row { ++i;    int j=0,k=0;   //should be clear when printed the "." and "*" while(j<row-i)  //print the " . " { ++j;    cout<<Fill;    }; while(k<i) //print "*" { ++k; cout<<Print; }; cout<<endl;    } cin.get(); return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐