c/c++语言开发共享C++实现宠物商店信息管理系统

本文实例为大家分享了c++实现宠物商店信息管理系统的具体代码,供大家参考,具体内容如下一、问题描述设计一个程序实现对小动物商店的简单管理,主要功能:宠物基本信息(编号,名称,体重, 年龄,类别,价格,

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

一、问题描述

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

二、基本要求

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

(2)输入和输出可以使用文c/c++开发分享C++实现宠物商店信息管理系统件重定向输入(保存数据为磁盘文件);也可以使用标准输入输出进行(提交时需要提交txt格式输入数据)。程序运行时进行初始化数据,使用vector数组存放。交易数据记录交易的日期、宠物名称、宠物类别、顾客姓名、交易金额等,有6条以上记录。

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

系统流程图

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

源代码

#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;  }

以上就是c/c++开发分享C++实现宠物商店信息管理系统的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

需要了解更多c/c++开发分享C++实现宠物商店信息管理系统,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1082249.html

(0)
上一篇 2022年4月24日
下一篇 2022年4月24日

精彩推荐