ChatGPT implements a student performance management system written in C language

As ChatGPT becomes popular, everyone is using ChatGPT to help them improve their efficiency. What about programmers using it to write code? Today I tried ChatGPT and wrote a student performance management system.

The problem is: use C language to write a student performance management system, which requires the use of linked lists to implement addition, deletion, modification and check functions.

The following is the code written by ChatGPT. It takes less than 30 seconds, and the speed is still amazing:

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

//定义链表结构体。
struct student {
    char name[20]; //学生姓名。
    int id; //学生编号。
    float score; //学生成绩。
    struct student *next; //指向下一个结构体的指针。
} *head;

//声明函数。
void add(); //添加学生信息。
void display(); //显示所有学生信息。
void search(); //根据学生姓名或编号查找学生信息。
void modify(); //修改学生信息。
void quit(); //程序退出。

//主函数。
int main() {
    int ch;
    printf("\n\t学生成绩管理系统\n");
    printf("\t==================\n");
    while (1) {
        printf("1. 添加学生信息\n");
        printf("2. 显示所有学生信息\n");
        printf("3. 查找学生信息\n");
        printf("4. 修改学生信息\n");
        printf("5. 退出程序\n");
        printf("请输入选项: ");
        scanf("%d", &ch);
        switch (ch) {
            case 1:
                add();
                break;
            case 2:
                display();
                break;
            case 3:
                search();
                break;
            case 4:
                modify();
                break;
            case 5:
                quit();
                break;
            default:
                printf("无效的选项,请重新输入。\n");
        }
    }
    return 0;
}

//添加学生信息。
void add() {
    struct student *newstudent, *laststudent;
    newstudent = (struct student *) malloc(sizeof(struct student)); //申请新的结构体。
    printf("\n请输入学生姓名: ");
    scanf("%s", newstudent->name);
    printf("请输入学生编号: ");
    scanf("%d", &newstudent->id);
    printf("请输入学生成绩: ");
    scanf("%f", &newstudent->score);
    newstudent->next = NULL;
    if (head == NULL) {
        head = newstudent;
    } else {
        laststudent = head;
        while (laststudent->next != NULL) {
            laststudent = laststudent->next;
        }
        laststudent->next = newstudent;
    }
    printf("\n学生信息已成功添加。\n");
}

//显示所有学生信息。
void display() {
    struct student *displaystudent = head; //定义指向链表结构体的指针。
    if (displaystudent == NULL) {
        printf("\n暂无学生信息。\n");
    } else {
        printf("\n所有学生信息如下:\n");
        while (displaystudent != NULL) {
            printf("====================\n");
            printf("姓名: %s", displaystudent->name);
            printf("编号: %d", displaystudent->id);
            printf("成绩: %.2f", displaystudent->score);
            displaystudent = displaystudent->next;
        }
        printf("====================\n");
    }
}

//根据学生姓名或编号查找学生信息。
void search() {
    struct student *searchstudent = head;
    char searchname[20];
    int searchid;
    printf("\n请选择查找方式:\n");
    printf("1. 根据姓名查找");
    printf("2. 根据编号查找");
    printf("请输入选项: ");
    int ch;
    scanf("%d", &ch);
    switch (ch) {
        case 1:
            printf("\n请输入学生姓名: ");
            scanf("%s", searchname);
            while (searchstudent != NULL) {
                if (strcmp(searchname, searchstudent->name) ==0) {
                    printf("====================\n");
                    printf("姓名: %s", searchstudent->name);
                    printf("编号: %d", searchstudent->id);
                    printf("成绩: %.2f", searchstudent->score);
                    printf("====================\n");
                    return;
                }
                searchstudent = searchstudent->next;
            }
            printf("\n未找到该学生信息。\n");
            break;
        case 2:
            printf("\n请输入学生编号: ");
            scanf("%d", &searchid);
            while (searchstudent != NULL) {
                if (searchid == searchstudent->id) {
                    printf("====================\n");
                    printf("姓名: %s", searchstudent->name);
                    printf("编号: %d", searchstudent->id);
                    printf("成绩: %.2f", searchstudent->score);
                    printf("====================\n");
                    return;
                }
                searchstudent = searchstudent->next;
            }
            printf("\n未找到该学生信息。\n");
            break;
        default:
            printf("\n无效的选项,请重新输入。\n");
    }
}

//修改学生信息。
void modify() {
    struct student *modifystudent = head;
    char modifyname[20];
    int modifyid;
    printf("\n请选择修改方式:\n");
    printf("1. 根据姓名查找");
    printf("2. 根据编号查找");
    printf("请输入选项: ");
    int ch;
    scanf("%d", &ch);
    switch (ch) {
        case 1:
            printf("\n请输入学生姓名: ");
            scanf("%s", modifyname);
            while (modifystudent != NULL) {
                if (strcmp(modifyname, modifystudent->name) ==0) {
                    printf("\n请输入新的学生成绩: ");
                    scanf("%f", &modifystudent->score);
                    printf("\n学生信息已成功修改。\n");
                    return;
                }
                modifystudent = modifystudent->next;
            }
            printf("\n未找到该学生信息。\n");
            break;
        case 2:
            printf("\n请输入学生编号: ");
            scanf("%d", &modifyid);
            while (modifystudent != NULL) {
                if (modifyid == modifystudent->id) {
                    printf("\n请输入新的学生成绩: ");
                    scanf("%f", &modifystudent->score);
                    printf("\n学生信息已成功修改。\n");
                    return;
                }
                modifystudent = modifystudent->next;
            }
            printf("\n未找到该学生信息。\n");
            break;
        default:
            printf("\n无效的选项,请重新输入。\n");
    }
}

//程序退出。
void quit() {
    struct student *currentstudent = head, *nextstudent;
    while (currentstudent != NULL) {
        nextstudent = currentstudent->next;
        free(currentstudent);
        currentstudent = nextstudent;
    }
    exit(0);
}

So what about the code you wrote? Let’s take a look at whether it can be run directly.

Directly copy and paste this code into clion, click Run, the result is as follows:


D:\wangc\CLionProjects\demo_10\cmake-build-debug\demo_10.exe

        Student performance management system
    ==================

  1. Add student information

  2. Show all student information

  3. Find student information

  4. Modify student information

  5. To exit the program please enter option: 1

Please enter student name: zhangsan Please enter student number: 1001 Please enter student score: 88

Student information has been added successfully.

  1. Add student information

  2. Show all student information

  3. Find student information

  4. Modify student information

  5. To exit the program please enter option: 1

Please enter student name: lisi Please enter student number: 1002 Please enter student score: 98

Student information has been added successfully.

  1. Add student information

  2. Show all student information

  3. Find student information

  4. Modify student information

  5. To exit the program please enter option: 1

Please enter student name: wangwu Please enter student number: 1003 Please enter student score: 87

Student information has been added successfully.

  1. Add student information

  2. Show all student information

  3. Find student information

  4. Modify student information

  5. To exit the program please enter option: 2

All student information is as follows:

Name: zhangsan Number: 1001 Score: 88.00==================== Name: lisi Number: 1002 Score: 98.00============ ======== Name: wangwu Number: 1003 Score: 87.00====================

  1. Add student information

  2. Show all student information

  3. Find student information

  4. Modify student information

  5. To exit the program please enter option: 3

Please select how to search:

  1. Search by name 2. Search by number Please enter option: 2

Please enter student number: 1002

Name: lisi Number: 1002 Score: 98.00====================

  1. Add student information

  2. Show all student information

  3. Find student information

  4. Modify student information

  5. To exit the program please enter option: 4

Please choose how to modify:

  1. Search by name 2. Search by number Please enter option: 1

Please enter student name: zhangsan

Please enter new student grade: 97

Student information has been modified successfully.

  1. Add student information

  2. Show all student information

  3. Find student information

  4. Modify student information

  5. To exit the program please enter option: 2

All student information is as follows:

Name: zhangsan Number: 1001 Score: 97.00==================== Name: lisi Number: 1002 Score: 98.00============== ======== Name: wangwu Number: 1003 Score: 87.00====================

  1. Add student information

  2. Show all student information

  3. Find student information

  4. Modify student information

  5. Please enter options to exit the program:


After testing, it was found that there is no problem in writing the basic code for ChatGPT, it can be run directly, and the running results are in line with expectations.

Guess you like

Origin blog.csdn.net/m0_59795797/article/details/130114137