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;}
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;}
相关文章推荐
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 1-5
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 6-10
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第六章(chapter 6) 1-4
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第六章(chapter 6) 5-9
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第八章(chapter 8) 1-4
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 1-5
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第九章(chapter 9) 1-4
- C++ Primer Plus第五版 第五章 编程练习答案
- C++ Primer Plus 第六版_编程练习(3)(Chapter_four 1-10)
- C++ Primer Plus第六版 第五章 编程练习答案
- C++ Primer Plus 第六版_编程练习(5)(Chapter_five 6-10)
- c++ primer plus(第6版)中文版 第五章编程练习答案
- C++ Primer Plus 课后编程练习——第五章循环和关系表达式
- C++primer plus第六版课后编程练习答案4.9
- C++primer plus第六版课后编程练习答案5.8
- C++primer plus第六版课后编程练习答案6.6
- C++primer plus第六版课后编程练习答案7.8
- C++primer plus第六版课后编程练习答案8.1
- C++ Primer Plus第六版 第七章 编程练习答案
- c++ primer plus(第6版)中文版 第六章编程练习答案