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

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

2014-02-17 19:05 381 查看
程序都是自己编的哦,如有雷同不胜荣幸,如有错误,纯属正常,拜托各位有错误或者有好办法要评论啊~共同进步~5.1#include <iostream>using namespace std;int main(){ cout<<"Please enter two integers in order: "<<endl; int little_number,big_number; int sum=0; cin>>little_number; cin>>big_number; for(little_number;little_number<=big_number;++little_number)    sum=sum+little_number; cout<<"the sum of all integers between the two is: "<<sum; return 0;}
5.2#include <iostream>#include <array>const int ArSize=100; using namespace std;int main(){ array<long double,101> factorials;  //the array has at least have 101 variable factorials[0]=factorials[1]=1; for(int i=2;i<101;++i) factorials[i]=factorials[i-1]*i; for(int i=0;i<101;++i)    cout<<i<<"! = "<<factorials[i]<<endl; return 0;
}
5.3#include<iostream>using namespace std;int main(){ cout<<"Please enter a series of numbers, "<<endl; cout<<"the programe will end when you enter 0."<<endl; double number,sum=0; cin>>number; while(number!=0) { sum=sum+number; cout<<"the former numbers' sum is: "<<sum<<endl; cin>>number; } cout<<"Done!the sum of all numbers is: "<<sum<<endl; cin.get(); return 0; }
5.4#include <iostream>using namespace std;int main(){ const int Daphne_interest_per_year=10; const double Cleo_interest_rate=1.05; double daphne=100,cleo=100; int year=0; while(daphne>=cleo)   //can be replaced by "for(year;daphne>=cleo;++year)" { ++year;       //and should be delete(if write the program with for) daphne=daphne+Daphne_interest_per_year; cleo=cleo*Cleo_interest_rate; } cout<<"After "<<year<<" years , Cleo's  investment value will be worth more than Daphne's.\n"; cout<<"Cleo's investment value is: "<<cleo<<endl; cout<<"Daphne's investment value is: "<<daphne<<endl; return 0; }

5.5#include <iostream>using namespace std;int main(){ const int Month=12; const char* month[Month] =   //can also write with string  { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int sum=0; int sale_volume[Month]; for(int i=0;i<12;++i) { cout<<"The sale volume of "<<month[i]<<" is: "; cin>>sale_volume[i]; sum=sum+sale_volume[i]; } cout<<"the total sale volume of 《C++ For Fools》 this year is: "<<sum<<" ."<<endl; return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐