ZBLOG

将此代码改为使用c++顺序表方式实现:#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...

下面是使用C++顺序表方式实现的代码:

”`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 { private:

LNode* data;  // 数据数组
int _size;   // 当前元素个数
int capacity;  // 数组容量

public:

List(int cap = 100) : capacity(cap), _size(0) {
    data = new LNode[capacity];
    for (int i = 0; i < capacity; i++) {
        data[i].month = 0;
        data[i].day = 0;
        data[i].name = "";
        data[i].price = 0.0;
    }
}

~List() {
    delete[] data;
}

void insert(int& a, int& b, string& c, double& d) {
    if (_size >= capacity) { // 容量不够时扩容
        expandCapacity();
    }
    for (int i = _size - 1; i >= 0; i--) { // 向后移动元素,为新元素腾出位置
        data[i + 1] = data[i];
    }
    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;
	}
	if (_size >= capacity) { // 容量不够时扩容
        expandCapacity();
    }
    for (int i = _size - 1; i >= index - 1; i--) { // 向后移动元素,为新元素腾出位置
        data[i + 1] = data[i];
    }
    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 << data[i].month << " " << data[i].day << " ";
    }
    cout << endl;
}

int located(int a, int b) {
	int e = 1;
	for (int i = 0; i < _size; i++) {
		if (a < data[i].month) {
			e++;
		}
		else if (a == data[i].month) {
			if (b < data[i].day) {
				e++;
			}
		}
		else {
			break;
		}
	}

    return e;
}

private:

void expandCapacity() { // 扩容数组容量
	int new_capacity = capacity * 2;
	LNode* new_data = new LNode[new_capacity];
	for (int i = 0; i < _size; ++i) {
    	new_data[i] = data[i];
	}

	delete[] data;

	data = new_data;
	capacity = new_capacity;
}

};

int main() {

int m, n;
string str;
string c;
double d;

cin >> m >> n;

List L1;

double ave1 = 0;
double ave2 = 0;

for (int i = 0; i < m; i++) {
	cin >> str >> c >> d;
	int a = stoi(str, 0, 10);
	if (a >= 10)
		str.erase(0, 3);
	else
		str.erase(0, 2);
	int b = stoi(str, 0, 10);

	int e = L1.located(a, b);
	L1.insert(e, a, b, c, d);
}

double sum1 = 0;
int count1 = 0;
double sum2 = 0;
int count2 = 0;

for (int i = 0; i < m; i++) {
	if (count1 >= n) {
		break;
	}
    else if (L1.data[i].name == "keyboard") {
		sum1 += L1.data[i].price;
		count1++;
    }

}

for (int i = 0; i < m; i++) {
    if (count2 >= n) {
        break;
    }
    else if (L1.data[i].name == "mouse") {
        sum2 += L1.data[i].price;
        count2++;
    }

}

cout << fixed << setprecision(1) << sum1 / n << endl;
cout << fixed << setprecision(1) << sum2 / n << endl;

return 0;

} “`


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?