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

程序都是自己编的哦,如有雷同不胜荣幸,如有错误,纯属正常,拜托各位有错误或者有好办法要评论啊~共同进步~
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值