服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现商店仓库管理系统

C++实现商店仓库管理系统

2022-10-24 12:34陆鳴笙 C/C++

这篇文章主要为大家详细介绍了C++实现商店仓库管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现商店仓库管理系统的具体代码,供大家参考,具体内容如下

一、问题描述

系统应具有下列主要功能:输入记录功能:从键盘输入货物信息:商品代号,商品名称, 数量,价格,所属类别(如家用电器、日用品等)等;修改商品数量、删除记录功能、按商品代号查询、按商品代号排序并显示等功能。

二、基本要求

(1)使用面向对象编程思想编写开发过程中需要用到的类,使用继承的方法构造至少 3个类(即商品类(虚基类),家用电器类和日用品类(派生类)),另外再设计一个管理类,实现对商品的管理;
(2)输入和输出可以使用文本文件重定向输入(保存数据为磁盘文件);也可以使用标准输入输出进行(提交时需要提交TXT格式输入数据)。包含各类商品信息,程序运行时进行初始化数据,使用vector 数组存放对象指针。并能保存数据为磁盘文件。
(3)程序运行时使用菜单显示添加(输入)记录,修改商品数量,浏览商品信息,按商品代号查找 ,删除记录。
(4)编写同名 display() 成员函数既虚函数,用来输出所有商品的信息。要求对<< 和>>
运算符进行重载,实现信息的输入输出。
(5) 基本功能要求具有增、删、改、查。

基本流程图

C++实现商店仓库管理系统

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <iostream>//基本的输入输出 
#include <fstream> //文件操作 
#include <cstring>//strcmp函数,比较两个字符串 
#include <conio.h>//用getch();
#include <vector>//vector数组 
#define SIZE 100  //采用宏定义,定义char数组的大小 
using namespace std;
class Goods//Goods类定义 
{
    public:
        Goods(){}//无参数无初值的构造函数 ,缺省构造函数 
        char Number[SIZE];//编号 
        char Name[SIZE];//商品名 
        int Amount;//数量 
        float Price;//价格 
        char Type[SIZE];//类别 
        Goods * Next;//指针 
        vector<Goods> Manage; //vector数组的定义 
        friend ostream& operator<<(ostream& out,Goods&  obj)//重载<<输出运算符 
        {
                out<<obj.Number<<obj.Name<<obj.Amount<<obj.Type;
        }
        friend istream& operator>>(istream& in,Goods& obj)//重载>>输入运算符 
        {
                in>>obj.Number>>obj.Name>>obj.Amount>>obj.Type;
        }
        void SetType()//设置商品类别 
        {    cout<<" 请选择种类:";    cin>>Type;}
        void SetName()//设置商品名 
        {    cout<<" 请输入商品的名称:"; cin>>Name;}
        void SetNumber()//设置商品编号 
        {    cout<<"  请输入商品的编号:"; cin>>Number;}
        void SetPrice()//设置类商品价格 
        {cout<<" 请输入商品单价:"; cin>>Price;}
        void SetAmount()//设置商品数量 
        {cout<<" 请输入商品库存:"; cin>>Amount;}
        void SetOther() //设置其他数据 
        {    cout<<"  请输入商品价格:"; cin>>Price;
            cout<<"  请输入存货数量:"; cin>>Amount;}     
        void ReadFile(istream & in)//读取文件 
        {    in>>Name>>Type>>Number>>Price>>Amount;}    
        void SetAll()//成员函数  功能:输入信息 
        {
            SetName();
            SetType();
            SetNumber();
            SetOther();         
        }
        void Show()//输出商品信息 
        {    cout<<"商品名: "<<Name<<endl<<"种类:"<<Type<<endl<<"编号: "<<Number<<endl<<"价格 "<<Price<<endl<<"商品库存: "<<Amount<<endl<<endl;}
};
class LifeGoods:public Goods//生活用品类 
{
    public:
        LifeGoods(){}//构造函数 
        ~ LifeGoods(){} //析构函数 
        char Number[SIZE];//编号 
        char Name[SIZE];//商品名 
        int Amount;//数量 
        float Price;//价格 
};
class electricLifeGoods:public Goods//家用电器类 
{
    electricLifeGoods() {}//构造函数 
    ~electricLifeGoods(){}//析构函数 
    char Number[SIZE];//编号 
    char Name[SIZE];//商品名 
    int Amount;//数量 
    float Price;//价格 
};
class Manage:public Goods//管理类 
{
    public:
        Goods * Head,* End;//定义商品类的头结点和尾节点的指针 
        int i;//记录商品总数目 
        ifstream in;//打开文件输入信息 
        ofstream out;//关闭文件储存信息 
        Manage();//构造函数 
        ~Manage();//析构函数 
        void AddGoods();//添加商品信息 
        void ShowMenu(int n);//显示菜单,参数n用于switch进行增删改查作 
        void FindGoods();//查找商品 
        void SaveGoods();//保存商品信息 
        void ChangeGoods();//修改商品内容 
        void DelGoods();//删除商品信息 
        int ListCount();//计算商品个数
        void Display()//显示所有商品信息 
        {
            system("cls"); //清空屏幕,美观 
            i=0;
            for(Goods * goods=Head->Next;goods!=End;goods=goods->Next)//从头结点循环到尾节点,输出全部的商品信息 
            {
                goods->Show(); //输出每一个结点的各条信息 
                i++;
            }
            cout<<"共有"<<i<<"个商品"<<"\n"<<endl;    
            cout<<"按任意键继续......";
            getch();
        }
        Goods *FindName(char * Name)//按姓名查找 
        {
            for(Goods * goods=Head;goods->Next!=End;goods=goods->Next)//匹配成功则返回上一个指针,不成功就返回空
                if(!strcmp(goods->Next->Name,Name))return goods;
                return NULL;
        }
        Goods *FindNumber(char * Number)//按编号查找 
        {
            for(Goods * goods=Head;goods->Next!=End;goods=goods->Next)//匹配成功则返回上一个指针,不成功就返回空
                if(!strcmp(goods->Next->Number,Number))return goods;
              return NULL;
        }
}; 
void Manage::AddGoods()//从键盘输入商品信息
{
    system("cls");//清空屏幕 
    ShowMenu(1);//调用菜单函数 
    End->SetName();
    End->SetType();
    do
    {End->SetNumber();}while(FindNumber(End->Number));//当编号不为空时输入每一条信息 
    End->SetOther();
    End->Next = new Goods;//开辟新空间,存储新的商品信息 
    End=End->Next;
    cout<<"添加成功!"<<endl;
    SaveGoods();
    cout<<"按任意键继续......";
    getch();
}
Manage::Manage()     //构造函数在类外实现 
{
    Head=new Goods;//开辟一个新的结点,让头指针指向新结点 
    Head->Next=new Goods; 
    End=Head->Next;
    in.open("仓库.txt");//打开仓库文件 
    if(!in)//如果打开失败 
        cout<<"没有库存"<<endl;
    else
    {
        while(!in.eof())//循环读入仓库文件内的数据,直到空为止 
        {
            End->ReadFile(in);
            if(End->Name[0]=='\0')break;//当名字为0是结束读取 
            End->Next=new Goods;
            End=End->Next;
        }
        in.close();//关闭文件 
        cout<<" 读取商品信息成功!"<<endl<<endl;
    }
}
void Manage::ShowMenu(int n)//菜单 ,参数不同调用不同的菜单 
{
    switch(n) //根据n来调用不同的菜单 
    {
        case 1:
        {
            cout<<"************************************************************"<<endl
                <<"*         1、生活用品              2、家用电器             *" <<endl 
                <<"************************************************************"<<endl;
        break;}
        case 2:
        {
        system("cls");
        cout<<"************************************************************"<<endl
            <<"*                   商 店 仓 库 管 理 系 统                *" <<endl 
            <<"************************************************************"<<endl
            <<"*                      1、增 加 商 品                      *" <<endl
            <<"*                      2、显 示 商 品                      *" <<endl
            <<"*                      3、查 找 商 品                      *" <<endl
            <<"*                      4、删 除 商 品                      *" <<endl
            <<"*                      5、修 改 商 品                      *" <<endl
            <<"*                      6、保 存 商 品                      *" <<endl
            <<"*                      0、退 出 系 统                      *" <<endl
            <<"************************************************************"<<endl
            <<endl<<"  请选择(0-7):  ";
        break;}
        case 3:
        {
        system("cls");
        cout<<"************************************************************"<<endl
            <<"*      1、修改商品名             4、修改价格               *" <<endl 
            <<"*      2、修改类别               5、修改编商品量           *" <<endl 
            <<"*      3、修改编号               10、修改全部               *" <<endl 
            <<"************************************************************"<<endl
            <<endl<<"  请选择:  ";
        break;
        }
        case 5:
        {
        system("cls");
        cout<<"************************************************************"<<endl
            <<"*                     1、按商品名查找                      *" <<endl 
            <<"*                     2、按商品编号查找                    *" <<endl 
            <<"************************************************************"<<endl    
            <<endl<<"  请选择:  ";
        break;
        }
    }
}
Manage::~Manage() //析构函数 
{
    for(Goods * temp;Head->Next!=End;) //循环遍历,释放所有的指针 
    {
        temp=Head->Next;
        Head->Next=Head->Next->Next;
        delete temp;
    }
    delete Head,End;
}
void Manage::FindGoods() //查找商品 
{
    system("cls");
    char Name[SIZE] ,Number[10];
    int Input;
    Goods * goods=NULL;//初始化指针 
    ShowMenu(5);//调用菜单 
    cin>>Input;//按姓名或者编号查询 
    switch(Input)
    {
        case 1:{cout<<" 请输入要查找的商品的名称:";cin>>Name;
            if(goods=FindName(Name))
            {    goods->Next->Show();
                   cout<<"按任意键继续......";
                    getch();}
            else{
                cout<<" 没有找到该名称的商品!"<<'\n'<<endl;
                cout<<"按任意键继续......";
                getch();}    }break;
        case 2:
            {     cout<<" 请输入要查找的商品的编号:";cin>>Number;
                if(goods=FindNumber(Number))
                {
                    goods->Next->Show();
                    cout<<"按任意键继续......";
                    getch();
                }
                else{
                    cout<<" 没有找到该编号的商品!"<<'\n'<<endl;
                    cout<<"按任意键继续......";
                    getch();
                }
         }break;
    }        
}
void Manage::ChangeGoods() //修改商品信息
{
    ShowMenu(3);//调用菜单 
    int Input;
    cin>>Input;
    switch(Input)
    {
        case 1:
        {
            char Number[SIZE];
            Goods * goods=NULL;
            cout<<" 请输入要修改的商品的编号:";cin>>Number;
            if(goods=FindNumber(Number))
            {     cout<<" 已找到商品的信息,请输入新的信息!"<<endl;
                goods->Next->SetName();//将新输入的姓名存到磁盘中 
                cout<<"修改成功!"<<endl;
                cout<<"按任意键继续......";
                getch();}
            else{
                    cout<<"  未找到指定商品,请确认后重新查找!"<<endl;
                    cout<<"按任意键继续......";
                    getch();
                }
                break;
            }
        case 2:
        {
            char Number[SIZE];
            Goods * goods=NULL;
            cout<<" 请输入要修改的商品的编号:";cin>>Number;
            if(goods=FindNumber(Number))
            {
                cout<<" 已找到商品的信息,请输入新的信息!"<<endl;
                goods->Next->SetType();//将新输入的类别存到磁盘中 
                cout<<"修改成功!"<<endl;
                    cout<<"按任意键继续......";
                    getch();}
            else{
                cout<<" 未找到指定商品,请确认后重新查找!"<<endl;
                cout<<"按任意键继续......";
                getch();}    break;}
        case 3:
            {
            char Number[SIZE];
            Goods * goods=NULL;
            cout<<" 请输入要修改的商品的编号:";cin>>Number;
            if(goods=FindNumber(Number))
            {
                cout<<" 已找到商品的信息,请输入新的信息!"<<endl;
                goods->Next->SetNumber();//将新输入的编号存到磁盘中 
                cout<<"修改成功!"<<endl;
                cout<<"按任意键继续......";
                getch();
            }
            else{
                cout<<" 未找到指定商品,请确认后重新查找!"<<endl;
                cout<<"按任意键继续......";
                getch();}    break;}
        case 4:
            {
            char Number[SIZE];
            Goods * goods=NULL;
            cout<<" 请输入要修改的商品的编号:";cin>>Number;
            if(goods=FindNumber(Number))
            {
                cout<<" 已找到商品的信息,请输入新的信息!"<<endl;
                goods->Next->SetPrice();//将新输入的价格存到磁盘中 
                cout<<"修改成功!"<<endl;
                cout<<"按任意键继续......";
                getch();}
            else
                {
                    cout<<" 未找到指定商品,请确认后重新查找!"<<endl;
                    cout<<"按任意键继续......";
                    getch();}
                break;
            }
        case 5:
        {
            char Number[SIZE];
              Goods * goods=NULL;
            cout<<" 请输入要修改的商品的编号:";cin>>Number;
            if(goods=FindNumber(Number))
            
                cout<<" 已找到商品的信息,请输入新的信息!"<<endl;
                goods->Next->SetAmount();//将新输入的数量存到磁盘中 
                cout<<"修改成功!"<<endl;
                cout<<"按任意键继续......";
                getch();}
            else
                cout<<" 未找到指定商品,请确认后重新查找!"<<endl;
                cout<<"按任意键继续......";
                getch();}    break;
            }
        }    
}
void Manage::DelGoods() // 删除信息 
{
    system("cls");
    char Number[SIZE];
    Goods * goods=NULL,*temp=NULL;
    cout<<" 请输入要删除的商品的编号:"<<endl;cin>>Number;
    if(goods=FindNumber(Number))//调用 FindNumber()函数按照编号查找,找到后进行删除 
    {
        temp=goods->Next;
        goods->Next=goods->Next->Next; 
        delete temp;
        cout<<" 删除成功!"<<endl;
        cout<<"按任意键继续......";
        getch();
    }
    else
    {
        cout<<" 未找到指定商品,请确认后重新查找!"<<endl;
        cout<<"按任意键继续......";
        getch();
    }
}
int Manage::ListCount() //统计当前链表的记录总数,返回一个整数
{
    if(! Head)
        return 0;
    int n=0;
    for(Goods * goods=Head->Next;goods!=End;goods=goods->Next)//对所有结点进行遍历,遍历结束后n即为总数 
        n++;
    return n;
}
void Manage::SaveGoods() // 将磁盘中的文件写入文本文件中 
{
    out.open("仓库.txt");
    for(Goods *goods=Head->Next;goods!=End;goods=goods->Next)//循环写入 
        out<<goods->Name<<" "<<goods->Type<<" "<<goods->Number<<" "<<goods->Price<<" "<<goods->Amount<<'\n';
    out.close();
    cout<<"信息保存成功"<<endl;
}
int main() //主函数
{
    Manage G;
    cout<<endl<<endl<<endl<<endl
    <<"\t\t\t欢迎进入商品仓库管理系统,按任意键继续"<<endl<<endl<<endl<<endl; 
    getch();    
    int Input;
    bool quit =false;
    while(!quit)
    {
        G.ShowMenu(2);
        cin>>Input;
        switch(Input)
        
            case 0:{quit=true;break;}
            case 1:{G.AddGoods();break;}
            case 2:{G.Display();break;}
            case 3:{G.FindGoods();break;}
            case 4:{G.DelGoods();break;}
            case 5:{G.ChangeGoods();break;}
            case 6:{G.SaveGoods();break;}
            }
        }
    return 0; 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/cjl1831050185/article/details/109231553

延伸 · 阅读

精彩推荐
  • C/C++C++之IO类,文件输入输出,string流练习题

    C++之IO类,文件输入输出,string流练习题

    这篇文章主要介绍了C++实现IO类的几道数组练习题,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    Bitdancing11972022-01-04
  • C/C++C语言 fseek(f,0,SEEK_SET)函数案例详解

    C语言 fseek(f,0,SEEK_SET)函数案例详解

    这篇文章主要介绍了C语言 fseek(f,0,SEEK_SET)函数案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    andy89121810602021-12-22
  • C/C++C语言实现通用数据结构之通用集合(HashSet)

    C语言实现通用数据结构之通用集合(HashSet)

    这篇文章主要为大家详细介绍了C语言实现通用数据结构之通用集合,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    swwlqw8212022-03-05
  • C/C++C++中gSOAP的使用详解

    C++中gSOAP的使用详解

    这篇文章主要介绍了C++中gSOAP的使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    time-flies10652022-02-20
  • C/C++一篇文章带你了解C++的KMP算法

    一篇文章带你了解C++的KMP算法

    这篇文章主要介绍了c++ 实现KMP算法的示例,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下,希望能给你带来帮助...

    冯董事长8852021-12-16
  • C/C++Opencv实现联合双边滤波

    Opencv实现联合双边滤波

    这篇文章主要为大家详细介绍了Opencv实现联合双边滤波,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    时光碎了天12112022-01-20
  • C/C++浅析VSCode launch.json中的各种替换变量的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等

    浅析VSCode launch.json中的各种替换变量的意思 ${workspaceFolder} ${fi

    这篇文章主要介绍了VSCode launch.json中的各种替换变量的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等,非常不错具有一定的参考借鉴价值,需要的朋友...

    bat674452021-08-27
  • C/C++Linux网络编程之基于UDP实现可靠的文件传输示例

    Linux网络编程之基于UDP实现可靠的文件传输示例

    这篇文章主要介绍了Linux网络编程之基于UDP实现可靠的文件传输示例,是很实用的技巧,需要的朋友可以参考下...

    C语言程序设计12762021-01-28