我的学生成绩管理系统

 

 

 
 
目的:本次作业综合考察应用数组、函数、指针、结构体、文件等知识进行编程的能力。
  • 功能:设计一个菜单驱动的学生成绩管理程序,实现以下管理功能。
    • 输入、修改、删除和显示n个学生的m门考试科目的成绩,计算总分和平均分;
    • 分别按总分和学号进行排序;
    • 输入学生的学号,显示该学生的姓名、各门功课的成绩;
    • 将输入的学生成绩数据保存到文件中;
    • 从文件中读出学生成绩数据,查询学生成绩。
  • 具体要求
    • 使用动态数据结构(链表)来存储和管理n个学生的学号、姓名、成绩等信息,进行编程;
    • 程序能够进行异常处理,检查用户输入数据的有效性,在用户输入数据有错误(如类型错误)或无效时,不会中断程序的执行,程序具有一定的健壮性;
    • 注意程序界面的友好性,即给用户一定的操作提示信息;
  • 程序运行时,菜单形式如下:
Management for Students' scores 
1. Append record 
2. List record 
3. Delete record 
4. Modify record 
5. Search record 
6. Sort Score in descending order by sum 
7. Sort Score in ascending order by sum 
8. Sort Score in descending order by num 
9. Sort Score in ascending order by num 
W. Write to a File 
R. Read from a File 
0. Exit
Please Input your choice:

#include <stdio.h>
#include <stdlib.h>
struct student/*构建学生结构体*/
{
    char stuNum[11];
    char stuNam[20];
    float cYuyan;
    float English;
    float shuZiLuoJi;
    float jiSuanJiDaoLun;
    float xianShu;
    float gaoShu;
    float aver;
    float sum;
    struct student *next;
};
typedef struct student stu;
stu* appendRecord(stu * student1)
{/*入口参数:为链表的头
返回值:链表的头
功能:在链表中添加新的结构块*/
    int num = 0,i=0;
    stu * zanYong1=NULL,*zanYong2=NULL;
    zanYong1=student1;
    printf("How many students you want to add ?/n");
    scanf("%d",&num);
    if (num>0)
    {
        zanYong2=(stu*)malloc(sizeof(stu));
        if (zanYong2==NULL)
        {
            printf("No enough memory to alloc/n");
            exit(0);
        }
        if (student1==NULL)
        {
            student1 = zanYong2;
        }
        else
        {
            while (zanYong1->next!=NULL)
            {
                zanYong1=zanYong1->next;
            }
            zanYong1->next=zanYong2;
        }
        zanYong1=zanYong2;
        printf("Please put in the number of the %dth student:/n",i+1);
        scanf("%s",&(zanYong1->stuNum));
        printf("The name/n");
        scanf("%s",&(zanYong1->stuNam));
        printf("The score of cYuyan:/n");
        scanf("%f",&(zanYong1->cYuyan));
        while (zanYong1->cYuyan<0||zanYong1->cYuyan>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->cYuyan));
        }
 
        printf("The score of English:/n");
        scanf("%f",&(zanYong1->English));
        while (zanYong1->English<0||zanYong1->English>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->English));
        }
        printf("The score of shuZiLuoJi:/n");
        scanf("%f",&(zanYong1->shuZiLuoJi));
        while (zanYong1->shuZiLuoJi<0||zanYong1->shuZiLuoJi>100)
        {
            printf("Wrong! Please put again!");
scanf("%f",&(zanYong1->shuZiLuoJi));
        }
        printf("The score of jiSuanJiDaoLun:/n");
        scanf("%f",&(zanYong1->jiSuanJiDaoLun));
        while (zanYong1->jiSuanJiDaoLun<0||zanYong1->jiSuanJiDaoLun>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->jiSuanJiDaoLun));
        }
 
        printf("The score of xianShu:/n");
        scanf("%f",&(zanYong1->xianShu));
        while (zanYong1->xianShu<0||zanYong1->xianShu>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->xianShu));
        }
        printf("The score of gaoShu:/n");
        scanf("%f",&(zanYong1->gaoShu));
        while (zanYong1->gaoShu<0||zanYong1->gaoShu>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->gaoShu));
        }
        zanYong1->sum=zanYong1->gaoShu+zanYong1->xianShu+zanYong1->
jiSuanJiDaoLun+zanYong1->shuZiLuoJi+zanYong1->English+zanYong1->cYuyan;
        zanYong1->aver=zanYong1->sum/6;/*计算平均值*/
        for (i=1;i<num;i++)/*继续存入数据直到存入数据达到一定数量时结束*/
        {
            zanYong2=(stu*)malloc(sizeof(stu));
            zanYong1->next=zanYong2;
            zanYong1=zanYong2;
            printf("Please put in the number of the %dth student:/n",i+1);
            scanf("%s",&(zanYong1->stuNum));
            printf("The name/n");
            scanf("%s",&(zanYong1->stuNam));
            printf("The score of cYuyan:/n");
            scanf("%f",&(zanYong1->cYuyan));
            while (zanYong1->cYuyan<0||zanYong1->cYuyan>100)/*只有所输入的数据满
足要求时才能输入下一个数据*/
            {
                printf("Wrong! Please put again!");
                scanf("%f",&(zanYong1->cYuyan));
            }
 
            printf("The score of English:/n");
            scanf("%f",&(zanYong1->English));
            while (zanYong1->English<0||zanYong1->English>100)
            {
                printf("Wrong! Please put again!");
                scanf("%f",&(zanYong1->English));
            }
            printf("The score of shuZiLuoJi:/n");
            scanf("%f",&(zanYong1->shuZiLuoJi));
            while (zanYong1->shuZiLuoJi<0||zanYong1->shuZiLuoJi>100)
            {
                printf("Wrong! Please put again!");
                scanf("%f",&(zanYong1->shuZiLuoJi));
            }
            printf("The score of jiSuanJiDaoLun:/n");
            scanf("%f",&(zanYong1->jiSuanJiDaoLun));
            while (zanYong1->jiSuanJiDaoLun<0||zanYong1->jiSuanJiDaoLun>100)
            {
                printf("Wrong! Please put again!");
                scanf("%f",&(zanYong1->jiSuanJiDaoLun));
            }
 
            printf("The score of xianShu:/n");
            scanf("%f",&(zanYong1->xianShu));
            while (zanYong1->xianShu<0||zanYong1->xianShu>100)
            {
                printf("Wrong! Please put again!");
                scanf("%f",&(zanYong1->xianShu));
            }
            printf("The score of gaoShu:/n");
            scanf("%f",&(zanYong1->gaoShu));
            while (zanYong1->gaoShu<0||zanYong1->gaoShu>100)
            {
                printf("Wrong! Please put again!");
                scanf("%f",&(zanYong1->gaoShu));
            }
            zanYong1->sum=zanYong1->gaoShu+zanYong1->xianShu+zanYong1->
jiSuanJiDaoLun+zanYong1->shuZiLuoJi+zanYong1->English+zanYong1->cYuyan;
            zanYong1->aver=zanYong1->sum/6;
        }
        zanYong1->next=NULL;
        zanYong1=NULL;
    }

return student1;

    free(zanYong2);
}
void listRecord(stu * student1 )
{/*入口参数:链表的头
            无返回值
            功能:将链表中的所有数据输出来*/
    stu *zanYong1=NULL;
    zanYong1 = student1;
    printf("_____________________________________________________________________________/n");
printf("|number    |name        |cYuyan|English|shuZi|jiDao|xianShu|gaoShu|aver |sum
 |/n",zanYong1->stuNum);
    printf("_____________________________________________________________________________/n");
    while (zanYong1!=NULL)
    {
 
        printf("|%10s|%12s|%.2f|%.2f|%.2f|%.2f|%.2f|%.2f|%2.2f|%2.2f|/n",zanYong1->stuNum,
zanYong1->stuNam,zanYong1->cYuyan,zanYong1->English,zanYong1->shuZiLuoJi,zanYong1->jiSuanJiDaoLun,
zanYong1->xianShu,zanYong1->gaoShu,zanYong1->aver,zanYong1->sum);
        printf("__________________________________________________________________
_____________/n");
 
        zanYong1=zanYong1->next;
    }
}
stu* descendSortSum(stu * student1 )
{/*入口参数:链表的头
            返回值:链表的头
            功能:按总分降序排列结构体
            */
    int i=0;
stu * zanYong1=NULL,*zanYong2=NULL,*zanYong=NULL,*max=NULL,
*bmax=NULL,*bzanYong2=NULL;
    zanYong1=student1;
    while (zanYong1->next!=NULL)
    {
        max=zanYong1;/*将第一个结构体当作max*/
        zanYong2=zanYong1;
        while (zanYong2!=NULL)
        {
            if (zanYong2->sum>max->sum)/*如果后面的结构体总分中有比第一个
总分大的则将该结构体记作MAX*/
            {
                bmax=bzanYong2;
                max=zanYong2;
            }
            bzanYong2=zanYong2;/*下沿一个结构体继续比较,直到末尾*/
            zanYong2=zanYong2->next;
        }
        if (i==0)
        {
            zanYong=max;/*将最大的结构体记为zanyong*/
            student1=zanYong;/*让student1指向最大的结构体*/
        }
        else
        {
            zanYong->next=max;/*将其余的选出的最大结构按顺序依次连接,建立新的链表*/
            zanYong=zanYong->next;
        }
        if (zanYong1==max)/*将所选出的最大结构体从原链表中提出*/
        {
            zanYong1=zanYong1->next;
        }
        else
        {
            bmax->next=max->next;
            max->next=NULL;
        }
        i++;
    }zanYong->next=zanYong1;/*将原链表中仅剩的结构体放在新链表的末尾*/
 
    printf("ALL RIGHT!/n");
   listRecord(student1);
   return student1;
}
stu* ascendSortSum(stu * student1)
{/*入口参数:链表的头
            返回值:链表的头
           功能:按总分升序排列结构体
            具体步骤同上
            */
    int i=0;
stu * zanYong1=NULL,*zanYong2=NULL,*zanYong=NULL,*max=NULL
*bmax=NULL,*bzanYong2=NULL;
    zanYong1=student1;
    while (zanYong1->next!=NULL)
    {
 max=zanYong1;
        zanYong2=zanYong1;
        while (zanYong2!=NULL)
        {
            if (zanYong2->sum<max->sum)
            {
                bmax=bzanYong2;
                max=zanYong2;
            }
            bzanYong2=zanYong2;
            zanYong2=zanYong2->next;
        }
        if (i==0)
        {
            zanYong=max;
            student1=zanYong;
        }
        else
        {
            zanYong->next=max;
            zanYong=zanYong->next;
        }
        if (zanYong1==max)
        {
            zanYong1=zanYong1->next;
        }
        else
        {
            bmax->next=max->next;
            max->next=NULL;
        }
        i++;
    }zanYong->next=zanYong1;
    printf("ALL RIGHT!/n");
    listRecord(student1);
    return student1;
}
int ziFuZhuanZhengXing(char * Num)
{/*入口参数:字符串指针
    返回值:整形数
    功能:将字符型数字转换成整形数*/
   long i=0,s=0,l=0;
    for (i=0;i<11;i++)
    {
        if(Num[i]>='0'&&Num[i]<='9')
        l= Num[i]-'0';
        else
        l=0;
        s = s*10 + l;
    }
    return s ;
}
stu* ascendSortNum(stu * student1)
{/*入口参数:链表的头
            返回值:链表的头
            功能:按学号升序排列结构体
            具体步骤同上
            */
    int i=0,m=0,n=0;
stu * zanYong1=NULL,*zanYong2=NULL,*zanYong=NULL,*max=NULL,
*bmax=NULL,*bzanYong2=NULL;
    zanYong1=student1;
    while (zanYong1->next!=NULL)
    {
        max=zanYong1;/*入口参数:链表的头
            返回值:链表的头
            功能:按学号升序排列结构体
            具体步骤同上
            */
        zanYong2=zanYong1;
        while (zanYong2!=NULL)
        {
            n=ziFuZhuanZhengXing(zanYong2->stuNum);
            m=ziFuZhuanZhengXing(max->stuNum);
            if (n<m)
            {
                bmax=bzanYong2;
                max=zanYong2;
            }
            bzanYong2=zanYong2;
            zanYong2=zanYong2->next;
        }
        if (i==0)
        {
            zanYong=max;
            student1=zanYong;
        }
        else

 max=zanYong1;

        zanYong2=zanYong1;

        while (zanYong2!=NULL)

        {

            if (zanYong2->sum<max->sum)

            {

                bmax=bzanYong2;

                max=zanYong2;

            }

            bzanYong2=zanYong2;

            zanYong2=zanYong2->next;

        }

        if (i==0)

        {

            zanYong=max;

            student1=zanYong;

        }

        else

        {

            zanYong->next=max;

            zanYong=zanYong->next;

        }

        if (zanYong1==max)

        {

            zanYong1=zanYong1->next;

        }

        else

        {

            bmax->next=max->next;

            max->next=NULL;

        }

        i++;

    }zanYong->next=zanYong1;

    printf("ALL RIGHT!/n");

    listRecord(student1);

    return student1;

}

int ziFuZhuanZhengXing(char * Num)

{/*入口参数:字符串指针

    返回值:整形数

    功能:将字符型数字转换成整形数*/

   long i=0,s=0,l=0;

    for (i=0;i<11;i++)

    {

        if(Num[i]>='0'&&Num[i]<='9')

        l= Num[i]-'0';

        else

        l=0;

        s = s*10 + l;

    }

    return s ;

}

stu* ascendSortNum(stu * student1)

{/*入口参数:链表的头

            返回值:链表的头

            功能:按学号升序排列结构体

            具体步骤同上

            */

    int i=0,m=0,n=0;

stu * zanYong1=NULL,*zanYong2=NULL,*zanYong=NULL,*max=NULL,

*bmax=NULL,*bzanYong2=NULL;

    zanYong1=student1;

    while (zanYong1->next!=NULL)

    {

        max=zanYong1;/*入口参数:链表的头

            返回值:链表的头

            功能:按学号升序排列结构体

            具体步骤同上

            */

        zanYong2=zanYong1;

        while (zanYong2!=NULL)

        {

            n=ziFuZhuanZhengXing(zanYong2->stuNum);

            m=ziFuZhuanZhengXing(max->stuNum);

            if (n<m)

            {

                bmax=bzanYong2;

                max=zanYong2;

            }

            bzanYong2=zanYong2;

            zanYong2=zanYong2->next;

        }

        if (i==0)

        {

            zanYong=max;

            student1=zanYong;

        }

        else

{
            zanYong->next=max;
            zanYong=zanYong->next;
        }
        if (zanYong1==max)
        {
            zanYong1=zanYong1->next;
        }
        else
        {
            bmax->next=max->next;
            max->next=NULL;
        }
        i++;
    }zanYong->next=zanYong1;
 
    printf("ALL RIGHT!/n");
    listRecord(student1);
    return student1;
}
stu* descendSortNum(stu * student1)
{/*入口参数:链表的头
            返回值:链表的头
            功能:按学号降序排列结构体
            具体步骤同上
            */
    int i=0,m=0,n=0;
stu * zanYong1=NULL,*zanYong2=NULL,*zanYong=NULL,*max=NULL,
*bmax=NULL,*bzanYong2=NULL;
    zanYong1=student1;
    while (zanYong1->next!=NULL)
    {
        max=zanYong1;
        zanYong2=zanYong1;
        while (zanYong2!=NULL)
        {
            n=ziFuZhuanZhengXing(zanYong2->stuNum);
            m=ziFuZhuanZhengXing(max->stuNum);
            if (n>m)
            {
                bmax=bzanYong2;
                max=zanYong2;
            }
            bzanYong2=zanYong2;
            zanYong2=zanYong2->next;
        }
        if (i==0)
        {
            zanYong=max;
            student1=zanYong;
        }
        else
        {
            zanYong->next=max;
            zanYong=zanYong->next;
        }
        if (zanYong1==max)
        {
            zanYong1=zanYong1->next;
        }
        else
        {
            bmax->next=max->next;
            max->next=NULL;
        }
        i++;
    }zanYong->next=zanYong1;
 
    printf("ALL RIGHT!/n");
   listRecord(student1);
   return student1;
}
stu *  deleteRecord(stu * student1)
{/*入口参数:链表的头
    返回值:链表的头
    功能:删除链表中的某一结构体*/
    char c;
    stu * bZanYong=NULL,*zanYong1=NULL;
    printf("Who do you want to delect ?/n");
    printf("                   1.His/Her name/n");
    printf("                   2.His/Her number/n");
    fflush(stdin);
    scanf("%c",&c);/*选择是按姓名还是按学号删除该结构体*/
    if (c=='1')
    {
        char name[20]={'/0'},name1[20]={'/0'};
        printf("Please put in his/her name:/n");
        scanf("%s",name);
        zanYong1 = student1;
        strcpy(name1,zanYong1->stuNam);
        if (strcmp(name,name1)==0)/*找到该姓名的同学将其删除*/
        {
            bZanYong=student1;
            student1=student1->next;
            free(bZanYong);
        }
        else
        {
            zanYong1=zanYong1->next;
            bZanYong=student1;
            while (zanYong1!=NULL)
            {
                strcpy(name1,zanYong1->stuNam);
                if (strcmp(name,name1)==0)
                {
                    bZanYong->next=zanYong1->next;
                    free(zanYong1);
                    zanYong1=bZanYong;
                }
                zanYong1=zanYong1->next;
                bZanYong=bZanYong->next;
            }
        }
    }
    else if('2'==c)
    {
        char num[11]={'/0'},num1[11]={'/0'};
        printf("Please put in his/her num:/n");
        scanf("%s",num);
        zanYong1 = student1;
        strcpy(num1,zanYong1->stuNum);
        if (strcmp(num,num1)==0)/*找到该学号的同学将其删除*/
        {
            bZanYong=student1;
            student1=student1->next;
           free(bZanYong);
        }
        else
        {
            zanYong1=zanYong1->next;
            bZanYong=student1;
            while (zanYong1!=NULL)
            {
                strcpy(num1,zanYong1->stuNum);
                if (strcmp(num,num1)==0)
                {
                    bZanYong->next=zanYong1->next;
                   free(zanYong1);
                    zanYong1=bZanYong;
                }
                zanYong1=zanYong1->next;
                bZanYong=bZanYong->next;
            }
        }
        }
    return student1;
}
stu * jiLu(stu * zanYong1)
{/*入口参数:结构体
    返回值;结构体
    功能:修改某一结构体内部数据*/
    char c;
    printf("What do you want to modify?/n");
    printf("          1.name/n");
    printf("          2.number/n");
    printf("          3.the score of cYuYan/n");
    printf("          4.the score of English/n");
    printf("          5.the score of shuZiLuoJi/n");
    printf("          6.the score of jiSuanJiDaoLun/n");
    printf("          7.the score of xianShu/n");
    printf("          8.the score of gaoShu/n");
    fflush(stdin);
    scanf("%c",&c);
    switch (c)
    {
    case '1':
        printf("Please put in the name/n");
        scanf("%s",&zanYong1->stuNam);
        break;
    case '2':
        printf("Please put in the number/n");
        scanf("%s",&zanYong1->stuNum);
        break;
    case '3':
        printf("Please put in the score/n");
        scanf("%f",&(zanYong1->cYuyan));
        while (zanYong1->cYuyan<0||zanYong1->cYuyan>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->cYuyan));
        }
        break;
    case '4':
        printf("Please put in the score/n");
        scanf("%f",&zanYong1->English);
        while (zanYong1->English<0||zanYong1->English>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->English));
        }
        break;
    case '5':
        printf("Please put in the score/n");
        scanf("%f",&zanYong1->shuZiLuoJi);
        while (zanYong1->shuZiLuoJi<0||zanYong1->shuZiLuoJi>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->shuZiLuoJi));
        }
        break;
    case '6':
        printf("Please put in the score/n");
        scanf("%f",&zanYong1->jiSuanJiDaoLun);
        while (zanYong1->jiSuanJiDaoLun<0||zanYong1->jiSuanJiDaoLun>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->jiSuanJiDaoLun));
        }
        break;
    case '7':
printf("Please put in the score/n");
        scanf("%f",&zanYong1->xianShu);
        while (zanYong1->xianShu<0||zanYong1->xianShu>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->xianShu));
        }
        break;
    case '8':
        printf("Please put in the score/n");
        scanf("%f",&zanYong1->gaoShu);
        while (zanYong1->gaoShu<0||zanYong1->gaoShu>100)
        {
            printf("Wrong! Please put again!");
            scanf("%f",&(zanYong1->gaoShu));
        }
        break;
    }
    return zanYong1;
}
void modifyRecord(stu * student1)
{/*入口参数:链表的头
    无返回值
    函数功能:修改某一结构体的数据*/
    char name[20]={'/0'},name1[20]={'/0'};
    char num[11]={'/0'},num1[11]={'/0'};
    char c;
    printf("What do you want to find the student by?/n");
    printf("                   1.His/Her name/n");
    printf("                   2.His/Her number/n");
    fflush(stdin);
    scanf("%c",&c);/*先找到所要修改的结构体*/
    if (c=='1')
    {
        printf("Please put in his/her name:/n");
        scanf("%s",name);
        stu *zanYong1=NULL;
        zanYong1 = student1;
        while (zanYong1!=NULL)
        {
            strcpy(name1,zanYong1->stuNam);
            if (strcmp(name,name1)==0)
            {
                zanYong1=jiLu(zanYong1);
                break;
            }
            else
                zanYong1=zanYong1->next;
        }
    }
    else if (c=='2')
    {
        printf("Please put in his/her number:/n");
        scanf("%s",num);
        stu *zanYong1=NULL;
        zanYong1 = student1;
        while (zanYong1!=NULL)
        {
            strcpy(num1,zanYong1->stuNum);
            if (strcmp(num,num1)==0)
            {
                zanYong1=jiLu(zanYong1);
                break;
            }
            else
                zanYong1=zanYong1->next;
        }
 
    }
}
void searchRecord(stu * student1)
{/*入口参数:链表的头
    无返回值
    函数功能:寻找某一已知其部分内容的的结构体*/
    char c;
    printf("What do you want to find the student by?/n");
    printf("                   1.His/Her name/n");
    printf("                   2.His/Her number/n");
    fflush(stdin);
    scanf("%c",&c);
    if (c=='1')
    {
        char name[20]={'/0'},name1[20]={'/0'};
        printf("Please put in his/her name:/n");
        scanf("%s",name);
        stu *zanYong1=NULL;
        zanYong1 = student1;
        while (zanYong1!=NULL)

{

            strcpy(name1,zanYong1->stuNam);

            if (strcmp(name,name1)==0)

            {

                printf("_____________________________________________________________

________________/n");

                printf("|number    |name        |cYuyan|English|shuZi|jiDao|xianShu|gaoShu|aver |

sum  |/n",zanYong1->stuNum);

                printf("_________________________________________________________

____________________/n");

                printf("|%10s|%10s|%.2f|%.2f|%.2f|%.2f|%.2f|%.2f|%2.2f|%2.2f|/n",

zanYong1->stuNum,zanYong1->stuNam,

zanYong1->cYuyan,zanYong1->English,zanYong1->shuZiLuoJi,zanYong1->jiSuanJiDaoLun,

zanYong1->xianShu,zanYong1->gaoShu,zanYong1->aver,zanYong1->sum);

                printf("___________________________________________________________

____________________/n");

                break;

            }

            else

                zanYong1=zanYong1->next;

        }

    }

    else if (c=='2')

    {

        char num[11]={'/0'},num1[11]={'/0'};

        printf("Please put in his/her number:/n");

        scanf("%s",num);

        stu *zanYong1=NULL;

        zanYong1 = student1;

        while (zanYong1!=NULL)

        {

            strcpy(num1,zanYong1->stuNum);

            if (strcmp(num,num1)==0)

            {

                printf("______________________________________________________________

_______________/n");

                printf("|number    |name        |cYuyan|English|shuZi|jiDao|xianShu|gaoShu|aver

|sum  |/n",zanYong1->stuNum);

                printf("_________________________________________________________

____________________/n");

                printf("|%10s|%10s|%.2f|%.2f|%.2f|%.2f|%.2f|%.2f|%2.2f|%2.2f|/n",

zanYong1->stuNum,zanYong1->stuNam,zanYong1->cYuyan,zanYong1->English,

zanYong1->shuZiLuoJi,zanYong1->jiSuanJiDaoLun,zanYong1->xianShu,zanYong1->gaoShu,

zanYong1->aver,zanYong1->sum);

                printf("_____________________________________________________

__________________________/n");

                break;

            }

            else

                zanYong1=zanYong1->next;

        }

    }

}

void writeFile(char * fileName,stu * student1)

{/*入口参数:文件的名字和链表的头

    无返回值

    函数功能:将链表内数据写入文件中*/

    FILE * fp;

    stu * zanYong1;

    if (fp=fopen(fileName,"a+"))

    {

        zanYong1=student1;

        while (zanYong1!=NULL)

        {

            fwrite(zanYong1,sizeof(stu),1,fp);

            zanYong1=zanYong1->next;

        }

        printf("ALL ARIGHT !");

    }

    fclose(fp);

}

stu*readFile(char*fileName)

{/*入口参数:文件的名字

    返回值:链表的头

    函数功能:将已有文件中的内容按结构体的大小读出并放到链表中*/

    stu * student1=NULL,*zanYong1=NULL,*zanYong2=NULL;

    FILE * fp;

    int i=0;

    if (fp=fopen(fileName,"r"))

    {

        student1=(stu*)malloc(sizeof(stu));

        zanYong1=student1;

        fread(zanYong1,sizeof(stu),1,fp);

        fgetc(fp);

        i=1;

        while (!feof(fp))

        {

            fseek(fp,-1,SEEK_CUR);

            zanYong2=malloc(sizeof(stu));

            zanYong1->next=zanYong2;

            zanYong1=zanYong2;

            fread(zanYong1,sizeof(stu),1,fp);

            i++;

            fgetc(fp);

        }

        zanYong1=NULL;

        return student1;

        fclose(fp);

    }

    else

        printf("There is nothing to be done!");

}

int main()

{/*入口参数:无

    返回值:整形数

    函数功能:列出主菜单并控制实现哪种功能*/

    stu *student1=NULL;

    char c,num;

    char *fileName;

    printf("Editor:1093710201/n");

    do

    {

        printf("             **********Welecome to use the system!**********/n");

        printf("                        1. Append record/n");

        printf("                        2. List record /n");

        printf("                        3. Delete record /n");

        printf("                        4. Modify record /n");

        printf("                        5. Search record/n");

        printf("                        6. Sort Score in descending order by sum/n");

        printf("                        7. Sort Score in ascending order by sum /n");

        printf("                        8. Sort Score in descending order by num/n");

        printf("                        9. Sort Score in ascending order by num /n");

        printf("                        W. Write to a File /n");

        printf("                        R. Read from a File  /n");

        printf("                        0. Exit /n");

        printf("Please Input your choice:/n");

        scanf("%c",&num);

        switch (num)

        {

        case '1':

            system("cls");

            student1=appendRecord(student1);

 break;

        case '2':

            system("cls");

            listRecord(student1);

            break;

        case '3':

            system("cls");

            deleteRecord(student1);

            break;

        case '4':

            system("cls");

            modifyRecord(student1);

            break;

        case '5':

            system("cls");

            searchRecord(student1);

            break;

        case '6':

            system("cls");

            student1=descendSortSum(student1);

            break;

        case '7':

            system("cls");

            student1=ascendSortSum(student1);

            break;

        case '8':

            system("cls");

            student1=descendSortNum(student1);

            break;

        case '9':

            system("cls");

            student1=ascendSortNum(student1);

            break;

        case 'W':

            system("cls");

            fileName=(char*)malloc(sizeof(char)*20);

            printf("Please put int the name of the file /n");

            scanf("%s",fileName);

            writeFile(fileName,student1);

            free(fileName);

            break;

        case 'R':

            system("cls");

            fileName=(char*)malloc(sizeof(char)*20);

            printf("Please put int the name of the file ");

            scanf("%s",fileName);

            student1=readFile(fileName);

            break;

        case '0':

            system("cls");

            exit(0);

        }

        printf("Do you want to continue(y/n)?/n");

        scanf("%c",&c);

        getch();

        system("cls");

    }

    while (c!='n');

    free(fileName);

    return 0;

}

 

展开阅读全文

没有更多推荐了,返回首页