在您提供的代码中,我注意到了一些错误。以下是已经修改过的代码:
#ifndef NULL
#define NULL 0
#endif
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct LNode {
int month;
int day;
string name;
double price;
};
class List {
public:
int _size;
LNode* data;
List() {
_size = 0;
data = new LNode[100];
}
~List() {
delete[] data;
}
void insert(int a, int b, string c, double d) {
for (int i = _size; i > 0; i--) {
data[i] = data[i - 1];
}
data[0].month = a;
data[0].day = b;
data[0].name = c;
data[0].price = d;
_size++;
}
void insert(int index, int a, int b, string c, double d) {
if (index <= 0 || index > _size + 1) {
return;
}
for (int i = _size; i >= index; i--) {
data[i] = data[i - 1];
}
data[index - 1].month = a;
data[index - 1].day = b;
data[index - 1].name = c;
data[index - 1].price = d;
_size++;
}
void print_L() {
for (int i=0;i<_size;i++) { cout << setw(2) << setfill('0')<<data[i].day<<" " ;
cout<<setw(2)<<setfill('0')<<data[i].month<<" " ;
cout<<data[i].name<<" ";
cout<<fixed << setprecision(1)<<data[i].price<<endl; }
}
int located(int a, int b) {
if (_size == 0)
return 1;
int e = 1;
for (int i = 0; i < _size; i++) {
if (a < data[i].month) {
e++;
if (i == _size - 1) {
return e;
}
}
else if (a > data[i].month) {
return e;
}
else if (a == data[i].month) {
if (b < data[i].day) {
e++;
if (i == _size - 1) {
return e;
}
}
else if (b > data[i].day) {
return e;
}
else if (b == data[i].day) {
return e;
}
}
}
return e;
};
};
int main() {
int m, n;
string str, c;
double d;
List L1;
double sum1 = 0.0;
int count1 = 0;
double sum2 = 0.0;
int count2 = 0;
cin >> m >> n;
for (int i = 0; i < m; i++) {
cin >> str >> c >> d;
int a = stoi(str.substr(2));
int b = stoi(str.substr(0, 2));
L1.insert(a, b, c, d);
// 统计键盘和鼠标的总价格
if (count1 < n && c == "keyboard") {
sum1 += d;
count1++;
}
if (count2 < n && c == "mouse") {
sum2 += d;
count2++;
}
}
// 输出平均价格
cout << fixed << setprecision(1) << sum1 / count1 << endl;
cout << fixed << setprecision(1) << sum2 / count2 << endl;
return 0;
}
我做了以下修改:
- 在
insert
函数中,不再传入引用类型的参数。 - 修改了
print_L()
函数,以正确打印节点的信息。 - 在主函数中,在插入数据时统计键盘和鼠标的总价格,并计算平均价格。
请注意,这只是一个针对提供代码中错误的修正版本。如果还有其他要求或问题,请随时告诉我。
内容由零声教学AI助手提供,问题来源于学员提问