结构体数组实现学生成绩管理系统

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <mmsystem.h>
#include <ctype.h>
#pragma comment(lib,"winmm,lib")
#define N 13//学生姓名的最大位数
#define STU struct student
#define M 30//暂时拟定的最大学生数
struct student
{
	long studentID;
	char studentName[N];
	int score[5];
};
int Menu(void);
void InputData(STU *p,int n);
void Calculatecourse(STU *p,int n);
void Calculatestudent(STU *p,int n);
void Descendingtotal(STU *p,int n);
void Ascendingtotal(STU *p,int n);
void Sortnumber(STU *p,int n);
void Sortname(STU *p,int n);
void SearchstudentID(STU *p,int n);
void SearchstudentName(STU *p,int n);
void Analysiscourse(STU *p,int n);
void Listrecord(STU *p,int n);
void WritetoFile(STU *p,int n);
void ReadfromFile(STU *p,int n);
int main()
{
	//PlaySound(TEXT("D:\\xuexi\\C Primer Plus\\shiyandazuoye\\2.wav"),NULL,SND_FILENAME | SND_ASYNC | SND_LOOP);
	//下载wav文件后请将上述路径改为电脑中.wav所在路径
	Sleep(0);
	system("color F9");//更改控制台窗口的背景和字体颜色0 = 黑色 8 = 灰色
    /*1 = 蓝色 9 = 淡蓝色
      2 = 绿色 A = 淡绿色
      3 = 湖蓝色 B = 淡浅绿色
      4 = 红色 C = 淡红色
      5 = 紫色 D = 淡紫色
      6 = 黄色 E = 淡黄色
      7 = 白色 F = 亮白色
      0 = 黑色 */
	int i;
	char c;//功能1下判断Y/N
	int count=0;//用来统计功能1下用户成功输入的学生人数
	STU stu[M];
END:i = Menu();
    switch (i)
	{
	case 0:
		printf("Welcome back next time.\n");
		return 0;
	case 1:
	    printf("Do you want to input a record?(Y/N)\n");
	    scanf(" %c",&c);
	    while(c=='Y'||c=='y')
        {
            InputData(stu,count);
            count++;
            printf("Do you want to input another record?(Y/N)\n");
            scanf(" %c",&c);
        }
        printf("%d new records have been added.",count);
        system("pause");
        system("cls");
        goto END;
    case 2:
        Calculatecourse(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 3:
        Calculatestudent(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 4:
        Descendingtotal(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 5:
        Ascendingtotal(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 6:
        Sortnumber(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 7:
        Sortname(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 8:
        SearchstudentID(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 9:
        SearchstudentName(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 10:
        Analysiscourse(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 11:
        Listrecord(stu,count);
        system("pause");
        system("cls");
        goto END;
    case 12:
        WritetoFile(stu,count);
        printf("The operation is over.\n");
        system("pause");
        system("cls");
        goto END;

	case 13:
        ReadfromFile(stu,count);
        printf("The operation is over.\n");
        system("pause");
        system("cls");
        goto END;

    }



}

int Menu(void)
{
	int i;
	printf("                                **************************************************************\n");
	printf("                                * Welcome to the Student Achievement Managment System\n");
	printf("                                * Please select one of the following features\n");
	printf("                                * 1.Input record\n");
	printf("                                * 2.Calculate total and average score of every course\n");
	printf("                                * 3.Calculate total and average score of every student\n");
	printf("                                * 4.Sort in descending order by total score of every student\n");
	printf("                                * 5.Sort in ascending order by total score of every student\n");
	printf("                                * 6.Sort in ascending order by number\n");
	printf("                                * 7.Sort in dictionary order by name\n");
	printf("                                * 8.Search by number\n");
	printf("                                * 9.Search by name\n");
	printf("                                * 10.Statistic analysis for every course\n");
	printf("                                * 11.List record\n");
	printf("                                * 12.Write to a file\n");
	printf("                                * 13.Read from a file\n");
	printf("                                * 0.Exit\n");
	printf("                                * Please enter your choice:\n");
	printf("                                **************************************************************\n");
	scanf("%d", &i);
	return i;
}

void InputData(STU *p ,int n)
{
    int j;//用来输入各科成绩
    printf("Please enter in the following order:studentID,studentName(not more than 13),\n");
    printf("score(Maths,English,Chinese,Physics,Chemistry\n");
    scanf("%ld",&p[n].studentID);
    scanf("%s",p[n].studentName);
    for(j=0;j<5;j++)
    {
        scanf("%d",&p[n].score[j]);
    }
}

void Calculatecourse(STU *p,int n)
{
    int j;//给学科排序
    int i;//用来确定某个学生
    int sum[5];//计算每一个科目的总分
    float aver[5];//计算每一个科目的平均分
    char sub[5][10]={"Maths","English","Chinese","Physics","Chemistry"};
    for(j=0;j<5;j++)
    {
       sum[j] = 0;
       for(i=0;i<n;i++)
       {
           sum[j]+=p[i].score[j];
       }
       aver[j] = (float)sum[j]/n;
       printf("The total score of %s is %d\n",sub[j],sum[j]);
       printf("The average score of %s is %.2f\n",sub[j],aver[j]);
       putchar('\n');
    }

}

void Calculatestudent(STU *p,int n)
{
     int i;//给学生计数
     int j;//給科目计数
     int sum[n];
     float aver[n];
     for(i=0;i<n;i++)
     {
         sum[i] = 0;
         for(j=0;j<5;j++)
         {
             sum[i]+=p[i].score[j];
         }
         aver[i] = sum[i]/5;
         printf("The total score of %s is %d.\n",p[i].studentName,sum[i]);
         printf("The average score of %s is %.2f.\n",p[i].studentName,aver[i]);
         putchar('\n');
     }
}

void Descendingtotal(STU *p,int n)
{
     int i;//给学生计数
     int j;//給科目计数
     int sum[n];
     int temp1;//储存sum
     STU temp2;//交换结构体
     for(i=0;i<n;i++)
     {
         sum[i] = 0;
         for(j=0;j<5;j++)
         {
             sum[i]+=p[i].score[j];
         }
     }
     for(i=0;i<n;i++)
     {
         for(j=0;j<n-1-i;j++)
         {
             if(sum[j]<sum[j+1])
             {
                 temp1 = sum[j+1];
                 sum[j+1] = sum[j];
                 sum[j] = temp1;
                 temp2 = p[j+1];
                 p[j+1] = p[j];
                 p[j] = temp2;
             }
         }
     }
     printf("Students are sorted in descending order by grade as follows:\n");
     putchar('\n');
     for(i=0;i<n;i++)
     {
      printf("%s's  total grades: %d\n",p[i].studentName,sum[i]);
      putchar('\n');
     }

}

void Ascendingtotal(STU *p,int n)
{
     int i;//给学生计数
     int j;//給科目计数
     int sum[n];
     int temp1;//储存sum
     STU temp2;//交换结构体
     for(i=0;i<n;i++)
     {
         sum[i] = 0;
         for(j=0;j<5;j++)
         {
             sum[i]+=p[i].score[j];
         }
     }
     for(i=0;i<n;i++)
     {
         for(j=0;j<n-1-i;j++)
         {
             if(sum[j]>sum[j+1])
             {
                 temp1 = sum[j+1];
                 sum[j+1] = sum[j];
                 sum[j] = temp1;
                 temp2 = p[j+1];
                 p[j+1] = p[j];
                 p[j] = temp2;
             }
         }
     }
     printf("Students are sorted in ascending order by grade as follows:\n");
     putchar('\n');
     for(i=0;i<n;i++)
     {
      printf("%s's  total grades: %d\n",p[i].studentName,sum[i]);
      putchar('\n');
     }

}

void Sortnumber(STU *p,int n)
{
    int i;
    STU temp;//用于冒泡排序
    int j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(p[j].studentID>p[j+1].studentID)
            {
                temp = p[j+1];
                p[j+1] = p[j];
                p[j]= temp;
            }
        }
    }//将结构体数组按学号从小到大重新赋予顺序*/
    printf("Students are sorted in ascending order by studentID as follows:\n");
    putchar('\n');
    for(i=0;i<n;i++)
    {
        printf("%10ld%12s",p[i].studentID,p[i].studentName);
        for(j=0;j<5;j++)
        {
            printf("%4d",p[i].score[j]);
        }
        putchar('\n');
        putchar('\n');
    }

}

void Sortname(STU *p,int n)
{

    int i;
    int j;//用于冒泡排序
    STU temp;//交换结构体数组顺序
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(strcmp(p[j].studentName,p[j+1].studentName)>0)
            {
                temp = p[j+1];
                p[j+1] = p[j];
                p[j]= temp;
            }
        }
    }//冒泡排序
    printf("Students are sorted in ascending order by studentName as follows:\n");
    putchar('\n');
    for(i=0;i<n;i++)
    {
        printf("%10ld%12s",p[i].studentID,p[i].studentName);
        for(j=0;j<5;j++)
        {
            printf("%4d",p[i].score[j]);
        }
        putchar('\n');
        putchar('\n');
    }
}

void SearchstudentID(STU *p,int n)
{
     int i;//给学生计数
     int j;//給科目计数
     int sum[n];
     int temp1;//储存sum
     STU temp2;//交换结构体
     long flag;
     int a;//判断是否找到
     for(i=0;i<n;i++)
     {
         sum[i] = 0;
         for(j=0;j<5;j++)
         {
             sum[i]+=p[i].score[j];
         }
     }
     for(i=0;i<n;i++)
     {
         for(j=0;j<n-1-i;j++)
         {
             if(sum[j]<sum[j+1])
             {
                 temp1 = sum[j+1];
                 sum[j+1] = sum[j];
                 sum[j] = temp1;
                 temp2 = p[j+1];
                 p[j+1] = p[j];
                 p[j] = temp2;
             }
         }
     }//冒泡排序
     printf("Please input the studentID to find:");
     scanf("%ld",&flag);
     putchar('\n');
     for(i=0,a=0;i<n;i++)
     {
         if(p[i].studentID == flag)
         {
               printf("%ld %s 's ranking is number %d\n",p[i].studentID,p[i].studentName,i+1);
               putchar('\n');
               printf("His grades are as follows:\n");
               putchar('\n');
               for(j=0;j<5;j++)
               {
                   printf("%-5d",p[i].score[j]);
               }
               a = 1;
         }
     }
     if(a==0)
     {
         printf("Not found!  Please check the studentID you entered.\n");
     }

}

void SearchstudentName(STU *p,int n)
{
     int i;//给学生计数
     int j;//給科目计数
     int sum[n];
     int temp1;//储存sum
     STU temp2;//交换结构体
     char flag[N];
     int a;//判断是否找到
     for(i=0;i<n;i++)
     {
         sum[i] = 0;
         for(j=0;j<5;j++)
         {
             sum[i]+=p[i].score[j];
         }
     }
     for(i=0;i<n;i++)
     {
         for(j=0;j<n-1-i;j++)
         {
             if(sum[j]<sum[j+1])
             {
                 temp1 = sum[j+1];
                 sum[j+1] = sum[j];
                 sum[j] = temp1;
                 temp2 = p[j+1];
                 p[j+1] = p[j];
                 p[j] = temp2;
             }
         }
     }//冒泡排序
     printf("Please input the studentName to find:");
     scanf("%s",flag);
     putchar('\n');
     for(i=0,a=0;i<n;i++)
     {
         if(strcmp(p[i].studentName,flag)==0)
         {
             printf("%ld %s 's ranking is number %d\n",p[i].studentID,p[i].studentName,i+1);
             putchar('\n');
             printf("His grades are as follows:\n");
             putchar('\n');
             for(j=0;j<5;j++)
               {
                   printf("%-5d",p[i].score[j]);
               }
             a = 1;

         }
     }
     if(a==0)
     {
         printf("Not found!  Please check the studentName you entered.\n");
     }

}

void Analysiscourse(STU *p,int n)
{

    int j;
    int i;
    int analysis[5][5]={0};//统计人数
    float percentage[5][5]={0};//人数所占百分比
    char sub[5][10]={"Maths","English","Chinese","Physics","Chemistry"};
    for(j=0;j<5;j++)
    {
        for(i=0;i<n;i++)
        {
            if(p[i].score[j]>=0&&p[i].score[j]<=59)
            {
                analysis[j][0]++;
            }
            else if(p[i].score[j]<69)
            {
                analysis[j][1]++;
            }
            else if(p[i].score[j]<79)
            {
                analysis[j][2]++;
            }
            else if(p[i].score[j]<89)
            {
                analysis[j][3]++;
            }
            else
            {
                analysis[j][4]++;
            }
        }
    }
    for(j=0;j<5;j++)
    {
        for(i=0;i<5;i++)
        {
            percentage[j][i] = 100*(float)analysis[j][i]/n;
        }
    }
    for(j=0;j<5;j++)
    {
        printf("The analysis of %s are as follows:\n",sub[j]);
        putchar('\n');
        putchar('\n');
        printf("不及格人数为: %-4d 所占比例为:%.2f%%\n",analysis[j][0],percentage[j][0]);
        putchar('\n');
        printf("及格人数为:   %-4d 所占比例为:%.2f%%\n",analysis[j][1],percentage[j][1]);
        putchar('\n');
        printf("中等人数为:   %-4d 所占比例为:%.2f%%\n",analysis[j][2],percentage[j][2]);
        putchar('\n');
        printf("良好人数为:   %-4d 所占比例为:%.2f%%\n",analysis[j][3],percentage[j][3]);
        putchar('\n');
        printf("优秀人数为:   %-4d 所占比例为:%.2f%%\n",analysis[j][4],percentage[j][4]);
        putchar('\n');
        putchar('\n');
    }


}

void Listrecord(STU *p,int n)
{
    int i;
    int j;
    int sum[5];//计算每一个科目的总分
    float aver[5];//计算每一个科目的平均分
    char sub[5][10]={"Maths","English","Chinese","Physics","Chemistry"};
    for(i=0;i<n;i++)
    {
        printf("%ld  %13s\n",p[i].studentID,p[i].studentName);
        printf("His scores are as follows:\n");
        for(j=0;j<5;j++)
        {
            printf("%10s score is %d\n",sub[j],p[i].score[j]);
        }
        putchar('\n');
    }
    for(j=0;j<5;j++)
    {
       sum[j] = 0;
       for(i=0;i<n;i++)
       {
           sum[j]+=p[i].score[j];
       }
       aver[j] = (float)sum[j]/n;
       printf("The total score of %s is %d\n",sub[j],sum[j]);
       printf("The average score of %s is %.2f\n",sub[j],aver[j]);
       putchar('\n');
    }
}

void WritetoFile(STU *p,int n)
{
    int i,j;
    FILE *fp;
    fp = fopen("student.txt","w");
    if(fp == NULL)
    {
        printf("Failure to open student.txt!\n");
        return;
    }
    fprintf(fp,"Number: Students and Subjects\n");
    fprintf(fp,"%d\t%d\n",n,5);
    fprintf(fp,"\n");
    for(i=0;i<n;i++)
    {
        fprintf(fp,"%ld%13s\n",p[i].studentID,p[i].studentName);
        for(j=0;j<5;j++)
        {
            fprintf(fp,"%4d\n",p[i].score[j]);
        }
        fprintf(fp,"\n");
    }
    fclose(fp);
}

void ReadfromFile(STU *p,int n)
{
    FILE *fp;
    int i,j;
    char str[30];
    int renshu,subject;
    char sub[5][10]={"Maths","English","Chinese","Physics","Chemistry"};
    fp = fopen("student.txt","r");
    if(fp == NULL)
    {
        printf("Failure to open student.txt!\n");
        return;
    }
    if(fgets(str,30,fp)!=NULL)
    {
        puts(str);
    }
    fscanf(fp,"%d\t%d\n",&renshu,&subject);
    printf("        %d            %d\n",renshu,subject);
    for(i=0;i<n;i++)
    {
        fscanf(fp,"%ld%13s\n",&p[i].studentID,p[i].studentName);
        for(j=0;j<5;j++)
        {
            fscanf(fp,"%4d\n",&p[i].score[j]);
        }
    }
    fclose(fp);
    for(i=0;i<n;i++)
    {
        printf("%ld  %13s\n",p[i].studentID,p[i].studentName);
        printf("His scores are as follows:\n");
        for(j=0;j<5;j++)
        {
            printf("%10s score is %d\n",sub[j],p[i].score[j]);
        }
        putchar('\n');
    }
}







 

  • 8
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值