C++實現(xiàn)寵物商店信息管理系統(tǒng)
本文實例為大家分享了C++實現(xiàn)寵物商店信息管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、問題描述
設計一個程序?qū)崿F(xiàn)對小動物商店的簡單管理,主要功能:寵物基本信息(編號,名稱,體重, 年齡,類別,價格,性格等)的輸入、顯示、查詢等功能;寵物的交易、狀態(tài)及顧客(寵物主人)的記錄查詢和修改。
二、基本要求
(1)使用面向?qū)ο笏枷腴_發(fā)需要的類,比如:寵物類包含寵物的基本屬性信息和基本操作,日期類記錄交易日期,顧客類記錄顧客的信息;管理類,實現(xiàn)對寵物情況的操作;輸入和輸出的操作要求對輸出運算符“>>”和輸出運算符“<<”進行重載
(2)輸入和輸出可以使用文本文件重定向輸入(保存數(shù)據(jù)為磁盤文件);也可以使用標準輸入輸出進行(提交時需要提交TXT格式輸入數(shù)據(jù))。程序運行時進行初始化數(shù)據(jù),使用vector數(shù)組存放。交易數(shù)據(jù)記錄交易的日期、寵物名稱、寵物類別、顧客姓名、交易金額等,有6條以上記錄。
(3)運行后使用菜單功能顯示所有寵物信息,根據(jù)類別顯示記錄,根據(jù)名稱查詢記錄,添加( 購入) 寵物,刪除(賣出)寵物,交易記錄,按日期查詢交易記錄。
系統(tǒng)流程圖
源代碼
#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()指鏈表結(jié)尾處 do{ ? ? cout<<"【請輸入寵物相關(guān)信息!】"<<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<<"★該寵物已經(jīng)存在!"<<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<<"★繼續(xù)添加寵物信息[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型的字符串轉(zhuǎn)換成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 ();//關(guān)閉打開的文件 } //讀取寵物信息 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())//判斷是否處于結(jié)尾? ?? ??? ?{ ?? ??? ??? ?PetAnimals pet; ?? ??? ??? ?string str; ?? ??? ??? ?fin>>str; ?? ??? ??? ?index=str.find(":");//要":"后的內(nèi)容? ?? ??? ??? ?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<<"★★★【歡迎進入寵物商店管理系統(tǒng)! 請選擇菜單:】★★★"<<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.退出系統(tǒng) ? ? ? ? ? ? ? ? ?┊"<<endl; ?? ?cout<<" \t└-------------------------┘\n"<<endl; ?? ?cin>>option; ?switch(option)//選擇不同函數(shù)功能? ?? ?{ ?? ??? ?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; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu),包括一道AMC相關(guān)的例題演示需要的朋友可以參考下2015-08-08

C++11右值引用和轉(zhuǎn)發(fā)型引用教程詳解