#include
class goods
{
public:
goods(int w){ weights=w; total_weight+=w; }
~goods(){ total_weight-=weights; }
int weight(){ return weights; }
static int TotalWeight(){ return total_weight; }
goods *next;
private:
int weights;
static int total_weight;
};
int goods::total_weight=0;
void purchase(goods * &f, goods * &r, int w)
{
goods *p= new goods(w);
p->next=NULL;
if(f==NULL)
{ f=f=p ; }
else
{ r->next=p; r=r->next;} //第二次添加物品重量时不知道为什么会内存溢出?
}
void sale(goods * &f , goods * &r) //可否帮我解一下个是什么意思"goods * &f , goods * &r"
{
if(f==NULL)
{
cout<<"No any goods!\n";
return;
}
goods *q = f;
f=f->next;
delete q;
cout<<"saled.\n";
}
void main()
{
goods *front =NULL, *rear=NULL;
int w;
int choice;
do{
cout<<"Please choice:\n";
cout<<"Key in 1 is purchase.\nKey in 2 is sale.\nKey in 0 is over.\n";
cin>> choice;
switch(choice)
{
case 1:
{
cout <<"Input weight:";
cin>>w;
purchase(front ,rear, w);
break;
}
case 2:
{ sale(front ,rear); break; }
case 0: break;
}
cout<<"Now total weight is:"<
}while(choice);
}
第二次添加物品重量时不知道为什么会内存溢出?
--------------------next---------------------
阅读(1271) | 评论(0) | 转发(0) |