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; }
朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。