医院管理系统是一个用于管理医院日常运营的系统。
需求分析:
以上是医院管理系统的一些基本功能,根据实际需求还可以进行扩展和优化。
代码实现:
#define _CRT_SECURE_NO_WARNINGS //在VS中使用 避免出现scanf printf不安全的警告
#include
#include
#include
#define MAX_ADMIN 1 //管理员数量上限
#define MAX_DOCTORS 12 //医生总人数上限
#define MAX_DOCTORS_DEPT 3 //单部门医生数量上限
#define MAX_DEPARTMENTS 4 //科室上限
#define MAX_PATIENTS 100 //病人人数上限
#define MAX_MEDICINES 10 //药品数量上限
//每种结构体存储对应的文件名
#define ADMIN_FILE "admin.dat"
#define DOCTOR_FILE "doctor.dat"
#define PATIENT_FILE "patient.dat"
#define MEDICINE_FILE "medicine.dat"
//管理员、医生、患者、药物结构体定义
struct Admin {
char id[50]; //管理员账号密码
char pwd[50];
};
struct Doctor {
char name[50]; //姓名
int sex; //性别
char position[50]; //职位
int id; //职工号
int department; //科室
int age; //年龄
char pwd[50]; //密码
};
struct Patient {
char name[50]; //姓名
int sex; //性别
int registrationNumber; //注册号
int age; //年龄
float totalCost; //总费用
};
struct Medicine {
char name[50]; //姓名
int code; //药品编号
float price; //价格
};
//用于存储管理员医生、患者、药品的数组
struct Admin admins[MAX_ADMIN];
struct Doctor doctors[MAX_DOCTORS];
struct Patient patients[MAX_PATIENTS];
struct Medicine medicines[MAX_MEDICINES];
int numAdmins = 0; //管理员数量
int numDoctors1 = 0, numDoctors2 = 0, numDoctors3 = 0, numDoctors4 = 0; //各科室医生数量
int numDoctors = 0; //医生总数量
int numPatients = 0; //患者数量
int numMedicines = 0; //药品数量
//添加医生信息
void addDoctor() {
int flag = 0;
FILE* fp;
//若医生人数达上限,提示并返回
if (numDoctors >= MAX_DOCTORS) {
printf("医生人数已达到上限!\n");
return;
}
struct Doctor newDoctor;
//输入医生信息
printf("\n输入医生姓名: ");
scanf("%s", newDoctor.name); //输入姓名
//输入并判断性别是否有误
printf("\n输入医生性别: (0:女,1:男)");
scanf("%d", &newDoctor.sex); //输入性别
while(newDoctor.sex != 0 && newDoctor.sex != 1) {
printf("输入的性别有误!请重新输入:\n");
printf("\n输入医生性别: (0:女,1:男)");
scanf("%d", &newDoctor.sex);
}
printf("\n输入医生职位: (医生/护士/临床/助理医师/后勤)");
scanf("%s", newDoctor.position); //输入职位
while (strcmp(newDoctor.position, "医生") != 0 && strcmp(newDoctor.position, "护士") != 0 && strcmp(newDoctor.position, "临床") != 0 && strcmp(newDoctor.position, "助理医师") != 0 && strcmp(newDoctor.position, "后勤") != 0) {
printf("输入的职位错误!请重新输入:\n");
printf("\n输入医生职位: (医生/护士/临床/助理医师/后勤)");
scanf("%s", newDoctor.position);
}
while (!flag) {
printf("\n输入科室 (1-4): ");
scanf("%d", &newDoctor.department); //输入科室
while (newDoctor.department < 1 || newDoctor.department > MAX_DEPARTMENTS) {
//若输入有误,提示并重新输入
printf("输入的科室号错误!请重新输入:\n");
printf("\n输入科室 (1-4): ");
scanf("%d", &newDoctor.department);
}
//查看该部门医生是否达到上限
switch(newDoctor.department) {
case 1: {
if(numDoctors1 < MAX_DOCTORS_DEPT){
//没有达到上限,可继续添加
numDoctors1++;
flag = 1;
}
else {
//若达到上限,提示并重新输入
printf("第%d科室医生已达到上限!请重新输入!\n", newDoctor.department);
continue;
}
break;
}
case 2: {
if(numDoctors2 < MAX_DOCTORS_DEPT){
numDoctors2++;
flag = 1;
}
else {
printf("第%d科室医生已达到上限!请重新输入!\n", newDoctor.department);
continue;
}
break;
}
case 3: {
if(numDoctors3 < MAX_DOCTORS_DEPT){
numDoctors3++;
flag = 1;
}
else {
printf("第%d科室医生已达到上限!请重新输入!\n", newDoctor.department);
continue;
}
break;
}
case 4: {
if(numDoctors4 < MAX_DOCTORS_DEPT){
numDoctors4++;
flag = 1;
}
else {
printf("第%d科室医生已达到上限!请重新输入!\n", newDoctor.department);
continue;
}
break;
}
default: break;
}
break;
}
printf("\n输入医生年龄: ");
scanf("%d", &newDoctor.age); //输入年龄
getchar();
newDoctor.id = numDoctors+1;
printf("您的职工号:%d\n", newDoctor.id);
printf("请输入密码:");
scanf("%s", newDoctor.pwd);
getchar();
//打开医生文件
if((fp = fopen(DOCTOR_FILE, "wb+")) == NULL){
printf("文件打开失败!\n");
return;
}
//信息的更新
doctors[numDoctors] = newDoctor;
numDoctors++;
// printf("sizeof(doctors) = %d\n", sizeof(doctors));
// printf("sizeof(struct Doctor) = %d\n", sizeof(struct Doctor));
//写入文件
fwrite(doctors, sizeof(doctors), 1, fp);
fwrite(&numDoctors, sizeof(int), 1, fp);
fclose(fp);
printf("\n医生添加成功!\n");
}
//展示医生信息
void displayDoctors() {
printf("医生信息:\n");
printf("%-5s\t%-20s\t%s\t%s\t\t%s\t%s\n", "职工号", "姓名", "性别", "职位", "科室", "年龄");
printf("---------------------------------------------------------------------------------\n");
//遍历医生数组,打印信息
for (int i = 0; i < numDoctors; i++) {
printf("%-5d\t%-20s\t%s\t%s\t\t%d\t%d\n", doctors[i].id, doctors[i].name, doctors[i].sex == 0 ? "女" : "男", doctors[i].position, doctors[i].department, doctors[i].age);
}
printf("\n");
}
//添加患者信息
void addPatient() {
//若病人达到上限,提示并返回
if (numPatients >= MAX_PATIENTS) {
printf("病人人数已达到上限!\n");
return;
}
FILE* fp;
struct Patient newPatient;
printf("\n输入病人姓名: ");
scanf("%s", newPatient.name);
//输入并判断是否有误
printf("\n输入病人性别: (0:女,1:男)");
scanf("%d", &newPatient.sex); //输入性别
while(newPatient.sex != 0 && newPatient.sex != 1) {
printf("输入的性别有误!请重新输入:\n");
printf("\n输入病人性别: (0:女,1:男)");
scanf("%d", &newPatient.sex);
}
newPatient.registrationNumber = numPatients + 1; //自动生成注册号
newPatient.totalCost = 0; //总费用初始为0
printf("\n输入病人年龄: ");
scanf("%d", &newPatient.age); //输入年龄
getchar();
if((fp = fopen(PATIENT_FILE, "wb+")) == NULL){
printf("文件打开失败!\n");
return;
}
//信息更新
patients[numPatients] = newPatient;
numPatients++;
fwrite(patients, sizeof(patients), 1, fp);
fwrite(&numPatients, sizeof(int), 1, fp);
fclose(fp);
printf("成功添加患者!注册号: %d\n", newPatient.registrationNumber);
}
//展示患者信息
void displayPatientInformation() {
int registrationNumber;
printf("输入患者注册号: ");
scanf("%d", ®istrationNumber);
if (registrationNumber < 1 || registrationNumber > numPatients) {
//注册号无效,提示并返回
printf("注册号无效!\n");
return;
}
struct Patient patient = patients[registrationNumber - 1]; //用临时变量接收
printf("%-20s\t%s\t%s\t%s\t%s\n", "姓名", "性别", "注册号", "年龄", "总费用");
printf("-------------------------------------------------------------------\n");
printf("%-20s\t%s\t%d\t%d\t%.2f\n\n", patient.name, patient.sex == 0 ? "女" : "男", registrationNumber, patient.age, patient.totalCost);
}
//展示当天所有患者信息
void displayAllPatientInformation(){
printf("今天的所有患者:\n");
printf("%-20s\t%s\t%s\t%s\t%s\n", "姓名", "性别", "注册号", "年龄", "总费用");
printf("-------------------------------------------------------------------\n");
for(int i = 0; i < numPatients; i++){
printf("%-20s\t%s\t%d\t%d\t%.2f\n", patients[i].name, patients[i].sex == 0 ? "女" : "男", patients[i].registrationNumber, patients[i].age, patients[i].totalCost);
}
printf("\n");
}
//添加药品信息
void addMedicine() {
//若达到最大数量,提示并返回
if (numMedicines >= MAX_MEDICINES) {
printf("已达到药品的最大数量!\n");
return;
}
int flag = 0, medicineIndex; //用于判断药品编号是否重复
FILE* fp;
struct Medicine newMedicine;
printf("\n输入药品名称: ");
scanf("%s", newMedicine.name);
do {
printf("\n输入药品编号: ");
scanf("%d", &newMedicine.code);
if(numMedicines == 0) break;
//判断是否重复
for(medicineIndex = 0; medicineIndex < numMedicines; medicineIndex++){
if(newMedicine.code == medicines[medicineIndex].code){
printf("药品编号%d已存在!请重新输入:", newMedicine.code);
break;
}
}
if(medicineIndex >= numMedicines) flag = 1;
} while(!flag);
printf("\n输入药品价格: ");
scanf("%f", &newMedicine.price);
getchar();
if((fp = fopen(MEDICINE_FILE, "wb+")) == NULL){
printf("文件打开失败!\n");
return;
}
//更新药品信息
medicines[numMedicines] = newMedicine;
numMedicines++;
fwrite(medicines, sizeof(medicines), 1, fp);
fwrite(&numMedicines, sizeof(int), 1, fp);
fclose(fp);
printf("\n药物添加成功!\n");
}
//展示所有药品信息
void displayMedicines() {
printf("所有药品:\n");
printf("%-20s %-10s %s\n", "药品名称", "药品编号", "药品价格");
printf("--------------------------------------\n");
for (int i = 0; i < numMedicines; i++) {
printf("%-20s %-10d %.2f\n", medicines[i].name, medicines[i].code, medicines[i].price);
}
printf("\n");
}
//医生给患者开药
void prescribe(){
int registrationNumber, medicineCode, medicineIndex, flag = 0;
FILE* fp;
//根据注册号查找到患者
scanf("%d", ®istrationNumber);
//检测是否有效
if (registrationNumber < 1 || registrationNumber > numPatients) {
printf("注册号无效!\n");
return;
}
printf("\n输入药品编号: ");
scanf("%d", &medicineCode);
//遍历查找该药品
for(medicineIndex = 0; medicineIndex < numMedicines; medicineIndex++){
if(medicines[medicineIndex].code == medicineCode){
flag = 1;
break;
}
}
if(!flag){
printf("未找到药品信息!\n");
return;
}
if((fp = fopen(PATIENT_FILE, "wb+")) == NULL){
printf("文件打开失败!\n");
return;
}
//更新患者费用
patients[registrationNumber - 1].totalCost +=medicines[medicineIndex].price;
//更新文件
fwrite(patients, sizeof(patients), 1, fp);
fwrite(&numPatients, sizeof(int), 1, fp);
fclose(fp);
printf("开药成功!\n");
}
//登录、注册菜单
void loginMenu(){
system("cls");
printf("*************************************************\n");
printf("* 1. 登录 *\n");
printf("* 2. 注册 *\n");
printf("* 3. 返回 *\n");
printf("*************************************************\n");
printf(" 输入你的选择 (1-3): [ ]\b\b");
}
//患者模式
void patientMode(){
int patientChoice;
do {
system("cls");
printf("\n**********************患者菜单**********************\n");
printf("* 1. 挂号(注册) *\n");
printf("* 2. 费用查询(登录) *\n");
printf("* 3. 返回 *\n");
printf("****************************************************\n");
printf(" 输入你的选择 (1-3): [ ]\b\b");
scanf("%d", &patientChoice);
switch(patientChoice) {
case 1: {
//挂号
char y_n; //用于判断是否继续添加
do {
addPatient();
// displayPatientInformation();
printf("是否继续添加?(y/n):");
scanf("%c", &y_n);
while(getchar() != '\n'); //清除多余字符
} while(y_n == 'y' || y_n == 'Y');
break;
}
case 2: {
//费用查询
displayPatientInformation();
break;
}
case 3: break;
default: printf("您的输入有误!请重新输入!\n");break;
}
system("pause");
} while(patientChoice != 3);
}
//医生登录
int doctorLogin(){
int type, index = 0, flag = 0;
do {
system("cls");
//登录菜单
loginMenu();
scanf("%d", &type);
int id;
char pwd[50];
switch(type){
case 1: {
//登录
if(numDoctors == 0){
printf("目前没有医生信息!请到管理员模式中录入医生信息!\n");
break;
}
printf("请输入职工号:");
scanf("%d", &id);
printf("请输入密码:");
scanf("%s", pwd);
for(index = 0; index < numDoctors; index++){
if((doctors[index].id == id) && (strcmp(doctors[index].pwd, pwd) == 0)){
flag = 1;
break;
}
}
if(flag == 0){
printf("用户名或密码有误!请核对后重试!");
break;
}
printf("登录成功!\n");
system("pause");
return 1;
}
case 2: {
//注册
printf("请在管理员模式中添加医生信息!\n");
break;
}
case 3: {
//返回
return 3;
break;
}
default: printf("输入有误,请重新输入:"); break;
}
system("pause");
}while(type != 3);
return type;
}
//医生模式
void doctorMode(){
int op = doctorLogin();
if(op == 3) return;
int doctorChoice;
do {
system("cls");
printf("\n**********************医生菜单**********************\n");
printf("* 1. 查询当天患者信息 *\n");
printf("* 2. 开药 *\n");
printf("* 3. 返回 *\n");
printf("*****************************************************\n");
printf(" 输入你的选择 (1-3): [ ]\b\b");
scanf("%d", &doctorChoice);
switch(doctorChoice){
case 1: {
//查询患者信息
displayAllPatientInformation();
break;
}
case 2: {
//开药
displayMedicines(); //展示药品信息
displayAllPatientInformation();
printf("请输入开药的患者注册号:");
prescribe();
break;
}
case 3: break;
default: printf("您的输入有误!请重新输入!\n"); system("pause"); break;
}
system("pause");
} while(doctorChoice != 3);
}
//管理员注册
void adminRegister(){
if(numAdmins >= MAX_ADMIN){
printf("管理员数量已达上限!\n");
system("pause");
return;
}
FILE* fp;
struct Admin newAdmin;
if((fp = fopen(ADMIN_FILE, "wb+")) == NULL){
printf("文件打开失败!\n");
return;
}
printf("请输入账号:");
scanf("%s", newAdmin.id);
printf("请输入密码:");
scanf("%s", newAdmin.pwd);
for(int index = 0; index < numAdmins; index++){
if(newAdmin.id == admins[index].id){
printf("账号有重复!请重新注册!\n");
break;
}
}
admins[numAdmins++] = newAdmin;
//写入到文件
fwrite(admins, sizeof(admins), 1, fp);
fwrite(&numAdmins, sizeof(int), 1, fp);
fclose(fp);
printf("管理员注册成功!\n");
}
//管理员登录
int adminLogin(){
int type, index = 0, flag = 0;
do {
system("cls");
//登录菜单
loginMenu();
scanf("%d", &type);
char id[50], pwd[50]; //用于输入账号和密码
switch(type){
case 1: {
//登录
//首先判断有没有账号
if(numAdmins == 0){
printf("还没有注册账号!请注册!\n");
break;
}
printf("请输入账号:");
scanf("%s", id);
printf("请输入密码:");
scanf("%s", pwd);
for(index = 0; index < numAdmins; index++){
if(strcmp(admins[index].id, id) == 0 && strcmp(admins[index].pwd, pwd) == 0){
flag = 1;
break;
}
}
if(flag == 0){
printf("用户名或密码有误!请核对后重试!");
break;
}
printf("登录成功!\n");
system("pause");
return 1;
}
case 2: {
//注册
adminRegister();
break;
}
case 3: {
//返回
return 3;
break;
}
default: printf("输入有误,请重新输入:"); break;
}
system("pause");
}while(type != 3);
return type;
}
//管理员模式
void adminMode(){
//登录操作
int op = adminLogin();
if(op == 3 || op == 2) return;
int adminChoice;
do {
system("cls");
// printf("欢迎管理员%s登录\n", admin[index].id);
printf("\n**********************管理员菜单**********************\n");
printf("* 1. 录入医生信息 *\n");
printf("* 2. 查询患者信息 *\n");
printf("* 3. 录入药品信息 *\n");
printf("* 4. 返回 *\n");
printf("******************************************************\n");
printf(" 输入你的选择 (1-4): [ ]\b\b");
scanf("%d", &adminChoice);
switch (adminChoice) {
case 1: {
//录入医生信息
char y_n; //用于判断是否继续添加
do {
addDoctor();
displayDoctors(); //展示医生信息
printf("是否继续添加?(y/n):");
scanf("%c", &y_n);
while(getchar() != '\n'); //清除多余字符
} while(y_n == 'y' || y_n == 'Y');
break;
}
case 2: {
//查询患者信息
displayPatientInformation();
break;
}
case 3: {
//录入药品信息
char y_n; //用于判断是否继续添加
do {
addMedicine();
displayMedicines(); //展示药品信息
printf("是否继续添加?(y/n):");
scanf("%c", &y_n);
while(getchar() != '\n'); //清除多余字符
} while(y_n == 'y' || y_n == 'Y');
break;
}
case 4: break;
default: printf("您的输入有误!请重新输入!\n"); break;
}
system("pause");
} while(adminChoice != 4);
}
//登录
void login(){
int userType;
do {
system("cls");
printf("\n**********************欢迎使用医院管理系统**********************\n");
printf("* 1. 管理员 *\n");
printf("* 2. 医生 *\n");
printf("* 3. 患者 *\n");
printf("* 4. 退出 *\n");
printf("****************************************************************\n");
printf(" 输入你的选择 (1-4): [ ]\b\b");
scanf("%d", &userType);
switch (userType) {
case 1: {
//管理员模式
adminMode();
break;
}
case 2: {
//医生登录
doctorMode();
break;
}
case 3: {
//患者登录
patientMode();
break;
}
case 4: break;
default: printf("您的输入有误!请重新输入!\n"); system("pause"); break;
}
} while(userType != 4);
}
//初始化程序,加载文件
void loadFiles(){
FILE* fp_doctors, * fp_admins, * fp_patients, * fp_medicines;
//加载医生文件
if((fp_doctors = fopen(DOCTOR_FILE, "rb")) == NULL){
printf("医生文件打开失败!\n");
}
else{
//读取文件
fread(doctors, sizeof(doctors), 1, fp_doctors);
fread(&numDoctors, sizeof(int), 1, fp_doctors);
fclose(fp_doctors);
}
//加载管理员文件
if(((fp_admins = fopen(ADMIN_FILE, "rb")) == NULL)){
printf("管理员文件打开失败!\n");
}
else{
fread(admins, sizeof(admins), 1, fp_admins);
fread(&numAdmins, sizeof(int), 1, fp_admins);
fclose(fp_admins);
}
//加载患者文件
if(((fp_patients = fopen(PATIENT_FILE, "rb")) == NULL)){
printf("患者文件打开失败!\n");
}
else {
fread(patients, sizeof(patients), 1, fp_patients);
fread(&numPatients, sizeof(int), 1, fp_patients);
fclose(fp_patients);
}
//加载药品文件
if(((fp_medicines = fopen(MEDICINE_FILE, "rb")) == NULL)){
printf("药品文件打开失败!\n");
}
else{
fread(medicines, sizeof(medicines), 1, fp_medicines);
fread(&numMedicines, sizeof(int), 1, fp_medicines);
fclose(fp_medicines);
}
}
int main() {
loadFiles();
login();
printf("\n欢迎您的下次使用!\n");
system("pause");
}