医院诊疗管理系统工程代码

最近为了腾出更多电脑空间,就决定这样云储存一下:
是一个大概三千行代码左右的大工程,还是学到了很多东西的。代码还是对于一个人综合能力的考察更多。先注明,这个是三个人合作写的,所以此处,respect 另外两位队友gzx和jjx。
我的部分是yh.c以及yh.h
其他两位的代码,也一并贴出了。为保证完整性。要是想赋值粘贴应付明年的课设的话,还是当心一点,毕竟我们这写的可以说的千疮百孔。

//gzx.c
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<inttypes.h>
#include"inform.h"
#include"gzx.h"
#include"jiang.h"
extern int dateInt;
void save(struct medicalRecord *head)
{
    
    
    struct medicalRecord *p = head;
    long long income = 0;
    FILE *income_pointer = fopen("income.txt", "r");
    fscanf(income_pointer, "%lld", &income);
    fclose(income_pointer);
    FILE *write_income = fopen("income.txt", "w");
    FILE *inhospital_pointer = fopen("inhospital.txt", "r");
    struct in_hospital *top = NULL, *tail = NULL;
    if (inhospital_pointer == NULL)
    {
    
    
        printf("无法读取住院表,请重试!");
        exit(1);
    }
    else
    {
    
    
        while (!feof(inhospital_pointer))
        {
    
    

            struct in_hospital *t = (struct in_hospital *)malloc(sizeof(struct in_hospital)); //1
            fscanf(inhospital_pointer, "%s%d%lld%lld%lld", t->nam, &t->patientind, &t->st, &t->end, &t->depo);
            if (top == NULL || tail == NULL)
            {
    
    
                top = t;
                tail = t;
            }
            else
            {
    
    
                tail->next = t;
                tail = t;
            }
        }
        fclose(inhospital_pointer);
    }

    char a[13];
    intToStr(dateInt,a);
    FILE *target_pointer = fopen(a, "w");

    if (target_pointer == NULL || inhospital_pointer == NULL)
    {
    
    
        printf("保存失败,请重试");
        exit(1);
    }
    else
    {
    
    
        while (p != NULL)
        {
    
    

            fprintf(target_pointer, "%s %s %lld %d %d %d ",a, p->name, p->index, p->patientIndex, p->doctorIndex, p->officeIndex);
            if (p->inHospitalOrNot == 0)
            {
    
    
                fprintf(target_pointer, "NULL  NULL  0\n");
            }
            else
            {
    
    
                char ststr[30];
                long long st =(long long)p->start.year*100000000+ p->start.month * 1000000 + p->start.day * 10000 + p->start.hour * 100 + p->start.minute;
                sprintf(ststr, "%lld", st);
                int eny = p->end.year;
                long long en=(long long)p->end.year*100000000+ p->end.month * 1000000 + p->end.day * 10000 + p->end.hour * 100 + p->end.minute;
                char estr[30];
                sprintf(estr, "%lld", en);
                fprintf(target_pointer, "%s %s %lld\n ", ststr,estr, p->deposit);
                struct in_hospital *t0;
                t0 = (struct in_hospital *)malloc(sizeof(struct in_hospital));
             strcpy(t0->nam,p->name);
             t0->patientind=p->patientIndex;
             t0->st=st;
             t0->end=en;
             t0->depo=p->deposit;
             t0->next=NULL;
                if (top == NULL || tail == NULL)
                {
    
    
                    top = t0;
                    tail = t0;
                }
                else
                {
    
    
                    tail->next = t0;
                    tail = t0;
                }
                t0 = top->next;
                FILE *write_inhospital=fopen("inhospital.txt","w");
                while (t0 != NULL)                                                                                         //
                {
    
    
                    fprintf(write_inhospital, "%s %d %lld %lld %lld", t0->nam, t0->patientind, t0->st, t0->end, t0->depo);
                    t0 = t0->next;
                }
                fclose(write_inhospital);
            }
            long long check_price = 0;
            for (int i = 0; i < Examination_max; i++)
            {
    
    
                if (p->exam[i] != 0)
                {
    
    
                    fprintf(target_pointer, "%s %lld ", Exam[i].name, Exam[i].price);
                    check_price += Exam[i].price;
                }
            }
            fprintf(target_pointer, "合计 %lld.%lld%lld元\n", check_price / 100, check_price /10% 10,check_price%10);                //3

            long long med_price = 0;
            for (int i = 0; i < 30; i++)
            {
    
    
                if (p->medicine[i] != 0)
                {
    
    
                    fprintf(target_pointer, "%s %d %lld ", Medicine[i].name, p->medicine[i], Medicine[i].price);
                    med_price += Medicine[i].price * p->medicine[i];
                }
            }
            income = check_price + med_price;
            fprintf(write_income, "%lld", income);
            fprintf(target_pointer, "合计 %lld.%lld%lld元\n", med_price / 100, med_price / 10%10,med_price%10);                                          //4
            fprintf(target_pointer, "\n");
            p = p->next;
        }
        printf("保存成功!");
        getch();
    }
    fclose(target_pointer);
    fclose(inhospital_pointer);

    return;
}

void sum_income(){
    
    
 long long income=0;
 FILE *income_pointer=fopen("income.txt","r");
 if(income_pointer==NULL){
    
    
 printf("无法访问收入文件,请重试\n");
 exit(1);
}else{
    
    
 fscanf(income_pointer,"%lld",&income);
 }
 printf("当前收入为:%lld.%lld元\n",income/100,income%100);
 printf("\n");
fclose(income_pointer);
printf("(按任意键返回)");
 getchar();
return;
}
void sum_doctor(){
    
    
 time_t timep;
 struct tm *p =NULL ;
 time (&timep);
 p=gmtime(&timep);
 for(int i=0;i<30;i++){
    
    
 if(!Doctor[i].workday[p->tm_wday]){
    
    
         printf("------------------------------------------------------------------------------------------------------------------------\n");
 printf("|%d号医生 %s 今日出诊!工作繁忙指数为:%d\n",Doctor[i].index,Doctor[i].name,Doctor[i].count/24);
  printf("------------------------------------------------------------------------------------------------------------------------\n");

 }else{
    
    
      printf("------------------------------------------------------------------------------------------------------------------------\n");
     printf("|%d号医生 %s 今日未出诊!工作繁忙指数为:0\n",Doctor[i].index,Doctor[i].name);
      printf("------------------------------------------------------------------------------------------------------------------------\n");}

 }
 printf("\n");
   printf("(按任意键返回)");
 getchar();
return;
}
void output_inhospital(){
    
    
 FILE *inhospital_pointer=fopen("inhospital.txt","r");
 if(inhospital_pointer==NULL){
    
    
 printf("无法访问住院信息文件,请重试");
 exit(1);
 }else{
    
    
 printf("住院患者报表:\n");
     if (feof(inhospital_pointer)==0) {
    
    
         printf("------------------------------------------------------------------------------------------------------------------------\n");
         printf("当前无住院患者\n");
         printf("------------------------------------------------------------------------------------------------------------------------\n");
         printf("\n");
   printf("(按任意键返回)");
       getchar();
       return;
     }else{
    
    
         char tmpstr[100];
while(fscanf(inhospital_pointer,"%s",tmpstr)!=EOF){
    
    
 char nam[100];
 strcpy(nam,tmpstr);
 int patientind;
 long long st;
 long long end;
 long long depo;
 fscanf(inhospital_pointer,"%d%lld%lld%lld",&patientind,&st,&end,&depo);
  printf("------------------------------------------------------------------------------------------------------------------------\n");
 printf("|患者姓名:%s |患者医疗卡号:%d |住院开始时间:%lld |预计出院时间:%lld |住院押金:%lld.%lld元|\n",nam,patientind,st,end,depo/100,depo%100);
    if (depo<=150000) {
    
    
        printf("该患者住院押金较低,请提醒患者及时缴纳!\n");
    }
  printf("------------------------------------------------------------------------------------------------------------------------\n");

    }
     printf("\n");
   printf("(按任意键返回)");
    getchar();
    return;
 }
 }
}
void check_inhospital(int y,int m,int d,int h){
    
    
    if (h==8) {
    
    
        long long income=0;
    FILE *read_income=fopen("income.txt", "r");
    fscanf(read_income, "%lld",&income);
    fclose(read_income);
    FILE *read_inhospital=fopen("inhospital.txt", "r");
    struct in_hospital *t,*top=NULL,*tail=NULL;
    if (read_inhospital==NULL) {
    
    
        printf("无法读取住院表,请重试");
        exit(1);
    }else{
    
    
        while (!feof(read_inhospital)) {
    
    

            t=(struct in_hospital*)malloc(sizeof(struct in_hospital));
            fscanf(read_inhospital,"%s%d%lld%lld%lld", t->nam,&t->patientind,&t->st,&t->end,&t->depo);
           if (top==NULL||tail==NULL) {
    
    
                top=t;
                tail=t;
            }else{
    
    
                tail->next=t;
                tail=t;
            }
        }
        fclose(read_inhospital);
    }
    t=top;
    struct in_hospital *t0=NULL;
        long long input=y*10000+m*100+d;
        long long checktime=800;
while (t!=NULL) {
    
    
          printf("%lld %lld %lld %lld\n",t->st/10000,t->end/10000,input,t->end%10000);
       if ((t->st/10000<=input)&&(t->end/10000>=input)&&(t->end%10000>=checktime)) {
    
    
            t=t->next;
                t0=t;

    }else{
    
    
            if (t==top&&t==tail) {
    
    
                free(t);
                top=NULL;
                tail=NULL;
                t=NULL;
            }
            else if(t==top&&t!=tail){
    
    
              top=t->next;
                free(t);
                t=top;
            }
            else if(t==tail&&t!=top){
    
    
                tail=t0;
                free(t);
            }
            else{
    
    
                t0->next=t->next;
                free(t);
                t=t0->next;
            }
        }

    }
    t=top;
    while (t!=NULL) {
    
    
        income+=20000;
        t->depo-=20000;
        t=t->next;
    }
         FILE *write_income=fopen("income.txt", "w");
    fprintf(write_income, "%lld",income);
         FILE *write_inhospital=fopen("inhospital.txt","w");
       t=top;
    while (t!=NULL) {
    
    
        fprintf(write_inhospital, "%s %d %lld %lld %lld",t->nam,t->patientind,t->st,t->end,t->depo);
        t=t->next;
    }
        fclose(write_income);
        fclose(write_inhospital);
  }
}

//jiang.c
#include "inform.h"
#include <direct.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#include"jiang.h"
//saveData
extern int income;
extern struct doctor Doctor[Doctor_max];
extern struct office Office[Office_max];
extern struct medicine Medicine[Medicine_kind_max];
extern struct examination Exam[Examination_max];
extern struct patient Patient[Patient_max];
extern int weekday;         //保存今天的星期
int dateInt;                //今天的日期,整型
int NOP; //实际病人数
int NOO; //实际科室数
int NOD; //实际医生数
int NOM; //实际药品数
int NOE; //实际检查数
const int MOD = 11;
/*函数定义部分开始*/
void cls()
{
    
    
    system("cls");
}

void importMenu()
{
    
    
    //system("color F0");
    while(1)
    {
    
    
        cls();
        printf("\n");
        printf(" 导入一日记录\n");
        printf(" 导入多日记录\n");
        printf(" 外部文件导入\n");
        printf(" 返回\n");
        int x=getKeyboard(3,0);cls();
        if(x==3)
            break;
        switch(x)
        {
    
    
            case 0: import(1,0); break;
            case 1: importPeriodOfTime(); break;
            case 2: import(0,0);break;
        }
    }
}
void SetPos(int i, int j)			//控制光标位置, 列, 行
{
    
    
	COORD pos={
    
    i,j};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int getKeyboard(int max,int if_input)
{
    
    
    CONSOLE_CURSOR_INFO cursor_info = {
    
    1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    int i=0,j=0;char key;
    SetPos(0,1);
    while(1)
    {
    
    
        if(_kbhit())
        {
    
    
            key =_getch();
            if(j==0&&key==80&&i<max)//下
            {
    
    
                SetPos(0,i+1);
                printf(" ");
                i++;
                j=0;
            }
            else if(i>0&&key==72)//上
            {
    
    
                SetPos(0,i+1);
                printf(" ");
                i--;
                j=0;
            }
            else if(key==13)
            {
    
    
                return i;
            }
        }
        Sleep(1);
        SetPos(j,i+1);
        printf(">");
    }
    return 0;
}
long long llmax(long long a, long long b)
{
    
    
    if (a > b)
        return a;
    return b;
}
int isLeapYear(int y) //闰年
{
    
    
    if (y % 400 == 0)
        return 1;
    if (y % 100 == 0)
        return 0;
    if (y % 4 == 0)
        return 1;
    return 0;
}
void getTime(struct Time *a) //获得当前系统时间,挂号用
{
    
    
    time_t timep;
    struct tm *p;
    time(&timep);
    p = gmtime(&timep);
    a->year = 1900 + p->tm_year; /*获取当前年份,从1900开始,所以要加1900*/
    a->month = 1 + p->tm_mon;    /*获取当前月份,范围0-11,要加1*/
    a->minute = p->tm_min;       /*获取当前分*/
    a->day = p->tm_mday;         /*获取当前月份日数,范围是1-31*/
    a->hour = 8 + p->tm_hour;    /*获取当前时,这里获取西方的时间,刚好相差八个小时*/
}

int getDate() //获取整型表示日期
{
    
    
    struct Time t;
    getTime(&t);
    return t.year * 1e4 + t.month * 1e2 + t.day;
}
int getWeekday(struct Time*t)  //获得当前星期几
{
    
    
    return (t->day + 2 * t->month + 3 * (t->month + 1) / 5 + t->year + t->year / 4 - t->year / 100 + t->year / 400) % 7;
}
bool checkOffice()              //检查每个科室是否有无人在岗的情况
{
    
    
    int i,j;
    bool map[Office_max+1][7];
    memset(map, 0, sizeof(map));
    for (i = 0; i < NOD; ++i)
    {
    
    
        for (j = 0; j < 7; ++j)
        {
    
    
            if (Doctor[i].workday[j])
                map[Doctor[i].office][j] = 1;
        }
    }
    for (i = 1; i <= NOO; ++i)
        for (int j = 0; j < 7; ++j)
        {
    
    
            if (!map[i][j])
                return 0;
        }
    return 1;
}
bool checkDate(int d)           //检查日期是否合理
{
    
    
    int a[13] = {
    
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (d / 10000 < 2000)
        return 0;
    int month = d / 100 % 100;
    if (month < 1 || month > 12)
        return 0;
    if (month != 2 && d % 100 > a[month])
        return 0;
    if (isLeapYear(d / 10000))
    {
    
    
        if (month == 2 && d % 100 > 29)
            return 0;
    }
    else
    {
    
    
        if (month == 2 && d % 100 > 28)
            return 0;
    }
    return 1;
}
bool checkPatient(int x,char *s)
{
    
    
    if(strcmp(s,Patient[x-1].name)==0)
        return 1;
    return 0;
}
bool checkOfficeOfDoctor(int doc,int off)
{
    
    
    if(Doctor[doc-1].office==off)
        return 1;
    return 0;
}
bool checkDoctorWork(int doc,int day)
{
    
    
    if(Doctor[doc-1].workday[day])
        return 1;
    return 0;
}
void loadIncome()               //从文件载入历史收入
{
    
    
    FILE *f;
    if ((f = fopen("income.txt", "rt")) == NULL)
    {
    
    
        printf("打开income.txt失败\n");
        return;
    }
    else
    {
    
    
        fscanf(f, "%lld", &income);
    }
    printf("income.txt           载入完成\n");
    //printf("%d\n", income);
    fclose(f);
}
void loadOfficeInfo()           //从文件载入科室信息
{
    
    
    FILE *f;
    NOO=0;
    if ((f = fopen("officeInfo.txt", "rt")) == NULL)
    {
    
    
        printf("打开officeInfo.txt失败\n");
        return;
    }
    else
    {
    
    
        for (int i = 0; i < Office_max; ++i)
        {
    
    
            if (fscanf(f, "%d%s", &Office[i].index, Office[i].name) == EOF)
                break;
            NOO++;
            //printf("%d %s\n", Office[i].index, Office[i].name);
        }
        printf("officeInfo.txt       载入完成\n");
        fclose(f);
    }
}
void loadDoctorInfo()           //从文件载入医生信息
{
    
    
    FILE *f;
    NOD=1;
    if ((f = fopen("doctorInfo.txt", "rt")) == NULL)
    {
    
    
        printf("打开doctorInfo.txt失败\n");
        return;
    }
    else
    {
    
    
        for (int i = 0; i < Doctor_max; ++i)
        {
    
    
            if (fscanf(f, "%d%s%d%d", &Doctor[i].index, Doctor[i].name, &Doctor[i].level, &Doctor[i].office) == EOF)
                break;
            Doctor[i].count = 0;
            for (int j = 0; j < 7; ++j)
                fscanf(f, "%d", &Doctor[i].workday[j]);
            NOD++;
        }
    }
    printf("doctorInfo.txt       载入完成\n");
    fclose(f);
}
void loadMedicineInfo()         //从文件载入药物信息
{
    
    
    FILE *f;
    NOM=0;
    if ((f = fopen("medicineInfo.txt", "rt")) == NULL)
    {
    
    
        printf("打开medicineInfo.txt失败\n");
        return;
    }
    else
    {
    
    
        for (int i = 0; i < Medicine_kind_max; ++i)
        {
    
    
            if (fscanf(f, "%s%lld", Medicine[i].name, &Medicine[i].price) == EOF)
                break;
            NOM++;
        }
    }
    printf("medicineInfo.txt     载入完成\n");
    fclose(f);
}
void loadExaminationInfo()      //从文件载入检查信息
{
    
    
    FILE *f;
    NOE=0;
    if ((f = fopen("examinationInfo.txt", "rt")) == NULL)
    {
    
    
        printf("打开exanminationInfo.txt失败\n");
        return;
    }
    else
    {
    
    
        for (int i = 0; i < Examination_max; ++i)
        {
    
    
            if (fscanf(f, "%s%lld", Exam[i].name, &Exam[i].price) == EOF)
                break;
            NOE++;
        }
    }
    printf("exanminationInfo.txt 载入完成\n");
    fclose(f);
}
void loadPatientInfo()          //从文件载入病人信息
{
    
    
    FILE *f;
    memset(Patient, 0, sizeof(Patient));
    NOP=0;
    if ((f = fopen("patientInfo.txt", "rt")) == NULL)
    {
    
    
        printf("打开patientInfo.txt失败\n");
    }
    else
    {
    
    
        for (int i = 0; i < Patient_max; ++i)
        {
    
    
            if (fscanf(f, "%s", Patient[i].name) == EOF)
                break;
            fscanf(f, "%d%d", &Patient[i].index, &Patient[i].age);
            Patient[i].count = 0;
            memset(Patient[i].Office, 0, sizeof(Patient[i].Office));
            NOP++;
        }
    }
    printf("patientInfo.txt      载入完成\n");
    fclose(f);
}

void init() //初始化 包括当前日期、星期、当日已有的诊疗记录以及各种信息
{
    
    
    struct Time T;
    getTime(&T);
    weekday = getWeekday(&T);
    dateInt = getDate();
    loadIncome();
    loadOfficeInfo();
    loadDoctorInfo();
    loadMedicineInfo();
    loadExaminationInfo();
    loadPatientInfo();
    if (!checkOffice())
    {
    
    
        printf("警告:存在某日无人在岗的科室\n");
    }
    struct medicalRecord *t = (struct medicalRecord *)malloc(sizeof(struct medicalRecord));
    memset(t, 0, sizeof(struct medicalRecord));
    head = t;
    tail = t;
    import(1,dateInt);
    printf("按任意键进入主菜单...\n");
    getch();
}
/*
bool IDcheck(const char *s) //检查身份证号
{
    int i;
    long long sum = 0;
    long long temp = 2;
    for (i = 16; i >= 0; --i)
    {
        sum += (s[i] - '0') * temp % MOD;
        sum %= MOD;
        temp *= 2;
    }
    if (12 - sum == 10)
    {
        if (s[17] != 'x')
            return 0;
        return 1;
    }
    if (sum == 0)
    {
        if (s[17] - '0' == 1)
            return 1;
        return 0;
    }
    if (sum == 1)
    {
        if (s[17] - '1' == 0)
            return 1;
        return 0;
    }
    if (s[17] - '0' == 12 - sum)
        return 1;
    return 0;
}
*/
/*int searchPatient(const char *s) //查找病人ID,返回病人编号
{
    int i;
    for (i = 0; i < NOP; ++i)
    {
        if (strcmp(s, Patient[i].IDnumber) == 0)
            return i;
    }
    return -1;
}*/

int timeCompare(const struct Time *a, const struct Time *b) //比较两个时间点,a比b大返回1,a比b小返回-1,相等返回0
{
    
    
    int t[5];
    t[0] = a->year - b->year;
    t[1] = a->month - b->month;
    t[2] = a->day - b->day;
    t[3] = a->hour - b->hour;
    t[4] = a->minute - b->minute;
    for (int i = 0; i < 5; ++i)
    {
    
    
        if (t[i] > 0)
            return 1;
        if (t[i] < 0)
            return -1;
    }
    return 0;
}

int countLeapYear(int y) //数闰年
{
    
    
    return (y - 2000) / 4 - (y - 2000) / 100 + 2 * (y - 2000) / 400 + 1;
}

long long countDays(const struct Time *t1, const struct Time *t2) //计算天数
{
    
    
    long long d1, d2;
    int a[13] = {
    
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    d1 = countLeapYear(t1->year - 1) + (long long)365 * (t1->year - 2000);
    d2 = countLeapYear(t2->year - 1) + (long long)365 * (t2->year - 2000);
    for (int i = 1; i < t1->month; ++i)
    {
    
    
        d1 += a[i];
    }
    for (int i = 1; i < t2->month; ++i)
    {
    
    
        d2 += a[i];
    }
    d1 += t1->day;
    d2 += t2->day;
    if (t1->month > 2)
        d1 += isLeapYear(t1->year);
    if (t2->month > 2)
        d2 += isLeapYear(t2->year);
    if (t2->hour < 8)
        d2--;
    return d2 - d1 + 1;
}

long long calculateFee(const struct medicalRecord *a) //计算费用
{
    
    
    long long sum = 0;
    int i;
    for (i = 0; i < NOE; ++i)
        sum += a->exam[i] * Exam[i].price;
    for (i = 0; i < NOM; ++i)
        sum += a->medicine[i] * Medicine[i].price;
    if (a->inHospitalOrNot)
        sum += 20000 * llmax(5, countDays(&a->start, &a->end));
    return sum;
}

bool inputOfficeIndex(struct medicalRecord *t, struct patient *p)              //挂号时选择科室
{
    
    
    cls();
    int offnum;
    int i;
    while (1)
    {
    
    
        printf("科室信息:\n");
        printf("科室编号        名称\n");
        printf("----------------------------------------\n");
        for(i=0;i<NOO;++i)
        {
    
    
            printf("%4d\t\t%s\n",Office[i].index,Office[i].name);
        }
        printf("----------------------------------------\n");
        printf("\n请输入科室编号,输入0取消:\n");
        offnum=inputNumber(INTLENGTH,0,0);
        if (offnum == 0)
        {
    
    
            free(t);
            t = NULL;
            return 0;
        }
        else
        {
    
    
            if (offnum > Office_max)
            {
    
    
                printf("非法科室编号,请重新输入或输入0退出:\n");
                getch();
                fflush(stdin);
                cls();
                continue;
            }
            int i;
            for (i = 0; i < p->count; ++i)
            {
    
    
                if (p->Office[i] == offnum)
                {
    
    
                    printf("患者今日已在该科室挂过号。输入其他科室编号。\n");
                    getch();
                    fflush(stdin);
                    cls();
                    break;
                }
            }
            if (p->Office[i] == offnum)
                continue;
            else
            {
    
    
                t->officeIndex=offnum;
                cls();
                break;
            }
        }
    }
    return 1;
}

bool inputDoctorIndex(struct medicalRecord *t)                                 //挂号时选择医生
{
    
    
    cls();
    int currentDoctor[Doctor_max];
    memset(currentDoctor, 0, sizeof(currentDoctor));
    int cnt = 0;
    for (int i = 0; i < NOD; ++i)
    {
    
    
        //printf("%d %d %d", Doctor[i].office, Doctor[i].workday[weekday], Doctor[i].count);
        if (Doctor[i].office == t->officeIndex && Doctor[i].workday[weekday] && Doctor[i].count < 20)
        {
    
    
            currentDoctor[cnt] = i;
            cnt++;
        }
    }
    if (cnt == 0)
    {
    
    
        printf("该科室没有可供挂号医生,操作取消\n");
        getch();
        fflush(stdin);
        free(t);
        t = NULL;
        return 0;
    }
    int docnum;
    while (1)
    {
    
    
        printf("当前科室可供选择的医生:\n");
        printf("编号              姓名              职称\n");
        printf("-----------------------------------------------------\n");
        for (int i = 0; i < cnt; ++i)
        {
    
    
            printf("%d %20s", Doctor[currentDoctor[i]].index, Doctor[currentDoctor[i]].name);
            switch (Doctor[currentDoctor[i]].level)
            {
    
    
                case 1:
                    printf("\t\t住院医师\n");
                    break;
                case 2:
                    printf("\t\t主治医师\n");
                    break;
                case 3:
                    printf("\t\t副主任医师\n");
                    break;
                case 4:
                    printf("\t\t主任医师\n");
                    break;
            }
        }
        printf("-----------------------------------------------------\n");
        printf("\n");
        printf("请输入医生编号,输入0取消:\n");
        docnum=inputNumber(INTLENGTH,0,0);
        if (docnum == 0)
        {
    
    
            free(t);
            t = NULL;
            return 0;
        }
        else
        {
    
    
            for (int i = 0; i < cnt; ++i)
            {
    
    
                if (docnum == Doctor[currentDoctor[i]].index)
                {
    
    
                    t->doctorIndex = docnum;
                    return 1;
                }
            }
            printf("非法医生编号,请重新输入\n");
            getch();
            fflush(stdin);
            continue;
        }
    }
}
void inputExaminationIndex(struct medicalRecord *t)                            //挂号时选择检查项目
{
    
    
    cls();
    printf("检查项目列表:\n");
    printf("编号\t\t名称\t\t价格\n");
    printf("-----------------------------------------\n");
    for (int i = 0; i < NOE; ++i)
    {
    
    
        printf("%4d%20s%10d.%lld%lld元\n", i, Exam[i].name, Exam[i].price / 100, Exam[i].price / 10 % 10, Exam[i].price % 10);
    }
    int n;
    printf("-----------------------------------------\n");
    while(1)
    {
    
    
        printf("请输入检查项目数:\n");
        n=inputNumber(INTLENGTH,0,0);
        if(n>NOE)
        {
    
    
            printf("非法项目数\n");
            getch();
            fflush(stdin);
        }
        else
            break;
    }
    for (int j = 0; j < n; ++j)
    {
    
    
        printf("请输入检查项目#%d的编号:\n",j+1);
        int temp = NOE;
        while (temp >= NOE || t->exam[temp] != 0)
        {
    
    
            temp=inputNumber(INTLENGTH,0,0);
            if (temp >= NOE || t->exam[temp] != 0)
            {
    
    
                printf("非法编号,请重新输入\n");
            }
        }
        t->exam[temp] = 1;
    }
}
void inputMedicineIndexAndAmount(struct medicalRecord *t)                      //挂号时选择药品及数量
{
    
    
    cls();
    int n;
    printf("药物信息列表:\n");
    printf("编号\t\t名称\t\t单价\n");
    printf("-----------------------------------------\n");
    for (int i = 0; i < NOM; ++i)
    {
    
    

        printf("%4d%20s", i, Medicine[i].name);


        printf("%10lld", Medicine[i].price / 100);
        printf(".%lld%lld元\n",Medicine[i].price / 10 % 10,Medicine[i].price%10);

    }
    printf("-----------------------------------------\n");
    while(1)
    {
    
    
        printf("请输入药品种类数:\n");
        n=inputNumber(INTLENGTH,0,0);
        if(n>NOM)
        {
    
    
            printf("非法药品种类数\n");
            getch();
            fflush(stdin);
        }
        else
            break;
    }
    for (int j = 0; j < n; ++j)
    {
    
    
        printf("请输入#%d药品的编号:\n",j+1);
        int temp1 = NOM, temp2;
        while (temp1 >= NOM || t->medicine[temp1] != 0)
        {
    
    
            temp1=inputNumber(INTLENGTH,0,0);
            if (temp1 >= NOM || t->medicine[temp1] != 0)
            {
    
    
                printf("非法编号,请重新输入\n");
            }
        }
        printf("请输入#%d药品的数量:\n",j+1);
        temp2=inputNumber(INTLENGTH,0,0);
        t->medicine[temp1] = temp2;
    }
}

void inputInhospitalInfo(struct medicalRecord *t)                              //挂号时选择住院信息
{
    
    
    cls();
    while(1)
    {
    
    
        fflush(stdin);
        printf("患者是否住院?(Y/N)\n请在此输入:");
        char c;
        scanf("%c", &c);
        if (c=='N')
        {
    
    
            t->inHospitalOrNot = 0;
            return;
        }
        else
        if(c=='Y')
        {
    
    
            t->inHospitalOrNot = 1;
            while(1)
            {
    
    
                printf("请输入住院结束时间:\n");
                struct Time temp;
                printf("年:");
                temp.year=inputNumber(INTLENGTH,0,0);
                printf("月:");
                temp.month=inputNumber(INTLENGTH,0,0);
                printf("日:");
                temp.day=inputNumber(INTLENGTH,0,0);
                printf("时:");
                temp.hour=inputNumber(INTLENGTH,0,0);
                printf("分:");
                temp.minute=inputNumber(INTLENGTH,0,0);
                if (timeCompare(&t->T, &temp) > -1 || !checkDate(temp.year * 1e4 + temp.month * 100 + temp.day))
                {
    
    
                    printf("非法时间. 请重新输入:\n");
                    getch();
                    fflush(stdin);
                    cls();
                    continue;
                }
                else
                {
    
    
                    if (countDays(&t->T, &temp))
                    {
    
    
                        t->start = t->T;
                        t->end = temp;
                        //printf("%d %d %d %d %d\n%d %d %d %d %d",t->start.year,t->start.month,t->start.day,t->start.hour,t->start.minute,t->end.year,t->end.month,t->end.day,t->end.hour,t->end.minute);
                        return;
                    }
                    else
                    {
    
    
                        printf("非法时间. 请重新输入:\n");
                        getch();
                        fflush(stdin);
                        cls();
                    }
                }
            }
        }
        else
        {
    
    
            printf("非法输入,请重试\n");
            getch();
            fflush(stdin);
            cls();
        }
    }
}
bool inputFeePaid(struct medicalRecord *t)                                     //挂号时计算收费
{
    
    
    cls();
    long long fee = calculateFee(t);
    while (1)
    {
    
    
        if (t->inHospitalOrNot)
        {
    
    
            printf("患者需至少支付 %lld.%d%d元", fee / 100, fee / 10 % 10, fee % 10);
            printf(", 其中包括住院押金至少 %lld.00元(押金应能被100元整除) .\n", 200*llmax(5, countDays(&t->start, &t->end)));
        }
        else
            printf("患者需至少支付 %lld.%d%d元.\n", fee / 100, fee / 10 % 10, fee % 10);
        printf("请输入患者支付金额,输入 -1 取消.\n");
        printf("在此输入(单位:元):");
        long long pay;
        pay=inputNumber(LONGLONGLENGTH,1,1);
        //printf("%lld",pay);
        if (pay == -1)
        {
    
    
            free(t);
            t = NULL;
            return 0;
        }
        if (t->inHospitalOrNot)
        {
    
    
            if (pay < fee || (pay - fee) % 10000 != 0)
            {
    
    
                printf("非法支付金额。 重新输入或输入 -1 取消\n");
                getch();
                fflush(stdin);
                cls();
                continue;
            }
        }
        else
        {
    
    
            if (pay < fee)
            {
    
    
                printf("非法支付金额。 重新输入或输入 -1 取消\n");
                getch();
                fflush(stdin);
                cls();
                continue;
            }
        }
        if (t->inHospitalOrNot)
            t->deposit = pay - fee + 20000 * llmax(5, countDays(&t->start, &t->end));
        else
            t->deposit = 0;
        return 1;
    }
}
void setMedicalRecordIndex(struct medicalRecord *t)                            //挂号时生成诊疗记录编号
{
    
    
    if (tail && tail->T.year == t->T.year && tail->T.month == t->T.month && tail->T.day == t->T.day)
        t->index = tail->index + 1;
    else
        t->index = t->T.year * 1e7 + t->T.month * 1e5 + t->T.day * 1e3 + 1;
    //printf("%lld",t->index);
    getch();
}
struct medicalRecord* createMedicalRecord(struct patient *p)                                    //尝试创建新的诊疗记录
{
    
    
    struct medicalRecord *t = (struct medicalRecord *)malloc(sizeof(struct medicalRecord));
    memset(t, 0, sizeof(struct medicalRecord));
    strcpy(t->name, p->name);
    t->patientIndex = p->index;
    getTime(&t->T);
    if (!inputOfficeIndex(t, p))
        return 0;
    if (!inputDoctorIndex(t))
        return 0;
    inputExaminationIndex(t);
    inputMedicineIndexAndAmount(t);
    inputInhospitalInfo(t);
    if (!inputFeePaid(t))
        return 0;
    setMedicalRecordIndex(t);
    t->next = NULL;
    tail->next = t;
    tail = t;
    p->Office[p->count] = t->officeIndex;
    p->count++;
    Doctor[t->doctorIndex].count++;
    return t;
}

/*int getAge(char *s, int a) //求年龄
{
    int i;
    int sum = 0;
    for (i = 6; i < 14; ++i)
    {
        sum *= 10;
        sum += s[i] - '0';
    }
    return (a - sum) / 1e4;
}*/

void tryToRegister()                                                           //尝试挂号
{
    
    
    if (dateInt == tail->index / 1000 && tail->index % 1000 == 500)
    {
    
    
        printf("今天已有500个挂号,请择日再来\n");
        getch();
        fflush(stdin);
        return;
    }
    int p;
    while(1)
    {
    
    
        printf("请输入患者编号,新患者请输入0:\n");
        p=inputNumber(INTLENGTH,0,0);
        if(p==0)
            break;
        if (p<NOP)
        {
    
    
            if (Patient[p-1].count == 5)
            {
    
    
                printf("该病人今日挂号次数已达上限,请择日再来\n");
                getch();
                fflush(stdin);
                return;
            }
            break;
        }
        else
        {
    
    
            printf("非法编号,请重新输入\n");
            getch();
            fflush(stdin);
            cls();
            continue;
        }
    }
    if(p==0)
    {
    
    
        if (NOP == Patient_max)
        {
    
    
            printf("患者人数溢出!\n");
            getch();
            fflush(stdin);
            return;
        }
        printf("请输入患者姓名:\n");
        scanf("%s", Patient[NOP].name);
        printf("请输入患者年龄:\n");
        Patient[NOP].age=inputNumber(INTLENGTH,0,0);
        Patient[NOP].index=NOP+1;
        NOP++;
        p=NOP;
    }
    struct medicalRecord*t;
    if (t=createMedicalRecord(&Patient[p-1]))
    {
    
    
        printf("挂号完成,诊疗记录创建成功\n");
        printf("详情:\n\n");
        printf("编号:%16lld\t\t",t->index);
        printf("姓名:%16s\t\t年龄:%16d\n",Patient[p-1].name,Patient[p-1].age);
        printf("科室:%16s\t\t医生:%16s\n",Office[t->officeIndex-1].name,Doctor[t->doctorIndex-1].name);
        printf("----------------------------------------------------------------------------\n");
        int i;
        int cnt=0;
        long long sum=0;
        for(i=0;i<NOE;++i)
        {
    
    
            if(t->exam[i])
            {
    
    
                if(!cnt)
                {
    
    
                    char tmpS1[100],tmpS2[100];
                    strcpy(tmpS1,"名称");
                    strcpy(tmpS2,"价格");
                    printf("检查:\n");
                    printf("%20s\t\t     %10s\n",tmpS1,tmpS2);
                }
                printf("%20s\t\t%10lld.%lld%lld元\n",Exam[i].name,Exam[i].price/100,Exam[i].price%100/10,Exam[i].price%10);
                sum+=Exam[i].price;
                cnt++;
            }
        }
        printf("----------------------------------------------------------------------------\n");
        cnt=0;
        for(i=0;i<NOM;++i)
        {
    
    
            if(t->medicine[i])
            {
    
    
                if(!cnt)
                {
    
    
                    char tmpS1[100],tmpS2[100],tmpS3[100],tmpS4[100];
                    strcpy(tmpS1,"名称");
                    strcpy(tmpS2,"单价");
                    strcpy(tmpS3,"数量");
                    strcpy(tmpS4,"总价");
                    printf("药品:\n");
                    printf("%20s\t\t      %10s\t%10s\t\t      %10s\n",tmpS1,tmpS2,tmpS3,tmpS4);
                }
                printf("%20s\t\t%10lld.%lld%lld元\t\t",Medicine[i].name,Medicine[i].price/100,Medicine[i].price/10%10,Medicine[i].price%10);
                printf("%10d\t\t%10lld.%lld%lld元\n",t->medicine[i],Medicine[i].price*(t->medicine[i])/100,Medicine[i].price*(t->medicine[i])/10%10,Medicine[i].price*(t->medicine[i])%10);
                cnt++;
                sum+=(Medicine[i].price)*(t->medicine[i]);
            }
        }
        printf("----------------------------------------------------------------------------\n");
        printf("以上项目合计%lld.%lld%lld元\n",sum/100,sum/10%10,sum%10);
        printf("----------------------------------------------------------------------------\n");
        printf("住院:");
        if(t->inHospitalOrNot)
            printf("\t\t  是\n");
        else
            printf("\t\t  否\n");
        if(t->inHospitalOrNot)
        {
    
    
            printf("住院开始时间:     %6d年%4d月%4d日%4d时%4d分\n",t->start.year,t->start.month,t->start.day,t->start.hour,t->start.minute);
            printf("住院结束时间:     %6d年%4d月%4d日%4d时%4d分\n",t->end.year,t->end.month,t->end.day,t->end.hour,t->end.minute);
            printf("押金:%10lld.%lld%lld元\n",t->deposit/100,t->deposit/10%10,t->deposit%10);
        }
        printf("----------------------------------------------------------------------------\n");
        printf("按任意键返回主菜单...");
        getch();
    }
}

void insertRecord(struct medicalRecord *a)                                     //将一条诊疗记录插入链表
{
    
    
    struct medicalRecord *pt = head;
    struct medicalRecord *t = head->next;
    while (t && a->index>t->index)
    {
    
    
        pt = pt->next;
        t = t->next;
    }
    if (pt == tail)
        tail = a;
    pt->next = a;
    a->next = t;
}

void intToStr(int a, char *s)                                                  //将整型的日期转换成字符串形式的文件名
{
    
    
    for (int i = 0; i < 8; ++i)
    {
    
    
        s[7 - i] = '0' + a % 10;
        a /= 10;
    }
    s[8] = '.';
    s[9] = 't';
    s[10] = 'x';
    s[11] = 't';
    s[12] = 0;
}
void import(int mode, int d)                                                   //导入文件中的诊疗记录。mode参数为0,手动输入文件名;mode参数为1,则d参数表示需要导入的日期
{
    
    
    FILE *f;
    char s[100];
    if (mode == 0)
    {
    
    
        while (1)
        {
    
    
            printf("请输入文件名(*.txt), 输入0取消:\n");
            scanf("%s", s);
            if (strcmp(s, "0") == 0)
            {
    
    
                printf("操作取消");
                getch();
                fflush(stdin);
                return;
            }
            if ((f = fopen(s, "rt")) == NULL)
            {
    
    
                printf("打开文件%s失败, 请检查文件名\n", s);
                getch();
                fflush(stdin);
                cls();
                continue;
            }
            break;
        }
    }
    else
    {
    
    
        if (d == 0)
            while (1)
            {
    
    
                printf("请输入日期(格式yyyymmdd):\n");
                d=inputNumber(INTLENGTH,0,0);
                if (!checkDate(d))
                {
    
    
                    printf("输入有误,请重新输入\n");
                    getch();
                    fflush(stdin);
                    cls();
                    continue;
                }
                break;
            }
        intToStr(d, s);
        if ((f = fopen(s, "rt")) == NULL)
        {
    
    
            printf("%s         导入失败\n", s);
            getch();
            fflush(stdin);
            return;
        }
    }
    char tmpStr[100];
    long long tempIndex;
    bool flag = 0;
    while (fscanf(f, "%s", tmpStr) != EOF)
    {
    
    
        tempIndex=strToInt(tmpStr);
        if(tempIndex<0)
        {
    
    
            printf("%s文件存在错误,导入失败\n",s);
            getch();
            fflush(stdin);
            return;
        }
        bool f1;
        struct medicalRecord *pt;
        if (flag || searchRecord(tempIndex, &f1)->next == NULL)
            flag = 1;
        else
        {
    
    
            printf("%s文件存在冲突,导入失败\n",s);
            getch();
            fflush(stdin);
            return;
        }
        struct medicalRecord *t = (struct medicalRecord *)malloc(sizeof(struct medicalRecord));
        memset(t, 0, sizeof(struct medicalRecord));
        t->index = tempIndex;
        fscanf(f, "%s", t->name);
        if(!readNumberAndJudge(f,&t->T.year))
            return;
        if(!readNumberAndJudge(f,&t->T.month))
            return;
        if(!readNumberAndJudge(f,&t->T.day))
            return;
        if(!readNumberAndJudge(f,&t->T.hour))
            return;
        if(!readNumberAndJudge(f,&t->T.minute))
            return;
        if(!readNumberAndJudge(f,&t->patientIndex))
            return;
        if(!readNumberAndJudge(f,&t->doctorIndex))
            return;
        if(!readNumberAndJudge(f,&t->officeIndex))
            return;
        int i;
        for (i = 0; i < NOE; ++i)
        {
    
    
            if(!readNumberAndJudge(f,t->exam+i))
                return;
        }
        for (i = 0; i < NOM; ++i)
        {
    
    
            if(!readNumberAndJudge(f,t->medicine+i))
                return;
        }
        if(!readNumberAndJudge(f,&t->inHospitalOrNot))
            return;
        if (t->inHospitalOrNot)
        {
    
    
            if(!readNumberAndJudge(f,&t->start.year))
                return;
            if(!readNumberAndJudge(f,&t->start.month))
                return;
            if(!readNumberAndJudge(f,&t->start.day))
                return;
            if(!readNumberAndJudge(f,&t->start.hour))
                return;
            if(!readNumberAndJudge(f,&t->start.minute))
                return;
            if(!readNumberAndJudge(f,&t->end.year))
                return;
            if(!readNumberAndJudge(f,&t->end.month))
                return;
            if(!readNumberAndJudge(f,&t->end.day))
                return;
            if(!readNumberAndJudge(f,&t->end.hour))
                return;
            if(!readNumberAndJudge(f,&t->end.minute))
                return;
            if(!readNumberAndJudge(f,&t->deposit))
                return;
        }
        if (checkMedicalRecord(t))
            insertRecord(t);
        else
        {
    
    
            printf("%s文件存在错误,导入失败\n",s);
            free(t);
            t = NULL;
            return;
        }
    }
    fclose(f);
    refresh();
    printf("%s导入成功\n",s);
    return;
}

struct medicalRecord *searchRecord(long long a, bool *f)                       //遍历链表寻找目标诊疗记录,返回目标的**前驱**
{
    
    
    *f = 0;
    struct medicalRecord *pt = head;
    struct medicalRecord *t = head->next;
    while (t)
    {
    
    
        if (a / 1000 == t->index / 1000)
            *f = 1;
        if (a == t->index)
            return pt;
        pt = pt->next;
        t = t->next;
    }
    return pt;
}
void importPeriodOfTime()                                                      //导入一段时间(多日)的诊疗记录
{
    
    
    int a, b;
    while (1)
    {
    
    
        printf("请输入起始时间(格式:yyyymmdd),输入0取消\n");
        a=inputNumber(INTLENGTH, 0,0);
        if(a==0)
        {
    
    
            printf("操作取消\n");
            getch();
            fflush(stdin);
            return;
        }
        printf("请输入结束时间(格式:yyyymmdd),输入0取消\n");
        b=inputNumber(INTLENGTH,0,0);
        if(b==0)
        {
    
    
            printf("操作取消\n");
            getch();
            fflush(stdin);
            return;
        }
        if (checkDate(a) && checkDate(b) && b - a > 0 && dateInt - b >= 0)
        {
    
    
            int i;
            for (i = a; i <= b;++i)
            {
    
    
                if(checkDate(i))
                    import(1, i);
                else
                {
    
    
                    if(i%100/32>0)
                    {
    
    
                        i += 100;
                        i -= i % 100;
                    }
                    if(i%10000/100/13>0)
                    {
    
    
                        i += 10000;
                        i -= i % 10000;
                        i += 100;
                    }
                }
            }
        }
        else
        {
    
    
            printf("非法输入\n");
            getch();
            fflush(stdin);
            cls();
            continue;
        }
    }
}
int checkIndex(long long a)                                                    //检查诊疗记录编号是否合理
{
    
    
    if (a % 1000 > 500 || a % 1000 == 0)
        return 0;
    if (!checkDate(a / 1000))
        return 0;
    return a;
}
void leaveARecordWhenModify(long long index, char *s)                          //在修改、删除操作进行后在文件中留下记录
{
    
    
    FILE *f;
    if ((f = fopen("modifyRecord.txt", "a")) == NULL)
    {
    
    
        printf("警告: 无法打开 modifyRecord.txt, 修改将不被记录\n");
        getch();
        fflush(stdin);
    }
    else
    {
    
    
        struct Time tempt;
        getTime(&tempt);
        fprintf(f, "%lld %d %d %d %d %d %s\n", index, tempt.year, tempt.month, tempt.day, tempt.hour, tempt.minute, s);
        fclose(f);
    }
}
long long Delete()                                                             //删除一条诊疗记录
{
    
    
    printf("请输入你想修改/删除的诊疗记录编号,输入0退出:\n");
    printf("格式:20200101001(前8位表示日期,后3位表示当日编号)\n");
    long long tempIndex;
    while (1)
    {
    
    
        tempIndex=inputNumber(LONGLONGLENGTH,0,0);
        if (tempIndex == 0)
        {
    
    
            printf("操作取消\n");
            getch();
            fflush(stdin);
            return 0;
        }
        if (!checkIndex(tempIndex))
        {
    
    
            printf("非法编号。请重新输入或输入0退出\n");
            continue;
        }
        bool flag;
        struct medicalRecord *t;
        struct medicalRecord *pt = searchRecord(tempIndex, &flag);
        if (!flag)
        {
    
    
            printf("找不到该记录,可能它尚未被导入\n输入1尝试从文件中导入,输入0取消\n");
            int temp;
            temp=inputNumber(INTLENGTH,0,0);
            if (temp)
            {
    
    
                import(0,0); //指定输入日期
                printf("请输入你想修改/删除的诊疗记录编号,输入0退出:\n");
                continue;
            }
            printf("操作取消\n");
            getch();
            fflush(stdin);
            return 0;
        }
        else
        {
    
    
            if (pt->next)
            {
    
    
                t = pt->next;
                char s[100] = "delete";
                char fileName[100];
                FILE*f;
                intToStr(timeToInt(&t->T),fileName);
                if((f=fopen(fileName,"w"))!=NULL)
                    fclose(f);
                leaveARecordWhenModify(t->index, s);
                pt->next = t->next; //22日debug位置
                free(t);
                t = NULL;
                saveData();
                printf("记录删除成功\n");
                getch();
                fflush(stdin);
                return tempIndex;
            }
            else
            {
    
    
                printf("找不到该记录。请重新输入或输入0退出\n");
                continue;
            }
        }
    }
}
int timeToInt(struct Time *t)                                                  //将时间结构体转换为整数
{
    
    
    return t->year * 10000 + t->month * 100 + t->day;
}
bool checkMedicalRecord(struct medicalRecord *t)                               //检查一条诊疗记录是否合理
{
    
    
    bool f;
    struct medicalRecord *pt;
    if (searchRecord(t->index, &f)->next)
        return 0;
    if (!checkIndex(t->index))
        return 0;
    if (!checkDate(timeToInt(&t->T)))
        return 0;
    if (t->inHospitalOrNot && (!checkDate(timeToInt(&t->start)) || !checkDate(timeToInt(&t->end)) || countDays(&t->start, &t->end) <= 0))
        return 0;
    if (t->officeIndex > NOD)
        return 0;
    if (t->patientIndex > NOP)
        return 0;
    if(!checkPatient(t->patientIndex,t->name))
        return 0;
    if(!checkDoctorWork(t->doctorIndex,getWeekday(&t->T)))
        return 0;
    if(!checkOfficeOfDoctor(t->doctorIndex,t->officeIndex))
        return 0;
    if (dateInt == timeToInt(&t->T))
    {
    
    
        struct patient *p = &Patient[t->patientIndex];
        struct doctor *d = &Doctor[t->doctorIndex];
        if (p->count < 5)
        {
    
    
            int i;
            for (i = 0; i < p->count; ++i)
            {
    
    
                if (p->Office[i] == t->officeIndex)
                    return 0;
            }
            if (d->count >= 20)
                return 0;
            p->Office[p->count++] = t->officeIndex;
            d->count++;
        }
        else
            return 0;
    }
    return 1;
}
bool addOneMedicalRecord(int mode, long long index) //mode 1修改专用,mode 0添加一条
{
    
    
    struct medicalRecord *t = (struct medicalRecord *)malloc(sizeof(struct medicalRecord));
    memset(t, 0, sizeof(struct medicalRecord));
    if (mode == 0)
    {
    
    
        t->index = index;
    }
    if (mode == 1)
    {
    
    
        while (1)
        {
    
    
            printf("请输入诊疗记录编号,输入0退出:\n");
            printf("格式:20200101001(前8位表示日期,后3位表示当日编号).\n");
            long long tempIndex;
            tempIndex=inputNumber(LONGLONGLENGTH,0,0);
            if (tempIndex == 0)
            {
    
    
                printf("操作取消\n");
                getch();
                fflush(stdin);
                free(t);
                t = NULL;
                return 0;
            }
            bool flag;
            if (!checkIndex(tempIndex)||searchRecord(tempIndex,&flag)!=tail)
            {
    
    
                printf("非法编号\n");
                getch();
                fflush(stdin);
                cls();
                continue;
            }
            //printf("%lld",tempIndex);
            t->index = tempIndex;
            break;
        }
    }
    printf("是否是已登记患者?(1/0)\n");
    int flag=inputNumber(INTLENGTH,0,0);
    int p;
    if(flag==0)
    {
    
    
        if (NOP == Patient_max)
        {
    
    
            printf("患者人数溢出!\n");
            getch();
            fflush(stdin);
            return;
        }
        printf("注册患者信息\n");
        printf("请输入患者姓名:\n");
        scanf("%s", Patient[NOP].name);
        printf("请输入患者年龄:\n");
        Patient[NOP].age=inputNumber(INTLENGTH,0,0);
        Patient[NOP].index=NOP+1;                             //生成编号
        NOP++;
        p=NOP;
        cls();
    }
    else
    {
    
    
        while(1)
        {
    
    
            printf("请输入患者编号:\n");                     //检查编号
            p=inputNumber(INTLENGTH,0,0);
            if(p>NOP||p<=0)
            {
    
    
                printf("非法编号\n");
                getch();
                fflush(stdin);
                cls();
            }
            else
                break;
        }
    }
    cls();
    t->patientIndex=p;
    strcpy(t->name,Patient[p-1].name);
    while(1)
    {
    
    
        printf("请输入诊疗记录的时间信息:\n");
        printf("年:\n");
        t->T.year=inputNumber(INTLENGTH,0,0);
        printf("月:\n");
        t->T.month=inputNumber(INTLENGTH,0,0);
        printf("日:\n");
        t->T.day=inputNumber(INTLENGTH,0,0);
        printf("时:\n");
        t->T.hour=inputNumber(INTLENGTH,0,0);
        printf("分:\n");
        t->T.minute=inputNumber(INTLENGTH,0,0);
        if(checkT(&t->T)&&t->T.year*10000+t->T.month*100+t->T.day==t->index/1000)
            break;
        else
        {
    
    
            printf("时间信息有误,请重新输入\n");
            getch();
            fflush(stdin);
            cls();
        }
    }
    cls();
    printf("请输入医生编号:\n");
    t->doctorIndex=inputNumber(INTLENGTH,0,0);
    t->officeIndex=Doctor[t->doctorIndex-1].office;
    int i;
    inputExaminationIndex(t);
    inputMedicineIndexAndAmount(t);
    inputInhospitalInfo(t);
    if(t->inHospitalOrNot)
        while(1)
        {
    
    
            printf("请输入押金:\n");
            t->deposit=inputNumber(LONGLONGLENGTH,1,0);
            struct Time tmp;
            getTime(&tmp);
            int d=countDays(&t->end,&tmp);
            if(d>0&&t->deposit<20000*llmax(5,d))
            {
    
    
                printf("非法押金数额\n");
                getch();
                fflush(stdin);
                cls();
            }
            else
                break;
        }
        cls();
    if (checkMedicalRecord(t))
    {
    
    
        insertRecord(t);
        refresh();
        printf("操作完成\n");
        getch();
        fflush(stdin);
        return 1;
    }
    else
    {
    
    
        printf("输入信息有误,操作失败\n");
        getch();
        fflush(stdin);
        free(t);
        t = NULL;
        return 0;
    }
}
void modify()                                       //修改诊疗记录
{
    
    
    long long index;
    char s[100] = "modify";
    if (index = Delete())
    {
    
    
        if (addOneMedicalRecord(0, index))
            leaveARecordWhenModify(index, s);
    }
}
void refresh()                                        //更新病人、医生结构体相关
{
    
    
    int i;
    for (i = 0; i < NOP; ++i)
    {
    
    
        Patient[i].count = 0;
        memset(Patient[i].Office, 0, sizeof(Patient[i].Office));
    }
    for (i = 0; i < NOD; ++i)
    {
    
    
        Doctor[i].count = 0;
    }
    struct medicalRecord *t = head->next;
    while (t)
    {
    
    
        if (dateInt == timeToInt(&t->T))
        {
    
    
            Patient[t->patientIndex-1].Office[Patient[t->patientIndex-1].count++] = t->officeIndex;
            Doctor[t->doctorIndex-1].count++;
        }
        t = t->next;
    }
}
long long strToInt(char *s)
{
    
    
    long long ans=0,len=strlen(s),i=0;
    if(len>LONGLONGLENGTH)
        return -1;
    bool dot=0;
    while(i<len)
    {
    
    
        if(s[i]>='0'&&s[i]<='9')
        {
    
    
            ans*=10;
            ans+=s[i]-'0';
        }
        else
        {
    
    
            if(s[i]=='-')
                dot=1;
            else if(s[i]=='.');
            else
                return -1;
        }
        i++;
    }
    if(dot)
        return -ans;
    return ans;
}
long long inputNumber(int len,bool dot,bool Minus)
{
    
    
    char tmp[30];
    int cnt=0;
    char key;
    while(1)
    {
    
    
        if(_kbhit())
        {
    
    
            key =_getch();
            if(key<='9'&&key>='0'&&cnt<len)
            {
    
    
                tmp[cnt++]=key;
                printf("%c",key);
            }
            else if(key==13&&cnt>0)
            {
    
    
                tmp[cnt++]='\0';
                printf("\n");
                if(dot)
                    return strToInt(tmp)*100;
                return strToInt(tmp);
            }
            else if(key==8&&cnt>0)
            {
    
    
                HANDLE gh_std_out;
                gh_std_out = GetStdHandle(STD_OUTPUT_HANDLE);
                CONSOLE_SCREEN_BUFFER_INFO bInfo;
                GetConsoleScreenBufferInfo(gh_std_out,&bInfo);
                SetPos(bInfo.dwCursorPosition.X-1,bInfo.dwCursorPosition.Y);
                printf(" ");
                cnt--;
                tmp[cnt]='\0';
                SetPos(bInfo.dwCursorPosition.X-1,bInfo.dwCursorPosition.Y);
            }
            else if(key=='.'&&cnt>0&&dot)
            {
    
    
                tmp[cnt++]='.';
                printf("%c",key);
                int tempCnt=0;
                while(1)
                {
    
    
                    if(_kbhit())
                    {
    
    
                        key =_getch();
                        if(key<='9'&&key>='0'&&cnt<len&&tempCnt<2)
                        {
    
    
                            tmp[cnt++]=key;
                            printf("%c",key);
                            tempCnt++;
                        }
                        else if(key==13&&cnt>0&&tempCnt>0)
                        {
    
    
                            tmp[cnt++]='\0';
                            printf("\n");
                            if(tempCnt==1)
                                return strToInt(tmp)*10;
                            return strToInt(tmp);
                        }
                        else if(key==8&&cnt>0)
                        {
    
    
                            HANDLE gh_std_out;
                            gh_std_out = GetStdHandle(STD_OUTPUT_HANDLE);
                            CONSOLE_SCREEN_BUFFER_INFO bInfo;
                            GetConsoleScreenBufferInfo(gh_std_out,&bInfo);
                            SetPos(bInfo.dwCursorPosition.X-1,bInfo.dwCursorPosition.Y);
                            printf(" ");
                            cnt--;
                            tmp[cnt]='\0';
                            SetPos(bInfo.dwCursorPosition.X-1,bInfo.dwCursorPosition.Y);
                            if(tempCnt>0)
                                tempCnt--;
                            else
                                break;
                        }
                    }
                }
                continue;
            }
            else if(key=='-'&&cnt==0&&Minus)
            {
    
    
                tmp[cnt++]=key;
                printf("%c",key);
                int tempCnt=0;
                while(1)
                {
    
    
                    if(_kbhit())
                    {
    
    
                        key =_getch();
                        if(key=='1'&&cnt<len&&tempCnt<1)
                        {
    
    
                            tmp[cnt++]=key;
                            printf("%c",key);
                            tempCnt++;
                        }
                        else if(key==13&&cnt>0&&tempCnt>0)
                        {
    
    
                            tmp[cnt++]='\0';
                            printf("\n");
                            return strToInt(tmp);
                        }
                        else if(key==8&&cnt>0)
                        {
    
    
                            HANDLE gh_std_out;
                            gh_std_out = GetStdHandle(STD_OUTPUT_HANDLE);
                            CONSOLE_SCREEN_BUFFER_INFO bInfo;
                            GetConsoleScreenBufferInfo(gh_std_out,&bInfo);
                            SetPos(bInfo.dwCursorPosition.X-1,bInfo.dwCursorPosition.Y);
                            printf(" ");
                            cnt--;
                            tmp[cnt]='\0';
                            SetPos(bInfo.dwCursorPosition.X-1,bInfo.dwCursorPosition.Y);
                            if(tempCnt>0)
                                tempCnt--;
                            else
                                break;
                        }
                    }
                }
            }
        }
    }
    return 0;
}
bool checkT(const struct Time* a)
{
    
    
    if(a->hour<24&&a->minute<60&&checkDate(a->year*10000+a->month*100+a->day))
        return 1;
    return 0;
}
bool readNumberAndJudge(FILE*f,int *a)
{
    
    
    char s[100];
    fscanf(f,"%s",s);
    *a=strToInt(s);
    if(*a<0)
    {
    
    
        printf("文件存在错误,停止导入\n");
        getch();
        fflush(stdin);
        return 0;
    }
    return 1;
}

void saveData()
{
    
    
    struct medicalRecord* t=head->next;
    int tmp,cnt;
    if(t)
    {
    
    
        tmp=timeToInt(&t->T);
        cnt=0;
    }
    while(t)
    {
    
    
        FILE*f;
        char s[100];
        int tmp1=timeToInt(&t->T);
        intToStr(tmp,s);
        if((f=fopen(s,"w"))==NULL)
        {
    
    
            printf("保存失败\n");
            getch();
            fflush(stdin);
            return;
        }
        while(tmp1==tmp)
        {
    
    
            //printf("%d",tmp1);
            //getch();
            fflush(stdin);
            fprintf(f,"%lld %s %d %d %d %d %d ",t->index,t->name,t->T.year,t->T.month,t->T.day,t->T.hour,t->T.minute);
            fprintf(f,"%d %d %d ",t->patientIndex,t->doctorIndex,t->officeIndex);
            int i;
            for(i=0;i<NOE;++i)
                fprintf(f,"%d ",t->exam[i]);
            for(i=0;i<NOM;++i)
                fprintf(f,"%d ",t->medicine[i]);
            fprintf(f,"%d ",t->inHospitalOrNot);
            if(t->inHospitalOrNot)
            {
    
    
                fprintf(f,"%d %d %d %d %d ",t->start.year,t->start.month,t->start.day,t->start.hour,t->start.minute);
                fprintf(f,"%d %d %d %d %d ",t->end.year,t->end.month,t->end.day,t->end.hour,t->end.minute);
                fprintf(f,"%lld",t->deposit);
            }
            fprintf(f,"\n");
            t=t->next;
            if(t)
                tmp1=timeToInt(&t->T);
            else
                break;
        }
        fclose(f);
        tmp=tmp1;
    }
    FILE* f;
    if((f=fopen("patientInfo.txt","w"))==NULL)
    {
    
    
            printf("病人信息保存失败\n");
            getch();
            fflush(stdin);
            return;
    }
    for(int i=0;i<NOP;++i)
    {
    
    
        fprintf(f,"%s\t%5d\t%5d\n",Patient[i].name,Patient[i].index,Patient[i].age);
    }
    fclose(f);
}
/*函数定义部分结束*/

#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>

#include"jiang.h"
#include"inform.h"
#include"yh.h"

#define SIZE 256

char start_day[13];
char end_day[13];
char str[SIZE];
void delete_file(){
    
    
    while(1)
    {
    
    
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                    请输入要删除的诊疗记录所在的日期                                                               \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        char tfp[13];
        scanf("%s",tfp);
        //不符合则警告
        tfp[8]='.';
        tfp[9]='t';
        tfp[10]='x';
        tfp[11]='t';
        tfp[12]='\0';
        FILE * fp=NULL;
        if((fp=fopen(tfp,"r+"))==NULL)
        {
    
    
            printf("要删除的诊疗记录所在日期文件并不存在,请确认输入是否正确,进行任意输入后按回车退出\n");
            char t[10];
            scanf("\n%s",t);
            break;
        }
        int key=0;
        pNode head=NULL;//哨兵
        head=(pNode)malloc(sizeof(Node));
        pNode now=(pNode)malloc(sizeof(Node));
        now=head;
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                    请输入要删除的诊疗记录的挂号编码                                                               \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        char target[12];
        scanf("%s",target);
        int number=0;

        int flag=0;
        int temp=ftell(fp);

        char Des[SIZE]={
    
    };
        char exam[SIZE]={
    
    };
        char medicine[SIZE]={
    
    };

        while(!feof(fp))
        {
    
    
            char p[13];//临时保存时间,12位
            char q[20];//临时保存姓名
            char r[12];//临时保存挂号编码
            char remain[SIZE];//保存剩余的相对查找的无用数据
            char blank[]="        ";
            temp=ftell(fp);
            //判断
            fscanf(fp,"%s        %s        %s",p,q,r);
            fgets(remain,SIZE,fp);
            //要删除
            if(strcmp(r,target)==0&&!key)
            {
    
    
                strcat(Des,p);
                strcat(Des,blank);
                strcat(Des,q);
                strcat(Des,blank);
                strcat(Des,r);
                strcat(Des,remain);//记录情况

                fgets(exam,SIZE,fp);//到第二行
                fgets(medicine,SIZE,fp);//到第三行

                flag=1;
                key=1;

                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("                                                      是否进行保存(保存请输入1,否则输入0)                                                       \n");
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                int judge;
                scanf("%d",&judge);//是否保存
                if(judge)
                {
    
    
                    printf("                                                               删除的信息为                                                                      \n");
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|   住院开始时间   |   住院结束时间   |住院押金|\n");
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("|记录情况|%s",Des);
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("|检查情况|%s",exam);
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("|药品情况|%s",medicine);
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    fgetc(fp);//吃换行
                }else{
    
    
                    pNode son=(pNode)malloc(sizeof(Node));
                    son->next=NULL;
                    now->next=son;
                    fseek(fp,temp,0);
                    fgets(remain,SIZE,fp);
                    strcpy(son->str1,remain);//给到
                    fgets(remain,SIZE,fp);
                    strcpy(son->str2,remain);
                    fgets(remain,SIZE,fp);
                    strcpy(son->str3,remain);
                    now=now->next;//成为下一个
                    fgetc(fp);//吃换行
                    number++;
                }
            }
            else
            {
    
    
                pNode son=(pNode)malloc(sizeof(Node));
                son->next=NULL;
                now->next=son;
                fseek(fp,temp,0);
                fgets(remain,SIZE,fp);
                strcpy(son->str1,remain);//给到
                fgets(remain,SIZE,fp);
                strcpy(son->str2,remain);
                fgets(remain,SIZE,fp);
                strcpy(son->str3,remain);
                now=now->next;//成为下一个
                fgetc(fp);//吃换行
                number++;
            }
        }
        //写入文件
        fclose(fp);
        fp=fopen(tfp,"w");
        now=head;
        head=head->next;
        //int i=0;
        while(head&&--number)
        {
    
    
            fprintf(fp,"%s",head->str1);
            fprintf(fp,"%s",head->str2);
            fprintf(fp,"%s\n",head->str3);

            head=head->next;
        }
        fclose(fp);
        if(now!=NULL) release(now);

        if(!flag){
    
    
            printf("要删除的诊疗记录所在日期文件并不存在,请确认输入是否正确,进行任意输入后按回车退出\n");
            char t[10];
            scanf("\n%s",t);
            break;
        }
        if(flag){
    
    
            //留痕迹
            fp=fopen("query_log.txt","a");
            record(fp);
            fprintf(fp,"\t\t删除文件%s的诊疗信息\n",tfp);
            fprintf(fp,"删除的信息为:\n");
            fprintf(fp,"%s",Des);
            fprintf(fp,"%s",exam);
            fprintf(fp,"%s\n",medicine);
            fclose(fp);
        }
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                              输入0退出,输入1继续                                                                 \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        int next;
        scanf("%d",&next);
        if(!next) break;
    }
    return;
}
void add_file(){
    
    
    while(1)
    {
    
    
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                    请输入要增加的诊疗记录所在的日期                                                               \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        char tfp[13];
        scanf("%s",tfp);
        //不符合则警告
        tfp[8]='.';
        tfp[9]='t';
        tfp[10]='x';
        tfp[11]='t';
        tfp[12]='\0';
        FILE * fp=NULL;

        if((fp=fopen(tfp,"r+"))==NULL)
        {
    
    
            printf("对应日期的诊疗记录文件未生成,请确认输入是否正确,进行任意输入后按回车退出");
            char t[10];
            scanf("\n%s",t);
            break;
        }

        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                    请输入要增加的诊疗记录的具体时间                                                               \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        char time_p[13];
        scanf("%s",time_p);

        pNode head=NULL;//哨兵
        head=(pNode)malloc(sizeof(Node));
        pNode now=(pNode)malloc(sizeof(Node));
        now=head;
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                    请输入要增加的的诊疗记录的挂号编码                                                             \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        char target[12];
        scanf("%s",target);
        int number=0;

        char des[30][SIZE]= {
    
    };
        char exam[SIZE]= {
    
    };
        char medicine[SIZE]= {
    
    };

        char blank[]="        ";
        char List[][20]= {
    
    "患者姓名","当天序号","患者卡号","医生工号","科室号码","住院开始时间","住院结束时间","住院押金"};
        //按照顺序进行格式化修改
        for(int i=0,j=0; j<9; i+=2)
        {
    
    
            if(!j)
            {
    
    
                strcpy(des[i],time_p);//时间默认不变
                j++;
                continue;
            }
            strcpy(des[i-1],blank);//中间用空格分隔
            if(i/2==2)
            {
    
    
                strcpy(des[i],target);//挂号编码默认不变
                continue;
            }
            printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
            printf("                                                            请输入增加的%s                                                                         \n",List[j++-1]);
            printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
            scanf("%s",des[i]);

        }
        //进行另外两项的增加
        getchar();
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                              请输入修改后的检查情况(格式:检查名    数量    单价    ……合计    总价格(元))                                   \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        gets(exam);
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                              请输入修改后的药品情况(格式:药品名    数量    单价    ……合计    总价格(元))                                   \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        gets(medicine);
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                      是否进行保存(保存请输入1,否则输入0)                                                       \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        int judge;
        scanf("%d",&judge);//是否保存
        if(judge)
        {
    
    
            char blank[]="        ";//准备之后的格式化fprintf
            for(int i=1; i<30; i++)
            {
    
    
                strcat(des[0],des[i]);//全部合并到des[0]
            }
            int m=strlen(des[0]);
            des[0][m]='\n';
            m=strlen(exam);
            exam[m]='\n';
            m=strlen(medicine);
            medicine[m]='\n';
            printf("                                                                增加的信息为                                                                       \n");
            printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
            printf("|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|   住院开始时间   |   住院结束时间   |住院押金|\n");
            printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
            printf("|记录情况|%s",des[0]);
            printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
            printf("|检查情况|%s",exam);
            printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
            printf("|药品情况|%s",medicine);
            printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");


            char remain[SIZE]= {
    
    };
            while(!feof(fp))
            {
    
    
                pNode son=(pNode)malloc(sizeof(Node));
                son->next=NULL;
                now->next=son;

                fgets(remain,SIZE,fp);
                strcpy(son->str1,remain);//给到
                fgets(remain,SIZE,fp);
                strcpy(son->str2,remain);
                fgets(remain,SIZE,fp);
                strcpy(son->str3,remain);

                now=now->next;//成为下一个
                fgetc(fp);//吃换行
                number++;
            }

            fclose(fp);
            fp=fopen(tfp,"w");
            now=head;
            head=head->next;

            int flag=0;
            while(head&&--number)
            {
    
    
                if(flag||strcmp(head->str1,time_p)<=0)
                {
    
    
                    fprintf(fp,"%s",head->str1);
                    fprintf(fp,"%s",head->str2);
                    fprintf(fp,"%s\n",head->str3);
                    head=head->next;
                }
                else
                {
    
    
                    flag=1;
                    fprintf(fp,"%s",des[0]);
                    fprintf(fp,"%s",exam);
                    fprintf(fp,"%s\n",medicine);
                    number++;
                }
            }
            if(!flag)
            {
    
    
                fprintf(fp,"%s",des[0]);
                fprintf(fp,"%s",exam);
                fprintf(fp,"%s\n",medicine);
            }
        }
        fclose(fp);
        if(now!=NULL) release(now);

        //痕迹
        fp=fopen("query_log.txt","a");
        record(fp);
        fprintf(fp,"\t\t向文件%s中增加诊疗信息\n",tfp);
        fprintf(fp,"增加的信息为:\n");
        fprintf(fp,"%s",des[0]);
        fprintf(fp,"%s",exam);
        fprintf(fp,"%s\n",medicine);
        fclose(fp);

        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                              输入0退出,输入1继续                                                                 \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        int next;
        scanf("%d",&next);
        if(!next) break;
    }
    return;
}
void modify_file(){
    
    
    while(1)
    {
    
    
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                    请输入要修改的诊疗记录所在的日期                                                               \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        char tfp[13];
        scanf("%s",tfp);
        //不符合则警告
        tfp[8]='.';
        tfp[9]='t';
        tfp[10]='x';
        tfp[11]='t';
        tfp[12]='\0';
        FILE * fp=NULL;
        if((fp=fopen(tfp,"r+"))==NULL)
        {
    
    
            printf("要修改的诊疗记录不存在,请确认输入是否正确,进行任意输入后按回车退出\n");
            char t[10];
            scanf("\n%s",t);
            break;
        }
        int key=0;
        pNode head=NULL;//哨兵
        head=(pNode)malloc(sizeof(Node));
        pNode now=(pNode)malloc(sizeof(Node));
        now=head;
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                    请输入要修改的诊疗记录的挂号编码                                                               \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        char target[12];
        scanf("%s",target);
        int number=0;
        char des[30][SIZE]= {
    
    };
        int flag=0;
        int temp=ftell(fp);
        char Des[2][SIZE]={
    
    };
        char Exam[2][SIZE]={
    
    };
        char Medicine[2][SIZE]={
    
    };
        while(!feof(fp))
        {
    
    
            char p[13];//临时保存时间,12位
            char q[20];//临时保存姓名
            char r[12];//临时保存挂号编码
            char remain[SIZE];//保存剩余的相对查找的无用数据
            char blank[]="        ";
            temp=ftell(fp);

            fscanf(fp,"%s        %s        %s",p,q,r);
            fgets(remain,SIZE,fp);

            if(strcmp(r,target)==0&&!key)
            {
    
    

                strcat(Des[0],p);
                strcat(Des[0],blank);
                strcat(Des[0],q);
                strcat(Des[0],blank);
                strcat(Des[0],r);
                strcat(Des[0],remain);

                flag=1;
                key=1;
                char List[][20]= {
    
    "患者姓名","当天序号","患者卡号","医生工号","科室号码","住院开始时间","住院结束时间","住院押金"};
                //按照顺序进行格式化修改
                for(int i=0,j=0; j<9; i+=2)
                {
    
    
                    if(!j)
                    {
    
    
                        strcpy(des[i],p);//时间默认不变
                        j++;
                        continue;
                    }
                    strcpy(des[i-1],blank);//中间用空格分隔
                    if(i/2==2)
                    {
    
    
                        strcpy(des[i],target);//挂号编码默认不变
                        continue;
                    }
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("                                                            请输入修改后的%s                                                                       \n",List[j++-1]);
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    scanf("%s",des[i]);

                }
                //进行另外两项的修改
                getchar();
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("                              请输入修改后的检查情况(格式:检查名    数量    单价    ……合计    总价格(元))                                   \n");
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                char exam[SIZE]= {
    
    };
                gets(exam);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("                              请输入修改后的药品情况(格式:药品名    数量    单价    ……合计    总价格(元))                                   \n");
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                char medicine[SIZE]= {
    
    };
                gets(medicine);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("                                                      是否进行保存(保存请输入1,否则输入0)                                                       \n");
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                int judge;
                scanf("%d",&judge);//是否保存
                if(judge)
                {
    
    
                    char blank[]="        ";//准备之后的格式化fprintf
                    for(int i=1; i<30; i++)
                    {
    
    
                        strcat(des[0],des[i]);//全部合并到des[0]
                    }
                    int m=strlen(des[0]);
                    des[0][m]='\n';
                    m=strlen(exam);
                    exam[m]='\n';
                    m=strlen(medicine);
                    medicine[m]='\n';

                    printf("                                                               修改后的信息为                                                                      \n");
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|   住院开始时间   |   住院结束时间   |住院押金|\n");
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("|记录情况|%s",des[0]);
                    strcpy(Des[1],des[0]);
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("|检查情况|%s",exam);
                    strcpy(Exam[1],exam);
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    printf("|药品情况|%s",medicine);
                    strcpy(Medicine[1],medicine);
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");

                    pNode son=(pNode)malloc(sizeof(Node));
                    son->next=NULL;//设置为尾节点
                    now->next=son;//链接
                    strcpy(son->str1,des[0]);//给到
                    strcpy(son->str2,exam);
                    strcpy(son->str3,medicine);
                    now=now->next;//成为下一个

                    fgets(remain,SIZE,fp);//到第二行
                    strcpy(Exam[0],remain);

                    fgets(remain,SIZE,fp);//到第三行
                    strcpy(Medicine[0],remain);

                    fgetc(fp);//吃换行
                    number++;
                }
            }
            else
            {
    
    
                pNode son=(pNode)malloc(sizeof(Node));
                son->next=NULL;
                now->next=son;
                fseek(fp,temp,0);
                fgets(remain,SIZE,fp);
                strcpy(son->str1,remain);//给到
                fgets(remain,SIZE,fp);
                strcpy(son->str2,remain);
                fgets(remain,SIZE,fp);
                strcpy(son->str3,remain);
                now=now->next;//成为下一个
                fgetc(fp);//吃换行
                number++;
            }
        }
        //写入文件
        fclose(fp);
        fp=fopen(tfp,"w");
        now=head;
        head=head->next;

        while(head&&--number)
        {
    
    
            fprintf(fp,"%s",head->str1);
            fprintf(fp,"%s",head->str2);
            fprintf(fp,"%s\n",head->str3);

            head=head->next;
        }
        fclose(fp);
        if(now!=NULL) release(now);

        if(!flag){
    
    
            printf("要修改的诊疗记录不存在,请确认输入是否正确,进行任意输入后按回车退出\n");
            char t[10];
            scanf("\n%s",t);
            break;
        }
        if(flag){
    
    
            //留痕迹
            fp=fopen("query_log.txt","a");
            record(fp);
            fprintf(fp,"\t\t修改文件%s的诊疗信息\n",tfp);
            fprintf(fp,"修改之前的信息为:\n");
            fprintf(fp,"%s",Des[0]);
            fprintf(fp,"%s",Exam[0]);
            fprintf(fp,"%s\n",Medicine[0]);
            fprintf(fp,"修改之后的信息为:\n");
            fprintf(fp,"%s",Des[1]);
            fprintf(fp,"%s",Exam[1]);
            fprintf(fp,"%s\n",Medicine[1]);
            fclose(fp);
        }
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        printf("                                                              输入0退出,输入1继续                                                                 \n");
        printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
        int next;
        scanf("%d",&next);
        if(!next) break;
    }
    return;
}
void release(pNode head){
    
    
    while(head){
    
    
        pNode * last=&head;
        head=head->next;
        free(*last);
    }
    return;//释放内存
}
/*

//修改样例
20200928
20200928003
苏烟
001
080
04
01
202009281230
202010101830
140000
体重    500        直肠指测    5000        全身体格检查    15000        血常规    2500        便常规    2000        合计    25000
安神补心丸    1    895        丙戊酸镁片    4    340        去痛片    2    1300        碘伏    1    310        合计    5165

//增加样例
20200928
202009281250
20200928003
郭子轩
001
080
04
01
202009281230
202010101830
140000
体重    500        直肠指测    5000        全身体格检查    15000        血常规    2500        便常规    2000        合计    25000
安神补心丸    1    895        丙戊酸镁片    4    340        去痛片    2    1300        碘伏    1    310        合计    5165

*/
void search_office(int Office_id,struct medicalRecord * head)
{
    
    
    //把科室的号码转化成字符串
    char office_id[3];
    format(Office_id,2,office_id);
    office_id[2]='\0';

    //文件指针
    FILE *fp=NULL;
    char mix[13];//={"20200928.txt"};
    strcpy(mix,start_day);

    int sum=0;

    while((fp=fopen(mix,"r"))!=NULL)
    {
    
    
        while(!feof(fp))
        {
    
    
            char office_num[3]; //获得当前的科室字符串
            char temp[10][50];
            office_num[2]='\0';
            fscanf(fp,"%s        %s        %s        %s        %s        %s        %s%s",temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],office_num,temp[6]);//获得当前的科室字符串
            if(!strcmp(office_num,office_id))
            {
    
    
                sum++;
                printf("\n%d.\n",sum);
                fgets(str,SIZE,fp);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|   住院开始时间   |   住院结束时间   |住院押金|\n");
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("|记录情况|%s        %s        %s        %s        %s        %s        %s        %s%s",temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],office_num,temp[6],str);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                for(int i=0; i<2; i++)
                {
    
    
                    fgets(str,SIZE,fp);
                    if(!i)
                    {
    
    
                        printf("|检查情况|");
                    }
                    else
                    {
    
    
                        printf("|药品情况|");
                    }
                    printf("%s",str);
                    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    //若为当前查询科室,则直接输出,并且加上一个回车
                }
                fgets(str,SIZE,fp);
            }
            else
            {
    
    
                fgets(str,SIZE,fp);
                fgets(str,SIZE,fp);
                fgets(str,SIZE,fp);
            }
        }
        update(mix);
        fclose(fp);
        if(strcmp(mix,end_day)==0) break;
        fp=fopen(mix,"r");
    }
    //链表中的信息输出
    while(head)
    {
    
    
        if(head->officeIndex==Office_id){
    
    
            print(head);
            sum++;
        }
        head=head->next;
    }
    if(!sum) printf("当前科室诊疗记录为空\n");//若没有则显示为空

    ///加入到查询记录之中
    ///权限为:附加写
    fp=fopen("query_log.txt","a");
    record(fp);
    fprintf(fp,"\t\t查询科室%s的诊疗信息\n",office_id);
    return;
}//按照(时间顺序)一定顺序打印某个科室的诊疗信息
void search_doctor(int Doctor_id,struct medicalRecord * head)
{
    
    
    int sum=0;

    char temp[3];
    format(Doctor_id,2,temp);
    temp[2]='\0';

    FILE *fp=NULL;
    char mix[13];
    strcpy(mix,start_day);

    while((fp=fopen(mix,"r"))!=NULL)
    {
    
    
        while(!feof(fp))
        {
    
    
            char doc_num[4];
            char Temp[10][50];
            fscanf(fp,"%s        %s        %s        %s        %s        %s        %s",Temp[0],Temp[1],Temp[2],Temp[3],Temp[4],doc_num,Temp[5]);//获得当前的科室字符串
            doc_num[3]='\0';
            if(!strcmp(doc_num,temp))
            {
    
    
                sum++;
                printf("\n%d.\n",sum);
                fgets(str,SIZE,fp);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|   住院开始时间   |   住院结束时间   |住院押金|\n");
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("|记录情况|%s        %s        %s        %s        %s        %s        %s%s",Temp[0],Temp[1],Temp[2],Temp[3],Temp[4],doc_num,Temp[5],str);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                for(int i=0; i<2; i++)
                {
    
    
                    fgets(str,SIZE,fp);
                    if(!i)
                    {
    
    
                        printf("|检查情况|");
                    }
                    else
                    {
    
    
                        printf("|药品情况|");
                    }
                printf("%s",str);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                    //若为当前查询医生,则直接输出,并且加上一个回车
                }
                fgets(str,SIZE,fp);
            }
            else
            {
    
    
                fgets(str,SIZE,fp);
                fgets(str,SIZE,fp);
                fgets(str,SIZE,fp);//将指针向下走三行
            }
        }
        update(mix);
        fclose(fp);
        if(strcmp(mix,end_day)==0) break;
        fp=fopen(mix,"r");
    }
    while(head)
    {
    
    
        if(head->doctorIndex==Doctor_id){
    
    
            print(head);
            sum++;
        }
        head=head->next;
    }
    if(!sum) printf("当前医生诊疗记录为空\n");//若没有则显示为空

    ///加入到查询记录之中
    ///权限为:附加写
    fp=fopen("query_log.txt","a");
    record(fp);
    fprintf(fp,"\t\t查询工号为%s的医生的诊疗信息\n",temp);

    return;
}        //按照一定顺序打印某位医生的诊疗信息
void search_patient(int Patient_id,struct medicalRecord * head)
{
    
    
    int sum=0;

    char temp[4];
    format(Patient_id,3,temp);
    temp[3]='\0';

    FILE * fp=NULL;
    char mix[13];//={"20200928.txt"};
    strcpy(mix,start_day);
    fp=fopen(mix,"r");
//|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|住院开始时间|住院结束时间|住院押金|
//|记录情况|202009281240        梁志业        20200928002        002        070        13        04        NULL        NULL        0
    while(fp)
    {
    
    
        while(!feof(fp))
        {
    
    
            char pat_num[4];
            char Temp[10][50];
            fscanf(fp,"%s        %s        %s        %s        %s        %s",Temp[0],Temp[1],Temp[2],Temp[3],pat_num,Temp[5]);//获得当前的科室字符串
            pat_num[3]='\0';
            if(!strcmp(pat_num,temp))
            {
    
    
                sum++;
                printf("\n%d.\n",sum);
                fgets(str,SIZE,fp);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|   住院开始时间   |   住院结束时间   |住院押金|\n");
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("|记录情况|%s        %s        %s        %s        %s        %s%s",Temp[0],Temp[1],Temp[2],Temp[3],pat_num,Temp[5],str);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                for(int i=0; i<2; i++)
                {
    
    
                    fgets(str,SIZE,fp);
                    if(!i)
                    {
    
    
                        printf("|检查情况|");
                    }
                    else
                    {
    
    
                        printf("|药品情况|");
                    }
                    printf("%s",str);//若为当前查询患者,则直接输出,并且加上一个回车
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                }
                fgets(str,SIZE,fp);
            }
            else
            {
    
    
                fgets(str,SIZE,fp);
                fgets(str,SIZE,fp);
                fgets(str,SIZE,fp);
            }
        }
        update(mix);
        fclose(fp);
        if(strcmp(mix,end_day)==0) break;
        fp=fopen(mix,"r");
    }
    while(head)
    {
    
    
        if(head->patientIndex==Patient_id){
    
    
            print(head);
            sum++;
        }
        head=head->next;
    }
    if(sum==0) printf("当前患者诊疗记录为空,请确认输入是否正确\n");//若没有则显示为空

    ///加入到查询记录之中
    ///权限为:附加写
    fp=fopen("query_log.txt","a");
    record(fp);
    fprintf(fp,"\t\t查询号码为%s的患者的诊疗信息\n",temp);

    return;
}      //按照一定顺序打印某位患者的历史诊疗信息
void history(struct Time start,struct Time end,struct medicalRecord * head)
{
    
    
    int sum=0;
    char time_start[9];
    char time_end[9];
    int temp1=start.year;
    int temp2=end.year;
    for(int i=0; i<4; i++)
    {
    
    
        time_start[3-i]=temp1%10+'0';
        time_end[3-i]=temp2%10+'0';
        temp1/=10;
        temp2/=10;
    }//生成
    temp1=start.month;
    temp2=end.month;
    for(int i=0; i<2; i++)
    {
    
    
        time_start[5-i]=temp1%10+'0';
        time_end[5-i]=temp2%10+'0';
        temp1/=10;
        temp2/=10;
    }//生成
    temp1=start.day;
    temp2=end.day;
    for(int i=0; i<2; i++)
    {
    
    
        time_start[7-i]=temp1%10+'0';
        time_end[7-i]=temp2%10+'0';
        temp1/=10;
        temp2/=10;
    }//生成

    temp1=start.hour;
    temp2=end.hour;
    char l[5]={
    
    },r[5]={
    
    };
    for(int i=0; i<2; i++)
    {
    
    
        l[1-i]=temp1%10+'0';
        r[1-i]=temp2%10+'0';
        temp1/=10;
        temp2/=10;
    }
    temp1=start.minute;
    temp2=end.minute;
    for(int i=0; i<2; i++)
    {
    
    
        l[3-i]=temp1%10+'0';
        r[3-i]=temp2%10+'0';
        temp1/=10;
        temp2/=10;
    }

    time_start[8]='\0';
    time_end[8]='\0';
    //将日期生成,已经可以进行搜索文件
    char time_now[13];
    strcpy(time_now,time_start);

    time_now[8]='.';
    time_now[9]='t';
    time_now[10]='x';
    time_now[11]='t';
    time_now[12]='\0';

    char time_start_p[13];
    strcpy(time_start_p,time_start);
    strcat(time_start_p,l);

    char time_end_p[13];
    strcpy(time_end_p,time_end);
    strcat(time_end_p,r);

    FILE * fp=fopen(time_now,"r");
    if(fp==NULL)
    {
    
    
        printf("当前查询时间段诊疗记录为空,请确认输入是否正确\n");
        return ;
    }//异常处理
    //printf("%d   %d   %d",strcmp(time_now,time_end)>=0,strcmp(time_now,time_start)<=0,strcmp(time_now,time_end)>=0&&strcmp(time_now,time_start)<=0);
    while(fp)
    {
    
    
        while(!feof(fp))
        {
    
    
            fgets(str,SIZE,fp);
            char temp[]= {
    
    str[8],str[9],str[10],str[11],'\0'};
            for(int i=0;i<5;i++) time_now[8+i]='\0';
            strcat(time_now,temp);//当前读取的时间
            //printf("%s %s %s",time_now,time_start_p,time_end_p);
            //printf("%d   %d   %d",strcmp(time_now,time_end)>=0,strcmp(time_now,time_start)<=0,strcmp(time_now,time_end)>=0&&strcmp(time_now,time_start)<=0);
            if(strcmp(time_now,time_end_p)<=0&&strcmp(time_now,time_start_p)>=0)//如果满足时间要求则输出
            {
    
    
                sum++;//则已经能一个满足,不为空
                printf("\n%d.\n",sum);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|   住院开始时间   |   住院结束时间   |住院押金|\n");
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                printf("|记录情况|%s",str);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
                for(int i=0; i<2; i++)
                {
    
    
                    fgets(str,SIZE,fp);
                    if(!i)
                    {
    
    
                        printf("|检查情况|");
                    }
                    else
                    {
    
    
                        printf("|药品情况|");
                    }
                printf("%s",str);
                printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");

                }
                printf("\n");
                fgets(str,SIZE,fp);
            }
            else break;//按照时间排列,若有一个不满足,则后面的都不满足
        }
        update(time_now);
        time_now[8]='.';
        time_now[9]='t';
        time_now[10]='x';
        time_now[11]='t';
        time_now[12]='\0';
        fclose(fp);
        fp=fopen(time_now,"r");
        if(strcmp(time_now,end_day)<0) break;
        fp=fopen(time_now,"r");
    }
    while(head)
    {
    
    
        //printf("链表中的信息:\n");
        struct Time * ft=&(head->T);
        int temp=ft->year;
        for(int i=0; i<4; i++)
        {
    
    
            str[3-i]=temp%10;
            temp/=10;
        }
        temp=ft->month;
        for(int i=0; i<2; i++)
        {
    
    
            str[5-i]=temp%10;
            temp/=10;
        }
        temp=ft->day;
        for(int i=0; i<2; i++)
        {
    
    
            str[7-i]=temp%10;
            temp/=10;
        }
        temp=ft->hour;
        for(int i=0; i<2; i++)
        {
    
    
            str[9-i]=temp%10;
            temp/=10;
        }
        temp=ft->minute;
        for(int i=0; i<2; i++)
        {
    
    
            str[11-i]=temp%10;
            temp/=10;
        }//格式化
        str[12]='\0';
        if(strcmp(str,time_start_p)<=0&&strcmp(str,time_end_p)>=0)
        {
    
    
            print(head);
            sum++;
        }else break;
        head=head->next;
    }
    if(!sum) printf("当前查询时间段诊疗记录为空\n");

    ///加入到查询记录之中
    ///权限为:附加写
    fp=fopen("query_log.txt","a");
    record(fp);
    fprintf(fp,"\t\t查询从%s到%s的历史诊疗记录\n",time_start,time_end);

    return;
}    //打印某段时间范围内的所有诊疗信息
void update(char date[])
{
    
    
    int month[12]= {
    
    31,28,31,30,31,30,31,31,30,31,30,31}; //比较时间不考虑年
    int day=10*(date[6]-'0')+date[7]-'0';
    day++;
    int mon=10*(date[4]-'0')+date[5]-'0';
    if(day>month[mon-1])
    {
    
    
        mon++;
        day=1;
    }
    date[4]=mon/10+'0';
    date[5]=mon%10+'0';
    date[6]=day/10+'0';
    date[7]=day%10+'0';
}
void print(struct medicalRecord * head)
{
    
    
    if(head==NULL) return;
    struct Time * ft=&(head->T);
    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
    printf("|  目录  |   时间        |     姓名     |   挂号编码       | 当天序号 | 患者卡号 | 医生工号| 科室号|   住院开始时间   |   住院结束时间   |住院押金|\n");
    printf("---------------------------------------------------------------------------------------------------------------------------------------------------\n");
    printf("|记录情况|%d%d%d%d%d",ft->year,ft->month,ft->day,ft->hour,ft->minute);
    printf("\t\t");
    printf("%6s",head->name);
    printf("\t\t");
    char patientIndex[4];
    format(head->patientIndex,3,patientIndex);
    printf("%3s",patientIndex);
    printf("\t\t");
    char index[12];
    format(head->index,11,index);
    printf("%11s",index);
    printf("\t\t");
    char office[3];
    format(head->officeIndex,2,office);
    printf("%2s",office);
    printf("\t\t");
    char doctor[3];
    format(head->doctorIndex,2,office);
    printf("%2s",doctor);
    printf("\t\t");
    if(head->inHospitalOrNot)
    {
    
    
        ft=&(head->start);
        printf("%d%d%d%d%d",ft->year,ft->month,ft->day,ft->hour,ft->minute);
        printf("\t\t");
        ft=&(head->end);
        printf("%d%d%d%d%d",ft->year,ft->month,ft->day,ft->hour,ft->minute);
        printf("\t\t");
        printf("%d\n",(int)head->deposit);
    }
    else
    {
    
    
        printf("NULL\t\tNULL\t\t0\n");
    }
    int sum_exam=0;
    printf("|检查情况|");
    for(int i=0; i<Examination_max; i++)
        if(head->exam[i])
        {
    
    
            printf("%s",Exam[i].name);
            printf("\t");
            int all=Exam[i].price;
            sum_exam+=all;
            int cent=all%10;
            all/=10;
            int jiao=all%10;
            all/=10;
            int yuan=all;
            printf("单价:%d元%d角%d分",yuan,jiao,cent);
            printf("\t\t");
        }
    int cent=sum_exam%10;
    sum_exam/=10;
    int jiao=sum_exam%10;
    sum_exam/=10;
    int yuan=sum_exam;
    printf("合计:%d元%d角%d分\n",yuan,jiao,cent);
    int sum_medicine=0;
    printf("|药品情况|");
    for(int i=0; i<Medicine_number_max; i++)
        if(head->medicine[i])
        {
    
    
            printf("%s",Medicine[i].name);
            printf("\t");
            int all=Medicine[i].price;
            sum_exam+=all*(head->medicine[i]);
            int cent=all%10;
            all/=10;
            int jiao=all%10;
            all/=10;
            int yuan=all;
            printf("数量:%d",head->medicine[i]);
            printf("\t\t");
            printf("单价:%d元%d角%d分",yuan,jiao,cent);
            printf("\t");
        }
    cent=sum_medicine%10;
    sum_medicine/=10;
    jiao=sum_medicine%10;
    sum_medicine/=10;
    yuan=sum_medicine;
    printf("合计:%d元%d角%d分\n",yuan,jiao,cent);
    return;
}
void format(int a,int n,char t[])
{
    
    
    for(int i=0; i<n; i++)
    {
    
    
        t[n-i-1]=a%10+'0';
        a/=10;
    }
}
void record(FILE * fp)
{
    
    
    if(fp==NULL) exit(1);
    time_t timep;
    struct tm *p;
    time (&timep);
    p=localtime(&timep);
    fprintf(fp,"\n%d-",1900+p->tm_year);      /*获取当前年份,从1900开始,所以要加1900*/
    fprintf(fp,"%d-",1+p->tm_mon);          /*获取当前月份,范围是0-11,所以要加1*/
    fprintf(fp,"%d ",p->tm_mday);           /*获取当前月份日数,范围是1-31*/
    fprintf(fp,"%d:",p->tm_hour);           /*获取当前时*/
    fprintf(fp,"%d",p->tm_min);             /*获取当前分*/
}
int make_sure(int year,int month,int day,int hour,int minute){
    
    
    //先检查是否符合时间的进制规范
    int Month[12]= {
    
    31,28,31,30,31,30,31,31,30,31,30,31};
    if(year==2020&&month>=1&&month<=12&&day>=1&&day<=Month[month-1]&&hour>=1&&hour<=12&&minute>=0&&minute<=59) return 1;
    else return 0;
}
void init_start_end(){
    
    
    printf("——————————————————————————————————————\n");
    printf("             请输入要查询的起始日期文件名(如20200920.txt):                \n");
    printf("——————————————————————————————————————\n");
    scanf("%s",start_day);

    printf("——————————————————————————————————————\n");
    printf("             请输入要查询的结束日期文件名(如20200920.txt):                \n");
    printf("——————————————————————————————————————\n");
    scanf("%s",end_day);
}
int search()
{
    
    
    init_start_end();
    while(1)
    {
    
    
        printf("——————————————————————————————————————\n");
        printf("                输入“查找”以继续操作,输入“结束”返回                    \n");
        printf("                 输入“显示”显示所有的医生和患者的信息                     \n");
        printf("——————————————————————————————————————\n");
        char in[50];
        scanf("%s",in);
        if(strcmp(in,"查找")==0)
        {
    
    
            printf("——————————————————————————————————————\n");
            printf("            输入“病人”“医生”“科室”“历史”分别查询对应信息            \n");
            printf("——————————————————————————————————————\n");
            char in0[50];
            scanf("%s",in0);
            int tag;
            if(strcmp(in0,"病人")==0)
            {
    
    
                printf("——————————————————————————————————————\n");
                printf("                            请输入病人的卡号                                \n");
                printf("——————————————————————————————————————\n");
                while(1)
                {
    
    
                    scanf("%d",&tag);
                    if(tag>100||tag<=0)
                    {
    
    
                        printf("——————————————————————————————————————\n");
                        printf("                          病人卡号错误,请重新输入                          \n");
                        printf("——————————————————————————————————————\n");
                        break;
                    }else{
    
    
                        search_patient(tag,head);
                        break;
                    }
                }
            }
            else if(strcmp(in0,"医生")==0)
            {
    
    
                printf("——————————————————————————————————————\n");
                printf("                            请输入医生的工号                                \n");
                printf("——————————————————————————————————————\n");
                while(1)
                {
    
    
                    scanf("%d",&tag);
                    if(tag>30||tag<=0)
                    {
    
    
                        printf("——————————————————————————————————————\n");
                        printf("                          医生工号错误,请重新输入                          \n");
                        printf("——————————————————————————————————————\n");
                        break;
                    }
                    else
                    {
    
    
                        search_doctor(tag,head);
                        break;
                    }
                }
            }
            else if(strcmp(in0,"科室")==0)
            {
    
    
                printf("——————————————————————————————————————\n");
                printf("                              请输入科室的编号                              \n");
                printf("——————————————————————————————————————\n");
                while(1)
                {
    
    
                    scanf("%d",&tag);
                    if(tag>10||tag<=0)
                    {
    
    
                        printf("——————————————————————————————————————\n");
                        printf("                          科室编号错误,请重新输入                          \n");
                        printf("——————————————————————————————————————\n");
                        break;
                    }
                    else
                    {
    
    
                        search_office(tag,head);
                        break;
                    }
                }
            }
            else if(strcmp(in0,"历史")==0)
            {
    
    
                printf("——————————————————————————————————————\n");
                printf("请输入要查询的历史的开始时间(样例:202009011230,代表2020年9月1日12时30分)\n");
                printf("——————————————————————————————————————\n");
                int year,month,day,hour,minute;
                scanf("%4d%2d%2d%2d%2d",&year,&month,&day,&hour,&minute);
                if(!make_sure(year,month,day,hour,minute)){
    
    
                    printf("——————————————————————————————————————\n");
                    printf("                   格式错误,请重新选择您要进行的操作                        \n");
                    printf("——————————————————————————————————————\n");
                    continue;
                }
                struct Time start={
    
    year,month,day,hour,minute};
                printf("——————————————————————————————————————\n");
                printf("请输入要查询的历史的结束时间(样例:202010011230,代表2020年10月1日12时30分)\n");
                printf("——————————————————————————————————————\n");
                scanf("%4d%2d%2d%2d%2d",&year,&month,&day,&hour,&minute);//若后者先于前者则输出空
                if(!make_sure(year,month,day,hour,minute)){
    
    
                    printf("——————————————————————————————————————\n");
                    printf("                  格式错误,请重新选择您要进行的操作                        \n");
                    printf("——————————————————————————————————————\n");
                    continue;
                }
                struct Time end={
    
    year,month,day,hour,minute};
                if(timeCompare(&start,&end)==1){
    
    
                    printf("——————————————————————————————————————\n");
                    printf("                   开始时间晚于结束时间,不予搜索                           \n");
                    printf("——————————————————————————————————————\n");
                }else history(start,end,head);
            }
        }else if(strcmp(in,"结束")==0){
    
    
            break;
        }else if(strcmp(in,"显示")==0){
    
    
            char grade[4][20]={
    
    "主任医师","副主任医师","主治医师","住院医师"};
            printf("——————————————————————————————————————\n");
            printf("                             医生信息                                       \n");
            printf("——————————————————————————————————————\n");
            for(int i=0;i<30;i++)
                printf("|编号| %-4d |姓名| %-10s |级别| %-15s |科室| %-10s\n",Doctor[i].index,Doctor[i].name,grade[Doctor[i].level-1],Office[Doctor[i].office-1].name);
            printf("——————————————————————————————————————\n");
            printf("                             病人信息                                       \n");
            printf("——————————————————————————————————————\n");
            for(int i=0;i<100;i++){
    
    
                if(Patient[i].index==0) break;
                printf("|编号|%-4d |姓名|%-10s |年龄|%-4d\n",Patient[i].index,Patient[i].name,Patient[i].age);
            }
        }else{
    
    
            printf("——————————————————————————————————————\n");
            printf("                          输入错误,请重新输入                              \n");
            printf("——————————————————————————————————————\n");
        }

    }
    return 0;
}

//main.c
#include<windows.h>
#include<stdio.h>

#include"inform.h"
#include"jiang.h"
#include"gzx.h"
#include"yh.h"

extern int dateInt;
void searchMenu()
{
    
    
    while(1)
    {
    
    
        cls();
        printf("\n");
        printf(" 诊疗记录查询\n");
        printf(" 查询营业额\n");
        printf(" 查询医生繁忙程度\n");
        printf(" 生成住院患者报表\n");
        printf(" 返回\n");
        int x=getKeyboard(4,0);cls();
        if(x==4)
            break;
        switch(x)
        {
    
    
            case 0: search(); break;
            case 1: sum_income(); break;
            case 2: sum_doctor();break;
            case 3: output_inhospital();break;
        }
    }
}
/*
int income;
while(){

}

*/
void main_file(){
    
    //历史文件操作
    while(1)
    {
    
    
        cls();
        printf("\n");
        printf(" 添加\n");
        printf(" 删除\n");
        printf(" 修改\n");
        printf(" 返回\n");
        int x=getKeyboard(3,0);
        cls();
        if(x==3)
            break;
        switch(x)
        {
    
    
            case 0: add_file();break;
            case 1: delete_file();break;
            case 2: modify_file();break;
        }
    }
}

void mainMenu()
{
    
    
    //system("color F0");
    while(1)
    {
    
    
        cls();
        printf("\n");
        printf(" 挂号\n");
        printf(" 添加\n");
        printf(" 导入\n");
        printf(" 删除\n");
        printf(" 查询\n");
        printf(" 保存\n");
        printf(" 历史文件操作\n");
        printf(" 退出\n");
        int x=getKeyboard(7,0);cls();
        if(x==7)
            break;
        switch(x)
        {
    
    
            case 0: tryToRegister(); break;
            case 1: addOneMedicalRecord(1,0); break;
            case 2: importMenu();break;
            case 3: Delete();break;
            case 4: searchMenu();break;
            case 5: save();break;
            case 6: main_file();break;
        }
    }
}


int main()
{
    
    
    init();
    mainMenu();
    return 0;
}

头文件部分如下:

//inform.h
#ifndef INFORM_H_INCLUDED
#define INFORM_H_INCLUDED


#define Doctor_max 30            //医生数量上限
#define Medicine_kind_max 30     //药品种类上限
#define Patient_max 100          //病人数量上限
#define Medicine_number_max 30  //开药数量上限
#define Hospital_register_max 500//每日受理挂号上限
#define Examination_max 10       //检查项目数上限
#define Office_max 10            //科室数上限

struct Time                         //时间结构体,包含年月日时分
{
    
    
    int year;
    int month;
    int day;
    int hour;
    int minute;
};

struct medicalRecord                   //诊疗记录
{
    
    
    struct Time T;                     //挂号时间
    char name[100];                    //姓名
    long long index;                   //诊疗记录单号
    int patientIndex;                  //患者编号
    int doctorIndex;                   //医生编号
    int officeIndex;                   //科室编号
    int exam[Examination_max];         //检查项目
    int medicine[Medicine_number_max]; //开药数量
    int inHospitalOrNot;               //是否住院
    struct Time start;                 //住院起始时间
    struct Time end;                   //住院结束时间
    long long deposit;                 //押金
    struct medicalRecord *next;
};

struct doctor
{
    
    
    int index;                         //医生编号
    char name[100];                    //医生姓名
    int level;                         //医生级别
    int office;                        //所属科室
    int workday[7];                    //工作日
    int count;                         //当日接诊量
};

struct office
{
    
    
    int index;                         //科室编号
    char name[100];                    //科室名称
};

struct medicine
{
    
    
    long long price;                   //药物单价
    char name[100];                    //药物名称
};

struct examination
{
    
    
    long long price;                   //检查价格
    char name[100];                    //检查名称
};

struct patient
{
    
    
    char name[100];                    //病人名字
    int index;                         //病人编号
    int age;                           //病人年龄
    int count;                         //单日挂号次数
    int Office[5];                     //保存已经看过的科室
};

int income;
struct doctor Doctor[Doctor_max];            //医生数组
struct office Office[Office_max];            //科室数组
struct medicine Medicine[Medicine_number_max];      //药物信息数组
struct examination Exam[Examination_max];    //检查项目数组
struct patient Patient[Patient_max];             //病人信息数组
struct medicalRecord *head;                      //诊疗记录链表头
struct medicalRecord *tail;                      //诊疗记录链表尾
int weekday;


#endif // INFORM_H_INCLUDED

#ifndef GZX2_H_INCLUDED
#define GZX2_H_INCLUDED

struct in_hospital{
    
    
    char nam[100];
    int patientind;
    long long st;
    long long end;
    long long depo;
    struct in_hospital *next;

};

void sum_income();
void sum_doctor();
void output_inhospital();
void save();
void check_inhospital();
void out_hospital();

#endif // GZX2_H_INCLUDED

//jiang.h
#ifndef JIANG_H_INCLUDED
#define JIANG_H_INCLUDED
#include"inform.h"
#include <stdbool.h>
#define INTLENGTH 9
#define LONGLONGLENGTH 18
/*函数声明部分开始*/
long long llmax(long long, long long);
int isLeapYear(int);
void getTime(struct Time *);
int getDate();
int getWeekday(struct Time*);
bool checkOffice();
bool checkDate(int);
void loadIncome();
void loadOfficeInfo();
void loadDoctorInfo();
void loadMedicineInfo();
void loadExaminationInfo();
void loadPatientInfo();
void init();
bool IDcheck(const char *);
int searchPatient(const char *);
int timeCompare(const struct Time *, const struct Time *);
int countLeapYear(int);
long long countDays(const struct Time *, const struct Time *);
long long calculateFee(const struct medicalRecord *);
bool inputOfficeIndex(struct medicalRecord *, struct patient *);
bool inputDoctorIndex(struct medicalRecord *t);
void inputExaminationIndex(struct medicalRecord *t);
void inputMedicineIndexAndAmount(struct medicalRecord *t);
void inputInhospitalInfo(struct medicalRecord *t);
bool inputFeePaid(struct medicalRecord *t);
void setMedicalRecordIndex(struct medicalRecord *t);
struct medicalRecord* createMedicalRecord(struct patient *p);
int getAge(char *s, int a);
void tryToRegister();
void insertRecord(struct medicalRecord *a);
void import(int, int);
struct medicalRecord *searchRecord(long long a, bool *f);
int checkIndex(long long a);
void leaveARecordWhenModify(long long index, char *s);
long long Delete();
int timeToInt(struct Time *t);
bool checkMedicalRecord(struct medicalRecord *t);
bool addOneMedicalRecord(int mode, long long index);
void modify();
void refresh();
void intToStr(int a, char *s);
long long strToInt(char *s);
void importPeriodOfTime();
int getKeyboard(int, int);
void SetPos(int, int);
void cls();
void importMenu();
long long inputNumber(int,bool,bool);
bool checkT(const struct Time*a);
bool readNumberAndJudge(FILE*f,int *a);
bool checkPatient(int x,char *s);
bool checkOfficeOfDoctor(int doc,int off);
bool checkDoctorWork(int doc,int day);
/*函数声明部分结束*/

#endif // JIANG_H_INCLUDED

//yh.h
#ifndef YH_H_INCLUDED
#define YH_H_INCLUDED

#define SIZE 256
typedef struct compensation{
    
    
    char str1[SIZE];//记录情况
    char str2[SIZE];//检查情况
    char str3[SIZE];//药品情况
    struct compensation * next;
}Node;//诊疗信息的读取
typedef Node * pNode;
void format(int a,int n,char t[]);//格式化字符串
void record(FILE * fp);//输出当前时间
void init_start_end();//初始化开始和结束时间
int make_sure(int year,int month,int day,int hour,int minute);//判断时间的进制合理性
void update(char date[]);//递增日期
void print(struct medicalRecord * head);//打印
void search_office(int Office_id,struct medicalRecord * head);//寻找科室
void search_doctor(int Doctor_id,struct medicalRecord * head);//寻找医生
void search_patient(int Patient_id,struct medicalRecord * head);//寻找患者
void history(struct Time start,struct Time end,struct medicalRecord * head);//查询历史

void delete_file();//文件中的删除
void add_file();//文件中的增加
void modify_file();//文件中的修改
void release(pNode head);//释放内存

int search();//菜单
#endif // YH_H_INCLUDED

猜你喜欢

转载自blog.csdn.net/weixin_47741017/article/details/113513585