[C++ Primer Plus] 第9章、内存模型和名称空间——(二)课后习题

          

 

头文件 golf.h

 1 //golf.h   --for pe9-1.cpp
 2 
 3 const int Len = 40;
 4 struct golf
 5 {
 6     char fullname[Len];
 7     int handicap;
 8 };
 9 
10 void setgolf(golf &g, const char *name, int hc);
11 
12 int setgolf(golf &g);
13 
14 void handicap(golf &g, int hc);
15 
16 void showgolf(const golf &g);

 golf.cpp

 1 #include<iostream>
 2 #include"golf.h"
 3 using namespace std;
 4 void setgolf(golf &g, const char *name, int hc)
 5 {
 6     strcpy_s(g.fullname, name);           //注意:char型数组要用strcpy()
 7     g.handicap = hc;
 8 }
 9 
10 int setgolf(golf &g)
11 {
12     cout << "Enter the fullname: ";
13     cin.get(g.fullname,Len);
14     if (g.fullname[0] == '\0')            //若姓名是空字符串则返回0
15         return 0;
16     cin.get();                            //跳过换行符
17     cout << "Enter the handicap: ";
18     cin >> g.handicap;
19     cin.get();                            //跳过换行符
20     return 1;
21 }
22 
23 void handicap(golf &g, int hc)
24 {
25     g.handicap = hc;
26 }
27 
28 void showgolf(const golf &g)
29 {
30     cout << "Golfer: " << g.fullname << endl;
31     cout << "Handicap: " << g.handicap << endl;
32 }

main.cpp

 1 #include <iostream>
 2 #include"golf.h"
 3 using namespace std;
 4 
 5 const int cnt = 5;
 6 
 7 int main()
 8 {
 9     golf ann;
10     setgolf(ann, "Ann Birdfree", 24);
11     showgolf(ann);
12     cout << endl;
13 
14     golf andy[cnt];
15     cout << "输入" << cnt << "名球队成员信息:" << endl;
16     int i;
17     for(i=0;i<cnt;i++)
18     {
19         if (setgolf(andy[i]) == 0)
20             break;
21     }
22     cout << endl;
23     for (int j = 0; j < i; j++)
24     {
25         showgolf(andy[j]);
26     }
27     cout << endl;
28 
29     handicap(ann, 88);
30     showgolf(ann);
31 
32     system("pause");
33     return 0;
34 }

                

 2、修改程序清单9.9:用string对象代替字符数组.这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串""进行比较,比判断是否为空行

 修改前

 1 #include<iostream>
 2 using namespace std;
 3 
 4 const int Size = 10;
 5 void strcount(const char *str);
 6 
 7 int main() 
 8 {
 9     char in[Size];
10     char next;
11     cout << "Enter a line:" << endl;
12     cin.get(in, Size);     //cin.get(in, Size)将一直读取,直到到达行尾或读取了Size-1个字符为止,把换行符留在输入队列
13     while (cin)    // ==while(!cin.fail()),即读入流成功 
14     {
15         cin.get(next);        //用cin.get(next)读取行输入后的字符,如果next是换行符,则说明cin.get(in, Size)读取了整行;
16         while (next != '\n')    //如果next不是换行符,则说明行中还有字符没有被读取,通过while循环丢弃余下的字符
17             cin.get(next);
18         strcount(in);
19         cout << "Enter next line (empty line to quit):\n";
20         cin.get(in, Size);
21     }
22     cout << "Bye!" << endl;
23     system("pause");
24     return 0;
25 }
26 
27 void strcount(const char *str)   //const表示str指针不能修改指向的内容(不过指针可以指向另外一块内容)
28 {
29     static int total = 0;  //static使total成为静态持续性,无链接性变量,首次初始化后,其值一直存在(即第二次调用strcount函数时,total的值不会再次初始化)
30     int count = 0;
31     cout << "\"" << str << "\" contains ";
32     while (*str++)      
33         count++;
34     total += count;
35     cout << count << " characters\n";
36     cout << total << " characters total!\n";
37 }

修改后

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 const int Size = 10;
 6 void strcount(const string &str);
 7 
 8 int main() 
 9 {
10     string in;
11     cout << "Enter a line:" << endl;
12     getline(cin,in);     
13     while (in!=" ")  
14     {
15         strcount(in);
16         cout << "Enter next line (empty line to quit):\n";
17         getline(cin, in);
18     }
19     cout << "Bye!" << endl;
20     system("pause");
21     return 0;
22 }
23 
24 void strcount(const string &str)   
25 {
26     static int total = 0;  
27     int count;
28     cout << "\"" << str << "\" contains ";
29     count = str.length();
30     total += count;
31     cout << count << " characters\n";
32     cout << total << " characters total!\n";
33 }

 

 3.下面是一个结构声明

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 struct chaff
 6 {
 7     char dross[20];
 8     int slag;
 9 };
10 
11 int main() 
12 {
13     //将一个静态数组用作缓冲区
14     char buffer[500];
15     chaff *p;
16     p = new (buffer) chaff[2];
17     for (int i = 0; i < 2; i++)
18     {
19         cout << "Enter dross: ";
20         char dross[20];
21         cin.get(dross, 20);
22         strcpy_s(p[i].dross, dross);
23         cout << "Enter slag: ";
24         cin >> p[i].slag;
25         cin.get();
26     }
27     for (int i = 0; i < 2; i++)
28     {
29         cout << "Dross: " << p[i].dross << endl;
30         cout << "Slag: " << p[i].slag << endl;
31     }
32     cout << endl;
33     
34     //使用常规new运算符来分配缓冲区
35     chaff *p2 = new chaff[2];
36     for (int i = 0; i < 2; i++)
37     {
38         cout << "Enter dross: ";
39         char dross[20];
40         cin.get(dross, 20);
41         strcpy_s(p2[i].dross, dross);
42         cout << "Enter slag: ";
43         cin >> p2[i].slag;
44         cin.get();
45     }
46     for (int i = 0; i < 2; i++)
47     {
48         cout << "Dross: " << p2[i].dross << endl;
49         cout << "Slag: " << p2[i].slag << endl;
50     }
51     delete[]p2;
52 
53     system("pause");
54     return 0;
55 }

 

 头文件 namesp.h

 1 //namesp.h
 2 namespace SALES
 3 {
 4     const int QUARTERS = 4;
 5     struct Sales
 6     {
 7         double sales[QUARTERS];
 8         double average;
 9         double max;
10         double min;
11     };
12     void setSales(Sales &s, const double ar[], int n);
13 
14     void setSales(Sales &s);
15 
16     void showSales(const Sales &s);
17 }

namesp.cpp

 1 #include<iostream>
 2 #include"namesp.h"
 3 using namespace std;
 4 
 5 namespace SALES
 6 {
 7     //将4个或n个对象从数组ar中复制到s的sales成员,并计算和存储输入对象的平均值、最大值和最小值
 8     //sales的其余元素(如果有的话)设置为0
 9     void setSales(Sales &s, const double ar[], int n)
10     {
11         double total = 0;
12         for (int i = 0; i < QUARTERS; i++)
13         {
14             if (i >= n)
15                 s.sales[i] = 0;
16             else
17                 s.sales[i] = ar[i];
18             if (i == 0)
19             {
20                 s.max = s.sales[i];
21                 s.min = s.sales[i];
22             }
23             else
24             {
25                 if (s.sales[i] > s.max)
26                     s.max = s.sales[i];
27                 if (s.sales[i] < s.min)
28                     s.min = s.sales[i];
29             }
30             total += s.sales[i];
31         }
32         s.average = total / QUARTERS;
33     }
34 
35     //以交互方式收集4个季度的销售额,将其存储在s的销售成员中,并计算和存储平均值、最大值和最小值
36     void setSales(Sales &s)
37     {
38         double d[QUARTERS];
39         for (int i = 0; i < QUARTERS; i++)
40         {
41             cout << "Enter the sales:" << endl;
42             cin >> d[i];
43         }
44         setSales(s, d, QUARTERS);
45     }
46 
47     //显示出来
48     void showSales(const Sales &s)
49     {
50         cout << "Sales:" << endl;
51         for (int i = 0; i < QUARTERS; i++)
52         {
53             cout << s.sales[i] << endl;
54         }
55         cout << "Average: " << s.average << endl;
56         cout << "Max: " << s.max << endl;
57         cout << "Min: " << s.min << endl;
58     }
59 }

main.cpp

 1 #include<iostream>
 2 #include"namesp.h"
 3 using namespace std;
 4 
 5 int main() 
 6 {
 7     using SALES::Sales;
 8     using SALES::setSales;
 9     using SALES::showSales;
10 
11     double d[4] = { 111.11, 222.22, 333.33, 444.44 };
12     Sales s1, s2;
13     setSales(s1, d, 4);
14     showSales(s1);
15     setSales(s2);
16     showSales(s2);
17 
18     system("pause");
19     return 0;
20 }

 

猜你喜欢

转载自www.cnblogs.com/Fionaaa/p/12693319.html
今日推荐