c用餐管理系统----------文件链表相关操作


另附:学生成绩管理系统(附文档)

        学生用餐管理系统

 实现方式都不一样,可供参考. 



#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define MaxSize 20
typedef struct guest_info {
    char name[8];
    int sum;
    char time[10];
    int number;
    struct guest_info * next;
} GuestLink,* Pointer;
void Insert(Pointer * Head);
void Search(Pointer Head );
void Update(Pointer Head);
void Delete(Pointer * Head);
void Show(Pointer Head);

Pointer Sort(Pointer );///排序
void Storage(Pointer ); ///储存
Pointer ReadFile();///浏览文件
int main() {
    Pointer Head=NULL;
    int i;
    do {
        printf("\n");
        printf("1---插入(Insert)\n");
        printf("2---查询(Search)\n");
        printf("3---修改(Update)\n");
        printf("4---删除-(Delete)\n");
        printf("5---显示(Show)\n");
        printf("6---退出(Exit)\n");
        printf("7---排序(Sort)\n");
        printf("8---录入文件(Storage)\n");
        printf("9---读取数据(ReadFile)\n");
        scanf("%d",&i);
        switch(i) {
        case 1:
            Insert(&Head);
            break;
        case 2:
            Search(Head);
            break;
        case 3:
            Update(Head);
            break;
        case 4:
            Delete(&Head);
            break;
        case 5:
            Show(Head);
            break;
        case 6:
            break;
        case 7:
            Head = Sort(Head);
            break;
        case 8:
            Storage(Head);
            break;
        case 9:
            Head = ReadFile();
            Show(Head);
            break;
        default:
            printf("错误选择!请重选");
            break;
        }
    } while(i!=6);
    return 0;
}


void Insert(Pointer * Head) {
    int in_number;
    Pointer p,q,r;
    printf("请输入编号:");
    scanf("%d",&in_number);
    p=q=* Head;
    while(p!=NULL) {
        if(p->number==in_number) {
            printf("已经有相同的编号:");
            return;
        } else {
            q=p;
            p=p->next;
        }
    }
    r=(Pointer)malloc(sizeof(GuestLink));
    r->next=NULL;
    if(r==NULL) {
        printf("分配空间失败!");
        return;
    }
    if(q==NULL)
        * Head=r;
    else {
        q->next=r;
    }
    r-> number=in_number;
    printf("请输入姓名:");
    scanf("%s",r->name);
    printf("请输入人数:");
    scanf("%d", &r->sum);
    printf("请输入用餐时间:");
    scanf("%s", r->time);
}



void Search(Pointer Head) {
    int flag=1;
    int number;
    Pointer p;
    printf("请输入要查询的编号:");
    scanf("%d", &number);
    p=Head;
    while(p!=NULL&&flag) {
        if(p->number==number) {
            printf("姓名:%s",p->name);
            printf("人数:%d",p->sum);
            printf("用餐时间:%s",p->time);
            flag=0;
        } else
            p=p->next;
    }
    if(flag)
        printf("没有查询到!!");
}



void Update(Pointer Head) {
    int flag=1;
    int number;
    Pointer p;
    printf("请输入要修改的编号:");
    scanf("%d",&number);
    p=Head;
    while(p!=NULL&&flag) {
        if(p->number==number) {
            printf("请输入人数:");
            scanf("%d",p->sum);
            printf("请输入用餐时间:");
            scanf("%s",p->time);
            flag=0;
        } else
            p=p->next;
    }
    if(flag)
        printf("没有找到要修改的记录!!");
}


void Delete(Pointer *Head) {
    int flag=1;
    int number;
    Pointer p,q;
    printf("请输入要删除数据的编号");
    scanf("%d",&number);
    p=q=* Head;
    while(p!=NULL&&flag) {
        if(p->number==number) {
            if(p==* Head) {
                *Head=p->next;
                free(p);
            } else {
                q->next=p->next;
                free(p);
            }
            flag=0;
        } else {
            q=p;
            p=p->next;
        }
    }
    if(flag)
        printf("没有找到可以删除的数据!!");
}



void Show(Pointer Head) {
    Pointer p;
    p=Head;
    while(p!=NULL) {
        printf("姓名: %-10s",p->name);
        printf("人数: %-10d",p->sum);
        printf("用餐时间: %-10s",p->time);
        printf("编号: %-10d\n",p->number);
        p=p->next;
    }
}
Pointer Sort(Pointer Head) {
    Pointer pfirst,ptail,pminBefore,pmin,p;
    pfirst = NULL;

    while(Head!=NULL) {
        for (p = Head, pmin = Head; p->next != NULL; p = p->next) {
            if(strcmp(p->next->time,pmin->time) < 0) {
                pminBefore = p;
                pmin = p->next;
            }
        }
        if(pfirst == NULL) {
            pfirst = pmin;
            ptail = pmin;
        } else {
            ptail->next = pmin;
            ptail = pmin;
        }
        if(pmin == Head)
            Head = Head->next;
        else
            pminBefore->next = pmin->next;
    }
    if(pfirst != NULL)
        ptail->next = NULL;
    Head = pfirst;
    return Head;
}
void Storage(Pointer Head) { ///储存
     Pointer cur;
     cur = Head;
     FILE *fp = fp = fopen( "学生信息.txt", "w" );
     while(cur != NULL){
       fprintf(fp, "%s\t%d\t%s\t%d\n", cur->name,cur->sum,cur->time,cur->number);
       cur = cur->next;
     }
     fclose(fp);
}
Pointer ReadFile() {
    char name[8];
    int sum;
    Pointer r,s;
    Pointer first;
    first = (Pointer)malloc(sizeof(GuestLink));
    r = first;
    first->next = NULL;

    char time[10];
    int number;
    FILE* fp;
    fp = fopen("学生信息.txt", "r");
    while(1) {
        fscanf(fp, "%s%d%s%d",name,&sum,time,&number);
        if(feof(fp))
            break;
        s=(Pointer)malloc(sizeof(GuestLink));
        strcpy(s->name,name);
        s->sum = sum;
        strcpy(s->time,time);
        s->number = number;
        r->next = s;
        r = s;
    }
    r->next = NULL;
    return first->next;
    fclose(fp);
}
///浏览文件*/

posted @ 2018-03-07 09:41  秃头大师  阅读(133)  评论(0编辑  收藏  举报