zoukankan      html  css  js  c++  java
  • 货品的进出库模型

    货品包括货号和重量属性,实现简单的入库出库统计功能,实例用于加深指针和链表的认识以及类的使用。

    #include <iostream>
    #include <string>
    using namespace std;
    class StockWeight
    {
        public:
            StockWeight(int c,int w = 0):weight(w),code(c)
            {
                totalWeight += w;//入库总重量增加
                cout << c << "stock input! total " << totalWeight << endl;
            }
            ~StockWeight()
            {
                totalWeight -= weight;//出库析构总重量减少
                cout << code << "stock output! left " << totalWeight << endl;
            }
            int getCode()
            {
                return code;//返回货号
            }
            static int getTotalWeight()
            {
                return totalWeight;//返回总重量
            }
            StockWeight *next;
        private:
            int weight;//重量
            int code;//货号
            static int totalWeight;//静态成员 总共的重量
    };
    int StockWeight::totalWeight = 0;
    void inputWeight(StockWeight *&head,int code,int w)
    {
        StockWeight *g,*p = new StockWeight(code, w);//新对象结点
        p->next = NULL;
        if (head == NULL){head = p;return;}//链表没有内容
        //循环将新结点插入到最后
        g = head;
        while (g->next != NULL)
        {
            g = g->next;//指针挪动
        }
        g->next = p;
    }
    void outputWeight(StockWeight *&head, int code)
    {
        StockWeight *g,*p;
        if (head == NULL){cout << " there is no goods!" << endl; return;}//链表没有内容
        g = head;//跟踪指针
        while (g->getCode() != code && g->next != NULL)//如果当前指针的code不等于特定的code 或者链表已经没有下一结点便停止循环
        {
            g = g->next;//指针挪动
        }
        if (g->getCode() != code) { cout << " there is no goods code " << code << endl; return; }//没找到货品code
        //找到了code
        //第一个结点特殊处理
        if (g == head){
            head = head->next;//头指针改变
            delete g;//出库析构
            return;
        }
        //遍历找出它的上结点
        p = head;
        while (p->next != g)
        {
            p = p->next;//指针挪动
            break;
        }
        p->next = g->next;//重指向
        delete g;//出库析构
    }
    void main()
    {
        StockWeight *head = NULL;//链表头指针
        int leng,code,weight;//leng是指令。。code是货号。。weight是重量
        do
        {
            cin >> leng;//输入指令
            switch (leng)
            {
                case 1://入货
                {
                    cout << "inputWeight:" << endl;//入库
                    cout << "code:";
                    cin >> code;//输入货号
                    cout << "weight:";
                    cin >> weight;//输入重量
                    inputWeight(head, code, weight);//调用方法入库
                    break;
                }
                case 2://出货
                {
                    cout << "outputWeight:" << endl;//出库
                    cout << "code:";
                    cin >> code;//输入货号
                    outputWeight(head, code);//调用方法出库
                    break;
                }
                case 3://输出总重量
                {
                    cout << "total Weight:" << StockWeight::getTotalWeight() << endl;
                    break;
                }
                default:
                    break;
            }
        } while (leng);//无限循环知道输入0
        getchar();
        getchar();
    }
  • 相关阅读:
    WPF 关于拖拽打开文件的注意事项
    asp.net core 3.1中对Mongodb BsonDocument的序列化和反序列化支持
    用百度webuploader分片上传大文件
    多线程学习笔记
    web.config数据库连接字符串加密
    Visual Studio 2010 常用快捷方式
    Team Foundation Server 2013 日常使用使用手册(四)分支与合并
    Team Foundation Server 2013 日常使用使用手册(三)上传新工程、创建任务、创建bug、设置预警
    Team Foundation Server 2013 日常使用使用手册(二)修改、签入、撤销、回滚、对比代码变更
    Team Foundation Server 2013 日常使用使用手册(一)-本地连接TFS、查看任务
  • 原文地址:https://www.cnblogs.com/godehi/p/8320862.html
Copyright © 2011-2022 走看看