简单学生管理系统----标准I/O

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#define fileName "stuInfo"

struct Stu {
    char m_name[10];
    char m_sno[20];
    int m_math;
    int m_english;
    struct Stu *next;
};

struct Stu *readStuInfo(void);
struct Stu *delStu(struct Stu *head);
void printStu(struct Stu *stu1);
void menu(void);
void addStu(void);
void getStuInfo(struct Stu *stu1);
void *freeList(struct Stu *head);
void *findStu(struct Stu *head);
void saveStu(struct Stu *head);
void modifStu(struct Stu *head);
int main(){
    struct Stu *stu1;
    int choise=1;
    stu1=readStuInfo();
    while(1){
        menu();
        scanf("%d",&choise);
        switch(choise){
            case 1:
                printStu(stu1);
                break;
            case 2:
                addStu();
                freeList(stu1);
                stu1=readStuInfo();
                break;
            case 3:
                findStu(stu1);
                break;
            case 4:
                stu1=delStu(stu1);
                saveStu(stu1);
                break;
            case 5:
                modifStu(stu1);
                saveStu(stu1);
                break;
            case 0:
                return 0;
            default :break;
        }
    }
    return 0;
}
void menu(void){
    system("clear");
    printf("*****************************\n");
    printf("1.显示\n");
    printf("2.增加\n");
    printf("3.查询\n");
    printf("4.删除\n");
    printf("5.修改\n");
    printf("0.退出\n");
    printf("*****************************\n");
}
void printStu(struct Stu *stu1){
    char ch=0;
    if(stu1==NULL){
        printf("there is no record!\n");
        return ;
    }
    printf("%-10s","name");
    printf("%-10s","sno");
    printf("%-12s","math score");
    printf("%-10s\n","english score");
    while(stu1){
        printf("%-10s",stu1->m_name);
        printf("%-10s",stu1->m_sno);
        printf("%-12d",stu1->m_math);
        printf("%-10d\n",stu1->m_english);
        stu1=stu1->next;
    }
    printf("input any number to continue: ");
    while(1){
        ch=getchar();
        if((ch>='0') && (ch<='9'))break;
    }
}
struct Stu *readStuInfo(void){
    FILE *fpStu=NULL;
    struct Stu *head=NULL;
    struct Stu *p1=NULL,*p2=NULL;
    int n=0;

    fpStu=fopen(fileName,"r");
    if(fpStu==NULL){
        perror("open fileName:");
        return NULL;
    }
    while(1){
        p1=(struct Stu*)calloc(1,sizeof(struct Stu));
        p1->next=NULL;
        n=fread((void *)p1,1,sizeof(struct Stu),fpStu);
        if(n!=sizeof(struct Stu)){
            //perror("read stuInfo");
            //printf("n=%d,sizeof(struct Stu)=%d",n,sizeof(struct Stu));
            break;
        }
        if(head==NULL){
            head=(struct Stu*)p1;
            p2=(struct Stu*)p1;
        }else{
            p2->next=(struct Stu*)p1;
            p2=p1;
        }
    }
    fclose(fpStu);
    return head;
}
void getStuInfo(struct Stu *stu1){
    fflush(stdin);
    printf("input name:");
    scanf("%s",stu1->m_name);
    printf("input Student ID:");
    scanf("%s",stu1->m_sno);
    printf("input math score:");
    scanf("%d",&stu1->m_math);
    printf("input english score:");
    scanf("%d",&stu1->m_english);
}
void addStu(void){
    struct Stu stu1;
    FILE *fpStu=NULL;
    int n;
    stu1.next=NULL;
    getStuInfo(&stu1);

    fpStu=fopen("stuInfo","a");
    if(fpStu==NULL){
        perror("open stuInfo:");
        return ;
    }
    fwrite(&stu1,sizeof(struct Stu),1,fpStu);
    fclose(fpStu);
}
void *freeList(struct Stu *head){
    struct Stu *p1=head;
    struct Stu *p2=head;
    while(p1){
        p2=p1->next;
        free(p1);
        p1=p2;
    }
    head=NULL;
}
void *findStu(struct Stu *head){
    struct Stu *p1=NULL,*p2=NULL,*h=NULL;
    char ch[256]={0};
    getchar();
    printf("input key work: ");
    fgets(ch,sizeof(ch),stdin);    //末尾会加换行,会读入空
    ch[strlen(ch)-1]=0;
    while(head!=NULL){
        if( (strstr((const char*)head->m_name,(const char *)ch)!=NULL) ||
            (strstr((const char*)head->m_sno,(const char *)ch)!=NULL)  ||
            (head->m_math==atoi(ch))  ||
            (head->m_english==atoi(ch)) ){
                p1=(struct Stu *)calloc(1,sizeof(struct Stu));
                strcpy(p1->m_name,head->m_name);
                strcpy(p1->m_sno,head->m_sno);
                p1->m_math=head->m_math;
                p1->m_english=head->m_english;
                if(h==NULL){
                    h=p1;
                    p2=p1;
                }else{
                    p2->next=p1;
                    p2=p1;
                }
            }
        head=head->next;
    }
    if(h!=NULL){
        printf("find record:\n");
        printStu(h);
    }else{
        printf("there is record!\n");
    }
    freeList(h);
}
struct Stu *delStu(struct Stu *head){
    struct Stu *h=head,*p1=NULL;
    char name_sno[256];
    printf("please input name or Student ID : ");
    getchar();
    fgets(name_sno,sizeof(name_sno),stdin);
    name_sno[strlen(name_sno)-1]=0;
    while(h){
        if((strcmp(h->m_name,name_sno)==0)||(strcmp(h->m_sno,name_sno)==0)){
            if(h==head){
                p1=head;
                head=head->next;
                free(p1);
            }else{
                p1->next = h->next;
                free(h);
                h=NULL;
            }
            break;
        }else{
            p1=h;
            h=h->next;
        }
    }
    return head;
}
void saveStu(struct Stu *head){
    FILE *fpStu=NULL;

    fpStu=fopen(fileName,"w");
    if(fpStu==NULL){
        perror("open fileName:");
        return ;
    }
    while(head){
        fwrite(head,sizeof(struct Stu),1,fpStu);
        head=head->next;
    }
    fclose(fpStu);
}
void modifStu(struct Stu *head){
    struct Stu *h=head,*p1=NULL;
    char name_sno[256];
    printf("please input name or Student ID : ");
    getchar();
    fgets(name_sno,sizeof(name_sno),stdin);
    name_sno[strlen(name_sno)-1]=0;
    while(h){
        if((strcmp(h->m_name,name_sno)==0)||(strcmp(h->m_sno,name_sno)==0)){
            getStuInfo(h);
            break;
        }
        h=h->next;
    }
}
只是简单的实现了增删改查,很多边界条件没有考虑

猜你喜欢

转载自blog.csdn.net/qq_36337149/article/details/81042011