C语言学生管理系统源代码

  • Post author:
  • Post category:其他


新人博主不易,希望看完点赞

/**
*autor:旋尘
*time:2020.4.20
*/
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#define MAXSIZE  50
typedef struct 
{
    char num[12];//学号之所以用char,是为了后面strcmp看输入是否重复,他的类型是char
    char name[20];
    char sex[4];//性别
    int score[3];//三科成绩
    float avg;//平均分
    int sum;//总分
}Student;
int student_number=0;///全局变量,可随便在函数里面改变引用
Student student[MAXSIZE];///全局变量,可随便在函数里面改变引用

int Menu()
{
    int check_number;
    do{
        system("cls");  /*运行前清屏,把选择清掉*/
        printf("\t************学生成绩管理系统*************\n"); 
        printf("\t*| 1. 添加学生信息                      *\n");
        printf("\t*| 2. 显示学生信息                      *\n");
        printf("\t*| 3. 按学号排序                        *\n");
        printf("\t*| 4. 按总成绩排序                      *\n");
        printf("\t*| 5. 查找单个学生                      *\n");
        printf("\t*| 6. 删除指定学生                      *\n");
        printf("\t*| 7. 修改学生信息                      *\n"); 
        printf("\t*| 8. 查看各门课程的平均分              *\n");
        printf("\t*| 9. 查看不及格的学生的信息            *\n");
        printf("\t*| 10. 清空已保存数据                   *\n");
        printf("\t*| 0. 保存退出                          *\n");
        printf("\t*****************************************\n");
        printf("请输入选择(0-10):");
        scanf("%d",&check_number);  
        if(check_number<0||check_number>10)
        {
            printf("错误选择");
            system("pause");
        }
    }while(check_number<0||check_number>10);
    return check_number;
}

添加学生信息
void Input()
{
    int i;
    char choice='y';
    char clear[10];//清空缓冲区
    char student_num[12];//暂存学号,为了识别学号是否重复
    do
    {
        i=0;
        printf("请输入学号\t:    ");
        scanf("%s",student_num);
        while(strcmp(student[i].num,student_num)!=0&&i<student_number)
        {
            i++;
        }
        if(i<student_number)
        {
            printf("学号已存在,请重新输入\n");
        }
        else
        {
            /*memset(student[i].number,0,sizeof(student[i].number));//我觉得没必要存在*/
            strcpy(student[i].num,student_num);
            printf("学生姓名\t:    ");
            scanf("%s",student[i].name);
            printf("学生性别\t:    ");
            scanf("%s",student[i].sex);            
            printf("c语言成绩\t:    ");
            scanf("%d",&student[i].score[0]);
            printf("数据结构成绩\t:    ");
            scanf("%d",&student[i].score[1]);
            printf("数据库成绩\t:    ");
            scanf("%d",&student[student_number].score[2]);
            student[i].avg=(float)(student[i].score[0]+student[i].score[1]+student[i].score[2])/3;
            student[i].sum=student[i].score[0]+student[i].score[1]+student[i].score[2];
            gets(clear);
            printf("此学生信息录入完毕,是否继续?(Y/N)    :");
            scanf("%c",&choice);
            student_number++;不要漏了
            gets(clear);///防止当打多字符是影响后面的整形输入比如前面输入yy系统剩一个y输入%d,导致系统卡死,或者输入到学号里面去
        }
        
    } while (choice=='Y'||choice=='y');
}


显示学生信息
void Show()
{
    int i;
    printf("---------------------------------------------------------------------------------------------------------------------\n");
    printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
    printf("---------------------------------------------------------------------------------------------------------------------\n");
    for(i=0;i<student_number;i++)
    {
        printf("%s\t\t%s     \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
    }之所以        %s     \t是因为名字在4-8个格加5个空格能够大于八,就能被\t凑成16个格与上面对齐
    
}

///根据学号排序,大的在前
void SortByNum()
{
    Student temp;
    int i,j;
    for(i=0;i<student_number;i++)
    {
        for(j=0;j<student_number-i-1;j++)
        {
            if(strcmp(student[j].num,student[j+1].num)>0)
            {
                temp=student[j];
                student[j]=student[j+1];
                student[j+1]=temp;
            }
        }
    }
    Show();
}

//根据总成绩排序,da\\大的在前
void SortBySum()
{
    Student temp;
    int i,j;
    for(i=0;i<student_number;i++)
    {
        for(j=0;j<student_number-i-1;j++)
        {
            if(student[j].sum<student[j+1].sum)
            {
                temp=student[j];
                student[j]=student[j+1];
                student[j+1]=temp;
            }
        }
    }
    Show();
}

/查找单个学生
void Search()
{
    int i=0;
    int choice;
    char testnum[12];
start :
    printf("请输入学号  :  ");
    scanf("%s",testnum);
    i=0;不然循环回来时,i就不为0了
    while(strcmp(testnum,student[i].num)!=0&&i<student_number)
    {
        i++;
    }
    if(i<student_number)
    {
        printf("------------------------------------------------------------------------------------\n");
        printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
        printf("------------------------------------------------------------------------------------\n");
        printf("%s\t\t%s     \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
        printf("若继续查找按1,退出按0   :");
    }
    else
    {
        printf("查无此人,若继续查找按1,退出按0   :");
    }
    scanf("%d",&choice);
    if(choice)
    {
        goto start;
    }
}

/删除学生信息
void Delete()
{
    int i=0,j;
    int choice;
    char testnum[12];
start :
    printf("请输入学号  :  ");
    scanf("%s",testnum);
    while(strcmp(testnum,student[i].num)!=0&&i<student_number)
    {
        i++;
    }
    
    if(i<student_number)
    {
        for(j=i;j<student_number-1;j++)
        {
            student[j]=student[j+1];
        }
        student_number--;//没有删除,只是减少输出,之后再覆盖掉他
        printf("删除成功,若继续查找按1,退出按0   :");
    }
    else
    {
        printf("查无此人,若继续查找按1,退出按0   :");
    }
    scanf("%d",&choice);
    if(choice)
    {
        goto start;
    }
}

修改学生信息
void Modify()
{
    int i=0,j;
    int choice;
    char testnum[12];
start :
    printf("请输入学号  :  ");
    scanf("%s",testnum);
    while(strcmp(testnum,student[i].num)!=0&&i<student_number)
    {
        i++;
    }
    if(i<student_number)
    {
        printf("-----------------------------------------修改前的数据------------------------------------------------------------------\n");
        printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
        printf("-----------------------------------------------------------------------------------------------------------------------\n");
        printf("%s\t\t%s     \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
        printf("开始修改\n");
        printf("学生学号\t:    ");
        scanf("%s",student[i].num);
        printf("学生姓名\t:    ");
        scanf("%s",student[i].name);
        printf("学生性别\t:    ");
        scanf("%s",student[i].sex);            
        printf("c语言成绩\t:    ");
        scanf("%d",&student[i].score[0]);
        printf("数据结构成绩\t:    ");
        scanf("%d",&student[i].score[1]);
        printf("数据库成绩\t:   ");
        scanf("%d",&student[i].score[2]);
        student[i].avg=(float)(student[i].score[0]+student[i].score[1]+student[i].score[2])/3;
        student[i].sum=student[i].score[0]+student[i].score[1]+student[i].score[2];
        printf("-----------------------------------------修改后的数据------------------------------------------------------------------\n");
        printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
        printf("-----------------------------------------------------------------------------------------------------------------------\n");
        printf("%s\t\t%s     \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
        printf("修改成功,若继续查找按1,退出按0   :");
    }
    else
    {
        printf("查无此人,若继续查找按1,退出按0   :");
    }
    scanf("%d",&choice);
    if(choice)
    {
        goto start;
    }
}

查看各门课程平均分
void Show_avg()
{
    int temp0=0,temp1=0,temp2=0;
    float a,b,c;
    int i;
    for(i=0;i<student_number;i++)
    {
        temp0=temp0+student[i].score[0];
        temp1=temp1+student[i].score[1];
        temp2=temp2+student[i].score[2];
    }
    a=(float)(temp0/student_number);
    b=(float)(temp1/student_number);
    c=(float)(temp2/student_number);
    printf("c语言成绩平均分是%-.2f\n数据结构成绩平均分是%-.2f\n数据库成绩平均分是%-.2f\n",a,b,c);
    
}


///查看不及格的学生信息
void Show_Max_And_Min()
{
    int i,j=0,max,min;
    int keep_score[MAXSIZE];
    for(i=0;i<student_number;i++)
    {
        if(student[i].score[0]<60||student[i].score[1]<60||student[i].score[2]<60)
        {
            keep_score[j]=i;
            j++;
        }
    }
    j--;//因为退出循环的时候j又加了1,但这时keep_score[j]里面是空的,会引起异常
    printf("------------------------------------------------------------------------------------\n");
    printf("学生学号\t学生姓名\t学生性别\tc语言成绩\t数据结构成绩\t数据库成绩\t平均成绩\t总成绩\n");
    printf("------------------------------------------------------------------------------------\n");
    for(i=0;i<=j;i++)
    {
        printf("%s\t\t%s     \t%s\t\t%d\t\t%d\t\t%d\t\t%f\t%d\n",student[keep_score[i]].num,student[keep_score[i]].name,student[keep_score[i]].sex,student[keep_score[i]].score[0],student[keep_score[i]].score[1],student[keep_score[i]].score[2],student[keep_score[i]].avg,student[keep_score[i]].sum);
    }
    
}

void AddFromText()
{
    FILE *fp;
    int i=0;
    if((fp=fopen("D:\\student.txt","r"))==NULL)fp=fopen("D:\\student.txt","w")要加括号才能与NULL比较
    {
        printf("打开文件失败,无读取数据");
        Sleep(1000);
    }
    else
    {
        fscanf(fp,"%d",&student_number); 
        while(i<student_number)
        {
            fscanf(fp,"%s%s%s%d%d%d%f%d",student[i].num,student[i].name,student[i].sex,&student[i].score[0],&student[i].score[1],&student[i].score[2],&student[i].avg,&student[i].sum);
            i++;
        }
    }
    fclose(fp);
}
将数据保存
void Write()
{
    /*int i;*/
    int i=0;///错误,没有初始化
    FILE *fp;
    if((fp=fopen("D:\\student.txt","w"))==NULL)fp=fopen("D:\\student.txt","w")要加括号才能与NULL比较
    {
        printf("保存失败");
        system("pause");
    }
    else
    {
        fprintf(fp,"%d",student_number);注意格式,中间是"%d",不是%d
        while(i<student_number)
        {
            fprintf(fp,"\t%s\t%s\t%s\t%d\t%d\t%d\t%f\t%d\n",student[i].num,student[i].name,student[i].sex,student[i].score[0],student[i].score[1],student[i].score[2],student[i].avg,student[i].sum);
            上一行fprintf  student[i].score等不要&,不然就是输入地址fscanf输入单个变量才需要这样用
            i++;
        }
        fclose(fp);
        printf("保存成功,正在退出系统");
        Sleep(1000);
        exit(0);
    }
}

/清空数据
void  FreeAll()
{
    FILE *fp;
    int choice;
    printf("请确认是否清除,保存的信息删除无法恢复,确认清除输入1,否则输入0返回系统   :   ");
    scanf("%d",&choice);
    if(choice)
    {
        if((fp=fopen("D:\\student.txt","w"))==NULL)fp=fopen("D:\\student.txt","w")要加括号才能与NULL比较
        {
            printf("清除失败");
            system("pause");
        }
        else
        {
            printf("清除成功\n");
        
        fclose(fp);}
    }
}

int main()
{
    AddFromText();
    for(;;)
    {
        switch(Menu())
        {
            case 1:Input();system("pause");continue;
            case 2:Show();system("pause");continue;
            case 3:SortByNum();system("pause");continue;
            case 4:SortBySum();system("pause");continue;
            case 5:Search();system("pause");continue;
            case 6:Delete();system("pause");continue;
            case 7:Modify();system("pause");continue;
            case 8:Show_avg();system("pause");continue;
            case 9:Show_Max_And_Min();system("pause");continue;
            case 10:FreeAll();system("pause");continue;
            case 0:Write();
        }
    }
}








版权声明:本文为weixin_47071411原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。