药房管理系统(c语言课程设计)

是有瑕疵的,比如删除药品的时候无论是否有该药品都会显示删除成功,可自行修改

话不多说,直接上源码和辅助文件

阿里云盘链接: https://www.aliyundrive.com/s/vwEWYHpcYTf 

需要两个辅助文件  一个是data1.txt,存放药品信息;一个是data3.txt,存放管理员账号和密码  各个变量之间用空格隔开

#include

#include

#include

#include

#include

#include

//本系统管理员密码默认不超过20位

void mainmenu();//主菜单

void menu1();//药品管理菜单

void lmain();//登陆函数

void land();//输入账户密码的登录函数

void toxy(int x, int y)      //将光标移动到X,Y坐标处

{
    COORD pos = { x , y };

    HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleCursorPosition(Out, pos);

}

int match(int m, char a[20]);//核对账号密码

void main1();    //成功登录

void ypmain();  //药品系统

void rymain();  //人员系统

void ypmenu();//药品菜单

void rymenu();//人员菜单

void zjyp();  //增加药品

void scyp();//删除药品

void cxyp();//查询药品

void yprk();//药品入库

void ypck();//药品出库

void ypzl();//查询药库情况

int  xz();//新增管理员

void sc();//删除管理员

void xg();//修改

void xgmm();//修改密码

int tj1();//统计药品文本个数

int tj3();//统计账号密码个数

int tj2;//统计输入密码次数

void kcyj();//库存预警,显示库存不大于20的药品

void gqyj();//过期预警  将药品距当前登陆时间增序输出

int y2,m2,d2; //读取当前时间放在y2,m2,d2中

int ts(int tyear,int tmonth,int tday,int tbzq,int year,int month,int day);//计算过期时间

int day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end);//计算现在与过期的天数差

typedef   struct    //为了过期预警里的排序

     {
        char s1[20];

        char s2[20];

        char s3[20];

        int m;

        float n;

        int y;

        int month;

        int d;

        int b;

        int day;

}point;

void quick_sort(point array[], int left, int right)

{
    point temp;

    int i=left,j=right,pivot;

    pivot=array[(left+right)/2].day;

    while(i<=j)

    {
        while(array[i].daypivot)   j--;

        if(i<=j)

        {
            temp=array[i];

            array[i]=array[j];

            array[j]=temp;

            i++;

            j--;

        }

    }

    if(lefttm_year;

    m2 = 1 + p->tm_mon;

    d2 = p->tm_mday;

}

#define LEN2 sizeof(struct land)

struct land//登录信息

{
    int zhanghao;

    char password[20];

    struct land *next;

};

int tj1()//统计药品文本个数

{
    FILE* fp;

    int  n,tkucun,tyear,tmonth,tday,tbzq;

    float tprice = 0;

    char tname[20] = { '\0' }, tshengchanshang[20] = { '\0' }, txuhao[20] = { '\0' };

    fp = fopen("data1.txt", "r");//打开文件

    for (n = 0; !feof(fp); n++)//逐个读文件

        fscanf(fp, "%s%s%s%d%f%d%d%d%d", tname, txuhao, tshengchanshang, &tkucun, &tprice,&tyear,&tmonth,&tday,&tbzq);

    n--;

    fclose(fp);//关闭文件

    return (n);//返回个数

}

int tj3()//统计账号密码文本个数

{
    FILE* fp;

    int zhao = 0, n;

    char mm[20] = { '\0' };

    fp = fopen("data3.txt", "r");//打开文件

    for (n = 0; !feof(fp); n++)//逐个读文件

        fscanf(fp, "%d %s", &zhao, mm);

    n--;

    fclose(fp);//关闭文件

    return (n);//返回个数

}

#define LEN sizeof(struct YP)

struct YP

{
    char mc[20];        //药品名

    char xh[20];        //序号

    char scs[20];       //生产商

    int kc;             //库存

    float jg;           //价格

    int year;           //年月日

    int month;

    int day;

    int bzq;            //保质期(天)

    struct YP* next;

};

void mainmenu()//主菜单

{
    system("cls");

    toxy(40, 8);

    printf("**************************************************");

    toxy(40, 10);

    printf("1.药品系统");

    toxy(40, 12);

    printf("2.管理系统");

    toxy(40, 14);

    printf("3.退出系统");

    toxy(40, 16);

    printf("请按键选择,回车确定");

    toxy(40, 18);

    printf("**************************************************");

    toxy(40,20);

    return;

}

void menu1()    //菜单

{
        system("cls");

        toxy(50, 4);//将光标移动到(50,5)坐标处

        printf(" 欢迎来到药品系统");

        toxy(48, 6);

        printf("|     1.药品增加         |");

        toxy(48, 8);

        printf("|     2.药品删除         |");

        toxy(48, 10);

        printf("|     3.药品信息查询     |");

        toxy(48, 12);

        printf("|     4.药品入库         |");

        toxy(48, 14);

        printf("|     5.药品出库         |");

        toxy(48,16);

        printf("|     6.药品总览         |");

        toxy(48,18);

        printf("|     7.库存预警         |");

        toxy(48,20);

        printf("|     8.过期预警         |");

        toxy(48,22);

        printf("|     9.返回上一级       |");

        toxy(48,24);

}

void lmain()//登录功能函数

{
    char choose;

    toxy(48, 8);

    printf("1.登录");

    toxy(48, 10);

    printf("2.退出");

    toxy(48, 12);

    scanf(" %c", &choose);

    switch (choose)//功能函数

    {
    case'1':

        land();

        break;

    case'2':

        system("cls");

        getch();

        exit(0);

        break;

    }

}

void land()//输入账户密码的登录函数

{
    int zhanghao;

    char password[20];

    int i = 2, j, k, n;

    char hit = 0;

    system("cls");

    do

    {
        system("cls");

        if(tj2==0)

        {
            toxy(44,6);

            printf("请仔细按照要求登录,登陆三次仍未成功系统将自动退出!");

        }

        toxy(44,8);

        printf("请输入账号:");

        scanf("%d", &zhanghao);

        system("cls");

        toxy(44,10);

        printf("确认输入请按回车,重新输入请按Esc");

        hit = getch();

        for (; hit != 13 && hit != 27;)

        {
            hit = getch();

        }

    } while (hit == 27);

    system("cls");

    toxy(44,12);

    printf("请输入密码:");

    {  

        int p;

        for(p=0;p<20;p++)

        {
            password[p]=getch();

            if(password[p]=='\b'&&p>0)

                {
                    p--;

                    p--;

                    printf("\b \b");

                }

        else

          {
            if(password[p]==13)

             {
                break;

             }

            printf("*");

         }

       }

        password[p]='\0';

    }

    i = match(zhanghao, password);

    if (i == 1)

    {
        system("cls");

        toxy(44,10);

        printf("登陆成功!按任意键继续");

        getch();

        main1();

    }

    else

    {
        tj2++;

        if(tj2==3)

        {
            system("cls");

            toxy(44,10);

            printf("输入三次错误,系统将强制退出!");

            Sleep(1000);

            exit(0);

        }

        if (i == -1&&tj2!=3)

        {
            system("cls");

            toxy(44,10);

            printf("登录失败!按任意键继续");

            getch();

            land();

        }

        if (i == 0&&tj2!=3)

            {
                system("cls");

                toxy(44,10);

                printf("不存在此用户!按任意键继续");

            }

        getch();

        system("cls");

        lmain();

    }

}

int match(int m, char a[20])//匹配数据库中的账号密码

{
    FILE* fp;

    int n = 0, i = 0;

    int zhanghao;

    char password[20];

    if ((fp = fopen("data3.txt", "r")) == NULL)//不存在读者文件

    {
        system("cls");

        printf("\n 账号不存在,按任意键继续");

        getch();

        system("cls");

        land();

    }

    for (; !feof(fp);)

    {
        fscanf(fp, "%d%s", &zhanghao, password);

        if (m == zhanghao)

        {
            if (strcmp(a, password) == 0)

                {
                    return 1;

                }

            else

            {
                return -1;

            }

        }

    }

    return 0;

}

void main1()//成功登录

{
    char choose;

    mainmenu();

    scanf(" %c", &choose);

    switch (choose)//功能函数

    {
    case'1':

        ypmain();//药品系统

        break;

    case'2':

        rymain();//管理人员系统

        break;

    case'3':     //退出系统

        system("cls");

        getch();

        exit(0);

        break;

    }

}

void zjyp()

{
    FILE* fp;

    char i;

    int tmany,many = 0,j=0,k,year,month,day,bzq,tyear,tmonth,tday,tbzq;

    float price = 0,tprice=0;

    char  name[20] = { '\0' }, xuhao[20] = { '\0' }, shengchanshang[20] = { '\0' };

    char  tname[20] = { '\0' }, txuhao[20] = { '\0' }, tshengchanshang[20] = { '\0' };

    system("cls");

    if ((fp = fopen("data1.txt", "r")) == NULL)//if语句:打开文件,不存在此文件则新建

    {
        fp = fopen("data1.txt", "w");

        fclose(fp);

    }

    fp = fopen("data1.txt", "a");

    FILE *fp2;

    fp2=fopen("data1.txt","r");

    printf("\t请按以下格式输入药品信息:\n\t药名 序号 生产商 进库量 单价 生产日期 保质期(生产日期请用空格隔开如2000 01 01)");

    for (; i != 27;)//为了实现输入一次后按Esc退出

    {
        k=tj1();

        printf("\n请输入:\t");

        scanf("%s %s %s%d%f%d%d%d%d", name,xuhao,shengchanshang,&many,&price,&year,&month,&day,&bzq);

        for (j = 0; j < k; j++)

        {
            fscanf(fp2, "%s%s%s%d%f%d%d%d%d", tname, txuhao, tshengchanshang, &tmany, &tprice,&tyear,&tmonth,&tday,&tbzq);

            if (!strcmp(xuhao, txuhao))

            {
                printf("本次增加药品失败!药房已有该序号药品");

                Sleep(500);

                printf("\n若确认为该药品,请进入药品入库进行增加库存");

                Sleep(500);

                printf("\n请按任意键返回药品系统");

                getch();

                ypmain();

            }

        }

        fprintf(fp, "%-14s%-20s%-18s%-5d   %-.2f  %d  %d  %d  %d\n",name,xuhao,shengchanshang,many,price,year,month,day,bzq);

        printf("\n");

        printf("继续输入请按回车,结束输入请按Esc\n");

        i = getch();//暂停程序当i接收后继续下一条指令

        for (; i != 13 && i != 27;)

        {
           i=getch();  

        }

    }

    fclose(fp);

    printf("\n保存成功,按任意键返回上一层!");

    getch();

    ypmain();//返回上一层

}

void scyp()

{
    struct YP* head = NULL;

    struct YP* p, * p1, * p2;

    int tmany = 0, n = 0, j, k,tyear,tmonth,tday,tbzq;

    float tprice = 0;

    char  tname[20] = { '\0' }, txuhao[20] = { '\0' }, tshengchanshang[20] = { '\0' },jjnam[20] = { '\0' };

    char i;

    FILE* fp;

    if ((fp = fopen("data1.txt", "r")) == NULL)//打开文件

    {
        system("cls");

        printf("\n记录文件不存在!按任意键返回");

        getch();

        ypmain();

    }

    else//实现删除的功能

    {
        system("cls");

        printf("\n请输入你要删除的药品序号:");//输入删除药品序号

        scanf("%s", jjnam);

        printf("\n确认删除请按回车,取消请按Esc\n");

        i = getch();

        for (; i != 13 && i != 27;)

            {
                i = getch();

            }

        if (i == 27)

            {
                ypmain();

            }

        fp = fopen("data1.txt", "r");

        j = tj1();

        for (k = 0; k < j; k++)

        {
            fscanf(fp, "%s%s%s%d%f%d%d%d%d", tname, txuhao, tshengchanshang, &tmany, &tprice,&tyear,&tmonth,&tday,&tbzq);

            if (strcmp(jjnam, txuhao))//比较序号,将不同序号的信息复制到链表

            {
                n++;//相同返回值为0不执行if语句继续循环,不同则执行直到将所有不同的药品建立成链表

                if (n == 1)//建立链表

                {
                    p1 = p2 = (struct YP*)malloc(LEN);

                    head = p1;

                }

                else

                {
                    p2->next = p1;

                    p2 = p1;

                    p1 = (struct YP*)malloc(LEN);//新建链表

                }

                strcpy(p1->mc, tname);//复制药品名

                strcpy(p1->xh, txuhao);//复制药品序号

                strcpy(p1->scs, tshengchanshang);//复制生产商

                p1->kc = tmany;//复制个数

                p1->jg = tprice;//复制单价

                p1->year=tyear; //复制年份

                p1->month=tmonth;//复制月份

                p1->day=tday;  //复制天

                p1->bzq=tbzq;  //复制保质期      

            }

        }

        if (n == 0)//如果药品只有一项且这一项刚好和要删除的相同

        {
            head = NULL;

        }

        else//建立链表的最后剩余一个储存空间,所以封底

        {
            p2->next = p1;

            p1->next = NULL;

            fclose(fp);

        }

    }

    fp = fopen("data1.txt", "w");//清空文件,只写打开,然后关闭

    fclose(fp);

    fp = fopen("data1.txt", "a");//追加文件

    p = head;

    for (; p != NULL;)//把链表内容覆盖到文件

    {
        fprintf(fp, "%-14s%-20s%-18s%-5d   %-.2f  %d  %d  %d  %d\n",  p->mc, p->xh, p->scs,p->kc, p->jg,p->year,p->month,p->day,p->bzq);

        p = p->next;

    }

    fclose(fp);

    system("cls");

    printf("\n删除成功 \n\n按任意键返回上一层\n");

    getch();//返回上一层

    ypmain();

}

void cxyp ()

{
    FILE* fp;

    int k = 0, tmany = 0, m = 0, n = 0,tyear,tmonth,tday,tbzq;

    float tprice = 0;

    char  tname[20] = { '\0' }, txuhao[20] = { '\0' }, tshengchanshang[20] = { '\0' };

    char i;

    char chazhao[20] = { '\0' };

    if ((fp = fopen("data1.txt", "r")) == NULL)//打开文件

    {
        system("cls");

        printf("\n记录文件不存在!按任意键返回");

        getch();

        ypmain();

    }

    system("cls");

    toxy(44,10);

    printf("请输入药品名,序号或生产商查询:");

    scanf("%s", chazhao);

    system("cls");

    m = tj1();

    for (n = 0; n < m; n++)

    {
        fscanf(fp, "%s%s%s%d%f%d%d%d%d", tname, txuhao, tshengchanshang, &tmany, &tprice,&tyear,&tmonth,&tday,&tbzq);

        if (!strcmp(chazhao, tname) || !strcmp(chazhao, txuhao) || !strcmp(chazhao, tshengchanshang))

        {
            if (k == 0)

            {
                printf("\t\t\t\t\t\t查询结果:\n\n");

                printf("%-13s%-9s%-20s%-10s%-10s\t%-15s%-13s\n","商品名","序号","生产商","现存量","单价","生产日期","保质期");

            }

            printf("%-13s%-9s%-20s%-10d%-10.2f\t%d-%d-%d\t%d\n", tname, txuhao, tshengchanshang, tmany, tprice,tyear,tmonth,tday,tbzq);

            k++;

        }

    }

    if (k == 0)//文件夹为空则输出无记录并返回上一层

    {
        system("cls");

        toxy(44,10);

        printf("无符合记录!,按任意键返回!");

        getch();

        ypmain();

    }

    printf("\n\t\t\t\t\t查询结束,按任意键返回!");

    fclose(fp);//查询结束

    getch();

    ypmain();

}

void yprk()

{
    struct data1

     {
        char s1[20];

        char s2[20];

        char s3[20];

        int m;

        float n;

        int y;

        int month;

        int d;

        int b;

    }data[1001];

    FILE* fp;

    int k = 0, many = 0, p=0,q=0,flag,tyear,tmonth,tday,tbzq;

    q=tj1();

    float price = 0;

    char  name[20] = { '\0' }, xuhao[20] = { '\0' }, shengchanshang[20] = { '\0' };

    char chazhao1[20] = { '\0' }, chazhao2[20] = { '\0' }, chazhao3[20] = { '\0' };

    if ((fp = fopen("data1.txt", "r+")) == NULL)//打开文件

    {
        system("cls");

        printf("\n记录文件不存在!按任意键返回!");

        getch();

        ypmain();

    }

    int i = 1;

    while (!feof(fp))

    {
        fscanf(fp, "%s%s%s%d%f%d%d%d%d", data[i].s1, data[i].s2, data[i].s3, &data[i].m, &data[i].n,&data[i].y,&data[i].month,&data[i].d,&data[i].b);

        i += 1;

    }

    system("cls");

    i--;

    printf("请输入药品序号:");

    scanf("%s",chazhao2);

    system("cls");

    fp = fopen("data1.txt", "r+");

    for (p = 1; p < q+1; p++)

    {
        fscanf(fp, "%s%s%s%d%f%d%d%d%d", name, xuhao, shengchanshang, &many, &price,&tyear,&tmonth,&tday,&tbzq);

            if(!strcmp(chazhao2, xuhao))

            {
                if (k == 0)

                {
                    printf("请输入入库数量\n");

                    scanf("%d", &flag);

                    data[p].m += flag;

                }

                  k++;

            }      

    }

    fclose(fp);

    if (k == 0)//文件夹为空则输出无记录并返回上一层

    {
        system("cls");

        printf("\n无符合记录!,按任意键返回\n");

        getch();

        ypmain();

    }

    else

{
    fp = fopen("data1.txt", "w+");

    for (int j = 1; j < i; j++)

    {
        fprintf(fp, "%-14s%-20s%-18s%-5d   %-.2f  %d  %d  %d  %d\n", data[j].s1, data[j].s2, data[j].s3, data[j].m, data[j].n,data[j].y,data[j].month,data[j].d,data[j].b);

    }

    fclose(fp);

    printf("入库成功,按任意键返回\n");

    getch();

    ypmain();

}

}

void ypck()

{
    struct data1

     {
        char s1[20];

        char s2[20];

        char s3[20];

        int m;

        float n;

        int y;

        int month;

        int d;

        int b;

    }data[1001];

    FILE* fp;

    int k = 0, many = 0, p=0,q=0,flag,tyear,tmonth,tday,tbzq;

    q=tj1();

    float price = 0;

    char  name[20] = { '\0' }, xuhao[20] = { '\0' }, shengchanshang[20] = { '\0' };

    char chazhao1[20] = { '\0' }, chazhao2[20] = { '\0' }, chazhao3[20] = { '\0' };

    if ((fp = fopen("data1.txt", "r+")) == NULL)//打开文件

    {
        system("cls");

        printf("\n记录文件不存在!按任意键返回!");

        getch();

        ypmain();

    }

    int i = 1;

    while (!feof(fp))

    {
        fscanf(fp, "%s%s%s%d%f%d%d%d%d", data[i].s1, data[i].s2, data[i].s3, &data[i].m, &data[i].n,&data[i].y,&data[i].month,&data[i].d,&data[i].b);

        i += 1;

    }

    system("cls");

    i--;

    printf("请输入药品序号:");

    scanf("%s",chazhao2);

    system("cls");

    fp = fopen("data1.txt", "r+");

    for (p = 1; p < q+1; p++)

    {
        fscanf(fp, "%s%s%s%d%f%d%d%d%d", name, xuhao, shengchanshang, &many, &price,&tyear,&tmonth,&tday,&tbzq);

            if(!strcmp(chazhao2, xuhao))

            {
                if (k == 0)

                {
                    printf("请输入出库数量\n");

                    scanf("%d", &flag);

                    data[p].m -= flag;

                }

                k++;

            }  

    }

    fclose(fp);

    if (k == 0)//文件夹为空则输出无记录并返回上一层

    {
        system("cls");

        printf("\n无符合记录!,按任意键返回\n");

        getch();

        ypmain();

    }

    else

{
    fp = fopen("data1.txt", "w+");

    for (int j = 1; j < i; j++)

    {
        fprintf(fp, "%-14s%-20s%-18s%-5d   %-.2f  %d  %d  %d  %d\n", data[j].s1, data[j].s2, data[j].s3, data[j].m, data[j].n,data[j].y,data[j].month,data[j].d,data[j].b);

    }

    fclose(fp);

    printf("出库成功,按任意键返回\n");

    getch();

    ypmain();

}

}

void ypzl()

{
    int tmany = 0, j, k,tyear,tmonth,tday,tbzq,t3;

    float tprice = 0;

    char ch;

    char  tname[20] = { '\0' }, txuhao[20] = { '\0' }, tshengchanshang[20] = { '\0' };

    FILE* fp;

    if ((fp = fopen("data1.txt", "r")) == NULL)//打开文件

    {
        system("cls");

        printf("\n记录文件不存在!按任意键返回");

        getch();

        rymain();

    }

    else

    {
        system("cls");

        j = tj1();

        printf("为避免总数据太多,可选择每次输出k条数据\n");

        printf("共有%d条数据\n",j);

        printf("请输入每次输出多少数据:");

        scanf("%d",&t3);

        system("cls");

        fp = fopen("data1.txt", "r");

//      printf("药品名\t\t序号\t\t生产商\t\t现存量\t单价\n");

        printf("%-13s%-9s%-20s%-10s%-10s\t%-15s%-13s\n","商品名","序号","生产商","现存量","单价","生产日期","保质期");

        for (k = 0; k < j; k++)

        {
            if(k%t3==0&&k!=0)

            {
                printf("已输出%d条数据,按Esc键退出,按其他任意键将继续输出\n",k);

                ch=getch();

                if(ch==27)

                ypmain();

            }

            fscanf(fp, "%s%s%s%d%f%d%d%d%d", tname, txuhao, tshengchanshang, &tmany, &tprice,&tyear,&tmonth,&tday,&tbzq);

            printf("%-13s%-9s%-20s%-10d%-10.2f\t%d-%d-%d\t%d\n", tname, txuhao, tshengchanshang, tmany, tprice,tyear,tmonth,tday,tbzq);

//          printf("%s\t\t%s\t\t%s\t%d\t%.2f\n", tname, txuhao, tshengchanshang, tmany, tprice);

        }

    }

    printf("\n输出完成,按任意键返回\n");

    getch();

    ypmain();

}

void ypmain()

{
    char choose;

    menu1();

    scanf(" %c", &choose);

    for (;;)

    {
        switch (choose)

        {
        case'1':

            zjyp();

            break;

        case'2':

            scyp();

            break;

        case'3':

            cxyp();

            break;

        case'4':

            yprk();

            break;

        case'5':

            ypck();

            break;

        case'6':

            ypzl();

            break;

        case'7':

            kcyj();

            break;

        case'8':

            gqyj();

            break;

        case'9':

            main1();

            break;

        }

    }

}

void kcyj()

{
    int count=0;

    int tmany = 0, j, k,tyear,tmonth,tday,tbzq;

    float tprice = 0;

    char  tname[20] = { '\0' }, txuhao[20] = { '\0' }, tshengchanshang[20] = { '\0' };

    FILE* fp;

    if ((fp = fopen("data1.txt", "r")) == NULL)//打开文件

    {
        system("cls");

        printf("\n记录文件不存在!按任意键返回");

        getch();

        rymain();

    }

    else

    {
        system("cls");

        fp = fopen("data1.txt", "r");

        j = tj1();

        printf("%-13s%-9s%-20s%-10s%-10s\t%-15s%-13s\n","商品名","序号","生产商","现存量","单价","生产日期","保质期");

        for (k = 0; k < j; k++)

        {
            fscanf(fp, "%s%s%s%d%f%d%d%d%d", tname, txuhao, tshengchanshang, &tmany, &tprice,&tyear,&tmonth,&tday,&tbzq);

            if(tmany<=20)

            {
                count++;

                printf("%-13s%-9s%-20s%-10d%-10.2f\t%d-%d-%d\t%d\n", tname, txuhao, tshengchanshang, tmany, tprice,tyear,tmonth,tday,tbzq);

            }

        }

    }

    if(count==0) printf("\n无库存不大于20的产品,按任意键返回\n");

    else printf("\n输出完毕,按任意键返回\n");

    getch();

    ypmain();

}

void gqyj()

{
   system("cls");

   point data[1001];

   char tname[20]={'\0'},txuhao[20]={'\0'},tshengchanshang[20]={'\0'};

   int tmany,tyear,tmonth,tday,tbzq;

   float tprice;

   FILE *fp;

   fp=fopen("data1.txt","r");

   int i=1,j=tj1(),k;

   int day;

   printf("%-13s%-9s%-20s%-10s%-10s\t%-15s%-13s\n","商品名","序号","生产商","现存量","单价","生产日期","保质期");

   while (!feof(fp))

    {
        fscanf(fp, "%s%s%s%d%f%d%d%d%d", data[i].s1, data[i].s2, data[i].s3, &data[i].m, &data[i].n,&data[i].y,&data[i].month,&data[i].d,&data[i].b);

        data[i].day=ts(data[i].y,data[i].month,data[i].d,data[i].b,y2,m2,d2);

        i += 1;

    }  

    i-=2;

    quick_sort(data,1,i);

    for(k=1;k<=j;k++)

    {
        if(data[k].day<0)

        printf("%-13s%-9s%-20s%-10d%-10.2f\t%d-%d-%d\t%d\t\t已过期%d天\n", data[k].s1, data[k].s2, data[k].s3, data[k].m, data[k].n,data[k].y,data[k].month,data[k].d,data[k].b,-data[k].day);

        else

        printf("%-13s%-9s%-20s%-10d%-10.2f\t%d-%d-%d\t%d\t\t距过期%d天\n", data[k].s1, data[k].s2, data[k].s3, data[k].m, data[k].n,data[k].y,data[k].month,data[k].d,data[k].b,data[k].day);

       

      /*  if(data[k].day<0)

        printf("%-13s%-9s%-20s%-10d%-10.2f\t%d-%d-%d\t%d\t\t已过期%d天\n", data[k].s1, data[k].s2, data[k].s3, data[k].m, data[k].n,data[k].y,data[k].month,data[k].d,data[k].b,-data[k].day);

        if(data[k].day>=1&&data[k].day<=30)

        printf("%-13s%-9s%-20s%-10d%-10.2f\t%d-%d-%d\t%d\t\t距过期%d天\n", data[k].s1, data[k].s2, data[k].s3, data[k].m, data[k].n,data[k].y,data[k].month,data[k].d,data[k].b,data[k].day);

     */    

    }

   fclose(fp);

   printf("\n输出完成,按任意键返回药品系统");

   getch();

   ypmain();

}

int ts(int tyear,int tmonth,int tday,int tbzq,int year,int month,int day)//计算过期时间

{  

    int d=0;

    int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

    d=tday+tbzq;

    if(tmonth==2)                               //考虑润年的二月

        mon[2]=28+(tyear%4==0&&tyear%100!=0||tyear%400==0);

    while(d>mon[tmonth])

    {
        d-=mon[tmonth];

        tmonth=(tmonth==12)?1:tmonth+1;

        if(tmonth==1)

            tyear++;

        else if(tmonth==2)

            mon[2]=28+(tyear%4==0&&tyear%100!=0||tyear%400==0);

    }

    int answer=day_diff(year,month,day,tyear,tmonth,d);

    return answer;

}

int day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end)//计算过期时间距今

{   //考虑闰年影响2月天数,计算当前日期到0000-03-01的天数再相减

    int y2, m2, d2;

    int y1, m1, d1;

   

    m1 = (month_start + 9) % 12;     //用于判断日期是否大于3月,还用于纪录到3月的间隔月数

    y1 = year_start - m1/10;         //如果是1月和2月,则不包括当前年(因为是计算到0年3月1日的天数)

    d1 = 365*y1 + y1/4 - y1/100 + y1/400 + (m1*306 + 5)/10 + (day_start - 1);//306=365-28-31,5是一年中不是31天的月份个数

    m2 = (month_end + 9) % 12;

    y2 = year_end - m2/10;

    d2 = 365*y2 + y2/4 - y2/100 + y2/400 + (m2*306 + 5)/10 + (day_end - 1);

   

    return (d2 - d1);  

}

void rymenu()

{
    system("cls");

    toxy(38, 8);

    printf("*****************************************************");

    toxy(38, 10);

    printf("1.新增管理员\n\n");

    toxy(38, 12);

    printf("2.删除管理员\n\n");

    toxy(38, 14);

    printf("3.修改密码\n\n");

    toxy(38, 16);

    printf("4.返回上一级\n\n");

    toxy(38, 18);

    printf("请按键选择,回车确定\n");

    toxy(38, 20);

    printf("****************************************************");

    toxy(38, 22);

    return ;

}

int xz()

{
    FILE* fp,*fp2;

    int zhanghao,j,k=tj3(),tzhanghao;

    char password[20], password1[20],tpassword[20];

    char hit = 0;

    if ((fp = fopen("data3.txt", "r")) == NULL)//if语句:打开管理员文件,不存在此文件则新建

    {
        fp = fopen("data3.txt", "w");

        fclose(fp);

    }

    system("cls");

    fp = fopen("data3.txt", "a");

    fp2 = fopen("data3.txt", "r");

    for (;;)//输入两次密码确认,两次相同才能确认

    {
        toxy(48, 10);

        printf("请输入账号:");

        scanf("%d",&zhanghao);

        for(j=0;jnext = p1;

                    p2 = p1;

                    p1 = (struct land*)malloc(LEN2);//新建链表

                }

                p1->zhanghao= tzhanghao;//复制账号

                strcpy(p1->password, tmima);//复制密码

            }

        }

        if (n == 0)//如果账号只有一项且这一项刚好和要删除的相同

        {
            head = NULL;

        }

        else//建立链表的最后剩余一个储存空间,所以封底

        {
            p2->next = p1;

            p1->next = NULL;

            fclose(fp);

        }

    }

    fp = fopen("data3.txt", "w");//清空文件,只写打开,然后关闭

    fclose(fp);

    fp = fopen("data3.txt", "a");//追加文件

    p = head;

    for (; p != NULL;)//把链表内容覆盖到文件

    {
        fprintf(fp, "%d %s\n", p->zhanghao, p->password);

        p = p->next;

    }

    fclose(fp);

    system("cls");

    toxy(48, 10);

    printf("删除成功!按任意键返回上一层\n");

    getch();

    rymain();

}

void xg(int z, char m[20])//修改函数

{
    FILE* fp;

    int zhanghao1, n, j, k;

    char mima1[20];

    struct land* head, * p, * p1, * p2;

    fp = fopen("data3.txt", "r");

    j = tj3();

    for (k = 0; k <= j; k++)

    {
        fscanf(fp, "%d %s", &zhanghao1, mima1);

        if (z != zhanghao1)//比较账号,将不同账号的信息复制到链表

        {
            n++;//相同返回值为0不执行if语句继续循环,不同则执行直到将所有不同的账号建立成链表

            if (n == 1)//建立链表

            {
                p1 = p2 = (struct land*)malloc(LEN2);

                head = p1;

            }

            else

            {
                p2->next = p1;

                p2 = p1;

                p1 = (struct land*)malloc(LEN2);//新建链表

            }

            (p1->zhanghao) = zhanghao1;

            strcpy(p1->password, mima1);//复制账号密码

        }

    }

    if (n == 0)

    {
        head = NULL;

    }

    else//建立链表的最后剩余一个储存空间,所以封底

    {
        p2->next = p1;

        p1->next = NULL;

        fclose(fp);

    }

    fp = fopen("data3.txt", "w");//清空文件,只写打开,然后关闭

    fclose(fp);

    fp = fopen("data3.txt", "a");//追加文件

    p = head;

    for (; p != NULL;)//把链表内容覆盖到文件

    {
        fprintf(fp, "%d %s\n", p->zhanghao, p->password);

        p = p->next;

    }

    fprintf(fp, "%d %s\n", z, m);

    fclose(fp);

    system("cls");

}

void xgmm()

{
    FILE* fp;

    fp = fopen("data3.txt", "r");

    int zh = 0, k = 0, many = 0, m = 0, n = 0;

    int chazhao, hit;

    char mima[20] = { '\0' }, password1[20] = { '\0' };

    char  mm[20] = { '\0' };

    char i;

    system("cls");

    toxy(40, 10);

    printf("请输入你的帐号:");

    scanf("%d", &chazhao);

    toxy(40, 12);

    printf("请输入你的旧密码:");

    scanf("%s",mima);

    m = tj3();

    for (n = 0; n <= m; n++)

    {
        fscanf(fp, "%d %s", &zh, mm);

        if (zh == chazhao)

        {
            if (!strcmp(mm, mima))

            {
                system("cls");

                toxy(40, 10);

                printf("请输入新的密码:");

                scanf("%s", mima);

                system("cls");

                toxy(40, 10);

                printf("请再次输入密码:");

                scanf("%s", password1);

                if (strcmp(mima, password1) == 0)

                {
                    xg(chazhao, mima);

                    system("cls");

                    toxy(40, 10);

                    printf("修改成功,请按任意键重新登陆");

                    getch();

                    system("cls");

                    lmain();

                }

                else

                {
                    system("cls");

                    toxy(40, 12);

                    printf("两次输入密码不一致,按任意键退出重新修改!");

                    hit = getch();

                    system("cls");

                    xgmm();

                }

            }

            else

            {
                system("cls");

                toxy(40, 10);

                printf("旧密码错误,请重新登录!");

                getch();

                system("cls");

                lmain();

            }

        }

    }

    system("cls");

    toxy(40, 10);

    printf("不存在此账号,按任意键返回!");

    fclose(fp);//修改结束

    getch();

    system("cls");

    lmain();

}

void rymain()//人员系统

{
    char choose;

    rymenu();

    scanf(" %c", &choose);

    for (;;)

    {
        switch (choose)

        {
        case'1':

            xz();

            break;

        case'2':

            sc();

            break;

        case'3':

            xgmm();

            break;

        case'4':

            main1();

            break;

        }

    }

}

int main()

{
    system("color a");

    dqrq();

    lmain();

    return 0;

}

你可能感兴趣的:(实战,个人开发,1024程序员节,c语言,链表)