📄 10-1.cpp
字号:
//1. 某商店经销一种货物,货物成箱购进,成箱卖出,
//购进和卖出时以重量为单位,各箱的重量不一样,
//因此,商店需要记录下目前库存的货物的总重量,
//现在要求用C++语言来模拟商店货物购进和卖出的情况。
#include<iostream.h>
class Goods
{
public :
Goods(int w)
{
weight=w;
total_weight+=w;
}
~ Goods()
{
total_weight-=weight;
}
int Weight()
{
return weight ;
}
static int TotalWeight() //静态成员函数,返回货物总重量
{
return total_weight ;
}
Goods *next ;
private :
int weight ;
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=r=p;
else
{
r->next=p;r=r->next ;
}
}
void sale( Goods *&f , Goods *&r )//售出货物,从表头删除结点
{
if(f==NULL )
{cout<<"没有货物!\n" ;
return ;
}
Goods *q = f ;
f=f->next ;
delete q ;
cout << "货物卖出.\n" ;
}
void main()
{
Goods *front=NULL, *rear=NULL ;
int w ;
int choice ;
do
{
cout << "Boy or girl,你有3种选择:\n" ;
cout<<"-------------------------------\n";
cout << "1.购进1箱货物\n2.卖出1箱货物\n0.GAME OVER则选择结束.\n\n" ;
cout<<"-------------------------------\n";
cin >> choice ;
switch ( choice )
{
case 1 :
{
cout << "输入1箱货物的重量: " ;
cin >> w ;
purchase(front,rear,w) ; // 从表尾插入1个结点
break ;
}
case 2 :
{
sale(front,rear); // 从表头删除1个结点
break ;
}
case 0 : break ;
}
cout << "目前库存的货物的总重量:" << Goods::TotalWeight() << endl ;
} while(choice) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -