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

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

服务器之家 - 编程语言 - C/C++ - C++实现宠物商店信息管理系统

C++实现宠物商店信息管理系统

2022-10-25 13:31陆鳴笙 C/C++

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

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

一、问题描述

设计一个程序实现对小动物商店的简单管理,主要功能:宠物基本信息(编号,名称,体重, 年龄,类别,价格,性格等)的输入、显示、查询等功能;宠物的交易、状态及顾客(宠物主人)的记录查询和修改。

二、基本要求

(1)使用面向对象思想开发需要的类,比如:宠物类包含宠物的基本属性信息和基本操作,日期类记录交易日期,顾客类记录顾客的信息;管理类,实现对宠物情况的操作;输入和输出的操作要求对输出运算符“>>”和输出运算符“<<”进行重载

(2)输入和输出可以使用文本文件重定向输入(保存数据为磁盘文件);也可以使用标准输入输出进行(提交时需要提交TXT格式输入数据)。程序运行时进行初始化数据,使用vector数组存放。交易数据记录交易的日期、宠物名称、宠物类别、顾客姓名、交易金额等,有6条以上记录。

(3)运行后使用菜单功能显示所有宠物信息,根据类别显示记录,根据名称查询记录,添加( 购入) 宠物,删除(卖出)宠物,交易记录,按日期查询交易记录。

系统流程图

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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#include<iostream> 
#include<cstring>
#include<vector>
#include<fstream>
#include"list"
using namespace std;
class Data// 日期类 
{  
public:
    void set_time( );     
    void show_time( );    
private:   
    bool is_time(int, int, int);  
    int year;  
    int month;  
    int day;  
}; 
void Data::set_time( )   
{  
    char c1,c2;  
    cout<<"请输入日期(格式年-月-日)"<<endl;  
    while(1)  
    {   
        cin>>year>>c1>>month>>c2>>day;  
        if(c1!='-'||c2!='-')  
            cout<<"格式不正确,请重新输入"<<endl;   
        else   
            break;  
    }  
}  
void Data::show_time( )        
{   
    cout<<year<<"-"<<month<<"-"<<day<<endl;  
class Pet
{
public:
  PetAnimals(){}
  PetAnimals( string Number, string Name, string Age, string Weight, string Type,
            string Nature, string Price, string People )
{
    Cnumber=Number;//宠物编号:00,01,02 ...
    Cname=Name;//宠物名称:贝贝 
    Cage=Age;//宠物年龄:20,14
    Cweight=Weight;//宠物重量(斤):20,45 
    Ctype=Type;//宠物种类:cat or dog... 
    Cnature=Nature;//宠物性格:clver,ferocious...
    Cprice=Price;//宠物价格:20...
    Cpeople=People;//宠物主人:小明… 
 
    string Cnumber;
    string Cname;
    string Cage;
    string Cweight;
    string Ctype;
    string Cnature;
    string Cprice;
    string Cpeople;
 };
class guest
{
    public:
        string Cnumber;
        string Cname;
        string Cage;
        string Cweight;
        string Ctype;
        string Cnature;
        string Cprice;
        string Cpeople;
};
class PetAnimals:public Pet
{
    public:
        void Insert();//添加宠物信息
        bool Look();//查找宠物信息
        bool Change();//修改宠物信息
        void Show();//显示或浏览所有宠物信息
        bool Delete();//删除宠物信息
        void Read();//读取宠物信息文件
        void Write();//写出宠物信息文件
};
list<PetAnimals>PetList;//使用双向链表
//添加宠物信息
void PetAnimals::Insert()
{
    PetAnimals Pet;
    char ch;
    int symbol=0;//判断宠物信息是否存在
    list<PetAnimals>::iterator first,last;
    first=PetList.begin();//begin()指链表开始处
    last=PetList.end();//end()指链表结尾处
do{
    cout<<"【请输入宠物相关信息!】"<<endl;
    cout<<"编号:";
    cin>>Pet.Cnumber;
    cout<<"名称:";
    cin>>Pet.Cname;
    cout<<"年龄:";
    cin>>Pet.Cage;
    cout<<"重量:";
    cin>>Pet.Cweight;
    cout<<"种类:";
    cin>>Pet.Ctype;
    cout<<"性格:";
    cin>>Pet.Cnature;
    cout<<"价格:";
    cin>>Pet.Cprice;
    cout<<"主人:";
    cin>>Pet.Cpeople;
    for( ; first != last ; ++first )
    {
       if((Pet.Cname==(*first).Cname)&&(Pet.Cprice==(*first).Cprice)
          &&(Pet.Ctype==(*first).Ctype))//假设宠物可以重名
         {
            symbol=1;// 如果存在此宠物 
            cout<<endl<<"★该宠物已经存在!"<<endl;
            cout<<"编号:"<<(*first).Cnumber<<endl;
            cout<<"名称:"<<(*first).Cname<<endl;
            cout<<"年龄:"<<(*first).Cage<<endl;
            cout<<"重量:"<<(*first).Cweight<<endl;
            cout<<"种类:"<<(*first).Ctype<<endl;
            cout<<"性格:"<<(*first).Cnature<<endl;
            cout<<"价格:"<<(*first).Cprice<<endl;
            cout<<"主人:"<<(*first).Cpeople;              
            }
      }
    if(symbol==0)//如果不存在此宠物 
    {
        PetList.insert(PetList.end(),Pet);
      }
    cout<<endl<<"★继续添加宠物信息[Y或N]?";
    cin>>ch;
    }
while(ch=='Y'||ch=='y');
 }
 
//查找宠物信息
bool PetAnimals::Look()
{
    string name,price,type;
    int symbol=0;
    int option;
    list <PetAnimals>::iterator first,last;
 do
  {
     cout<<"\t【请输入你查找的方式】!"<<endl;
     cout<<"\t1.按名称查找"<<endl;
     cout<<"\t2.按价格查找"<<endl;
     cout<<"\t3.按种类查找"<<endl;
     cout<<"\t4.退出!"<<endl;
     cin>>option;
    switch(option)
      {
         case 1: cout<<"请输入名称:";
         cin>>name;break;
         case 2: cout<<"请输入价格:";
         cin>>price;break;
         case 3: cout<<"请输入种类:";
         cin>>type;break;
         case 4:break;
       }
        first=PetList.begin();
        last=PetList.end();
    for(;first!=last;++first)
       {
          if((name==(*first).Cname)&&(option==1))
            {
                symbol=1;
                cout<<"★宠物名称为"+(*first).Cname+"宠物信息如下:"<<endl;
                cout<<"编号:"+(*first).Cnumber<<endl;
                cout<<"名称:"+(*first).Cname<<endl;
                cout<<"年龄:"+(*first).Cage<<endl;
                cout<<"重量:"+(*first).Cweight<<endl;
                cout<<"种类:"+(*first).Ctype<<endl;
                cout<<"性格:"+(*first).Cnature<<endl;
                cout<<"价格:"+(*first).Cprice<<endl;
                cout<<"主人:"+(*first).Cpeople<<endl;
            }
          if((price==(*first).Cprice)&&(option==2))
            {
                symbol=1;
                cout<<"★宠物价格为"+(*first).Cprice+"宠物信息如下:"<<endl;
                cout<<"编号:"+(*first).Cnumber<<endl;
                cout<<"名称:"+(*first).Cname<<endl;
                cout<<"年龄:"+(*first).Cage<<endl;
                cout<<"重量:"+(*first).Cweight<<endl;
                cout<<"种类:"+(*first).Ctype<<endl;
                cout<<"性格:"+(*first).Cnature<<endl;
                cout<<"价格:"+(*first).Cprice<<endl;
                cout<<"主人:"+(*first).Cpeople<<endl;
            }
          if((type==(*first).Ctype)&&(option==3))
            {
                symbol=1;
                cout<<"★宠物种类为"+(*first).Ctype+"宠物信息如下:"<<endl;
                cout<<"编号:"+(*first).Cnumber<<endl;
                cout<<"名称:"+(*first).Cname<<endl;
                cout<<"年龄:"+(*first).Cage<<endl;
                cout<<"重量:"+(*first).Cweight<<endl;
                cout<<"种类:"+(*first).Ctype<<endl;
                cout<<"性格:"+(*first).Cnature<<endl;
                cout<<"价格:"+(*first).Cprice<<endl;
                cout<<"主人:"+(*first).Cpeople<<endl;
            }
        }
 }
while(option!=4);
if((first==last)&&(symbol==0))
  {
    cout<<"★没有该宠物信息!";
     return false;}
    else
     return true;
    }
 
//修改宠物资料
bool PetAnimals::Change()
{
    PetAnimals pet;
    string name,price,type;
    int symbol=0;
    cout<<"请输入名称:";
    cin>>name;
    cout<<"请输入价格:";
    cin>>price;
    cout<<"请输入种类:";
    cin>>type;
    list <PetAnimals>::iterator first,last;
    first=PetList.begin();
    last=PetList.end();
    for(;first!=last;++first)
    {
        if((name==(*first).Cname)&&(price==(*first).Cprice)&&(type==(*first).Ctype))
        {
            symbol=1;
            cout<<endl<<"★该宠物信息找到,其修改前的宠物信息为:"<<endl;
            cout<<"编号:"+(*first).Cnumber<<endl;
            cout<<"名称:"+(*first).Cname<<endl;
            cout<<"年龄:"+(*first).Cage<<endl;
            cout<<"重量:"+(*first).Cweight<<endl;
            cout<<"种类:"+(*first).Ctype<<endl;
            cout<<"性格:"+(*first).Cnature<<endl;
            cout<<"价格:"+(*first).Cprice<<endl;
            cout<<"主人:"+(*first).Cpeople<<endl;
            break;
        }
    }
   if(symbol)
    {
        cout<<endl<<"★修改后的宠物信息为:"<<endl;
        cout<<"年龄:";
        cin>>pet.Cage;
        cout<<"重量:";
        cin>>pet.Cweight;
        cout<<"性格:";
        cin>>pet.Cnature;
        cout<<"主人:";
        cin>>pet.Cpeople;
        pet.Cname=name;
        pet.Cprice=price;
        pet.Ctype=type;
        for(;first!=last;++first)
         {
            if((name==(*first).Cname)&&(price==(*first).Cprice)&&(type==(*first).Ctype))
              {
                (*first)=pet;
              }
         }
            return true;
    }
            else
             {
              cout<<"★没有该宠物信息!";
            return false;
             }
}
 
//显示所有宠物信息
void PetAnimals::Show()
{
    list <PetAnimals>::iterator first,last,it;
    first=PetList.begin();
    last=PetList.end();
    for(;first!=last;++first)
    {
        cout<<"******************您的宠物信息**********************"<<endl;
        cout<<"编号:"<<(*first).Cnumber<<endl;
        cout<<"名称:"<<(*first).Cname<<endl;
        cout<<"年龄:"<<(*first).Cage<<endl;
        cout<<"重量:"<<(*first).Cweight<<endl;
        cout<<"种类:"<<(*first).Ctype<<endl;
        cout<<"性格:"<<(*first).Cnature<<endl;
        cout<<"价格:"<<(*first).Cprice<<endl;
        cout<<"主人:"<<(*first).Cpeople<<endl;
        cout<<"****************************************"<<endl; 
    }
}
 
//删除宠物信息
bool PetAnimals::Delete()
{
    string name,price,type;
    int symbol=0;
    cout<<"请输入名称:";
    cin>>name;
    cout<<"请输入价格:";
    cin>>price;
    cout<<"请输入种类:";
    cin>>type;
    list <PetAnimals>::iterator first,last,it;
    first=PetList.begin();
    last=PetList.end();
    for(;first!=last;++first)
    {
        if((name==(*first).Cname)&&(price==(*first).Cprice)&&(type==(*first).Ctype))
        {
            symbol=1;
            cout<<"★找到该宠物信息!可删除!"<<endl;
            it=first;
            PetList.erase(first);
        }
    }
    if((first==last)&&(symbol==0))
    {
       cout<<"★没有该宠物信息!";
       return false;}
    else
    {
       PetList.erase(it); 
       return true;
    }
}
 
//保存宠物信息
void PetAnimals::Write()
{
    char file[256];
    string FileName;
    cout<<"★请输入文件名:(可以加扩展名!如.txt)";
    //若输入完整路径则在你输入的路径下读取文件,否则到程序所在位置的文件夹中读取
    cin>>FileName;
    if(FileName.find (".")>FileName.length())
     {
        FileName=FileName+".txt";
    
    //把String型的字符串转换成char*型的字符串
    strcpy(file,FileName.c_str());
    ofstream fout(file);
    if(!fout)
     {
        cout<<"★文件写入失败!请检查您的文件名!"<<endl;
         return;
     }
    else
     {
        list <PetAnimals>::iterator first,last;
        first=PetList.begin();
        last=PetList.end();
        for(;first!=last;++first)
         {
            fout<<endl<<"编号:"<<(*first).Cnumber<<endl<<"名称:"<<(*first).Cname<<endl
                   <<"年龄:"<<(*first).Cage<<endl<<"重量:"<<(*first).Cweight<<endl
                   <<"种类:"<<(*first).Ctype<<endl<<"性格:"<<(*first).Cnature<<endl
                   <<"价格:"<<(*first).Cprice<<endl<<"主人:"<<(*first).Cpeople<<endl;
        
        cout<<"★文件保存成功!"<<endl;
     }
    fout.close ();//关闭打开的文件
}
 
//读取宠物信息
void PetAnimals::Read()
{
    char file[256];
    string FileName;
    cout<<"★请输入文件名:(可以加扩展名!如.txt)";
    cin>>FileName;
    if(FileName.find (".")>FileName.length())
     {
        FileName=FileName+".txt";
     }
        strcpy(file,FileName.c_str());
        ifstream fin(file);
        int index;
    if(!fin)
     {
        cout<<"★文件写入失败!请检查您的文件名!"<<endl;
        return ;
      }
    else
    {
        PetList.clear ();
        while(!fin.eof())//判断是否处于结尾 
        {
            PetAnimals pet;
            string str;
            fin>>str;
            index=str.find(":");//要":"后的内容 
            pet.Cnumber =str.substr(index+1);//要":"后的剩下字符串 
            fin>>str;
            index=str.find (":");
            pet.Cname =str.substr(index+1);
            fin>>str;
            index=str.find (":");
            pet.Cage =str.substr(index+1);
            fin>>str;
            index=str.find (":");
            pet.Cweight =str.substr(index+1);
            fin>>str;
            index=str.find (":");
            pet.Ctype =str.substr(index+1);
            fin>>str;
            index=str.find (":");
            pet.Cnature=str.substr(index+1);
            fin>>str;
            index=str.find (":");
            pet.Cprice=str.substr(index+1);
            fin>>str;
            index=str.find (":");
            pet.Cpeople =str.substr(index+1);            
            PetList.insert(PetList.end(),pet);
            
        }
        cout<<"\n"<<"   ★请保护好您的爱宠哦(^。^*)!★ "<<endl;
        cout<<"   ★文件读取成功!             ★"<<endl;
    }
    fin.close();
}
int main()
{
    PetAnimals pet;
    int option;
do
 {
    cout<<endl<<"★★★【欢迎进入宠物商店管理系统! 请选择菜单:】★★★"<<endl;
    cout<<" \t┌-------------------------┐"<<endl; 
    cout<<" \t┊ 1.添加宠物的信息        ┊"<<endl; 
    cout<<" \t┊ 2.查找宠物的信息        ┊"<<endl;
    cout<<" \t┊ 3.修改宠物的信息        ┊"<<endl;
    cout<<" \t┊ 4.显示(浏览)宠物的信息┊"<<endl;
    cout<<" \t┊ 5.删除宠物的信息        ┊"<<endl;
    cout<<" \t┊ 6.保存文件              ┊"<<endl;
    cout<<" \t┊ 7.读取文件              ┊"<<endl;
    cout<<" \t┊ 8.退出系统                  ┊"<<endl;
    cout<<" \t└-------------------------┘\n"<<endl;
    cin>>option;
 switch(option)//选择不同函数功能 
    {
        case 1: { pet.Insert(); break; }
        case 2: { pet.Look(); break; }
        case 3: { pet.Change(); break; }
        case 4: { pet.Show(); break; }
        case 5: { pet.Delete(); break; }
        case 6: { pet.Write(); break; }
        case 7: { pet.Read(); break; }
        case 8: { break ; }
    }
 }
 while(option != 8);
 return 0;
}

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

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

延伸 · 阅读

精彩推荐
  • C/C++C语言实现BMP图像细化处理

    C语言实现BMP图像细化处理

    这篇文章主要为大家详细介绍了C语言实现BMP图像细化处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    傻不拉几的程序员7512022-02-13
  • C/C++C++中静态存储区与栈以及堆的区别详解

    C++中静态存储区与栈以及堆的区别详解

    本篇文章是对C++中静态存储区与栈以及堆的区别进行了详细的分析介绍,需要的朋友参考下...

    C++教程网1812020-12-14
  • C/C++详解C语言fscanf函数读取文件教程及源码

    详解C语言fscanf函数读取文件教程及源码

    这篇文章主要为大家介绍了详解C语言算法fscanf读取文件示例教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步...

    hxj76912022-09-22
  • C/C++C语言数据结构之二叉树详解

    C语言数据结构之二叉树详解

    二叉树(Binary tree)是树形结构的一个重要类型。许多实际问题抽象出来的数据结构往往是二叉树形式。本文将通过示例详细讲解一下二叉树,需要的可以...

    清欢有道9792022-10-14
  • C/C++c语言定时器示例分享

    c语言定时器示例分享

    在linux下开发,使用的是C语言。适用于需要定时的软件开发,以系统真实的时间来计算,它送出SIGALRM信号。每隔一秒定时一次...

    C语言程序设计11612021-01-19
  • C/C++C#如何调用原生C++ COM对象详解

    C#如何调用原生C++ COM对象详解

    这篇文章主要给大家介绍了C#如何调用原生C++ COM对象,在C++中实现C#的接口。文中通过示例代码介绍的很详细,相信对大家的理解和学习会一定的参考借鉴...

    fyter3452021-04-22
  • C/C++C++中图片类型的识别与转换详解方法

    C++中图片类型的识别与转换详解方法

    本文简单的介绍一下C++语言中如何识别图片文件的类型,以及各图片类型之间的转换方法,并提供相关的源码供大家参考,感兴趣的朋友快来看看吧...

    link-初扬7872022-02-23
  • C/C++马尔可夫链算法(markov算法)的awk、C++、C语言实现代码

    马尔可夫链算法(markov算法)的awk、C++、C语言实现代码

    这篇文章主要介绍了马尔可夫链算法(markov算法)的awk、C++、C语言实现代码,需要的朋友可以参考下...

    C++教程网10052021-01-29