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, ...

下面是使用顺序表(数组)实现的修改后的代码:

#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]; // 假设最多存储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 << data[i].month << " " << data[i].day << " ";
         }
         cout << endl;
     }

     int located(int a, int b) {
         int e = 1;
         if (_size == 0)
             return e;

         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 ave1 = 0.0, ave2 = 0.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));

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

    double sum1 = 0.0;
    int count1 = 0;

    double sum2 = 0.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;
}

这段代码将链表实现改为使用顺序表(数组)实现,并进行了一些相应的调整。注意,在原来的链表实现中,节点的插入是从头部进行的,而在顺序表中,需要将已有元素向后移动,然后插入新元素到指定位置。同时,对于字符串转换成整数的部分也进行了修改。最后,在输出结果时,需要使用fixedsetprecision函数来设置小数点精度。

请注意,我根据题目要求做了一些简化和适应顺序表实现的修改,请根据实际需求和数据结构课程要求进行适当的调整。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?