c++ primer plus第五章习题答案

循环是一个简单的事,但要明白for循环的执行原理:

初始化->测试表达式?->进入函数体->更新表达式->测试表达式?……

for和while循环都是先判断条件再执行,do-while是先执行再判断条件,应该视情况不同而选择

代码如下,注释稍后再补:


第一题

#include <iostream>
using namespace std;

int main()
{
int a, b;
int sum = 0;

cout << "Enter two numbers: ";
cin >> a >> b;

for(int i = a; i <= b; i++ )
{
sum = sum + i;
}

cout << "Sum is " << sum << endl;

return 0;
}


第二题

#include <iostream>
#include <array>
using namespace std;

int main()
{
int i, j;
array<long double, 100> num ;

for(i = 0; i < 100; i++ )
{
num[i] = 1;
}

for(i = 1; i <= 100; i++ )
{
for(j = 1; j <= i; j++ )
{
num[i-1] = num[i-1] * j;
}
}

for(i = 0; i < 100; i++ )
{
cout << i+1 << "! = " << num[i] << endl;
}

return 0;
}


第三题

#include <iostream>
using namespace std;

int main()
{
int num;
int sum = 0;

while(1)
{
cout << "Enter the number: ";
cin >> num;
if(num == 0)
{
cout << "Program done!" << endl;
break;
}
else
{
sum = sum + num;
cout << "The total value you have input is " << sum << endl;
}
}

return 0;
}

第四题

#include <iostream>
using namespace std;

const double DInte = 0.1;
const double CInte = 0.05;

int main()
{
double DInv = 100;
double CInv = 100;
int year = 0;

do{
DInv = DInv + 100 * DInte;
CInv = CInv + CInv * CInte;
year ++;
}while(DInv >= CInv);

cout << "After " << year << " years, Cleo's value goes over Daphne's." << endl;
cout << "Cleo's interests is " << CInv << endl;
cout << "While Daphne's is " << DInv << endl;

return 0;
}

第五题

#include <iostream>
#include <string>
using namespace std;

const int ArSize = 20;

typedef struct
{
char month[ArSize];
int count;
}sales;

int main()
{
sales sale[12];
int Sum = 0;

for(int i = 0; i < 12; i++ )
{
switch (i)
{
case 0:
strcpy(sale[i].month, "January");
break;
case 1:
strcpy(sale[i].month, "February");
break;
case 2:
strcpy(sale[i].month, "March");
break;
case 3:
strcpy(sale[i].month, "April");
break;
case 4:
strcpy(sale[i].month, "May");
break;
case 5:
strcpy(sale[i].month, "June");
break;
case 6:
strcpy(sale[i].month, "July");
break;
case 7:
strcpy(sale[i].month, "Augest");
break;
case 8:
strcpy(sale[i].month, "September");
break;
case 9:
strcpy(sale[i].month, "Octorber");
break;
case 10:
strcpy(sale[i].month, "November");
break;
case 11:
strcpy(sale[i].month, "December");
break;
default:
cout << "Impossible!" << endl;
}
}

for(int i = 0; i < 12; i++ )
{
cout << "Enter sales volume in " << sale[i].month << ": ";
cin >> sale[i].count;

Sum = Sum + sale[i].count;
}

cout << "This year sales " << Sum << " books." << endl;

return 0;
}

第六题

#include <iostream>
using namespace std;

#define YEAR 3
#define MONTH 12

int main()
{
int volume[YEAR][MONTH];
int SumYear[YEAR] = {0}, SumAll = 0;

cout << "Enter sales volume: " << endl;

for(int i = 0; i < YEAR; i++ )
{
cout << i+1 << "th year: " << endl;
for(int j = 0; j < MONTH; j++ )
{
cout << j+1 << "th month: ";
cin >> volume[i][j];
}
}

for(int i = 0; i < YEAR; i++ )
{
for(int j = 0; j < MONTH; j++ )
{
SumYear[i] = SumYear[i] + volume[i][j];
SumAll = SumAll + volume[i][j];
}
}

for(int i = 0; i < YEAR; i++ )
{
cout << i+1 << "th year sold " << SumYear[i] << " books." << endl;
}

cout << "Sold " << SumAll << " books totally." << endl;

return 0;
}

第七题

#include <iostream>
#include <string>
using namespace std;

typedef struct
{
string company;
int MakeYear;
}car;

int main()
{
int count;

cout << "How many cars do you wish to catalog? ";
cin >> count;
cin.get();

car * pt = new car [count];

for(int i = 0; i < count; i++ )
{
cout << "Car #" << i + 1 << ":" << endl
<< "Please entr the maker: ";
getline(cin, (pt+i)->company);
cout << "Please enter the year made: ";
cin >> (pt+i)->MakeYear;
cin.get();
}

cout << "Here is your collection:" << endl;
for(int i = 0; i < count; i++ )
{
cout << pt[i].MakeYear << " " << pt[i].company << endl;
}

delete [] pt;

return 0;
}

第八题

#include <iostream>
using namespace std;
int main()
{

    char s[1000];
    int count = 0;
    cout<<"Please words(to stop, type the word done):"<<endl;
    do{
        cin>>s;
      count++;
    }while(strcmp(s,"done")!=0);
    count--;
    cout<<"You enter a total of "<< count <<" words."<<endl;
    return 0;
}

第九题

#include <iostream>
#include <string>
using namespace std;

int main()
{
string word;
int count = 0;

cout << "Enter words (to stop, type the word done): " << endl;

while(cin >> word && word != "done")
{
count ++;
}

cout << "You entered a total of " << count << " words." << endl;

return 0;
}

第十题

#include <iostream>
using namespace std;

int main()
{
int num;

cout << "Enter number of rows: ";
cin >> num;

for(int i = 0; i < num; i++ )
{
for(int j = num; j > (i+1); 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、付费专栏及课程。

余额充值