zoukankan      html  css  js  c++  java
  • new、delete、以及queue类

    本来以为很容易的,结果还是写了我两个小时。
    用指针模拟queue类,再加上类,各种错误,总算是解决掉了--
    #include<iostream>
    #include<cstdlib>
    #include<string>
    using namespace std;
    class Item
    {
    private:
        int time;
        int cost;
    public:
        Item():time(0),cost(0){}
        Item(int k):time(k)
        {
            cost=rand()%3;
        }
        Item (const Item &st)
        {
            time=st.time;
            cost=st.cost;
        }
        Item &operator=(const Item &st)
        {
            time=st.time;
            cost=st.cost;
            return *this;
        }
        int dealt()
        {
            return time;
        }
        int dealc()
        {
            //cout<<"------"<<cost<<endl;
            return cost;
    
        }
        friend ostream &operator<<(ostream &os,Item &st)
        {
            os<<st.time<<endl<<st.cost;
            return os;
        }
    };
    struct ss
    {
        Item num;
        struct ss *next;
    };
    class Queue
    {
    private:
        struct ss *beg,*end;
        int cnt;
        int tolt;
        int size;
        int xx;
    public:
        Queue():beg(NULL),end(NULL),cnt(0),tolt(0),size(10),xx(0){};
        Queue(const Queue &st)
        {
            cnt=st.cnt;
            tolt=st.tolt;
            size=st.size;
            while(beg!=NULL)
            {
                ss *p=beg->next;
                delete beg;
                beg=p;
            }
            end=NULL;
            ss *p=st.beg;
            beg=new ss;
            beg->next=NULL;
            beg->num=p->num;
            end=beg;
            while(p->next!=NULL)
            {
                p=p->next;
                ss *p1=new ss;
                p1->num=p->num;
                p1->next=NULL;
                end->next=p1;
                end=p1;
            }
            //return *this;
        }
        Queue &operator=(const Queue &st)
        {
            cnt=st.cnt;
            tolt=st.tolt;
            size=st.size;
            while(beg!=NULL)
            {
                ss *p=beg->next;
                delete beg;
                beg=p;
            }
            end=NULL;
            ss *p=st.beg;
            beg=new ss;
            beg->next=NULL;
            beg->num=p->num;
            end=beg;
            while(p->next!=NULL)
            {
                p=p->next;
                ss *p1=new ss;
                p1->num=p->num;
                p1->next=NULL;
                end->next=p1;
                end=p1;
            }
            return *this;
        }
        bool empty()
        {
            if(cnt==0) return true;
            else return false;
        }
        bool full()
        {
            if(cnt==size) return true;
            return false;
        }
        bool push(Item &st)
        {
            if(full()) return false;
            cnt++;
            if(beg==NULL)
            {
                ss *p=new ss;
                p->num=st;
                p->next=NULL;
                beg=end=p;
            }
            else
            {
                ss *p=new ss;
                p->num=st;
                p->next=NULL;
                beg->next=p;
                end=p;
            }
            //cout<<beg->num<<endl;
            return true;
        }
        bool pop()
        {
            if(empty()) return false;
            cnt--;
            xx++;
            if(tolt<beg->num.dealt())
            {
                tolt=beg->num.dealt()+beg->num.dealc();
            }
            else
            {
                tolt+=beg->num.dealc();
            }
            ss *p=beg->next;
            delete beg;
            beg=p;
            return true;
        }
        int top(int w)
        {
            int tmp=beg->num.dealt()+beg->num.dealc();
            //cout<<"---"<<beg->num.dealt()<<"   "<<beg->num.dealc()<<endl;
            if(tmp<=w) pop();
        }
        void deal(int n)
        {
            tolt-=n;
        }
        ~Queue()
        {
            while(beg!=NULL)
            {
                ss *p=beg->next;
                delete beg;
                beg=p;
            }
        }
        friend ostream &operator<<(ostream &os,const Queue &st)
        {
            os<<"处理花费的总时间(分钟):"<<st.tolt<<endl<<"处理了多少人:"<<st.xx<<endl;
            os<<"队列里面还有多少人:"<<st.cnt<<endl;
            return os;
        }
    };
    int main()
    {
        Queue q;
        //int sum=1235;
        int n,m;
        cout<<"请输入n,m:";
        cin>>n>>m;
        if(n>m)
        {
            int tmp=n;
            n=m;
            m=n;
        }
        for(int i=n;i<=m;i++)
        {
            Item p(i);
            q.push(p);
            if(!q.empty())
            {
                q.top(i);
            }
        }
        q.deal(n);
        cout<<q;
        return 0;
    }
    

      

  • 相关阅读:
    软件体系架构复习要点
    Operating System on Raspberry Pi 3b
    2019-2020 ICPC North-Western Russia Regional Contest
    2019 ICPC ShenYang Regional Online Contest
    2019 ICPC XuZhou Regional Online Contest
    2019 ICPC NanChang Regional Online Contest
    2019 ICPC NanJing Regional Online Contest
    Codeforces Edu Round 72 (Rated for Div. 2)
    Codeforces Round #583 (Div.1+Div.2)
    AtCoder Beginning Contest 139
  • 原文地址:https://www.cnblogs.com/ziyi--caolu/p/4065000.html
Copyright © 2011-2022 走看看