程序大作业

C语言 | 2020-06-08 20:44:51 | 阅读 476 次 | 评论(0)

挑了简单的做

学生信息包含:学号、姓名、3门考试成绩(英语、数学、计算机)。并将学生信息用文件保存,并可在电脑中直接查找并打开该文件。
请设计一个学生成绩管理系统,该系统包括如下功能:
1. 添加学生信息  
2. 按学号排序  
3. 修改学生信息  
4. 查找  (输入一个学生学号,输出其各科成绩); 
5. 删除  (按输入的学号删除该学生);  
0. 退出 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/*定义学生结构体*/
struct student
{
    char num[20];
    char name[10];
    float score1;
    float score2;
    float score3;
}students[1000];
int shu=0;

/*添加学生信息*/
void creat()
{
    while(1)
    {
        printf("\n");
        printf("请输入学号:");
        scanf("%s",&students[shu].num);
        printf("\n");

        printf("请输入姓名:");
        scanf("%s",&students[shu].name);
        printf("\n");

        printf("请输入英语成绩:");
        scanf("%f",&students[shu].score1);
        printf("\n");

        printf("请输入数学成绩:");
        scanf("%f",&students[shu].score2);
        printf("\n");

        printf("请输入计算机成绩:");
        scanf("%f",&students[shu].score3);
        getchar();
        shu++;

        printf("\n是否继续添加?y/n: ");
        if(getchar()=='n')
            break;
        printf("\n\n");
    }

}

/*按学号排序*/
void sort()
{
    int i,j,k;
    struct student t;
    for(i=0;i<shu;i++)
    {
        for(j=1;j<shu-i;j++)
        {
            if(strcmp(students[j-1].num,students[j].num)>0)
            {
                t=students[j-1];
                students[j-1]=students[j];
                students[j]=t;
            }
        }
    }
    printf("%10s%10s%8s%8s%8s\n","学号","姓名","英语","数学","计算机");
    printf("---------------------------------------------\n");
    for(i=0;i<shu;i++)
        printf("%10s%10s%8.2f%8.2f%8.2f\n",students[i].num,students[i].name,students[i].score1,students[i].score2,students[i].score3);
}

/*将学号返回数组下标,判断是否有该学生*/
int search(char sub[])
{
    int i;
    for(i=0;i<shu;i++)
    {
        if(strcmp(students[i].num,sub)==0)
            return i;
    }
    return -1;
}

/*修改学生信息*/
void mod()
{
    while(1)
    {
        int i;
        char num[10];
        printf("请输入要修改的学生的学号:");
        scanf("%s",&num);
        getchar();
        i=search(num);
        if(i==-1)
            printf("该学生不存在!");
        else
        {
            printf("\n该学生成绩:\n");
            printf("\n%10s%10s%8s%8s%8s\n","学号","姓名","英语","数学","计算机");
            printf("---------------------------------------------\n");
            printf("%10s%10s%8.2f%8.2f%8.2f\n",students[i].num,students[i].name,students[i].score1,students[i].score2,students[i].score3);
            printf("\n\n");
            printf("请输入学号:");
            scanf("%s",&students[shu].num);
            printf("\n");

            printf("请输入姓名:");
            scanf("%s",&students[shu].name);
            printf("\n");

            printf("请输入英语成绩:");
            scanf("%f",&students[shu].score1);
            printf("\n");

            printf("请输入数学成绩:");
            scanf("%f",&students[shu].score2);
            printf("\n");

            printf("请输入计算机成绩:");
            scanf("%f",&students[shu].score3);
            getchar();
        }
        printf("\n是否要继续修改其他同学信息? y/n: ");
        if(getchar()=='n')
            break;
    }
}

/*查找学生成绩*/
void find()
{
    while(1)
    {
        int i;
        char num[10];
        printf("请输入要查询的学生的学号:");
        scanf("%s",&num);
        getchar();
        i=search(num);
        if(i==-1)    //==
            printf("该学生不存在!\n");
        else
        {
            printf("\n%10s%10s%8s%8s%8s\n","学号","姓名","英语","数学","计算机");
            printf("-----------------------------------------------\n");
            printf("%10s%10s%8.2f%8.2f%8.2f\n",students[i].num,students[i].name,students[i].score1,students[i].score2,students[i].score3);
        }
        printf("\n是否要继续查询其他学生信息? y/n: ");
        if(getchar()=='n')
            break;
    }
}

/*删除学生成绩*/
void delete()
{
    while(1)
    {
        int i,k;
        char num[10];
        printf("请输入要删除的学生的学号:");
        scanf("%s",&num);
        getchar();
        i=search(num);
        if(i==-1)
            printf("\n该学生不存在!");
        else
        {
            printf("\n该学生信息:\n\n");
            printf("%10s%10s%8s%8s%8s\n","学号","姓名","英语","数学","计算机");
            printf("-----------------------------------------------\n");
            printf("%10s%10s%8.2f%8.2f%8.2f\n",students[i].num,students[i].name,students[i].score1,students[i].score2,students[i].score3);
            printf("\n确认要删除? y/n :");
            if(getchar()=='y')
            {
                for(k=i;k<shu-1;k++)
                {
                    students[k]=students[k+1];
                }
                shu--;
            }
            printf("\n%10s%10s%8s%8s%8s\n","学号","姓名","英语","数学","计算机");
            printf("-----------------------------------------------\n");
            for(i=0;i<shu;i++)
                printf("%10s%10s%8.2f%8.2f%8.2f\n",students[i].num,students[i].name,students[i].score1,students[i].score2,students[i].score3);
        }
        getchar();
        printf("\n\n是否要继续删除其他学生信息? y/n: ");
        if(getchar()=='n')
            break;
    }
}

/*将学生成绩读出*/
void read()
{
    FILE *fp;
    int i;
    if((fp=fopen("student.txt","rb"))==NULL)
    {
        printf("不能打开文件!\n");
        exit(0);
    }
    fread(&shu,sizeof(int),1,fp);
    for(i=0;i<shu;i++)
    {
        fread(&students[i],sizeof(struct student),1,fp);
    }
    fclose(fp);
}

/*将学生成绩写入文件*/
void writein()
{
    FILE *fp;
    int i;
    if((fp=fopen("student.txt","wb"))==NULL)
    {
        printf("不能打开文件!\n");
        exit(0);
    }
    if(fwrite(&shu,sizeof(int),1,fp)!=1)
        printf("写入文件错误!");
    for(i=0;i<shu;i++)
    {
        if(fwrite(&students[i],sizeof(struct student),1,fp)!=1)
        {
            printf("写入文件错误!");
        }
    }
    fclose(fp);
}

void main()   //显示菜单并选择菜单项
{
    int choice;
    read();
    while(1)
    {
        printf("\n\n");
        printf("                成绩管理\n");
        printf("       ===========================\n\n");
        printf("\t1.输入学生成绩\n\n\t2.按学号排序\n\n\t3.修改学生信息\n\n\t4.查找学生信息\n\n\t5.删除学生信息\n\n   ");
        printf("\n请选择(1-5, 0:退出):");
        scanf("%d",&choice);
        printf("\n");
        while(choice<0||choice>5)
        {
            printf("选择有误,重新选择(0-5)! :");
            scanf("%d",&choice);
        }
        switch(choice)
        {
        case 1:
            creat();
            break;
        case 2:
            sort();
            break;
        case 3:
            mod();
            break;
        case 4:
            find();
            break;
        case 5:
            delete();
            break;
        case 0:
            exit(0);
            break;
        }
        writein();
    }
}

文章评论,共0条
游客请输入验证码
文章归档
最新评论