日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

基于C++實(shí)現(xiàn)信息管理系統(tǒng)_C 語(yǔ)言

作者:qq_996852067 ? 更新時(shí)間: 2022-05-21 編程語(yǔ)言

基于c++設(shè)計(jì)的信息管理系統(tǒng),供大家參考,具體內(nèi)容如下

1、使用類+函數(shù)實(shí)現(xiàn)
2、使用STL容器的vector
3、fstream的文件存儲(chǔ)方式
4、xls文件讀入 寫出
5、數(shù)據(jù)的四大功能增刪改查
6、一定的輸入容錯(cuò)能力

#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 << "重新輸入梁山好漢姓名 長(zhǎng)度[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 << "重新輸入梁山好漢籍貫 長(zhǎng)度[0 - 20]" << endl;
? ? ? ? return FALSE;
? ? }
? ? else
? ? {
? ? ? ? strcpy(loc, _loc);
? ? ? ? return TRUE;
? ? }
}

int LiangshanHeros::setprice(double the_Bouney)
{
? ? if (the_Bouney < 0)
? ? {
? ? ? ? cout << "重新輸入價(jià)格 大小[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& 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& 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& 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 << "請(qǐng)輸入需要修改變量的值:" << 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 << "還未錄入數(shù)據(jù)" << 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 << "數(shù)據(jù)錄入成功 存儲(chǔ)于 ./file.xls中" << endl;

? ? ofs.close();
}

void outputFile(vector& vec)
{
? ? ifstream ifs;
? ? ifs.open(PATH, ios::binary | ios::in);
? ? if (ifs.fail()) {

? ? ? ? cout << "文件未創(chuàng)建 請(qǐng)先錄入數(shù)據(jù)" << endl;
? ? ? ? return;
? ? }
? ? int i = vec.size();
? ? int age;
? ? double price;
? ? char loc[Num] = { 0 };
? ? char name[Num] = { 0 };

? ? //判斷是否為文件結(jié)尾
? ? while (!ifs.eof())
? ? {
? ? ? ? LiangshanHeros* par = new LiangshanHeros;
? ? ? ? ifs >> par->name >> par->age >> par->loc >> par->bounty;
? ? ? ? vec.push_back(par);
? ? }

? ? cout << "file.xls 文件讀入成功 ?數(shù)據(jù)已寫入" << 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基于梁山好漢的文件存儲(chǔ)系統(tǒng)\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");//控制臺(tái) 寬度135 高度20

? ? HANDLE 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;
}

原文鏈接:https://blog.csdn.net/weixin_43811333/article/details/109656273

欄目分類
最近更新