?? 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 + -