学生成绩管理--C语言

发布时间 2023-12-02 15:02:31作者: L*K

# 学生成绩管理系统

效果

image-20231202144849125

1.菜单选项

void welcome()//菜单
{
    printf("欢迎使用学生管理系统\n");
    printf("  1.增加学生信息\n");
    printf("  2.展示学生信息\n");
    printf("  3.删除学生信息\n");
    printf("  4.修改学生信息\n");
    printf("  5.查看学生信息\n");
    printf("  6.保存学生信息\n");
    printf("  7.读取学生信息\n");
    printf("  0.退出\n");
}

2.增加学生信息

void addstudnet()//增加学生信息
{
    node* pnewnode=(node*)malloc(sizeof(node));
    pnewnode->pnext=NULL;
    if(head==NULL)
    {
        head=pnewnode;
    }
    else
    {
        pnewnode->pnext=head;
        head=pnewnode;
    }
    printf("请输入学生的学号\n");
    scanf("%d",&pnewnode->stu.no);
    printf("请输入学生的姓名\n");
    scanf("%s",pnewnode->stu.name);
    printf("请输入学生的年龄\n");
    scanf("%d",&pnewnode->stu.age);
    printf("请输入学生的分数\n");
    scanf("%s",pnewnode->stu.score);
    printf("学生信息录入成功\n");
	system("pause");
	system("cls");
}

3.展示学生信息

void showstudent()//展示学生信息
{
    node* p=head;
    printf("学号\t姓名\t年龄\t分数\n");
    while(p!=NULL)
    {
        printf("%d\t%s\t%d\t%s\n",p->stu.no,p->stu.name,p->stu.age,p->stu.score);
        p=p->pnext;
    }
    system("pause");
    system("cls");
}

4.删除学生信息

void deletestudent()//删除学生信息
{
    int dno;
    printf("请输入需要删除学生的学号:");
    scanf("%d",&dno);
    if(head->stu.no==dno)
    {
         node* p1=head;
         head=head->pnext;
         free(p1);
        printf("删除成功\n");
        system("pause");
        system("cls");
         return;
    }
    node* p=head;
    node* p2;
    while(p->pnext!=NULL)
    {
        if(p->pnext->stu.no==dno)
        {
            p2=p->pnext;
            p->pnext=p->pnext->pnext;
            free(p2);
            printf("删除成功\n");
            system("pause");
            system("cls");
            return;
        }
        p=p->pnext;
        if(p->pnext==NULL)
        {
            break;
        }
    }
    if(p->pnext==NULL)
    {
        printf("查无此人\n");
        system("pause");
        system("cls");
    }
}

5.修改学生信息

void editstudent()//修改学生信息
{
    int eno;
    printf("请输入需要修改学生的学号:");
    scanf("%d",&eno);
    node* p=head;
    while(p->pnext!=NULL)
    {
        if(p->stu.no==eno)
        {
            printf("请输入学生的学号\n");
            scanf("%d",&p->stu.no);
            printf("请输入学生的姓名\n");
            scanf("%s",p->stu.name);
            printf("请输入学生的年龄\n");
            scanf("%d",&p->stu.age);
            printf("请输入学生的分数\n");
            scanf("%s",p->stu.score);
            printf("学生信息修改成功\n");
            system("pause");
            system("cls");
            return;
        }
        p=p->pnext;
        if(p==NULL)
        {
            break;
        }
    }
    printf("查无此人!\n");
    system("pause");
    system("cls");
}

6.查找学生信息

node* findstudent()//查看学生信息
{
    int fno;
    printf("请输入需要查找学生的学号:");
    scanf("%d",&fno);
    node* p=head;
    while (p!=NULL)
    {
        if(p->stu.no==fno)
        {
            printf("学号\t姓名\t年龄\t分数\n");
            printf("%d\t%s\t%d\t%s\n",p->stu.no,p->stu.name,p->stu.age,p->stu.score);
            system("pause");
            system("cls");
            return p;
        }
        p=p->pnext;
    }
    printf("查无此人\n");
    return NULL;
    system("pause");
    system("cls");
}

7.保存学生信息

void savestudent()
{
	FILE* fp = fopen("D:\\demo02.txt", "w");
    if(fp==NULL)
    {
        printf("打开文件失败\n");
        system("pause");
        system("cls");
        return;
    }
    node* p=head;
    while(p!=NULL)
    {
        fwrite(&p->stu,sizeof(student),1,fp);
        p=p->pnext;
    }
    fclose(fp);
    printf("保存成功\n");
    system("pause");
    system("cls");
}

8.读取学生信息

void readstudent()
{
    FILE* fp=fopen("D:\\demo02.txt","r");
    if(fp==NULL)
    {
        printf("打开文件失败\n");
        system("pause");
        system("cls");
        return;
    }
    student stu1;

    while(fread(&stu1,sizeof(student),1,fp))
    {
        node* p = (node*)malloc(sizeof(node));
        p->pnext=NULL;		
        memcpy(p, &stu1,sizeof(student));
		if (head == NULL)
		{
			head = p;
		}
		else
		{
			p->pnext = head;
			head = p;
		}
    }
    fclose(fp);
    printf("读取成功\n");
    system("pause");
    system("cls");
}

源码

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

typedef struct student
{
    int no;
    char name[10];
    int age;
    char score[10];
}student;
typedef struct node 
{
    struct student stu;
    struct node * pnext;
}node;

struct node* head=NULL;

void welcome();//菜单
void addstudnet();//增加学生信息
void showstudent();//展示学生信息
void deletestudent();//删除学生信息
void editstudent();//修改学生信息
node* findstudent();//查找学生信息
void savestudent();//保存学生信息
void readstudent();//读取学生信息

int main()
{
    int choice=0;
    while (1)
    {
        welcome();
        printf("请输入选择:");
        scanf("%d",&choice);
        switch (choice)
        {
        case 0:
            return 0;
            break;
        case 1:
            addstudnet();
            break;
        case  2:
            showstudent();
            break;
        case 3:
            deletestudent();
            break;
        case 4:
            editstudent();
            break;
         case 5:
            findstudent();
            break;
         case 6:
            savestudent();
            break;
         case 7:
            readstudent();
            break;
        default:
            printf("输入错误!\n");
            system("pause");
            system("cls");
            break;
        }
    }
}

void welcome()//菜单
{
    printf("欢迎使用学生管理系统\n");
    printf("  1.增加学生信息\n");
    printf("  2.展示学生信息\n");
    printf("  3.删除学生信息\n");
    printf("  4.修改学生信息\n");
    printf("  5.查看学生信息\n");
    printf("  6.保存学生信息\n");
    printf("  7.读取学生信息\n");
    printf("  0.退出\n");
}

void addstudnet()//增加学生信息
{
    node* pnewnode=(node*)malloc(sizeof(node));
    pnewnode->pnext=NULL;
    if(head==NULL)
    {
        head=pnewnode;
    }
    else
    {
        pnewnode->pnext=head;
        head=pnewnode;
    }
    printf("请输入学生的学号\n");
    scanf("%d",&pnewnode->stu.no);
    printf("请输入学生的姓名\n");
    scanf("%s",pnewnode->stu.name);
    printf("请输入学生的年龄\n");
    scanf("%d",&pnewnode->stu.age);
    printf("请输入学生的分数\n");
    scanf("%s",pnewnode->stu.score);
    printf("学生信息录入成功\n");
	system("pause");
	system("cls");
}

void showstudent()//展示学生信息
{
    node* p=head;
    printf("学号\t姓名\t年龄\t分数\n");
    while(p!=NULL)
    {
        printf("%d\t%s\t%d\t%s\n",p->stu.no,p->stu.name,p->stu.age,p->stu.score);
        p=p->pnext;
    }
    system("pause");
    system("cls");
}

void deletestudent()//删除学生信息
{
    int dno;
    printf("请输入需要删除学生的学号:");
    scanf("%d",&dno);
    if(head->stu.no==dno)
    {
         node* p1=head;
         head=head->pnext;
         free(p1);
        printf("删除成功\n");
        system("pause");
        system("cls");
         return;
    }
    node* p=head;
    node* p2;
    while(p->pnext!=NULL)
    {
        if(p->pnext->stu.no==dno)
        {
            p2=p->pnext;
            p->pnext=p->pnext->pnext;
            free(p2);
            printf("删除成功\n");
            system("pause");
            system("cls");
            return;
        }
        p=p->pnext;
        if(p->pnext==NULL)
        {
            break;
        }
    }
    if(p->pnext==NULL)
    {
        printf("查无此人\n");
        system("pause");
        system("cls");
    }
}

void editstudent()//修改学生信息
{
    int eno;
    printf("请输入需要修改学生的学号:");
    scanf("%d",&eno);
    node* p=head;
    while(p->pnext!=NULL)
    {
        if(p->stu.no==eno)
        {
            printf("请输入学生的学号\n");
            scanf("%d",&p->stu.no);
            printf("请输入学生的姓名\n");
            scanf("%s",p->stu.name);
            printf("请输入学生的年龄\n");
            scanf("%d",&p->stu.age);
            printf("请输入学生的分数\n");
            scanf("%s",p->stu.score);
            printf("学生信息修改成功\n");
            system("pause");
            system("cls");
            return;
        }
        p=p->pnext;
        if(p==NULL)
        {
            break;
        }
    }
    printf("查无此人!\n");
    system("pause");
    system("cls");
}

node* findstudent()//查看学生信息
{
    int fno;
    printf("请输入需要查找学生的学号:");
    scanf("%d",&fno);
    node* p=head;
    while (p!=NULL)
    {
        if(p->stu.no==fno)
        {
            printf("学号\t姓名\t年龄\t分数\n");
            printf("%d\t%s\t%d\t%s\n",p->stu.no,p->stu.name,p->stu.age,p->stu.score);
            system("pause");
            system("cls");
            return p;
        }
        p=p->pnext;
    }
    printf("查无此人\n");
    return NULL;
    system("pause");
    system("cls");
}

void savestudent()
{
	FILE* fp = fopen("D:\\demo02.txt", "w");
    if(fp==NULL)
    {
        printf("打开文件失败\n");
        system("pause");
        system("cls");
        return;
    }
    node* p=head;
    while(p!=NULL)
    {
        fwrite(&p->stu,sizeof(student),1,fp);
        p=p->pnext;
    }
    fclose(fp);
    printf("保存成功\n");
    system("pause");
    system("cls");
}

void readstudent()
{
    FILE* fp=fopen("D:\\demo02.txt","r");
    if(fp==NULL)
    {
        printf("打开文件失败\n");
        system("pause");
        system("cls");
        return;
    }
    student stu1;

    while(fread(&stu1,sizeof(student),1,fp))
    {
        node* p = (node*)malloc(sizeof(node));
        p->pnext=NULL;		
        memcpy(p, &stu1,sizeof(student));
		if (head == NULL)
		{
			head = p;
		}
		else
		{
			p->pnext = head;
			head = p;
		}
    }
    fclose(fp);
    printf("读取成功\n");
    system("pause");
    system("cls");
}

推荐视频

【C/C++课程设计】史上最全最详细的学生成绩管理系统上线啦,完成大学课程设计不是问题!_哔哩哔哩_bilibili