码迷,mamicode.com
首页 > 编程语言 > 详细

c++ primer plus 习题答案(2)

时间:2015-05-25 00:45:27      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

p259.4

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cctype>
 4 using namespace std;
 5 struct stringy{
 6     char* str;
 7     int ct;
 8 };
 9 void set(stringy &, const char*);
10 void show(const stringy &, int times = 1);
11 void show(const char*, int times = 1);
12 
13 int main(){
14     stringy beany;
15     char testing[] = "reality isn‘t what it used to be.";
16     set(beany, testing);
17     show(beany);
18     show(beany, 2);
19     delete beany.str;
20     testing[0] = D;
21     testing[1] = u;
22     show(testing);
23     show(testing, 3);
24     show("Done!");
25     system("pause");
26     return 0;
27 }
28 
29 void set(stringy &beany, const char* testing){
30     int num = strlen(testing);
31     beany.str = new char[num + 1];
32     strcpy(beany.str, testing);
33     beany.ct = num;
34 }
35 
36 void show(const stringy &beany, int times){
37     for (int i = 0; i < times; i++)
38         cout << beany.str << endl
39             << beany.ct << endl;
40 }
41 
42 void show(const char* testing, int times){
43     for (int i = 0; i < times; i++)
44         cout << testing << endl;
45 }

p296.4

 1 头文件
 2 namespace SALES
 3 {
 4     const int QUARTERS = 4;
 5     struct Sales{
 6         double sales[QUARTERS];
 7         double average;
 8         double max;
 9         double min;
10     };
11     void setsales(Sales &s, const double ar[], int n);
12     void setsales(Sales &s);
13     void showsales(const Sales&s);
14 }
15 
16 方法
17 #include<iostream>
18 #include"sales.h"
19 using namespace std;
20 
21 void SALES::setsales(Sales &s, const double ar[], int n){
22     int num, i, sum=0;
23     if (n <= QUARTERS)
24         num = n;
25     else num = QUARTERS;
26     for (i = 0; i < num; i++){
27         s.sales[i] = ar[i];
28         sum += s.sales[i];
29     }
30     s.average = sum / num;
31     s.max = s.sales[0];
32     s.min = s.sales[0];
33     for (i = 1; i < num; i++){
34         if (s.max < s.sales[i])
35             s.max = s.sales[i];
36         if (s.min>s.sales[i])
37             s.min = s.sales[i];
38     }
39 }
40 
41 void SALES::setsales(Sales &s){
42     cout << "enter double into an array\n";
43     int i;
44     double sum = 0;
45     for (i = 0; i < QUARTERS; i++){
46         cin >> s.sales[i];
47         sum += s.sales[i];
48     }
49     s.average = sum / QUARTERS;
50     s.max = s.sales[0];
51     s.min = s.sales[0];
52     for (i = 1; i < QUARTERS; i++){
53         if (s.max < s.sales[i])
54             s.max = s.sales[i];
55         if (s.min>s.sales[i])
56             s.min = s.sales[i];
57     }
58 }
59 
60 void SALES::showsales(const Sales & s){
61     for (int i = 0; i < QUARTERS; i++)
62         cout << s.sales[i] << " ";
63     cout << "average is " << s.average << endl <<
64         "max is " << s.max << endl <<
65         "min is " << s.min << endl;
66 }
67 
68 驱动
69 #include<iostream>
70 #include<cstdlib>
71 #include<string>
72 #include<cstring>
73 #include"sales.h"
74 using namespace std;
75 
76 int main(){
77     cout << "enter a number you want to fill the array\n";
78     int n, i;
79     cin >> n;
80     double *ar = new double[n+1];
81     cout << "enter an double array\n";
82     for (i = 0; i < n; i++)
83         cin >> ar[i];
84     SALES::Sales ex1, ex2;
85     SALES::setsales(ex1, ar, n);
86     SALES::setsales(ex2);
87     SALES::showsales(ex1);
88     SALES::showsales(ex2);
89     delete[]ar;
90     system("pause");
91     return 0;
92 }

c++ primer plus 习题答案(2)

标签:

原文地址:http://www.cnblogs.com/coding-time/p/4526775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!