您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇“基于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> #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 set setAll(par); return par; } void search(vector<LiangshanHeros*>& 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 faild if (flag == 0) { cout << "查找失败" << endl; } } void deletePar(vector<LiangshanHeros*>& 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 case i--; cout << "删除成功 " << endl; flag = 1; } } if (flag == 0) { cout << "未找到该梁山好汉" << endl; } } void change(vector<LiangshanHeros*>& 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<LiangshanHeros*>& 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<LiangshanHeros*>& 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<LiangshanHeros*>& 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 高度20 HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄 SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED); LiangshanHeros* par = new LiangshanHeros; par->showMenu(); vector<LiangshanHeros*> 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; }
以上就是关于“基于C++如何实现信息管理系统”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。