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;
}