实现方法–结构体数组
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stack>
#include <cstring>
#include <conio.h>
#include <math.h>
#include <regex>
#include <fstream>
#include <cstdlib>
#define MAX 101
using namespace std;
int len = 0;
//按照年龄排序后的数据输出所用到的结构体
struct Data
{
string name,outstr;
int age;
};
//病人结构体
struct People
{
int id,age;
string name,sex,address,phone,mail,cdt,doc;
};
// 疫苗结构体
struct YiMiao
{
string name,sex,phone, date;
int age,type;
};
// 定义结构体数组 Data用于展示数据,存储年龄进行降序后匹配输出
struct Data d[MAX];
// 写入txt--病人数据
void writetotxt(string filename, struct People *p, int n)
{
ofstream outfile;
outfile.open(filename, ios::app);
if (filename == "Record1.txt")
{
outfile << p[n].name << ' ' << p[n].sex << ' ' << p[n].age << ' ' << p[n].address << ' ' << p[n].phone << ' ' << p[n].mail << ' ' << p[n].cdt << ' ' << p[n].doc << endl;
}
outfile.close();
}
// 写入txt--疫苗数据
void writetotxt2(string filename, struct YiMiao *p, int n)
{
ofstream outfile;
outfile.open(filename, ios::app);
if (filename == "Record2.txt")
{
outfile << p[n].name << ' ' << p[n].sex << ' ' << p[n].age << ' ' << p[n].date << ' ' << p[n].phone << ' ' << p[n].type << endl;
}
outfile.close();
}
// 找出年龄的数据
vector<string> ParseLine(const string &line)
{
vector<string> v;
int pos = 0;
int pos2 = 0;
int len = line.length();
while (pos < len)
{
pos2 = line.find(" ", pos);
if (pos2 == -1)
pos2 = len;
v.push_back(line.substr(pos, pos2 - pos));
pos = pos2 + 1;
}
return v;
}
// 登录
void login()
{
printf("######################################\n");
printf("# 欢迎使用新冠管理系统 #\n");
printf("######################################\n");
printf("按任意键继续……\n");
printf("--------------------------------------------\n新冠管理系统\n--------------------------------------------\n");
printf("输入用户名及密码:\n");
char psw[5] = "1234", ipsw[5];
string uname = "admin", iuname;
int flag_1 = 0;
while (!flag_1)
{
printf("用户名:");
cin >> iuname;
printf("密码:");
for (int i = 0; i < 4; i++)
{
ipsw[i] = getch();
if (ipsw[i] >= '0' || ipsw[i] <= '9' || ipsw[i] >= 'a' || ipsw[i] <= 'z' || ipsw[i] >= 'A' || ipsw[i] <= 'Z')
{
putchar('*');
}
}
if (ipsw[0] == '1' && ipsw[1] == '2' && ipsw[2] == '3' && ipsw[3] == '4' && uname.compare(iuname) == 0)
{
flag_1 = 1;
break;
}
if (flag_1 == 0)
{
printf("\n用户名或密码错误,请重新输入:\n");
}
}
printf("\n登录成功,按任意键继续……\n");
}
// 菜单
int meau()
{
int n;
printf("--------------------------------------------\n新冠管理系统\n--------------------------------------------\n");
printf("1.新增患者记录\n");
printf("2.统计患者信息\n");
printf("3.查询患者信息\n");
printf("4.新增疫苗接种记录\n");
printf("5.退出系统\n");
printf("请选择相应的功能(1-5):\n");
cin >> n;
while (n < 1 || n > 5)
{
printf("输入无效,请重新选择:\n");
printf("请选择相应的功能(1-5):\n");
cin >> n;
}
return n;
}
// 新增信息
int message(struct People *p, int n)
{
printf("--------------------------------------------\n新冠管理系统\n--------------------------------------------\n!!!!!!!!!!!!!!新增一条患者记录!!!!!!!!!!!!!!\n");
char name[MAX],add[MAX];
int age;
string sex,phone,mail;
char cdt[MAX]; // 症状
char doc[MAX]; // 主治医生
// 姓名
printf("姓名:");
cin >> name;
while (strlen(name) < 4 || strlen(name) > 12)
{
printf("输入无效,姓名包含 2-6 个字符。请重新输入!\n");
printf("姓名:");
cin >> name;
}
p[n].name = name;
// 性别
printf("性别[男/女]:");
string male = "男", female = "女";
string::size_type pos1, pos2;
cin >> sex;
pos1 = sex.find(male);
pos2 = sex.find(female);
while (pos1 == string::npos && pos2 == string::npos)
{
printf("输入无效,性别只能输入为“男”或“女”。请重新输入!\n");
printf("性别[男/女]:");
cin >> sex;
pos1 = sex.find(male);
pos2 = sex.find(male);
}
p[n].sex = sex;
// 年龄
printf("年龄:");
cin >> age;
while (age < 1 || age > 120)
{
printf("输入无效,年龄限定在 1-120 之间。请重新输入!\n");
printf("年龄:");
cin >> age;
}
p[n].age = age;
// 住址
printf("住址:");
cin >> add;
while (strlen(add) < 4 || strlen(add) > 20)
{
printf("输入无效,住址只能包含 2-10 个字。请重新输入!\n");
printf("住址:");
cin >> add;
}
p[n].address = add;
// 电话
printf("电话:");
cin >> phone;
while (phone.size() != 11)
{
printf("输入无效,手机只能由 11 个数字组成。请重新输入!\n");
printf("电话:");
cin >> phone;
}
p[n].phone = phone;
// 邮箱
string re = "([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3})";
regex rule(re);
printf("邮箱:");
cin >> mail;
while (!regex_match(mail, rule))
{
printf("输入无效,邮箱必须满足 xxx@xxx.com 的格式。请重新输入!\n");
printf("邮箱:");
cin >> mail;
}
p[n].mail = mail;
// 症状
printf("症状:");
cin >> cdt;
while (strlen(cdt) < 4 || strlen(cdt) > 30)
{
printf("输入无效,症状只能包含 2-15 个字。请重新输入!\n");
printf("症状:");
cin >> cdt;
}
p[n].cdt = cdt;
// 主治医生
printf("主治医生:");
cin >> doc;
while (strlen(doc) < 4 || strlen(doc) > 12)
{
printf("输入无效,主治医生只能包含 2-6 个字。请重新输入!\n");
printf("主治医生:");
cin >> doc;
}
p[n].doc = doc;
writetotxt("Record1.txt", p, n);
printf("……………患者信息录入成功……………\n");
char s;
printf("是否继续录入[Y/N]:");
cin >> s;
if (s == 'Y')
{
message(p, n + 1);
}
else if (s == 'N')
{
printf("感谢使用!\n");
}
return n + 1; // 返回患者人数
}
// 展示出年龄降序后的数据
void PrintData(struct Data *d, int age[MAX], int len)
{
printf("姓名 性别 年龄 住址 手机 邮箱 症状 主治医生\n");
printf("========================================================================\n");
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (age[i] == d[j].age)
{
cout << d[j].outstr << endl;
}
}
}
}
// 年龄降序主函数 分析函数
void Analyse()
{
int age[MAX];
int i = 0;
printf("--------------------------------------------\n");
printf("新冠管理系统\n--------------------------------------------\n");
printf("!!!!!!!!!!!!!!!!患者信息统计!!!!!!!!!!!!!!!!\n");
ifstream infile("Record1.txt", ios::in);
string s;
if (!infile.fail())
{
while (getline(infile, s))
{
vector<string> string = ParseLine(s);
age[i] = stoi(string[2]);// 对字符串分割后,下标是2对应年龄数据
//存进Data结构体
d[i].age = age[i];
d[i].name = string[0];
d[i].outstr = s;
len++;
i++;
}
sort(age, age + len, greater<int>());//年龄降序
PrintData(d, age, len);
}
infile.close();
}
// 查找
void Research()
{
string name;
printf("--------------------------------------------\n");
printf("新冠管理系统\n");
printf("--------------------------------------------\n");
printf("!!!!!!!!!!!!!!!!患者信息查询!!!!!!!!!!!!!!!!\n");
printf("输入患者姓名:");
cin >> name;
bool f = false;
for (int i = 0; i < len; i++)
{
string rname = d[i].name;
if (rname == name)
{
printf("姓名 性别 年龄 住址 手机 邮箱 症状 主治医生\n");
printf("========================================================================\n");
cout << d[i].outstr << endl;
f = true;
break;
}
}
if (f == false)
{
cout << "未查询到该患者信息!" << endl;
}
char s;
cout << "是否继续查询[Y/N]:";
cin >> s;
if (s == 'N')
{
cout << "感谢使用!" << endl;
}
else
{
Research();
}
}
// 疫苗输入数据
int YiMiao(struct YiMiao *y, int k)
{
cout << "--------------------------------------------" << endl;
cout << "新冠管理系统" << endl;
cout << "--------------------------------------------" << endl;
cout << "!!!!!!!!!!!!!!!!疫苗接种系统!!!!!!!!!!!!!!!!" << endl;
string sex, phone, date;
char name[MAX];
int age, type;
//姓名
cout << "接种者姓名:";
cin >> name;
while (strlen(name) < 4 || strlen(name) > 12)
{
printf("输入无效,姓名包含 2-6 个字符。请重新输入!\n");
printf("姓名:");
cin >> name;
}
y[k].name = name;
//性别
string male = "男", female = "女";
string::size_type pos1, pos2;
printf("性别[男/女]:");
cin >> sex;
pos1 = sex.find(male);
pos2 = sex.find(female);
while (pos1 == string::npos && pos2 == string::npos)
{
printf("输入无效,性别只能输入为“男”或“女”。请重新输入!\n");
printf("性别[男/女]:");
cin >> sex;
pos1 = sex.find(male);
pos2 = sex.find(male);
}
y[k].sex = sex;
// 年龄
cout << "年龄:";
cin >> age;
while (age < 1 || age > 120)
{
printf("输入无效,年龄限定在 1-120 之间。请重新输入!\n");
printf("年龄:");
cin >> age;
}
y[k].age = age;
// 接种日期
cout << "接种日期(dd/mm/yy):";
cin >> date;
string re = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{2}";
regex rule(re);
while (!regex_match(date, rule))
{
printf("输入无效,接种日期必须满足 dd/mm/yy 的格式。请重新输入!\n");
printf("接种日期(dd/mm/yy):");
cin >> date;
}
y[k].date = date;
//手机号码
cout << "手机:";
cin >> phone;
while (phone.size() != 11)
{
printf("输入无效,手机只能由 11 个数字组成。请重新输入!\n");
printf("电话:");
cin >> phone;
}
y[k].phone = phone;
// 疫苗种类
cout << "选择疫苗:" << endl
<< "1.国药中生北京" << endl
<< "2.国药中生武汉" << endl
<< "3.北京科兴中维" << endl
<< "请选择(1-3):";
cin >> type;
while (type < 1 || type > 3)
{
printf("输入无效,选择疫苗限定在 1-3 之间。请重新输入!\n");
printf("请选择(1-3)");
cin >> type;
}
y[k].type = type;
writetotxt2("Record2.txt", y, k);// 写入txt
printf("…………国药中生北京疫苗接种信息录入成功!…………\n");
char s;
printf("是否继续录入[Y/N]:");
cin >> s;
if (s == 'Y')
{
YiMiao(y, k + 1);
}
else if (s == 'N')
{
printf("感谢使用!\n");
}
return k;
}
int main()
{
int n, p = 0, yi = 0; // 患者人数
struct People people[MAX]; // 病人
struct YiMiao yimiao[MAX]; // 疫苗
login();
n = meau();// 菜单选择
while (n != 5)
{
if (n == 1)
{
p = message(people, p);
}
if (n == 2)
{
Analyse();
}
if (n == 3)
{
Research();
}
if (n == 4)
{
yi = YiMiao(yimiao, yi);
}
if (n == 5)
{
system("pause");
exit(0);
}
n = meau();
}
}
该代码段展示了一个用C++编写的新冠管理系统,包括患者信息和疫苗接种记录的管理。系统支持添加、查看和排序患者数据,以及录入疫苗接种信息。数据以结构体数组的形式存储,并能按年龄排序输出。
1728

被折叠的 条评论
为什么被折叠?



