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

可以使用链表实现该代码,下面是另一种方法的实现:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct LNode {
    int month;
    int day;
    string name;
    double price;
    LNode* next;

    LNode(int m, int d, string n, double p) : month(m), day(d), name(n), price(p), next(nullptr) {}
};

class List {
public:
    int size;
    LNode* head;

    List() : size(0), head(nullptr) {}

    ~List() {
        LNode* current = head;
        while (current != nullptr) {
            LNode* next = current->next;
            delete current;
            current = next;
        }
        head = nullptr;
    }

    void insert(int m, int d, const string& n, double p) {
        size++;
        LNode* newNode = new LNode(m, d, n, p);
        
        if (head == nullptr || (m < head->month) || (m == head->month && d < head->day)) {
            newNode->next = head;
            head = newNode;
        } else {
            LNode* current = head;

            while (current->next != nullptr && 
                   ((current->next)->month < m || ((current->next)->month == m && (current->next)->day <= d))) {
                current = current->next;    
            }
            
            newNode->next = current->next;   
            current->next = newNode;          
        }
    }

    void print_L() const {
        LNode* current = head;

        while (current != nullptr) {
            cout << setw(2) << setfill('0') << current->month << " ";
            cout << setw(2) << setfill('0') << current->day << " ";
            cout << current->name << " ";
            cout << fixed << setprecision(1) << current->price;
            cout << endl;
            current = current->next;
        }
    }

    double averagePrice(const string& item, int n) const {
        LNode* current = head;
        double sum = 0.0;
        int count = 0;

        while (current != nullptr && count < n) {
            if (current->name == item) {
                sum += current->price;
                count++;
            }
            
            current = current->next;
        }

        return sum / count;
    }
};

int main() {
    int m, n;
    cin >> m >> n;

    List L1;

    for (int i = 0; i < m; i++) {
        string str, name;
        double price;
        
        cin >> str >> name >> price;

        int month = stoi(str.substr(2, 2));
        int day = stoi(str.substr(4, 2));

        L1.insert(month, day, name, price);
    }

    //L1.print_L();

    double avgKeyboardPrice = L1.averagePrice("keyboard", n);
    double avgMousePrice = L1.averagePrice("mouse", n);

    cout << fixed << setprecision(1) << avgKeyboardPrice << endl
         << fixed << setprecision(1) << avgMousePrice<< endl;

    return 0;
}

这个实现使用了链表来存储数据,按照月份和日期的顺序插入节点。然后,根据要求计算键盘和鼠标商品的平均价格,并输出结果。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?