Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2348968
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:09:00

#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) |
给主人留下些什么吧!~~