码迷,mamicode.com
首页 > 其他好文 > 详细

new、delete、以及queue类

时间:2014-10-31 15:32:14      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   for   sp   div   on   log   amp   

本来以为很容易的,结果还是写了我两个小时。
用指针模拟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;
}

  

new、delete、以及queue类

标签:blog   io   os   for   sp   div   on   log   amp   

原文地址:http://www.cnblogs.com/ziyi--caolu/p/4065000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!