用c语言点菜管理系统

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


char *MyFgets(char data[], int count)
{
    fgets(data, count, stdin);
    char *find = strchr(data, '\n');
    if(find)
        *find = '\0';
    return data;
}

void tablemain(void)
{
    printf("\n1) Order food\n");
    printf("2) Admin section\n");
    printf("3) Exit\n");
}

void Admin(void)
{
    printf("\n1.  Add a category\n");
    printf("2.  Delete a category\n");
    printf("3.  View categories\n");
    printf("4.  Add a food item\n");
    printf("5.  Delete a food item\n");
    printf("6.  View food items\n");
    printf("7.  Show transaction history\n");
    printf("8.  Change password\n");
    printf("9.  Exit\n");
}

typedef struct _card{
    int table;
    int cardID;
    char method[100];
    char horder[100];
    float total;
    int year;
    int mouth;
    int day;
    int hour;
    int min;
    int sec;
    struct _card *next;
}card;


typedef struct _categorie{
    int number;
    char name[100];
    struct _categorie *next;
}categorie;

typedef struct _food{
    int mune;
    int number;
    char name[100];
    float Price;
    int Ava;
    char Des[200];
    struct _food *next;
}food;

food* fodcreat(void)
{
    food* head=NULL;
    return head;
}

categorie* CATcreat(void)
{
    categorie* head=NULL;
    return head;
}

void fodadd(food **head,int number, char* name,float Price,int Ava,char* Des,int mune)
{
    // add to linked-list
    food *p = (food*)malloc(sizeof(food));
    p->mune=mune;
    p->number=number;
    strcpy(p->name, name);
    p->Price=Price;
    p->Ava=Ava;
    strcpy(p->Des, Des);
    p->next=NULL;
    // find the last
    food *last=*head;
    if (last) {
        while (last->next) {
            last=last->next;
        }
        //attach
        last->next=p;
    } else {
        *head=p;
    }
}

int fodserachnn( food** head,char* fname,int mune)
{
    int a=0;
    food *p;
    for (p=*head; p ; p=p->next) {
        if (strcmp(fname, p->name)==0 && p->mune==mune) {
            a=1;
            break;
        }
    }
    return a;
}

int fodsrarchn( food** head,int mune)
{
    int a=0;
    food *p;
    for (p=*head; p ; p=p->next) {
        if (mune==p->mune) {
            a=1;
            break;
        }
    }
    return a;
}

int fodserachN( food** head,int fnum,int mune)
{
    int a=0;
    food *p;
    for (p=*head; p ; p=p->next) {
        if (p->number==fnum && p->mune==mune) {
            a=1;
            break;
        }
    }
    return a;
}

int fodget(food** fhead,int mune)
{
    int cnt=0;
    food *p;
    for (p=*fhead; p ; p=p->next){
        if (p->mune==mune) {
            cnt=p->number;
        }
    }
    return cnt;
}

void fodprint(food *head,int mune)
{
    food *p;
    printf("\nFood no.\tName\tPrice\tAvailability\tDescription\n");
    printf("\n");
    for (p=head; p; p=p->next) {
        if (mune==p->mune) {
            printf("%d\t%s\t%.2f\t%d\t%s\n",p->number,p->name,p->Price,p->Ava,p->Des);
        }
    }
    printf("\n");
}

void fodremove(food** head, int index,int mune)
{
    food *p;
    food *q;
    for (q=NULL,p=*head; p; q=p,p=p->next) {
        if (p->number==index&&p->mune==mune) {
            if (q) {
                q->next=p->next;
            } else {
                *head=p->next;
            }
            free(p);
            break;
        }
    }
}

void fodbigmove(food** head,int mune)
{
    food *p;
    food *q;
    for (q=NULL,p=*head; p; q=p,p=p->next){
        if (p->mune==mune) {
            if (q) {
                q->next=p->next;
            } else {
                *head=p->next;
            }
            free(p);
        }
    }
}

void fodcchange(int ava,food** fhead,int cnum,int fnum)
{
    food *p;
    for (p=*fhead; p ; p=p->next){
        if (p->mune==cnum&&p->number==fnum) {
            p->Ava=p->Ava-ava;
            break;
        }
    }
}

int fodava( food** head,int mune,int fnum,int ava)
{
    int a;
    int boolen=1;
    food *p;
    for (p=*head; p ; p=p->next) {
        if (p->number==fnum && p->mune==mune) {
            a=p->Ava-ava;
            if (a<0) {
                boolen=0;
            }
            break;
        }
    }
    return boolen;
}

void fodfree(food** head)
{
    food *temp =*head;       
    while (temp !=NULL)
    {
        food* pt =temp;
        temp = temp->next;       
        free(pt);                    
    }
    *head =NULL;
}

void CATadd(categorie **head,int cnt, char* name)
{
    // add to linked-list
    categorie *p = (categorie*)malloc(sizeof(categorie));
    p->number=cnt;
    strcpy(p->name, name);
    p->next=NULL;
    // find the last
    categorie *last=*head;
    if (last) {
        while (last->next) {
            last=last->next;
        }
        //attach
        last->next=p;
    } else {
        *head=p;
    }
}

int CATget(categorie** chead)
{
    int cnt=0;
    categorie *p;
    for (p=*chead; p ; p=p->next){
        cnt=p->number;
    }
    return cnt;
}

void CATNprint(categorie** chead,int cnum)
{
    categorie *p;
    for (p=*chead; p ; p=p->next){
        if (cnum==p->number) {
            printf("\nCategory name:%s.\n",p->name);
            break;
        }
    }
}

int CATserach(categorie **head, char* name)
{
    int a=0;
    categorie *p;
    for (p=*head; p ; p=p->next) {
        if (strcmp(name, p->name)==0) {
            a=1;
            break;
        }
    }
    return a;
}

void CATprint(categorie *head)
{
    categorie *p;
    printf("\nCategory no.\tName\n");
    printf("\n");
    for (p=head; p; p=p->next) {
        printf("%d\t%s\n",p->number,p->name);
    }
    printf("\n");
}

int CATserachn(categorie **head, int number)
{
    int a=0;
    categorie *p;
    for (p=*head; p ; p=p->next) {
        if (p->number==number) {
            a=1;
            break;
        }
    }
    return a;
}

void CATremove(categorie** head, int index)
{
    categorie *p;
    categorie *q;
    for (q=NULL,p=*head; p; q=p,p=p->next) {
        if (p->number==index) {
            if (q) {
                q->next=p->next;
            } else {
                *head=p->next;
            }
            free(p);
            break;
        }
    }
}

void CATfree(categorie** head)
{
    categorie *temp =*head;       
    while (temp !=NULL)
    {
        categorie* pt =temp;
        temp = temp->next;        
        free(pt);                    
    }
    *head =NULL;
}

food* ordercreat(void)
{
    food* head=NULL;
    return head;
}

void orderadd(food** fhead,food** ohead,int fnum,int mune,int ava)
{
    food *p;
    for (p=*fhead; p ; p=p->next) {
        if (p->number==fnum && p->mune==mune) {
            food *q = (food*)malloc(sizeof(food));
            q->mune=mune;
            q->number=fnum;
            strcpy(q->name, p->name);
            q->Price=p->Price;
            q->Ava=ava;
            strcpy(q->Des, p->Des);
            q->next=NULL;
            food *last=*ohead;
            if (last) {
                while (last->next) {
                    last=last->next;
                }
                last->next=q;
            } else {
                *ohead=q;
            }
            break;
        }
    }
}

void orderoeder(food** fhead){
    food *p;
    food *q;
    food *v;
    for (p=*fhead; p; p=p->next) {
        for (q=p->next,v=p; q;v=q, q=q->next) {
            if (p->mune==q->mune&&p->number==q->number) {
                p->Ava=(p->Ava)+(q->Ava);
                v->next=q->next;
                free(q);
//                fodremove(fhead, q->number, q->mune);
            }
        }
    }
}

float orderprice(food *ohead)
{
    int a=0;
    food *p;
    for (p=ohead; p; p=p->next) {
        a=a+(p->Ava)*(p->Price);
    }
    return a;
}

void orderprint(food *ohead)
{
    food *p;
    printf("\nFood no.\tName\tPrice\toty\tDescription\n");
    printf("\n");
    for (p=ohead; p; p=p->next) {
            printf("%d-%d\t%s\t%.2f\t%d\t%s\n",p->mune,p->number,p->name,p->Price,p->Ava,p->Des);
    }
    printf("\n");
}

card* Paycreat(void)
{
    card* head=NULL;
    return head;
}

void payadd(card **head,int table, char* name,float total,int cardID)
{
    char a[20]="card";
    // add to linked-list
    card *p = (card*)malloc(sizeof(card));
    p->table=table;
    p->total=total;
    strcpy(p->horder, name);
    strcpy(p->method,a);
    p->total=total;
    p->cardID=cardID;
    time_t timep;
    struct tm *q;
    time(&timep);
    q=gmtime(&timep);
    p->day=q->tm_mday;
    p->mouth=(q->tm_mon)+1;
    p->year=(q->tm_year)-100;
    p->hour=q->tm_hour;
    p->min=q->tm_min;
    p->sec=q->tm_sec;
    p->next=NULL;
    // find the last
    card *last=*head;
    if (last) {
        while (last->next) {
            last=last->next;
        }
        //attach
        last->next=p;
    } else {
        *head=p;
    }
}

int mouthJ(int mouth,int day)
{
    int boolen=0;
    if (mouth==1&&day>0&&day<32) {
        boolen=1;
    }
    else if (mouth==2&&day>0&&day<29){
        boolen=1;
    }
    else if (mouth==3&&day>0&&day<32){
        boolen=1;
    }
    else if (mouth==4&&day>0&&day<31){
        boolen=1;
    }
    else if (mouth==5&&day>0&&day<32){
        boolen=1;
    }
    else if (mouth==6&&day>0&&day<31){
        boolen=1;
    }
    else if (mouth==7&&day>0&&day<32){
        boolen=1;
    }
    else if (mouth==8&&day>0&&day<32){
        boolen=1;
    }
    else if (mouth==9&&day>0&&day<31){
        boolen=1;
    }
    else if (mouth==10&&day>0&&day<32){
        boolen=1;
    }
    else if (mouth==11&&day>0&&day<31){
        boolen=1;
    }
    else if (mouth==12&&day>0&&day<32){
        boolen=1;
    }
    return boolen;
}

void payFadd(card **head,int table, char* name,float total,int cardID,int day,int year,int mouth,int hour,int min,int sec)
{
    char a[20]="card";
    // add to linked-list
    card *p = (card*)malloc(sizeof(card));
    p->table=table;
    p->total=total;
    strcpy(p->horder, name);
    strcpy(p->method,a);
    p->total=total;
    p->cardID=cardID;
    p->day=day;
    p->mouth=mouth;
    p->year=year;
    p->hour=hour;
    p->min=min;
    p->sec=sec;
    p->next=NULL;
    // find the last
    card *last=*head;
    if (last) {
        while (last->next) {
            last=last->next;
        }
        //attach
        last->next=p;
    } else {
        *head=p;
    }
}


void payprint(card *phead,int year,int mouth,int day,int syear,int smouth,int sday)
{
    int star=year*10000+mouth*100+day;
    int end=syear*10000+smouth*100+sday;
    int now;
    float total=0;
    card *p;
    printf("\nTable no.\tPayment\tmethod\tCard no.\tCard holder’ name\tAmt.\tDate\tTime\n");
    printf("\n");
    for (p=phead; p; p=p->next) {
        now=(p->year)*10000+(p->mouth)*100+(p->day);
        if (now>=star&&now<=end) {
            printf("%d\t%s\t%d\t%s\t%d/%d/%d\t%d:%d:%d \n",p->table,p->method,p->cardID,p->horder,p->year,p->mouth,p->day,p->hour,p->min,p->sec);
            total=total+p->total;
    }
        }
    printf("\n");
    printf("Total amount:%.2f\n",total);
}


void payfree(card** phead)
{
    card *temp =*phead;      
    while (temp !=NULL)
    {
        card* pt =temp;
        temp = temp->next;        
        free(pt);                   
    }
    *phead =NULL;
}


int RfileC(categorie** chead,food** fhead)
{
    int boolen;
    int cnum;
    char cname[50];
    int mune;
    int fnum;
    char fname[50];
    float Price;
    int ava;
    char des[100];
    FILE *fp = NULL ;
    fp = fopen("menu.csv","r");
    if (fp==NULL) {
        boolen=0;
    } else {
        boolen=1;
        while (1) {
            fscanf(fp, "%d,",&cnum);
            if (cnum==0) {
                break;
            }
            fscanf(fp, "%[^,],",cname);
            CATadd(chead, cnum, cname);
        }
        while (1) {
            fscanf(fp, "%d,",&fnum);
            if (fnum==0) {
                break;
            }
            fscanf(fp, "%d,%[^,],%f,%d,%[^,],",&mune,fname,&Price,&ava,des);
            fodadd(fhead, fnum, fname, Price, ava, des, mune);
        }
        fclose(fp);
    }
    return boolen;
}

int WfileC(categorie** chead,food** fhead)
{
    categorie *p=*chead;
    food *q=*fhead;
    int boolen=1;
    FILE *fp = NULL ;
    fp = fopen("menu.csv","w");
    if (fp==NULL) {
        boolen=0;
    } else {
        while (p!=NULL) {
            fprintf(fp,"%d,%s,\n",p->number,p->name );
            p=p->next;
        }
        fprintf(fp,"%d,\n",0);
        while (q!=NULL) {
            fprintf(fp,"%d,%d,%s,%f,%d,%s,\n",q->number,q->mune,q->name,q->Price,q->Ava,q->Des);
            q=q->next;
        }
        fprintf(fp,"%d,\n",0);
        fclose(fp);
    }
    return boolen;
}

int WfileP(card** phead,char* password)
{
    card *p=*phead;
    int boolen=1;
    FILE *fp = NULL ;
    fp = fopen("transaction.csv","w");
    if (fp==NULL) {
        boolen=0;
    } else {
        while (p!=NULL) {
            fprintf(fp,"%d,%s,%d,%s,%f,%d,%d,%d,%d,%d,%d,\n",p->table,p->method,p->cardID,p->horder,p->total,p->year,p->mouth,p->day,p->hour,p->min,p->sec );
            p=p->next;
        }
        fprintf(fp,"%d,",0);
        fprintf(fp,"%s,\n",password);
        fprintf(fp,"%d,\n",0);
        fclose(fp);
    }
    return boolen;
}

int RfileP(card** phead,char *password)
{
    int table;
    char method[20];
    int cardID;
    char horder[50];
    float total;
    int year;
    int mouth;
    int day;
    int boolen=1;
    int hour;
    int min;
    int sec;
    FILE *fp = NULL ;
    fp = fopen("transaction.csv","r");
    if (fp==NULL) {
        boolen=0;
    } else {
        while (1) {
            fscanf(fp, "%d,",&table);
            if (table==0){
                break;
            }
            fscanf(fp, "%[^,],%d,%[^,],%f,%d,%d,%d,%d,%d,%d,",method,&cardID,horder,&total,&year,&mouth,&day,&hour,&min,&sec);
            payFadd(phead, table, horder, total, cardID,day,year,mouth,hour,min,sec);
        }
        fscanf(fp, "%[^,],",password);
        fclose(fp);
    }
    return boolen;
}


int main(int argc, const char * argv[]) {
card* phead=Paycreat();
food* fhead=fodcreat();
categorie* chead=CATcreat();
food* Ohead=ordercreat();
int c;
int cntt;
char fname[20];
float fprice;
int table;
int fnum;
int availability;
char description[50];
int cnum;
char horder[20];
int Syear;
char ch;
int Smouth;
int Sday;
int day;
char a;
int i=0;
int mouth;
int year;
float total;
char YN;
int pay;
int choice;
int cardID=0;
char password[100]="admin";
char pass[10];
int boolen;
int cnt;
char cname[50];
if(RfileP(&phead,password)==1){
payfree(&phead);
}else{
char admin[100]="admin";
WfileP(&phead,admin);
}
do {
    tablemain();
    do {
        printf("\nOption: ");
        scanf("%d",&c);
        while((ch=getchar())!='\n'&&ch!=EOF);
        if (c==1||c==2||c==3) {
            break;
        }
        else{
            printf("\nUnknown option.\n");
        }
    } while (1);
    if (c==1) {
        RfileC(&chead, &fhead);
        do {
            printf("\nEnter the table number: ");
            scanf("%d",&table);
            while((ch=getchar())!='\n'&&ch!=EOF);
            if (table<100&&table>0) {
                break;
            }
            else{
                printf("\nInvalid table number.\n");
            }
        } while (1);
    add:
        CATprint(chead);
        do {
            printf("\nEnter the category number: ");
            scanf("%d",&cnum);
            while((ch=getchar())!='\n'&&ch!=EOF);
            if (CATserachn(&chead, cnum)==1){
                if (fodsrarchn(&fhead, cnum)==1) {
                    fodprint(fhead, cnum);
                    break;
                } else {
                    printf("\nEmpty food list.\n");
                }
            }else{
                printf("\nInvalid category number.\n");
            }
        } while (1);
        do {
            printf("\nEnter the food number: ");
            scanf("%d",&fnum);
            while((ch=getchar())!='\n'&&ch!=EOF);
            if (fodserachN(&fhead, fnum, cnum)==1) {
                    do {
                        printf("\nEnter the food item’s quantity: ");
                        scanf("%d",&availability);
                        while((ch=getchar())!='\n'&&ch!=EOF);
                        if (fodava(&fhead, cnum, fnum, availability)==1&&availability>0) {
                            orderadd(&fhead, &Ohead, fnum, cnum,availability);
                            fodcchange(availability, &fhead, cnum, fnum);
                            break;
                        } else {
                            printf("\nInvalid quantity.\n");
                        }
                    } while (1);
                    do {
                        printf("\nDo you want to order more food (y/n)?: ");
                        scanf("%c",&YN);
                        while((ch=getchar())!='\n'&&ch!=EOF);
                        if (YN=='y'||YN=='n'||YN=='Y'||YN=='N') {
                            break;
                        } else {
                            printf("\nInvalid input.\n");
                        }
                } while (1);
                if (YN=='n'||YN=='N') {
                    break;
                }
            } else {
                printf("\nInvalid food number.\n");
            }
        } while (1);
        do {
            CATprint(chead);
        star:
            printf("\nDo you want to explore more categories (y/n)?: ");
            scanf("%c",&YN);
            while((ch=getchar())!='\n'&&ch!=EOF);
            if (YN=='y'||YN=='n'||YN=='Y'||YN=='N') {
                if (YN=='Y'||YN=='y') {
                    do {
                        printf("\nEnter the category number: ");
                        scanf("%d",&cnum);
                        while((ch=getchar())!='\n'&&ch!=EOF);
                        if (CATserachn(&chead, cnum)==1){
                            if (fodsrarchn(&fhead, cnum)==1) {
                                fodprint(fhead, cnum);
                                break;
                            } else {
                                printf("\nEmpty food list.\n");
                            }
                        }else{
                            printf("\nInvalid category number.\n");
                        }
                    } while (1);
                    do {
                        printf("\nEnter the food number: ");
                        scanf("%d",&fnum);
                        while((ch=getchar())!='\n'&&ch!=EOF);
                        if (fodserachN(&fhead, fnum, cnum)==1) {
                                do {
                                    printf("\nEnter the food item’s quantity: ");
                                    scanf("%d",&availability);
                                    while((ch=getchar())!='\n'&&ch!=EOF);
                                    if (fodava(&fhead, cnum, fnum, availability)==1) {
                                        orderadd(&fhead, &Ohead, fnum, cnum,availability);
                                        fodcchange(availability, &fhead, cnum, fnum);
                                        break;
                                    } else {
                                        printf("\nInvalid quantity.\n");
                                    }
                                } while (1);
                                do {
                                    printf("\nDo you want to order more food (y/n)?: ");
                                    scanf("%c",&YN);
                                    while((ch=getchar())!='\n'&&ch!=EOF);
                                    if (YN=='y'||YN=='n'||YN=='Y'||YN=='N') {
                                        break;
                                    } else {
                                        printf("\nInvalid input.\n");
                                    }
                            } while (1);
                            if (YN=='n'||YN=='N') {
                                break;
                            }
                        } else {
                            printf("\nInvalid food number.\n");
                        }
                    } while (1);
                } else {
                    break;
                }
            } else {
                printf("\nInvalid input.\n");
                goto star;
            }
        } while (1);
        orderoeder(&Ohead);
        do {
            orderprint(Ohead);
            total=orderprice(Ohead);
            printf("Total:%.2f\n",total);
        second:
            printf("\nDo you want to check out (y/n)?: ");
            scanf("%c",&YN);
            while((ch=getchar())!='\n'&&ch!=EOF);
            if (YN=='N'||YN=='n') {
                do {
                    printf("\nDo you want to add to the selected food list (y/n)?: ");
                    scanf("%c",&YN);
                    while((ch=getchar())!='\n'&&ch!=EOF);
                    if (YN=='y'||YN=='Y') {
                        goto add;
                    }
                    else if (YN=='N'||YN=='n'){
                        break;
                    }
                    else{
                        printf("\nInvalid input.\n");
                    }
                } while (1);
                do {
                    printf("\nDo you want to remove from the selected food list (y/n)?: ");
                    scanf("%c",&YN);
                    while((ch=getchar())!='\n'&&ch!=EOF);
                    if (YN=='y'||YN=='Y') {
                        do {
                            printf("\nEnter the food number you want to remove from the selected list: ");
                            scanf("%d-%d",&cnum,&fnum);
                            while((ch=getchar())!='\n'&&ch!=EOF);
                            if (fodserachN(&Ohead, fnum, cnum)==1) {
                                fodremove(&Ohead, fnum, cnum);
                                break;
                            } else {
                                printf("\nInvalid input\n.");
                            }
                        } while (1);
                        break;
                    }
                    else if (YN=='N'||YN=='n'){
                        break;
                    }
                    else{
                        printf("\nInvalid input.\n");
                    }
                } while (1);
            }
            else if (YN=='y'||YN=='Y'){
                break;
            }
            else{
                printf("\nInvalid input.\n");
                goto second;
            }
        } while (1);
        fodfree(&Ohead);
    pay:
        do {
            printf("\n1. Card 2. Cash (1/2)?: ");
            scanf("%d",&pay);
            while((ch=getchar())!='\n'&&ch!=EOF);
            if (pay==1||pay==2) {
                break;
            } else {
                printf("\nInvalid input.\n");
            }
        } while (1);
        if (pay==1) {
            do {
                printf("\nEnter card number: ");
                scanf("%d",&cardID);
                while((ch=getchar())!='\n'&&ch!=EOF);
                if (cardID/100000000>0&&cardID/100000000<10) {
                    break;
                } else {
                    printf("\nInvalid card number.\n");
                }
            } while (1);
            printf("\nEnter the card holder’s name: ");
            fflush(stdin);
            MyFgets(horder, 20);
        }
        printf("\nPayment successful.\n");
        do {
            printf("\n1. Cancel the order 2. Change payment method (1/2)?: ");
            scanf("%d",&choice);
            while((ch=getchar())!='\n'&&ch!=EOF);
            if (choice==2) {
                goto pay;
            }
            else if(choice==1&&pay==1){
                RfileP(&phead,password);
                payadd(&phead, table, horder, total, cardID);
                WfileP(&phead,password);
                payfree(&phead);
                break;
            }
            else if (pay==2&&choice==1){
                break;
            }
            else{
                printf("\nInvalid input.\n");
            }
        } while (1);
        WfileC(&chead, &fhead);
        CATfree(&chead);
        fodfree(&fhead);
    }
    else if (c==2){
        RfileP(&phead,password);
        payfree(&phead);
        CATfree(&chead);
        fodfree(&fhead);
        i=0;
        printf("\nEnter the password: ");
        fflush(stdin);
        system("stty -icanon");
        system("stty -echo");
        while (1) {
            a = getchar();
            if (a == '\n') {
                pass[i++] = '\0';
                break;
            }
            printf("*");
            pass[i++] = a;
        }
        system("stty icanon");
        system("stty echo");
        if (strcmp(pass, password)==0) {
            do {
                Admin();
                do {
                    c=0;
                    printf("\nOption: ");
                    scanf("%d",&c);
                    while((ch=getchar())!='\n'&&ch!=EOF);
                    if (c==1||c==2||c==3||c==4||c==5||c==6||c==7||c==8||c==9) {
                        break;
                    }
                    else{
                        printf("\nUnknown option.\n");
                    }
                } while (1);
                if (c==1) {
                    do {
                        CATfree(&chead);
                        fodfree(&fhead);
                        boolen=RfileC(&chead, &fhead);
                        YN='y';
                        if (boolen==1) {
                            cnt=CATget(&chead)+1;
                        } else {
                            cnt=1;
                        }
                        printf("\nEnter a category name: ");
                        fflush(stdin);
                        MyFgets(cname, 100);
                        if (strlen(cname)==0) {
                            printf("\nInvalid input.\n");
                        }
                        else if (strlen(cname)>50){
                            printf("\nCategory name can’t be more than 50 characters.\n");
                        }
                        else if (CATserach(&chead, cname)==1){
                            printf("\nThe category you entered is already on the list.\n");
                        }
                        else{
                            CATadd(&chead, cnt, cname);
                            if (WfileC(&chead,&fhead)==1) {
                                CATfree(&chead);
                                fodfree(&fhead);
                                printf("\nSuccessfully created category.\n");
                            } else {
                                printf("\nCategory creation unsuccessful.\n");
                            }
                            do {
                                printf("\nDo you want to add more categories (y/n)?: ");
                                scanf("%c",&YN);
                                while((ch=getchar())!='\n'&&ch!=EOF);
                                if (YN=='y'||YN=='n'||YN=='Y'||YN=='N') {
                                    break;
                                }
                                else{
                                    printf("\nInvalid input.\n");
                                }
                            } while (1);
                        }
                        if (YN=='n'||YN=='N') {
                            break;
                        }
                     CATfree(&chead);
                     fodfree(&fhead);
                    } while (1);
                    
                }
                else if (c==2){
                    boolen=RfileC(&chead, &fhead);
                    CATfree(&chead);
                    fodfree(&fhead);
                    if (boolen==1) {
                        do {
                            CATfree(&chead);
                            fodfree(&fhead);
                            RfileC(&chead, &fhead);
                            YN='y';
                            cnum=0;
                            printf("\nEnter a category number: ");
                            scanf("%d",&cnum);
                            while((ch=getchar())!='\n'&&ch!=EOF);
                            if (CATserachn(&chead, cnum)==1) {
                                CATremove(&chead, cnum);
                                fodbigmove(&fhead, cnum);
                                if (WfileC(&chead, &fhead)==1) {
                                    printf("\nSuccessfully deleted.\n");
                                } else {
                                    printf("\nUnsuccessful deletion.\n");
                                }
                                do {
                                    printf("\nDo you want to delete more categories (y/n)?: ");
                                    scanf("%c",&YN);
                                    while((ch=getchar())!='\n'&&ch!=EOF);
                                    if (YN=='y'||YN=='n'||YN=='Y'||YN=='N') {
                                        break;
                                    }
                                    else{
                                        printf("\nInvalid input.\n");
                                    }
                                } while (1);
                                if (YN=='n'||YN=='N') {
                                    break;
                                }
                            } else {
                                printf("\nInvalid input.\n");
                            }
                        } while (1);
                    } else {
                        printf("\nEmpty category list.\n");
                    }
                    CATfree(&chead);
                    fodfree(&fhead);
                }
                else if (c==3){
                    RfileC(&chead, &fhead);
                    CATprint(chead);
                    CATfree(&chead);
                    fodfree(&fhead);
                }
                else if (c==4){
                    RfileC(&chead, &fhead);
                    do {
                        printf("\nEnter a category number: ");
                        scanf("%d",&cnum);
                        while((ch=getchar())!='\n'&&ch!=EOF);
                        if (CATserachn(&chead, cnum)==1) {
                            break;
                        }
                        printf("\nInvalid category number.\n");
                    } while (1);
                    CATfree(&chead);
                    fodfree(&fhead);
                    do {
                        CATfree(&chead);
                        fodfree(&fhead);
                        RfileC(&chead, &fhead);
                        printf("\nEnter food name: ");
                        fflush(stdin);
                        MyFgets(fname, 20);
                        if (fodserachnn(&fhead,fname,cnum)==0) {
                            printf("\nEnter price: ");
                            scanf("%f",&fprice);
                            printf("\nEnter availability: ");
                            scanf("%d",&availability);
                            printf("\nEnter a description: ");
getchar();
                            MyFgets(description, 20);
                            //while((ch=getchar())!='\n'&&ch!=EOF);
                            cntt=fodget(&fhead,cnum)+1;
                            fodadd(&fhead,cntt,fname, fprice, availability, description, cnum);
                            if (WfileC(&chead, &fhead)==1) {
                                printf("\nSuccessfully created food record.\n");
                            } else {
                                printf("\nFood record creation unsuccessful.\n");
                            }
                            do {
                                printf("\nDo you want to add more food records (y/n)?: ");
                                scanf("%c",&YN);
                                while((ch=getchar())!='\n'&&ch!=EOF);
                                if (YN=='y'||YN=='n'||YN=='Y'||YN=='N') {
                                    break;
                                }
                                else{
                                    printf("\nInvalid input.\n");
                                }
                            } while (1);
                            if (YN=='n'||YN=='N') {
                                break;
                            }
                        } else {
                            printf("\nThe food item already exists in the food list in the same category.\n");
                        }
                        CATfree(&chead);
                        fodfree(&fhead);
                    } while (1);
                    CATfree(&chead);
                    fodfree(&fhead);
                }
                else if (c==5){
                    boolen=RfileC(&chead, &fhead);
                    CATfree(&chead);
                    fodfree(&fhead);
                    if (boolen==1) {
                        RfileC(&chead, &fhead);
                        do {
                            cnum=0;
                            printf("\nEnter a category number: ");
                            scanf("%d",&cnum);
                            while((ch=getchar())!='\n'&&ch!=EOF);
                            if (CATserachn(&chead, cnum)==1) {
                                break;
                            }
                            printf("\nInvalid category number.\n");
                        } while (1);
                        if (fodsrarchn(&fhead, cnum)==1) {
                            do {
                                CATfree(&chead);
                                fodfree(&fhead);
                                RfileC(&chead, &fhead);
                                do {
                                    printf("\nEnter a food number: ");
                                    scanf("%d",&fnum);
                                    while((ch=getchar())!='\n'&&ch!=EOF);
                                    if (fodserachN(&fhead, fnum, cnum)==1) {
                                        break;
                                    }
                                    printf("\nInvalid input.\n");
                                } while (1);
                                fodremove(&fhead, fnum, cnum);
                                if (WfileC(&chead, &fhead)==1) {
                                    printf("\nSuccessfully deleted.\n");
                                } else {
                                    printf("\nUnsuccessful deletion.\n");
                                }
                                do {
                                    printf("\nDo you want to delete more food items (y/n)?: ");
                                    scanf("%c",&YN);
                                    while((ch=getchar())!='\n'&&ch!=EOF);
                                    if (YN=='y'||YN=='n'||YN=='Y'||YN=='N') {
                                        break;
                                    }
                                    else{
                                        printf("\nInvalid input.\n");
                                    }
                                } while (1);
                                if (YN=='n'||YN=='N') {
                                    break;
                                }
                            } while (1);
                        } else {
                            printf("\nEmpty food list.\n\n");
                        }
                        CATfree(&chead);
                        fodfree(&fhead);
                    } else {
                        printf("\nEmpty category list.\n\n");
                    }
                    CATfree(&chead);
                    fodfree(&fhead);
                }
                else if (c==6){
                    RfileC(&chead, &fhead);
                    do {
                        cnum=0;
                        printf("\nEnter a category number: ");
                        scanf("%d",&cnum);
                        while((ch=getchar())!='\n'&&ch!=EOF);
                        if (CATserachn(&chead, cnum)==1) {
                            break;
                        }
                        printf("\nInvalid category.\n");
                    } while (1);
                    CATNprint(&chead, cnum);
                    fodprint(fhead, cnum);
                    CATfree(&chead);
                    fodfree(&fhead);
                }
                else if (c==7){
                    do {
                        printf("\nEnter the start date: ");
                        scanf("%d/%d/%d",&year,&mouth,&day);
                        while((ch=getchar())!='\n'&&ch!=EOF);
                        if (mouthJ(mouth, day)==1) {
                            break;
                        } else {
                            printf("\nInvalid date.\n");
                        }
                    } while (1);
                    do {
                        printf("\nEnter the end date: ");
                        scanf("%d/%d/%d",&Syear,&Smouth,&Sday);
                        while((ch=getchar())!='\n'&&ch!=EOF);
                        if (mouthJ(Smouth, Sday)==1) {
                            if (Smouth>mouth) {
                                break;
                            }
                            else if (Smouth==mouth&&Sday>day){
                                break;
                            }
                            else if (Syear>year){
                                break;
                            }
                            else{
                                printf("\nStart date must be on or before the end date.\n");
                            }
                        } else {
                            printf("\nInvalid date.\n");
                        }
                    } while (1);
                    RfileP(&phead,password);
                    payprint(phead, year, mouth, day, Syear, Smouth, Sday);
                    payfree(&phead);
                }
                else if (c==8){
                    do {
                        i=0;
                        printf("\nEnter the old password: ");
                        fflush(stdin);
                        system("stty -icanon");
                        system("stty -echo");
                        while (1) {
                            a = getchar();
                            if (a == '\n') {
                                pass[i++] = '\0';
                                break;
                            }
                            printf("*");
                            pass[i++] = a;
                        }
                        system("stty icanon");
                        system("stty echo");
                        if (strcmp(pass, password)==0) {
                            do {
                                i=0;
                                printf("\nEnter new password: ");
                                fflush(stdin);
                                system("stty -icanon");
                                system("stty -echo");
                                while (1) {
                                    a = getchar();
                                    if (a == '\n') {
                                        pass[i++] = '\0';
                                        break;
                                    }
                                    printf("*");
                                    pass[i++] = a;
                                }
                                system("stty icanon");
                                system("stty echo");
                                if (strlen(pass)>4&&strlen(pass)<16) {
                                    i=0;
                                    printf("\nEnter the new password again: ");
                                    fflush(stdin);
                                    system("stty -icanon");
                                    system("stty -echo");
                                    while (1) {
                                        a = getchar();
                                        if (a == '\n') {
                                            pass[i++] = '\0';
                                            break;
                                        }
                                        printf("*");
                                        password[i++] = a;
                                    }
                                    system("stty icanon");
                                    system("stty echo");
                                    if (strcmp(pass, password)==0) {
                                        RfileP(&phead, pass);
                                        WfileP(&phead, password);
                                        payfree(&phead);
                                        printf("\nPassword successfully changed.\n");
                                        break;
                                    } else {
                                        printf("\nRe-entered new password and new password don’t match.\n");
                                    }
                                } else {
                                    printf("\nPassword can’t be less than 5 characters and more than 15 characters.\n");
                                }
                            } while (1);
                            break;
                        } else {
                            printf("\nInvalid password.\n");
                            do {
                                printf("\nDo you want to change the password (y/n)?: ");
                                scanf("%c",&YN);
                                while((ch=getchar())!='\n'&&ch!=EOF);
                                if (YN=='y'||YN=='n'||YN=='Y'||YN=='N') {
                                    break;
                                }
                                else{
                                    printf("\nInvalid input.\n");
                                }
                            } while (1);
                            if (YN=='n'||YN=='N') {
                                break;
                            }
                        }
                    } while (1);
                }
                else if (c==9){
                    break;
                }
            } while (1);
        } else {
            printf("\nInvalid password\n");
        }
    }
    else if (c==3){
        break;
    }
} while (1);
}

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值