基于C++实现信息管理系统

【基于C++实现信息管理系统】基于c++设计的信息管理系统,供大家参考,具体内容如下
1、使用类+函数实现
2、使用STL容器的vector
3、fstream的文件存储方式
4、xls文件读入 写出
5、数据的四大功能增删改查
6、一定的输入容错能力
基于C++实现信息管理系统
文章图片

#include #include #include #include #include #include #include #include #include #define Num 20#define FALSE 0#define TRUE 1#define PATH "./file.xls"using namespace std; class LiangshanHeros{public:char name[Num]; //梁山好汉姓名int age; //年龄char loc[Num]; //籍贯double bounty; //悬赏金public:LiangshanHeros(){age = 18; bounty = 6666; }LiangshanHeros(char* _name, int _age, char* _loc, double the_bounty){strcpy(name, _name); age = _age; strcpy(loc, _loc); bounty = the_bounty; }~LiangshanHeros(){}public:char* getName(); char* getLoc(); double getBounty(); int getAge(); public:int setName(char*); int setAge(int); int setLoc(char*); int setprice(double); public:void showMenu(); }; class params : public LiangshanHeros {public :params() {}~params() {}public:void InitSet(); void showMensu(); }; char* LiangshanHeros::getName(){return name; }char* LiangshanHeros::getLoc(){return loc; }double LiangshanHeros::getBounty(){return bounty; }int LiangshanHeros::getAge(){return age; }int LiangshanHeros::setName(char* _name){if (strlen(_name) > 20 || strlen(_name) < 2){cout << "重新输入梁山好汉姓名 长度[0 - 20]" << endl; return FALSE; }else{strcpy(name, _name); return TRUE; }}int LiangshanHeros::setAge(int _age){if (_age > 100 || _age < 0){cout << "重新输入年龄 大小[0 - 100]" << endl; return FALSE; }else{age = _age; return TRUE; }}int LiangshanHeros::setLoc(char* _loc){if (strlen(_loc) > 20 || strlen(_loc) < 2){cout << "重新输入梁山好汉籍贯 长度[0 - 20]" << endl; return FALSE; }else{strcpy(loc, _loc); return TRUE; }}int LiangshanHeros::setprice(double the_Bouney){if (the_Bouney < 0){cout << "重新输入价格 大小[0 - &]" << endl; return FALSE; }else{bounty = the_Bouney; return TRUE; }}void setAll(LiangshanHeros* par){while (1){cout << "输入梁山好汉姓名: " << endl; char n[Num] = { 0 }; cin >> n; if (par->setName(n) == TRUE) break; }while (1){cout << "输入年龄: " << endl; int a; cin >> a; if (par->setAge(a) == TRUE) break; }while (1){cout << "输入梁山好汉籍贯: " << endl; char l[Num] = { 0 }; cin >> l; if (par->setLoc(l) == TRUE) break; }while (1){cout << "输入赏金: " << endl; double p; cin >> p; if (par->setprice(p) == TRUE) break; }}LiangshanHeros* set(){LiangshanHeros* par = new LiangshanHeros; //do setsetAll(par); return par; }void search(vector& vec, char* name){int i = 0; int flag = 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; }}//case faildif (flag == 0){cout << "查找失败" << endl; }}void deletePar(vector& vec, char* name){int i = 0; int flag = 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); //sp casei--; cout << "删除成功 " << endl; flag = 1; }}if (flag == 0){cout << "未找到该梁山好汉" << endl; }}void change(vector& vec, char* name){int i = 0; int flag = 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; int choice; while (1){cin >> choice; if (choice > 4 || choice < 0) continue; else break; }cout << "输入修改后的值" << endl; switch (choice){case 1:{while (1){cout << "输入梁山好汉姓名: " << endl; char n[Num] = { 0 }; cin >> n; if (vec[i]->setName(n) == TRUE) break; }break; }case 2:{while (1){cout << "输入年龄: " << endl; int a; cin >> a; if (vec[i]->setAge(a) == TRUE) break; }break; }case 3:{while (1){cout << "输入梁山好汉籍贯: " << endl; char l[Num] = { 0 }; cin >> l; if (vec[i]->setLoc(l) == TRUE) break; }break; }case 4:{while (1){cout << "输入赏金: " << endl; double p; cin >> p; if (vec[i]->setprice(p) == TRUE) break; }break; }}flag = 1; }}if (flag == 0){cout << "未找到该梁山好汉" << endl; }}void inputFile(vector& vec){ofstream ofs; ofs.open(PATH, ios::ate | ios::binary); int i = 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(); }void outputFile(vector& vec){ifstream ifs; ifs.open(PATH, ios::binary | ios::in); if (ifs.fail()) {cout << "文件未创建 请先录入数据" << endl; return; }int i = vec.size(); int age; double price; char loc[Num] = { 0 }; char name[Num] = { 0 }; //判断是否为文件结尾while (!ifs.eof()){LiangshanHeros* par = new LiangshanHeros; ifs >> par->name >> par->age >> par->loc >> par->bounty; vec.push_back(par); }cout << "file.xls 文件读入成功数据已写入" << endl; ifs.close(); }void showAllParam(vector& vec){int i = 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; }}void LiangshanHeros::showMenu(){HANDLE hout = 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):"); }int main(){system("mode con cols=135 lines=30"); //控制台 宽度135 高度20HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); //句柄SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED); LiangshanHeros* par = new LiangshanHeros; par->showMenu(); vector vec; char name[Num] = { 0 }; SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED); while (1){int num; cin >> num; switch (num){case 1:{vec.push_back(set()); cout << "添加成功" << endl; break; }case 2:{printf("输入查找的梁山好汉名称:"); cin >> name; search(vec, name); break; }case 3:{printf("输入删除的梁山好汉名称:"); cin >> name; deletePar(vec, name); break; }case 4:{printf("输入查找的梁山好汉名称:"); cin >> name; change(vec, name); break; }case 5:{outputFile(vec); break; }case 6:{inputFile(vec); break; }case 7:{showAllParam(vec); break; }case 8:{cout << "kill process .. " << endl; exit(0); break; }}system("pause"); system("cls"); par->showMenu(); }return 0; }

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

    推荐阅读