C++第九章编程训练答案

[复制链接]
发表于 2026-1-18 03:45:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
题一


 头文件
test9_1.h
  1. #ifndef TEST9_1_H_
  2. #define TEST9_1_H_
  3. const int Len = 40;
  4. struct golf
  5. {
  6.     char fullname[Len];
  7.     int handicap;
  8. };
  9. void setgolf(golf &g, const char *name, int hc);
  10. // 赋值函数
  11. int setgolf(golf &g);
  12. // 按输入赋值
  13. void handicap(golf &g, int hc);
  14. // 对handicap重新赋值
  15. void showgolf(const golf &g);
  16. #endif
复制代码
源文件
  1. #include <iostream>
  2. #include "test9_1.h"
  3. #include <cstring>
  4. using namespace std;
  5. void setgolf(golf &g, const char *name, int hc)
  6. {
  7.     strcpy(g.fullname, name);
  8.     g.handicap = hc;
  9. }
  10. int setgolf(golf &g)
  11. {
  12.     cout << "Enter the fullname :";
  13.     cin.get(g.fullname, Len);
  14.     cout << "Enter the handicap: ";
  15.     cin >> g.handicap;
  16.     cin.get();
  17.     if (g.fullname[0] == '\0')
  18.         return 0;
  19.     else
  20.         return 1;
  21. }
  22. void handicap(golf &g, int hc)
  23. {
  24.     g.handicap = hc;
  25. }
  26. void showgolf(const golf &g)
  27. {
  28.     if (g.fullname[0] != '\0')
  29.     {
  30.         cout << "fullname: " << g.fullname << endl;
  31.         cout << "handicap: " << g.handicap << endl;
  32.     }
  33. }
复制代码
 源代码
  1. #include <iostream>
  2. #include "test9_1.h"
  3. using namespace std;
  4. int main()
  5. {
  6.     golf ann;
  7.     setgolf(ann, "Ann Birdfree", 24);
  8.     showgolf(ann);
  9.     golf *golfarr = new golf[3];
  10.     for (int i = 0; i < 3 && setgolf(golfarr[i]); i++)
  11.     {
  12.     }
  13.     for (int i = 0; i < 3; i++)
  14.         showgolf(golfarr[i]);
  15.     delete[] golfarr;
  16.     return 0;
  17. }
复制代码




题二

  1. // static.cpp--using a static local variable
  2. #include <iostream> //constants
  3. #include <string>
  4. #include <cstring>
  5. using namespace std;
  6. // const int ArSize = 10;
  7. //  function prototype
  8. void strcount(const string str);
  9. int main()
  10. {
  11.     // char input[ArSize];
  12.     string input;
  13.     char next;
  14.     cout << "Enter a line:\n";
  15.     getline(cin, input);
  16.     while (input != "")
  17.     {
  18.         strcount(input);
  19.         cout << "Enter next line(empty line to quit);\n";
  20.         getline(cin, input);
  21.     }
  22.     cout << "Bye\n";
  23.     return 0;
  24. }
  25. void strcount(const string str)
  26. {
  27.     using namespace std;
  28.     static int total = 0;
  29.     int count = 0;
  30.     // static local variable
  31.     // automatic local variable
  32.     cout << """ << str << ""contains \n";
  33.     count = str.size();
  34.     total += count;
  35.     cout << count << " characters\n";
  36.     cout << total << " characters total\n";
  37. }
复制代码

题三


方法一
  1. #include <iostream>
  2. #include <new>
  3. #include <cstring>
  4. const int BUF = 512;
  5. const int N = 5;
  6. char buffer[BUF];
  7. struct chaff
  8. {
  9.     char dross[20];
  10.     int salg;
  11. };
  12. int main()
  13. {
  14.     using namespace std;
  15.     chaff *array1 = new (buffer) chaff[2];
  16.     for (int i = 0; i < 2; i++)
  17.     {
  18.         cout << "dross" << i + 1 << ": ";
  19.         cin.getline(array1[i].dross, 20);
  20.         cout << "salg" << i + 1 << ":";
  21.         cin >> array1[i].salg;
  22.         cin.get();
  23.     }
  24.     for (int i = 0; i < 2; i++)
  25.     {
  26.         cout << "dross" << i + 1 << ": " << array1[i].dross << endl;
  27.         cout << "salg" << i + 1 << ": " << array1[i].salg << endl;
  28.     }
  29.     return 0;
  30. }
复制代码

方法二
  1. #include <iostream>
  2. #include <new>
  3. #include <cstring>
  4. const int BUF = 512;
  5. struct chaff
  6. {
  7.     char dross[20];
  8.     int salg;
  9. };
  10. int main()
  11. {
  12.     using namespace std;
  13.     char *buffer = new char[BUF];
  14.     chaff *array1 = new (buffer) chaff[2];
  15.     for (int i = 0; i < 2; i++)
  16.     {
  17.         cout << "dross" << i + 1 << ": ";
  18.         cin.getline(array1[i].dross, 20);
  19.         cout << "salg" << i + 1 << ":";
  20.         cin >> array1[i].salg;
  21.         cin.get();
  22.     }
  23.     for (int i = 0; i < 2; i++)
  24.     {
  25.         cout << "dross" << i + 1 << ": " << array1[i].dross << endl;
  26.         cout << "salg" << i + 1 << ": " << array1[i].salg << endl;
  27.     }
  28.     delete buffer;
  29.     return 0;
  30. }
复制代码

题四

头文件
  1. #ifndef TEST9_4_H_
  2. #define TEST9_4_H_
  3. namespace SALES
  4. {
  5.     const int QUARTERS = 4;
  6.     struct Sales
  7.     {
  8.         double sales[QUARTERS];
  9.         double average;
  10.         double max;
  11.         double min;
  12.     };
  13.     void setSales(Sales &s, const double ar[], int n);
  14.     void setSales(Sales &s);
  15.     void showSales(const Sales &s);
  16. }
  17. #endif
复制代码
源代码
  1. #include "test9_4.h"
  2. #include <iostream>
  3. using namespace std;
  4. void SALES::setSales(Sales &s, const double ar[], int n)
  5. {
  6.     double sum;
  7.     s.max = ar[0];
  8.     s.min = ar[0];
  9.     // int length = sizeof(ar) / sizeof(double);
  10.     for (int i = 0; i < QUARTERS; i++)
  11.     {
  12.         if (i < n)
  13.         {
  14.             sum += ar[i];
  15.             s.sales[i] = ar[i];
  16.             if (ar[i] > s.max)
  17.                 s.max = ar[i];
  18.             if (ar[i] < s.min)
  19.                 s.min = ar[i];
  20.         }
  21.         else
  22.             s.sales[i] = 0;
  23.     }
  24.     s.average = sum / n;
  25. }
  26. void SALES::setSales(Sales &s)
  27. {
  28.     s.max = 0;
  29.     s.min = 999999;
  30.     for (int i = 0; i < QUARTERS; i++)
  31.     {
  32.         cout << "Enter " << i + 1 << "-th quarter sales:";
  33.         cin >> s.sales[i];
  34.         s.average += s.sales[i];
  35.         if (s.sales[i] > s.max)
  36.             s.max = s.sales[i];
  37.         if (s.sales[i] < s.min)
  38.             s.min = s.sales[i];
  39.     }
  40.     s.average /= QUARTERS;
  41. }
  42. void SALES::showSales(const Sales &s)
  43. {
  44.     for (int i = 0; i < QUARTERS; i++)
  45.         cout << i + 1 << "-th quarter sales:" << s.sales[i] << endl;
  46.     cout << "average :" << s.average << endl;
  47.     cout << "max :" << s.max << endl;
  48.     cout << "min :" << s.min << endl;
  49. }
复制代码
源代码
  1. #include <iostream>
  2. #include "test9_4.h"
  3. using namespace std;
  4. using namespace SALES;
  5. int main()
  6. {
  7.     Sales FirstYear;
  8.     double earning[3] = {10, 20, 30};
  9.     setSales(FirstYear, earning, 3);
  10.     showSales(FirstYear);
  11.     Sales SecondYear;
  12.     setSales(SecondYear);
  13.     showSales(SecondYear);
  14.     return 0;
  15. }
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表