ZBLOG

用顺序表实现此代码: #ifndef NULL #define NULL 0 #endif #include #include #include using namespace std; struct LNode { //存储数据 int month; int day; string name; double price; LNode* next;//后继指针 LNode() {}; LNode(int a, int ...

以下是使用顺序表(数组)实现的代码:

  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. {
  10. //存储数据
  11. int month;
  12. int day;
  13. string name;
  14. double price;
  15. };
  16. class List
  17. {
  18. public:
  19. int _size;
  20. LNode* data; // 使用动态数组代替链表
  21. void init()
  22. {
  23. data = new LNode[100]; // 假设最大容量为100,可以根据需求调整大小
  24. _size = 0;
  25. }
  26. List() { init(); } // 构造函数
  27. ~List()
  28. {
  29. delete[] data; // 删除动态数组内存空间
  30. };
  31. // 头插法
  32. void insert(int a, int b, string c, double d)
  33. {
  34. for (int i = _size - 1; i >= 0; i--)
  35. {
  36. data[i + 1] = data[i]; // 元素后移一位
  37. }
  38. data[0].month = a;
  39. data[0].day = b;
  40. data[0].name = c;
  41. data[0].price = d;
  42. _size++;
  43. };
  44. // 在指定位置插入元素
  45. void insert(int index, int a, int b, string c, double d)
  46. {
  47. if (index < 1 || index > _size + 1) return;
  48. for (int i = _size - 1; i >= index - 1; i--)
  49. {
  50. data[i + 1] = data[i]; // 元素后移一位,腾出插入位置
  51. }
  52. data[index - 1].month = a;
  53. data[index - 1].day = b;
  54. data[index - 1].name = c;
  55. data[index - 1].price = d;
  56. _size++;
  57. };
  58. void print_L()
  59. {
  60. for (int i = 0; i < _size; i++)
  61. {
  62. cout << data[i].month << " " << data[i].day << " ";
  63. }
  64. cout << endl;
  65. };
  66. int located(int a, int b)
  67. {
  68. int e = 1;
  69. for (int i = 0; i < _size; i++)
  70. {
  71. if (a < data[i].month)
  72. {
  73. e++;
  74. continue;
  75. }
  76. else if (a > data[i].month)
  77. {
  78. return e;
  79. }
  80. if (b < data[i].day)
  81. {
  82. e++;
  83. continue;
  84. }
  85. else if (b > data[i].day)
  86. {
  87. return e;
  88. }
  89. return e; // 如果找到匹配的月份和日期,则返回位置
  90. }
  91. return e; // 如果没有找到匹配的月份和日期,返回位置也是在最后
  92. };
  93. };
  94. int main()
  95. {
  96. int m, n;
  97. string str, c;
  98. double d;
  99. List L1;
  100. double ave1 = 0.0, ave2 = 0.0;
  101. cin >> m >> n;
  102. for (int i = 0; i < m; i++)
  103. {
  104. cin >> str >> c >> d;
  105. int a = stoi(str.substr(2), nullptr);
  106. int b = stoi(str.substr(0, 2), nullptr);
  107. int e = L1.located(a, b);
  108. L1.insert(e, a, b, c, d);
  109. }
  110. double sum1 = 0.0;
  111. int count1 = 0;
  112. double sum2 = 0.0;
  113. int count2 = 0;
  114. for (int i = 0; i < m; i++)
  115. {
  116. if (count1 >= n)
  117. {
  118. break;
  119. }
  120. else
  121. {
  122. if (L1.data[i].name == "keyboard")
  123. {
  124. sum1 += L1.data[i].price;
  125. count1++;
  126. }
  127. }
  128. }
  129. for (int i = 0; i < m; i++)
  130. {
  131. if (count2 >= n)
  132. {
  133. break;
  134. }
  135. else
  136. {
  137. if (L1.data[i].name == "mouse")
  138. {
  139. sum2 += L1.data[i].price;
  140. count2++;
  141. }
  142. }
  143. }
  144. cout << fixed << setprecision(1) << sum1 / n << endl;
  145. cout << fixed << setprecision(1) << sum2 / n << endl;
  146. return 0;
  147. }

请注意,该代码是通过将链表更改为动态数组(顺序表)实现的。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?