首页 > 代码库 > C语言练手自己编写学生成绩管理系统

C语言练手自己编写学生成绩管理系统

#include<stdio.h>#include<stdlib.h>/*定义学生结构体*/struct Student{    char ID[20];    char Name[20];    float Mark1;    float Mark2;    float Mark3;    float Avarage;};/*声明学生数组及学生数量*/struct Student students[1000];int num=0;/*计算平均成绩*/float Avg(struct Student stu){    return (stu.Mark1+stu.Mark2+stu.Mark3)/3;    }/*根据学号寻找数组下标*/int GetIndexByStudentId(char id[]){    int i;    for(i=0;i<num;i++)    {        if(strcmp(students[i].ID,id)==0)        {            return i;        }    }    return -1;}/*根据姓名寻找数组下标*/int GetIndexByStudentName(char name[]){    int i;    for(i=0;i<num;i++)    {        if(strcmp(students[i].Name,name)==0)        {            return i;        }    }    return -1;}/*SHowStudent*/int ShowStudentInfo(){    int i;    IO_ReadInfo();        printf("%10s%10s%10s%10s%10s%10s\n","学号","姓名","成绩1","成绩2","成绩3","平均分");    printf("-----------------------------------------------------------\n");        for(i=0;i<=num;i++)    {        students[i].Avarage=Avg(students[i]);        printf("%10s%10s%10.2f%10.2f%10.2f%10.2f\n",students[i].ID,students[i].Name,students[i].Mark1,students[i].Mark2,        students[i].Mark3,students[i].Avarage);    }    return 0;}int ShowStudentInfo1(){    int i;    IO_ReadInfo();        printf("%10s%10s%10s%10s%10s%10s\n","学号","姓名","成绩1","成绩2","成绩3","平均分");    printf("-----------------------------------------------------------\n");        for(i=0;i<=num;i++)    {        students[i].Avarage=Avg(students[i]);        printf("%10s%10s%10.2f%10.2f%10.2f%10.2f\n",students[i].ID,students[i].Name,students[i].Mark1,students[i].Mark2,        students[i].Mark3,students[i].Avarage);    }    system("pause");    printMenu();    printf("%d\n",userChoice());}/*增加学生记录*/int addStudent(){    while(1)    {        printf("请输入学号:");        scanf("%s",&students[num].ID);        printf("请输入姓名:");        scanf("%s",&students[num].Name);        printf("请输入成绩1:");        scanf("%f",&students[num].Mark1);        printf("请输入成绩2:");        scanf("%f",&students[num].Mark2);        printf("请输入成绩3:");        scanf("%f",&students[num].Mark3);        ShowStudentInfo();        IO_WriteInfo();        num++;        getchar();        printf("是否继续?y/n");        if(getchar()==n)        {                        printMenu();            printf("%d\n",userChoice());            break;        }    }    return 0;}/*将信息写入文件*/int IO_WriteInfo(){    int i;    FILE *fp;    fp=fopen("C:\\12.txt","wb");    fwrite(&num,sizeof(int),1,fp);    for(i=0;i<=num;i++)    {        fwrite(&students[i],sizeof(struct Student),1,fp);    }    fclose(fp);}/*将文件信息读出*/int IO_ReadInfo(){    int i;    FILE *fp;    fp=fopen("C:\\12.txt","wb");    fread(&num,sizeof(int),1,fp);    for(i=0;i<=num;i++)    {        fread(&students[i],sizeof(struct Student),1,fp);    }    fclose(fp);}int main(){    printMenu();    printf("%d\n",userChoice());        system("pause");    }//菜单打印int printMenu(){    printf("欢迎使用学生成绩管理系统!\n");    printf("1.增加学生记录\n");    printf("2.修改学生记录\n");    printf("3.删除学生记录\n");    printf("4.查询学生记录\n");    printf("5.退出\n");    return 0;}//接收用户输入选择int userChoice(){    int choice;    printf("请输入选择《1-5》:");    scanf("%d",&choice);    switch(choice)    {    case 1:        addStudent();    case 2:        return 2;    case 3:        return 3;    case 4:        ShowStudentInfo1();    case 5:        exit(0);    }}

 

废话不多写,一步一步来。

第一步,要确定自己的需求,打印出需求。

int main(){    printf("欢迎使用学生成绩管理系统!\n");    printf("1.增加学生记录\n");    printf("2.修改学生记录\n");    printf("3.删除学生记录\n");    printf("4.查询学生记录\n");    printf("5.退出\n");    system("pause");}

第二步,将菜单打印写一个函数。

int main(){    printMenu();    system("pause");}//菜单打印int printMenu(){    printf("欢迎使用学生成绩管理系统!\n");    printf("1.增加学生记录\n");    printf("2.修改学生记录\n");    printf("3.删除学生记录\n");    printf("4.查询学生记录\n");    printf("5.退出\n");    return 0;}

第三步,接收用户输入的选择,并测试

int main(){    printMenu();    printf("%d\n",userChoice());    system("pause");    }//菜单打印int printMenu(){    printf("欢迎使用学生成绩管理系统!\n");    printf("1.增加学生记录\n");    printf("2.修改学生记录\n");    printf("3.删除学生记录\n");    printf("4.查询学生记录\n");    printf("5.退出\n");    return 0;}//接收用户输入选择int userChoice(){    int choice;    printf("请输入选择《1-6》:");    scanf("%d",&choice);    switch(choice)    {    case 1:        return 1;    case 2:        return 2;    case 3:        return 3;    case 4:        return 4;    case 5:        return 5;    }}

第4步,定义结构体变量Student并插入学生信息打印测试。

#include<stdio.h>/*定义学生结构体*/struct Student{    char ID[20];    char Name[20];    float Mark1;    float Mark2;    float Mark3;    float Avarage;};/*声明学生数组及学生数量*/struct Student students[1000];int num=0;/*增加学生记录*/int addStudent(){    printf("请输入学号:");    scanf("%s",&students[num].ID);    printf("请输入姓名:");    scanf("%s",&students[num].Name);    printf("请输入成绩1:");    scanf("%f",&students[num].Mark1);    printf("请输入成绩2:");    scanf("%f",&students[num].Mark2);    printf("请输入成绩3:");    scanf("%f",&students[num].Mark3);    printf("学号:%s,姓名:%s,成绩1:%0.2f,成绩2:%0.2f,成绩3:%0.2f",        students[num].ID,students[num].Name,students[num].Mark1,students[num].Mark2,students[num].Mark3);    num++;    return 0;}int main(){    printMenu();    printf("%d\n",userChoice());    system("pause");    }//菜单打印int printMenu(){    printf("欢迎使用学生成绩管理系统!\n");    printf("1.增加学生记录\n");    printf("2.修改学生记录\n");    printf("3.删除学生记录\n");    printf("4.查询学生记录\n");    printf("5.退出\n");    return 0;}//接收用户输入选择int userChoice(){    int choice;    printf("请输入选择《1-5》:");    scanf("%d",&choice);    switch(choice)    {    case 1:        addStudent();    case 2:        return 2;    case 3:        return 3;    case 4:        return 4;    case 5:        return 5;    }}

 第五步计算平均成绩,将数据读写到文件。