C++primerplus课后编程习题答案

C++primer plus 第六版课后编程练习答案(自己写的不对请指正)

c++primerplus课后习题编程答案

第五章



第五章


5.3

#include
using namespace std;
int main() {
int a,b=0;
cout<<“enter a number:”;
cin>>a;
if(a==0)
cout<<“sum of all in:0,program end”<<endl;
else
while(a)
{
b+=a;
cout<<“sum of all in:”<<b<<endl;
cout<<“enter a number:”;
cin>>a;
}
cout<<“sum of all in:”<<b<<" program end"<<endl;
return 0;
}

5.4

#include
using namespace std;
int main() {
int year;
const double a=0.1,b=0.05;
double cleo=100,daphne=100;
for(year=0;cleo<=daphne;year++)
{
daphne=daphne+10;
cleo=cleo*1.05;
}
cout<<“need “<<year<<” year cleo>daphne cleo:”<<cleo<<" daphne:"<<daphne<<endl;
return 0;
}

5.7

#include
#include “string.h”
using namespace std;
struct car
{
char make[20];
int year;
};
int main() {
int n;
cout<<“how many cars do you wish to catalog?”;
cin>>n;
cin.get();
car Car[n];
for(int i=0;i<n;i++)
{
cout<<“car #”<<i+1<<":"<<endl;
cout<<“please enter the make:”;
cin.getline(Car[i].make,20);
cout<<“please enter the year:”;
cin>>Car[i].year;
cin.get();
}
cout<<“here is your collection:”<<endl;
for(int i=0;i<n;i++)
{
cout<<Car[i].year<<" ,"<<Car[i].make<<endl;
}
return 0;
}

5.8

#include
#include “string.h”
using namespace std;
int main() {
char a[128];
int n=0;
cout<<“enter words(to stop,type the word done):”;
while(strcmp(a,“done”))
{
cin>>a;
n++;
}
cout<<“your entered atotal of “<<n-1<<” words”<<endl;
return 0;
}

5.10

#include
using namespace std;
int main() {
int n;
cout<<“enter nubmer of rows:”;
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=(n-1-i);j>0;j–)
{
cout<<".";
}
for(int j=0;j<i+1;j++)
cout<<"*";
cout<<endl;
};
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值