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

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值