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;
    LNode* next;  // 后继指针

    LNode() {};
    LNode(int a, int b, string c, double d, LNode* n = NULL) : month(a), day(b), name(c), price(d), next(n) {};
};

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

    void init()
    {
        head = new LNode;
        head->next = NULL;
        _size = 0;
    }

    List()
    {
        init();
    }  // 构造函数

   ~List()// 析构函数
   {
       while (head)
       {
           LNode* p = head->next;
           delete head;
           head = p;
       }
   };

   // 头插法
   void insert(int& a, int& b, string& c, double& d)
   {
       _size++;
       LNode* p = new LNode(a, b, c, d, head->next);
       head->next = p;
   };

   // 在指定位置插入元素
   void insert(int index, int a, int b, string c, double d)
   {
       if (index <= 0 || index > _size + 1)
           return;

       _size++;
       LNode* p = head;
       
       for (int i = 0; i < index - 1; i++)
           p = p->next;
       
       LNode* tmp = new LNode(a, b, c, d);
       tmp->next = p->next;
       p->next = tmp;
   };

   void print_L()
   {
       LNode* p = head->next;
       
       while (p)
       {
           cout << p->month << " " << p->day << " ";
           p = p->next;
       }
       
       cout << endl;
   };

   int located(int a, int b)
   {
        LNode* p = head->next;
        int e = 1;

        if (!p)
            return e;

        while (p)
        {
            if (a < p->month)
            {
                e++;
                p = p->next;

                if (!p)
                    return e;
            }
            else if (a > p->month)
            {
                return e;
            }
            else if (a == p->month)
            {
                if (b < p->day)
                {
                    e++;
                    p = p->next;

                    if (!p)
                        return e;
                }
                else if (b > p->day)
                {
                    return e;
                }
                else if (b == p->day)
                {
                    return e;
                }
            }
        }

        return -1;  // 找不到返回-1
    };
};

int main()
{
    int m, n;
    string str, c;
    double d;

    List L1;

    double ave1 = 0.0;
    double 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;

    LNode* p = L1.head->next;

    while (p && count1 < n)
    {
        if (p->name == "keyboard")
        {
            sum1 += p->price;
            count1++;
        }
        
        p = p->next;
    }

    LNode* s = L1.head->next;

    while (s && count2 < n)
    {
        if (s->name == "mouse")
        {
            sum2 += s->price;
            count2++;
        }
        
        s = s->next;
   }

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

   return 0;
}

此代码对原始代码进行了一些修改:

  • 修改了月份和日期的提取方式,使用substr()函数代替erase()stoi()
  • 修改了定位函数中的返回值,如果找不到对应的位置,返回-1。
  • 简化了计算平均值部分的逻辑,使用while循环遍历链表,并递增计数器。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?