ZBLOG

cpp #ifndef NULL #define NULL 0 #endif #include #include #include using namespace std; struct LNode { // 存储数据 int month; int day; string name; double price; }; class List { public: int _size; ...

在您提供的代码中,我注意到了一些错误。以下是已经修改过的代码:

  1. #ifndef NULL
  2. #define NULL 0
  3. #endif
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <string>
  7. using namespace std;
  8. struct LNode {
  9. int month;
  10. int day;
  11. string name;
  12. double price;
  13. };
  14. class List {
  15. public:
  16. int _size;
  17. LNode* data;
  18. List() {
  19. _size = 0;
  20. data = new LNode[100];
  21. }
  22. ~List() {
  23. delete[] data;
  24. }
  25. void insert(int a, int b, string c, double d) {
  26. for (int i = _size; i > 0; i--) {
  27. data[i] = data[i - 1];
  28. }
  29. data[0].month = a;
  30. data[0].day = b;
  31. data[0].name = c;
  32. data[0].price = d;
  33. _size++;
  34. }
  35. void insert(int index, int a, int b, string c, double d) {
  36. if (index <= 0 || index > _size + 1) {
  37. return;
  38. }
  39. for (int i = _size; i >= index; i--) {
  40. data[i] = data[i - 1];
  41. }
  42. data[index - 1].month = a;
  43. data[index - 1].day = b;
  44. data[index - 1].name = c;
  45. data[index - 1].price = d;
  46. _size++;
  47. }
  48. void print_L() {
  49. for (int i=0;i<_size;i++) { cout << setw(2) << setfill('0')<<data[i].day<<" " ;
  50. cout<<setw(2)<<setfill('0')<<data[i].month<<" " ;
  51. cout<<data[i].name<<" ";
  52. cout<<fixed << setprecision(1)<<data[i].price<<endl; }
  53. }
  54. int located(int a, int b) {
  55. if (_size == 0)
  56. return 1;
  57. int e = 1;
  58. for (int i = 0; i < _size; i++) {
  59. if (a < data[i].month) {
  60. e++;
  61. if (i == _size - 1) {
  62. return e;
  63. }
  64. }
  65. else if (a > data[i].month) {
  66. return e;
  67. }
  68. else if (a == data[i].month) {
  69. if (b < data[i].day) {
  70. e++;
  71. if (i == _size - 1) {
  72. return e;
  73. }
  74. }
  75. else if (b > data[i].day) {
  76. return e;
  77. }
  78. else if (b == data[i].day) {
  79. return e;
  80. }
  81. }
  82. }
  83. return e;
  84. };
  85. };
  86. int main() {
  87. int m, n;
  88. string str, c;
  89. double d;
  90. List L1;
  91. double sum1 = 0.0;
  92. int count1 = 0;
  93. double sum2 = 0.0;
  94. int count2 = 0;
  95. cin >> m >> n;
  96. for (int i = 0; i < m; i++) {
  97. cin >> str >> c >> d;
  98. int a = stoi(str.substr(2));
  99. int b = stoi(str.substr(0, 2));
  100. L1.insert(a, b, c, d);
  101. // 统计键盘和鼠标的总价格
  102. if (count1 < n && c == "keyboard") {
  103. sum1 += d;
  104. count1++;
  105. }
  106. if (count2 < n && c == "mouse") {
  107. sum2 += d;
  108. count2++;
  109. }
  110. }
  111. // 输出平均价格
  112. cout << fixed << setprecision(1) << sum1 / count1 << endl;
  113. cout << fixed << setprecision(1) << sum2 / count2 << endl;
  114. return 0;
  115. }

我做了以下修改:

  • insert函数中,不再传入引用类型的参数。
  • 修改了print_L()函数,以正确打印节点的信息。
  • 在主函数中,在插入数据时统计键盘和鼠标的总价格,并计算平均价格。

请注意,这只是一个针对提供代码中错误的修正版本。如果还有其他要求或问题,请随时告诉我。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=9435

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?