#include "stdio.h"
#include "string"
/*定义医生结构体*/
struct Doctor
{
char ID[50]; //医生编号
char Name[50]; //姓名
char Keshi[11]; //科室
char Room[11]; //诊室
};
/*声明医生数组及医生数量*/
struct Doctor Doctors[100];
int num=0;
/*通过医生编号返回数组下标*/
int Doctor_SearchByIndex(char id[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(Doctors[i].ID,id)==0)
{
return i;
}
}
return -1;
}
/*通过姓名返回数组下标*/
int Doctor_SearchByName(char name[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(Doctors[i].Name,name)==0)
{
return i;
}
}
return -1;
}
/*显示单条医生记录*/
void Doctor_DisplaySingle(int index)
{
printf("%10s%10s%10s%8s\n","医生编号","姓名","科室","诊室");
printf("-------------------------------------------------------------\n");
printf("%10s%10s%10s%8s\n",Doctors[index].ID,Doctors[index].Name,Doctors[index].Keshi,Doctors[index].Room);
}
/****1、增加医生记录*****/
void Doctor_Insert()
{
while(1)
{
printf("请输入医生编号:");
scanf("%s",&Doctors[num].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&Doctors[num].Name);
getchar();
printf("请输入科室:");
scanf("%s",&Doctors[num].Keshi);
getchar();
printf("请输入诊室:");
scanf("%s",&Doctors[num].Room);
getchar();
printf("是否继续?(Y/N),继续请按任意键,否则输入N:");
if (getchar()=='N')
{
break;
}
}
}
/******2、修改医生信息**********/
void Doctor_Modify()
{
while(1)
{
char id[50];
int index;
printf("请输入要修改的医生的医生编号:");
scanf("%s",&id);
getchar();
index=Doctor_SearchByIndex(id);
if (index==-1)
{
printf("该医生不存在!\n");
}
else
{
printf("你要修改的医生信息为:\n");
Doctor_DisplaySingle(index);
printf("-- 请输入新值--\n");
printf("请输入医生编号:");
scanf("%s",&Doctors[index].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&Doctors[index].Name);
getchar();
printf("请输入科室:");
scanf("%s",&Doctors[index].Keshi);
getchar();
printf("请输入诊室:");
scanf("%s",&Doctors[index].Room);
getchar();
}
printf("是否继续?(Y/N),继续请按任意键,否则输入N:");
if (getchar()=='N')
{
break;
}
}
}
/****3、删除医生信息****/
void Doctor_Delete()
{
int i;
while(1)
{
char id[50];
int index;
printf("请输入要删除的医生的医生编号:");
scanf("%s",&id);
getchar();
index=Doctor_SearchByIndex(id);
if (index==-1)
{
printf("医生不存在!\n");
}
else
{
printf("你要删除的医生信息为:\n");
Doctor_DisplaySingle(index);
printf("是否真的要删除?(Y/N)");
if (getchar()=='Y')
{
for (i=index;i<num-1;i++)
{
Doctors[i]=Doctors[i+1];//把后边的对象都向前移动
}
num--;
}
getchar();
printf("已删除\n");
}
printf("是否继续删除?(Y/N),继续请按任意键,否则输入N:");
if (getchar()=='N')
{
break;
}
}
}
/****4、按姓名查询******/
void Doctor_Select()
{
while(1)
{
char name[50];
int index;
printf("请输入要查询的医生的姓名:");
scanf("%s",&name);
getchar();
index=Doctor_SearchByName(name);
if (index==-1)
{
printf("医生不存在!\n");
}
else
{
printf("你要查询的医生信息为:\n");
Doctor_DisplaySingle(index);
}
printf("是否继续?(Y/N),继续请按任意键,否则输入N:");
if (getchar()=='N')
{
break;
}
}
}
/*显示医生信息*/
void Doctor_Display()
{
int i;
printf("%10s%10s%10s%8s\n","医生编号","姓名","科室","诊室");
printf("-------------------------------------------------------------\n");
for (i=0;i<num;i++)
{
printf("%10s%10s%10s%8f\n",Doctors[i].ID,Doctors[i].Name,Doctors[i].Keshi,Doctors[i].Room);
}
}
/*将医生信息从文件(Database.txt)中读出*/
void IO_ReadInfo()
{
FILE *fp;
int i;
if ((fp=fopen("Database.txt","rb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fread(&num,sizeof(int),1,fp)!=1)
{
num=-1;
}
else
{
for(i=0;i<num;i++)
{
fread(&Doctors[i],sizeof(struct Doctor),1,fp);
}
}
fclose(fp);
}
/*将医生信息写入文件(Database.txt)*/
void IO_WriteInfo()
{
FILE *fp;
int i;
if ((fp=fopen("Database.txt","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("写入文件错误!\n");
}
for (i=0;i<num;i++)
{
if (fwrite(&Doctors[i],sizeof(struct Doctor),1,fp)!=1)
{
printf("写入文件错误!\n");
}
}
fclose(fp);
}
/***********主程序*********/
void main()
{
int choice;
IO_ReadInfo(); //读取文件
while(1)
{
/*主菜单*/
printf("\n------ 医生预约系统------\n");
printf("1. 增加医生记录\n");
printf("2. 修改医生记录\n");
printf("3. 删除医生记录\n");
printf("4. 按姓名查询医生记录\n");
printf("5. 按诊室排序\n");
printf("6. 退出\n");
printf("请选择(1-6):");
scanf("%d",&choice);
getchar();
switch(choice)
{
case 1:
Doctor_Insert();
break;
case 2:
Doctor_Modify();
break;
case 3:
Doctor_Delete();
break;
case 4:
Doctor_Select();
break;
case 5:
break;
case 6:
exit(0);
break;
}
IO_WriteInfo();
}
}
C++代码可以运行,但是保存不了文件,怎么解决?谢谢!!!
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- qzjhjxj 2021-06-16 09:40关注
修改完善如下,供参考:
#include <stdio.h> #include <string> /*定义医生结构体*/ struct Doctor { char ID[50]; //医生编号 char Name[50]; //姓名 char Keshi[11]; //科室 char Room[11]; //诊室 }; /*声明医生数组及医生数量*/ struct Doctor Doctors[100]; int num=0; /*通过医生编号返回数组下标*/ int Doctor_SearchByIndex(char id[]) { int i; for (i=0;i<num;i++) { if (strcmp(Doctors[i].ID,id)==0) { return i; } } return -1; } /*通过姓名返回数组下标*/ int Doctor_SearchByName(char name[]) { int i; for (i=0;i<num;i++) { if (strcmp(Doctors[i].Name,name)==0) { return i; } } return -1; } /*显示单条医生记录*/ void Doctor_DisplaySingle(int index) { printf("%10s%10s%10s%8s\n","医生编号","姓名","科室","诊室"); printf("-------------------------------------------------------------\n"); printf("%10s%10s%10s%8s\n",Doctors[index].ID,Doctors[index].Name,Doctors[index].Keshi, Doctors[index].Room); } /****1、增加医生记录*****/ void Doctor_Insert() { while(1) { printf("请输入医生编号:"); //scanf("%s",&Doctors[index].Room); fflush(stdout);rewind(stdin); scanf("%s",Doctors[num].ID); //scanf("%s",&Doctors[num].ID); //getchar(); printf("请输入姓名:"); fflush(stdout);rewind(stdin); scanf("%s",Doctors[num].Name);//scanf("%s",&Doctors[num].Name); //getchar(); printf("请输入科室:"); fflush(stdout);rewind(stdin); scanf("%s",Doctors[num].Keshi);//scanf("%s",&Doctors[num].Keshi); //getchar(); printf("请输入诊室:"); fflush(stdout);rewind(stdin); scanf("%s",Doctors[num].Room);//scanf("%s",&Doctors[num].Room); //getchar(); num++; printf("是否继续?(Y/N),继续请按任意键,否则输入N:"); fflush(stdout);rewind(stdin); if ((getchar())=='N') { break; } } } /******2、修改医生信息**********/ void Doctor_Modify() { while(1) { char id[50]; int index; printf("请输入要修改的医生的医生编号:"); scanf("%s",id); //scanf("%s",&id); getchar(); index=Doctor_SearchByIndex(id); if (index==-1) { printf("该医生不存在!\n"); } else { printf("你要修改的医生信息为:\n"); Doctor_DisplaySingle(index); printf("-- 请输入新值--\n"); printf("请输入医生编号:"); fflush(stdout);rewind(stdin); scanf("%s",Doctors[index].ID); //scanf("%s",&Doctors[index].ID); //getchar(); printf("请输入姓名:"); fflush(stdout);rewind(stdin); scanf("%s",Doctors[index].Name);//scanf("%s",&Doctors[index].Name); //getchar(); printf("请输入科室:"); fflush(stdout);rewind(stdin); scanf("%s",Doctors[index].Keshi);//scanf("%s",&Doctors[index].Keshi); //getchar(); printf("请输入诊室:"); fflush(stdout);rewind(stdin); scanf("%s",Doctors[index].Room);//scanf("%s",&Doctors[index].Room); //getchar(); } printf("是否继续?(Y/N),继续请按任意键,否则输入N:"); fflush(stdout);rewind(stdin); if ((getchar())=='N') { break; } } } /****3、删除医生信息****/ void Doctor_Delete() { int i; while(1) { char id[50]; int index; printf("请输入要删除的医生的医生编号:"); fflush(stdout);rewind(stdin); scanf("%s",id); // scanf("%s",&id); //getchar(); index=Doctor_SearchByIndex(id); if (index==-1) { printf("医生不存在!\n"); } else { printf("你要删除的医生信息为:\n"); Doctor_DisplaySingle(index); printf("是否真的要删除?(Y/N)"); if ((getchar())=='Y') { for (i=index;i<num-1;i++) { Doctors[i]=Doctors[i+1];//把后边的对象都向前移动 } num--; } getchar(); printf("已删除\n"); } printf("是否继续删除?(Y/N),继续请按任意键,否则输入N:"); fflush(stdout);rewind(stdin); if ((getchar())=='N') { break; } } } /****4、按姓名查询******/ void Doctor_Select() { while(1) { char name[50]; int index; printf("请输入要查询的医生的姓名:"); fflush(stdout);rewind(stdin); scanf("%s",name); //scanf("%s",&name); //getchar(); index=Doctor_SearchByName(name); if (index==-1) { printf("医生不存在!\n"); } else { printf("你要查询的医生信息为:\n"); Doctor_DisplaySingle(index); } printf("是否继续?(Y/N),继续请按任意键,否则输入N:"); fflush(stdout);rewind(stdin); if ((getchar())=='N') { break; } } } /*显示医生信息*/ void Doctor_Display() { int i; printf("%10s%10s%10s%8s\n","医生编号","姓名","科室","诊室"); printf("-------------------------------------------------------------\n"); for (i=0;i<num;i++) { printf("%10s%10s%10s%8f\n",Doctors[i].ID,Doctors[i].Name,Doctors[i].Keshi, Doctors[i].Room); } } /*将医生信息从文件(Database.txt)中读出*/ void IO_ReadInfo() { FILE *fp; int i; if ((fp=fopen("Database.txt","rb"))==NULL) { printf("不能打开文件!\n"); return; } if (fread(&num,sizeof(int),1,fp)!=1) { num=-1; } else { for(i=0;i<num;i++) { fread(&Doctors[i],sizeof(struct Doctor),1,fp); } } fclose(fp); } /*将医生信息写入文件(Database.txt)*/ void IO_WriteInfo() { FILE *fp; int i; if ((fp=fopen("Database.txt","wb"))==NULL) { printf("不能打开文件!\n"); return; } if (fwrite(&num,sizeof(int),1,fp)!=1) { printf("写入文件错误!\n"); } for (i=0;i<num;i++) { if (fwrite(&Doctors[i],sizeof(struct Doctor),1,fp)!=1) { printf("写入文件错误!\n"); } } fclose(fp); } /***********主程序*********/ void main() { int choice; IO_ReadInfo(); //读取文件 while(1) { /*主菜单*/ printf("\n------ 医生预约系统------\n"); printf("1. 增加医生记录\n"); printf("2. 修改医生记录\n"); printf("3. 删除医生记录\n"); printf("4. 按姓名查询医生记录\n"); printf("5. 按诊室排序\n"); printf("6. 保存并退出\n"); printf("请选择(1-6):"); scanf("%d",&choice); getchar(); switch(choice) { case 1: Doctor_Insert(); break; case 2: Doctor_Modify(); break; case 3: Doctor_Delete(); break; case 4: Doctor_Select(); break; case 5: break; case 6: IO_WriteInfo(); exit(0); //break; } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用
悬赏问题
- ¥15 模电中二极管,三极管和电容的应用
- ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
- ¥15 气象网格数据与卫星轨道数据如何匹配
- ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
- ¥15 微软账户问题不小心注销了好像
- ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
- ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
- ¥20 关于web前端如何播放二次加密m3u8视频的问题
- ¥15 使用百度地图api 位置函数报错?
- ¥15 metamask如何添加TRON自定义网络