/*
某商店经销一种货物,货物的购进和卖出以箱为单位,各箱的重量不一样,
因此商店需要目前库存的总重量。
现在用c++模拟商店货物购进和卖出的情况 */ #include<iostream>
using namespace std; //货物类
class Goods{
public:
Goods(int w=){
this->weight = w;
}
int GetW(){
return weight;
}
Goods *next;
private:
//重量
int weight;
}; //商店类
class Shop{
public:
Shop(int s=){
this->weights = s;
pHead = NULL;
}
~Shop(){
if (pHead!=NULL)
{
Goods *pCurrent = pHead;
Goods *pNext = NULL;
while (pCurrent){
pNext = pCurrent->next;
delete pCurrent;
pCurrent = NULL;
pCurrent = pNext;
}
}
weights = ;
}
//卖出
void SellOut(){
if (pHead==NULL)
{
cout << "商店里已经没有货物了!" << endl;
}
else{
//队列 先进先出
Goods *pCurrent = pHead;
pHead = pHead->next;
delete pCurrent;
}
}
//购进
void Purchase(Goods *&pin){
if (pHead==NULL)
{
//插入第一箱货物
pHead = pin;
weights += pin->GetW();
}
else{
Goods *pCurrent = pHead;
while (pCurrent->next){
pCurrent = pCurrent->next;
}
pCurrent->next = pin;
weights += pin->GetW();
}
}
//查询货物重量
int GetWeight(){
return weights;
}
private:
int weights;
Goods *pHead;
}; void protectA(){
Shop *sp = new Shop();
int indexover = ;
while (indexover){
int num = ;
int w = ;
cout << "请输入操作:" << endl;
cout << "1购进货物" << endl;
cout << "2卖出货物" << endl;
cout << "3查看现有货物重量" << endl;
cout << "按任意键退出" << endl;
cin >> num;
switch (num)
{
case :
cout << "请输入货物的重量" << endl;
//备注:在c++中不可以在case语句里定义任意变量,如果非要定义,请外面套上大括号{}
//int ss = 0;
//报错 : error C2360: “ss”的初始化操作由“case”标签跳过
{
cin >> w;
Goods * g1 = new Goods(w);
sp->Purchase(g1);
}
break;
case :
{
sp->SellOut();
}
break;
case :
cout << "现有货物的重量是" << sp->GetWeight() << endl;
break;
default:
indexover = ;
break;
}
}
if (sp!=NULL)
{
delete sp;
}
}
void main(){
protectA();
system("pause");
}

最新文章

  1. mysql sum 和 count 函数 合并使用
  2. 初始Java DVD项目
  3. [CareerCup] 13.5 Volatile Keyword 关键字volatile
  4. python分析log
  5. mongod的主要参数解释
  6. 高级UIKit-06(UIImagePickerController)
  7. 调用函数的ALV、面向对象的ALV设置带选择列
  8. C# Dictionary.Add(key,value) 与 Dictionary[key]=value的区别
  9. SSIS从理论到实战,再到应用(2)----SSIS包的控制流
  10. win10新特性,ubuntu子系统(安装及配置)
  11. Angular环境准备和Angular cli
  12. 【spring源码分析】IOC容器初始化(总结)
  13. VisualStudio神级插件Resharper技巧基础入门到骨灰玩家使用全教程+Resharper性能优化
  14. spring boot 全局配置属性一览
  15. 今天遇到一个关于栈溢出的问题StackOverflowError
  16. js 判断数组中是否有某值
  17. hdu 4333&quot;Revolving Digits&quot;(KMP求字符串最小循环节+拓展KMP)
  18. Lodop调整打印项输出顺序 覆盖与层级
  19. java sigar.jar
  20. vue-cli 使用 font-awesome 字体插件

热门文章

  1. 在Form中调用请求并直接打印结果
  2. ubuntu开启ftp服务
  3. ubuntu16.4安装后做的事情
  4. python 的集合 set()操作
  5. EMQ ---websocket
  6. jenkins二
  7. Python 列表 max() 方法
  8. MySQL主从不一致情形与解决方法
  9. Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh
  10. [c#]分析器错误消息: 发现不明白的匹配。