基于C++如何实现信息管理系统
基于C++如何实现信息管理系统
这篇“基于C++如何实现信息管理系统”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“基于C++如何实现信息管理系统”文章吧。
1、使用类+函数实现
2、使用STL容器的vector
3、fstream的文件存储方式
4、xls文件读入 写出
5、数据的四大功能增删改查
6、一定的输入容错能力
#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<iostream>#include<windows.h>#include<vector>#include<fstream>#include<iterator>#defineNum20#defineFALSE0#defineTRUE1#definePATH"./file.xls"usingnamespacestd;classLiangshanHeros{public:charname[Num];//梁山好汉姓名intage;//年龄charloc[Num];//籍贯doublebounty;//悬赏金public:LiangshanHeros(){age=18;bounty=6666;}LiangshanHeros(char*_name,int_age,char*_loc,doublethe_bounty){strcpy(name,_name);age=_age;strcpy(loc,_loc);bounty=the_bounty;}~LiangshanHeros(){}public:char*getName();char*getLoc();doublegetBounty();intgetAge();public:intsetName(char*);intsetAge(int);intsetLoc(char*);intsetprice(double);public:voidshowMenu();};classparams:publicLiangshanHeros{public:params(){}~params(){}public:voidInitSet();voidshowMensu();};char*LiangshanHeros::getName(){returnname;}char*LiangshanHeros::getLoc(){returnloc;}doubleLiangshanHeros::getBounty(){returnbounty;}intLiangshanHeros::getAge(){returnage;}intLiangshanHeros::setName(char*_name){if(strlen(_name)>20||strlen(_name)<2){cout<<"重新输入梁山好汉姓名长度[0-20]"<<endl;returnFALSE;}else{strcpy(name,_name);returnTRUE;}}intLiangshanHeros::setAge(int_age){if(_age>100||_age<0){cout<<"重新输入年龄大小[0-100]"<<endl;returnFALSE;}else{age=_age;returnTRUE;}}intLiangshanHeros::setLoc(char*_loc){if(strlen(_loc)>20||strlen(_loc)<2){cout<<"重新输入梁山好汉籍贯长度[0-20]"<<endl;returnFALSE;}else{strcpy(loc,_loc);returnTRUE;}}intLiangshanHeros::setprice(doublethe_Bouney){if(the_Bouney<0){cout<<"重新输入价格大小[0-&]"<<endl;returnFALSE;}else{bounty=the_Bouney;returnTRUE;}}voidsetAll(LiangshanHeros*par){while(1){cout<<"输入梁山好汉姓名:"<<endl;charn[Num]={0};cin>>n;if(par->setName(n)==TRUE)break;}while(1){cout<<"输入年龄:"<<endl;inta;cin>>a;if(par->setAge(a)==TRUE)break;}while(1){cout<<"输入梁山好汉籍贯:"<<endl;charl[Num]={0};cin>>l;if(par->setLoc(l)==TRUE)break;}while(1){cout<<"输入赏金:"<<endl;doublep;cin>>p;if(par->setprice(p)==TRUE)break;}}LiangshanHeros*set(){LiangshanHeros*par=newLiangshanHeros;//dosetsetAll(par);returnpar;}voidsearch(vector<LiangshanHeros*>&vec,char*name){inti=0;intflag=0;for(i=0;i<vec.size();i++){if(strcmp(vec[i]->name,name)==0){cout<<"查找成功"<<endl;cout<<vec[i]->getName()<<endl;cout<<vec[i]->getAge()<<endl;cout<<vec[i]->getLoc()<<endl;cout<<vec[i]->getBounty()<<endl;flag=1;}}//casefaildif(flag==0){cout<<"查找失败"<<endl;}}voiddeletePar(vector<LiangshanHeros*>&vec,char*name){inti=0;intflag=0;for(i=0;i<vec.size();i++){if(strcmp(vec[i]->name,name)==0){cout<<"查找成功"<<endl;cout<<vec[i]->getName()<<endl;cout<<vec[i]->getAge()<<endl;cout<<vec[i]->getLoc()<<endl;cout<<vec[i]->getBounty()<<endl;vec.erase(vec.begin()+i);//spcasei--;cout<<"删除成功"<<endl;flag=1;}}if(flag==0){cout<<"未找到该梁山好汉"<<endl;}}voidchange(vector<LiangshanHeros*>&vec,char*name){inti=0;intflag=0;for(i=0;i<vec.size();i++){if(strcmp(vec[i]->name,name)==0){cout<<"查找成功"<<endl;cout<<vec[i]->getName()<<endl;cout<<vec[i]->getAge()<<endl;cout<<vec[i]->getLoc()<<endl;cout<<vec[i]->getBounty()<<endl;cout<<"请输入需要修改变量的值:"<<endl;cout<<"1.梁山好汉姓名2.年龄3.籍贯4.赏金"<<endl;intchoice;while(1){cin>>choice;if(choice>4||choice<0)continue;elsebreak;}cout<<"输入修改后的值"<<endl;switch(choice){case1:{while(1){cout<<"输入梁山好汉姓名:"<<endl;charn[Num]={0};cin>>n;if(vec[i]->setName(n)==TRUE)break;}break;}case2:{while(1){cout<<"输入年龄:"<<endl;inta;cin>>a;if(vec[i]->setAge(a)==TRUE)break;}break;}case3:{while(1){cout<<"输入梁山好汉籍贯:"<<endl;charl[Num]={0};cin>>l;if(vec[i]->setLoc(l)==TRUE)break;}break;}case4:{while(1){cout<<"输入赏金:"<<endl;doublep;cin>>p;if(vec[i]->setprice(p)==TRUE)break;}break;}}flag=1;}}if(flag==0){cout<<"未找到该梁山好汉"<<endl;}}voidinputFile(vector<LiangshanHeros*>&vec){ofstreamofs;ofs.open(PATH,ios::ate|ios::binary);inti=0;if(vec.size()<0){cout<<"还未录入数据"<<endl;return;}for(i=0;i<vec.size();i++){ofs<<vec[i]->name<<"\t"<<vec[i]->age<<"\t"<<vec[i]->loc<<"\t"<<vec[i]->bounty;ofs<<"\n";}cout<<"数据录入成功存储于./file.xls中"<<endl;ofs.close();}voidoutputFile(vector<LiangshanHeros*>&vec){ifstreamifs;ifs.open(PATH,ios::binary|ios::in);if(ifs.fail()){cout<<"文件未创建请先录入数据"<<endl;return;}inti=vec.size();intage;doubleprice;charloc[Num]={0};charname[Num]={0};//判断是否为文件结尾while(!ifs.eof()){LiangshanHeros*par=newLiangshanHeros;ifs>>par->name>>par->age>>par->loc>>par->bounty;vec.push_back(par);}cout<<"file.xls文件读入成功数据已写入"<<endl;ifs.close();}voidshowAllParam(vector<LiangshanHeros*>&vec){inti=0;for(i=0;i<vec.size();i++){cout<<"这是第"<<i+1<<"位梁山好汉:"<<endl;cout<<"姓名:"<<vec[i]->name<<endl;cout<<"年龄:"<<vec[i]->age<<endl;cout<<"籍贯:"<<vec[i]->loc<<endl;cout<<"赏金:"<<vec[i]->bounty<<endl;cout<<endl;}}voidLiangshanHeros::showMenu(){HANDLEhout=GetStdHandle(STD_OUTPUT_HANDLE);//句柄SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_RED);printf("\t基于梁山好汉的文件存储系统\n");SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED);printf("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n");printf("\t丨1.添加梁山好汉\n");printf("\t丨2.查找梁山好汉\n");printf("\t丨3.删除梁山好汉\n");printf("\t丨4.修改梁山好汉信息\n");printf("\t丨5.读取已存在信息\n");printf("\t丨6.保存信息\n");printf("\t丨7.查阅所有信息\n");printf("\t丨8.退出\n");SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED);printf("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n\t");SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_BLUE);printf("输入您的选择(1-8):");}intmain(){system("modeconcols=135lines=30");//控制台宽度135高度20HANDLEhout=GetStdHandle(STD_OUTPUT_HANDLE);//句柄SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED);LiangshanHeros*par=newLiangshanHeros;par->showMenu();vector<LiangshanHeros*>vec;charname[Num]={0};SetConsoleTextAttribute(hout,FOREGROUND_INTENSITY|FOREGROUND_RED);while(1){intnum;cin>>num;switch(num){case1:{vec.push_back(set());cout<<"添加成功"<<endl;break;}case2:{printf("输入查找的梁山好汉名称:");cin>>name;search(vec,name);break;}case3:{printf("输入删除的梁山好汉名称:");cin>>name;deletePar(vec,name);break;}case4:{printf("输入查找的梁山好汉名称:");cin>>name;change(vec,name);break;}case5:{outputFile(vec);break;}case6:{inputFile(vec);break;}case7:{showAllParam(vec);break;}case8:{cout<<"killprocess.."<<endl;exit(0);break;}}system("pause");system("cls");par->showMenu();}return0;}
以上就是关于“基于C++如何实现信息管理系统”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注恰卡编程网行业资讯频道。