jiwwoo 2023-11-04 11:53 采纳率: 73.7%
浏览 7
已结题

C语言成绩管理系统,这个程序还要啥错吗?为什么输入一组数据之后查询的时候显示“没有此人”



#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#pragma warning(disable:4996)
typedef struct s1
{
    char  id[50];
    char  name[50];
    int   cnt;
    float pscj[10];
    float examine;
    float examine2;
    float sum;
    struct s1 *next;
    int      inputflag;
}Msg,*linklist;
void menu()
{
    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| 请输入选择(1-6):            |\n");
    printf("\t|--------------------------------|\n\t");
}
int add_stu(const char *filename, const char *mode)
{
    FILE *fp;
    Msg stu;
    int i;
    char ch;
    fp = fopen(filename, mode);
    if (fp == NULL)
    {
        printf("fail to open file!\n");
        return 0;
    }
    printf("Successfully opened file!\n");
    while (1)
    {
        printf("输入学号:\n");
        scanf("%s", &stu.id);
        printf("输入姓名:\n");
        scanf("%s", stu.name);
        stu.cnt = 0;
        for (i = 0; i < 3; i++)
            stu.pscj[i] = 0;
        stu.examine = 0;
        stu.sum = 0;
        stu.inputflag = 0;
        fwrite(&stu, sizeof(Msg), 1, fp);
        getchar();
        printf("输入y或Y继续:\n");
        ch = getchar();
        if ((ch == 'Y') || (ch == 'y'))
            continue;
        else
            break;
    }
    fclose(fp);
    return 1;
}
int input_stu(const char *filename, const char *mode)
{
    FILE *fp;
    Msg stu;
    int i, len;
    float ave;
    fp = fopen(filename, mode);
    if (fp == NULL)
    {
        printf("fail to open file!\n");
        return 0;
    }
    printf("Successfully opened file!\n");
    while (fread(&stu, sizeof(Msg), 1, fp))
    {
        //fread(&stu, sizeof(Msg), 1, fp);
        if (stu.inputflag == 1)
            continue;
        len = sizeof(Msg);
        fseek(fp, -len, 1);//由于读过后,指针已经指向下一个学生,所以需要返回
        printf("输入%s->%s的缺勤次数:", stu.id, stu.name);
        scanf("%d", &stu.cnt);
        printf("输入%s->%s的平时成绩:\n", stu.id, stu.name);
        ave = 0;
        for (i = 0; i < 9; i++)
        {
            printf("输入第%d次课后作业成绩:", i + 1);
            scanf("%f", &stu.pscj[i]);
            ave += stu.pscj[i];
        }
        stu.pscj[9] = ave / 9;
        printf("输入%s->%s的大作业成绩:", stu.id, stu.name);
        scanf("%f", &stu.examine2);
        printf("输入%s->%s的考试成绩:", stu.id, stu.name);
        scanf("%f", &stu.examine);
        stu.sum = stu.pscj[9] * 0.15 + stu.examine*0.7+ stu.examine2*0.15;
        stu.inputflag = 1;
        fwrite(&stu, sizeof(Msg), 1, fp);
        fseek(fp, -len, 1);//由于读过后,指针已经指向下一个学生,所以需要返回
    }
    fclose(fp);
}
int find_stu(const char *filename, const char *mode,linklist &H)
{
    FILE *fp;
    linklist p;
    char ch;
    Msg stu[50];
    p = H;
    char no[50];
    char id[50];
    fp = fopen(filename, mode);
    if (fp == NULL)
    {
        printf("fail to open file!\n");
        return 0;
    }
    printf("Successfully opened file!\n");
        printf("要查询的学生学号:");
        scanf("%s", id);
        while (p != NULL)
        {
            if(strcmp(p->id,no)==0)
            {
                break;
            }
            else
            {
                p = p->next;
            }
        }
        if (p != NULL)
        {
            printf("|-------------------------------------TUTE--------------------------------------|\n");
            printf("| 学号  |  姓名  |  考勤  | 课后作业成绩 | 大作业成绩 | 期末考核成绩 | 总评成绩 |\n");
            printf("| %s  |  %s  |  %d  | %f | %f | %f | %f |\n", p->id, p->name, p->cnt, p->pscj, p->examine2, p->examine, p->sum);
            printf("|-------------------------------------TUTE--------------------------------------|\n");
        }
        else
        {
            printf("没有此人");

        }
            ch = getchar();
            fread(&stu, sizeof(Msg), 1, fp);
    fclose(fp);
}
int modify_stu(const char *filename, const char *mode)
{
    FILE *fp;
    fp = fopen(filename, mode);
    if (fp == NULL)
    {
        printf("fail to open file!\n");
        return 0;
    }
    printf("Successfully opened file!\n");
    fclose(fp);
}
int print_stu(const char *filename, const char *mode)
{
    FILE *fp;
    Msg stu;
    int i;
    fp = fopen(filename, mode);
    if (fp == NULL)
    {
        printf("fail to open file!\n");
        return 0;
    }
    printf("Successfully opened file!\n");
    while (fread(&stu, sizeof(Msg), 1, fp))
    {
        printf("学号:");
        printf("%s\n", stu.id);
        printf("姓名:");
        printf("%s\n", stu.name);
        printf("考勤:");
        printf("%d\n", stu.cnt);
        printf("平时成绩:");
        for (i = 0; i < 8; i++)
            printf("%6.1f", stu.pscj[i]);
        printf("\n");
        printf("考试:");
        printf(" %.1f\n", stu.examine);
        printf("总评:");
        printf(" %.1f\n", stu.sum);
    }
    fclose(fp);
}
int main()
{
    linklist H=NULL;
    int sel;
    const char *filename = "classinf.txt";
    const char *mode[3] = { "a","r","r+" };
    while (1)
    {
        menu();
        scanf("%d", &sel);
        if (sel == 1)
            add_stu(filename, mode[0]);
        else if (sel == 2)
            input_stu(filename, mode[2]);
        else if (sel == 3)
            find_stu(filename, mode[1],H);
        else if (sel == 4)
            modify_stu(filename, mode[2]);
        else if (sel == 5)
            print_stu(filename, mode[1]);
        else if (sel == 6)
            break;
        else
            break;
        system("pause");
        system("cls");
    }

    return 1;
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/067099960996162.jpg "#left")

img

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-11-04 14:31
    关注

    【以下回答由 GPT 生成】

    问题的原因是在查询学生信息时,没有正确比较输入的学号和链表中存储的学号。修复问题的方法是在查询学生信息的函数中,添加比较学号的代码,并打印对应学生的信息。

    void search_stu(linklist head)
    {
        char id[50];
        printf("请输入要查询的学号:");
        scanf("%s", id);
        linklist p = head->next;
        while (p != NULL)
        {
            if (strcmp(p->id, id) == 0)
            {
                printf("学号:%s\n", p->id);
                printf("姓名:%s\n", p->name);
                printf("考试成绩:%.2f\n", p->examine);
                printf("考试成绩2:%.2f\n", p->examine2);
                printf("总分:%.2f\n", p->sum);
                return;
            }
            p = p->next;
        }
        printf("没有此人\n");
    }
    

    将以上代码添加到原有的代码中的合适位置,然后重新编译运行,问题应该就会被修复。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月19日
  • 已采纳回答 11月11日
  • 创建了问题 11月4日