关于链表函数中存在的bug

函数代码如下,在代码运行后,每次输入到第二个学生后就会自动跳出,只会记录一个学生,运行的效果如下他截图。

 score *creatlink()
{
    score *head;
    score *p1, *p2, *p3, *max;
    int i, j;
    float fen;
    char t[10];
    n = 0;
    p1 = p2 = p3 = (score *)malloc(LEN); 
    head = p3; //开辟新单元
    printf("请输入学生资料,输入0退出!\n");
repeat1: printf("请输入学生学号(学号应该大于0):");
    scanf("%d", &p1->number);
    //判断学生输入的学号是否小于0
    while (p1->number < 0)
    {
        getchar();
        printf("输入错误,请重新输入学生学号:");
        scanf("%d", &p1->number);
    }
    //学号为0时直接转到结尾,链表结束
    if (p1->number == 0)
        goto end;
    else
    {
        p3 = head;
        if (n > 0)
        {
            for (i = 0; i < n; i++)
            {
                if (p1->number != p3->number)
                    p3 = p3->next;
                else
                {
                    //学号如果已经存在,程序报错,返回前面,重新输入
                    printf("学号重复,请重新输入!\n");
                    goto repeat1;
                }
            }
        }
    }
    //输入学生姓名
    printf("请输入学生姓名:");
    scanf("%s", p1->name);
    //输入学生语文成绩及作出判断
    printf("请输入学生的语文成绩(0~100):");
    scanf("%f", &p1->chinese);
    while (p1->chinese <0 || p1->chinese >100)
    {
        getchar();
        printf("输入错误,请重新输入语文成绩:");
        scanf("%f", &p1->chinese);
    }
    //输入学生数学成绩及作出判断
    printf("请输入学生的数学成绩(0~100)");
    scanf("%f", &p1->mathmatic);
    while (p1->mathmatic < 0 || p1->mathmatic > 100)
    {
        getchar();
        printf("输入错误,请重新输入数学成绩:");
        scanf("%f", &p1->mathmatic);
    }
    //输入英语成绩并作出判断
    printf("请输入学生的英语成绩(0~100):");
    scanf("%f", &p1->english);
    while (p1->english < 0 || p1->english > 100)
    {
        getchar();
        printf("输入错误,请重新输入英语的成绩:");
        scanf("%f", p1->english);
    }
    head = NULL;
    while (p1->number != 0)
    {
        n += 1;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (score *)malloc(LEN);
        printf("请输入学生资料,输入0退出!\n");
    repeat2:printf("请输入学生学号(学号应该大于0):");
        scanf("%d", &p1->number);
        //判断学生输入的学号是否小于0
        while (p1->number < 0)
        {
            getchar();
            printf("输入错误,请重新输入学生学号:");
            scanf("%d", &p1->number);
        }
        //学号为0时直接转到结尾,链表结束
        if (p1->number == 0)
            goto end;
        else
        {
            p3 = head;
            if (n > 0)
            {
                for (i = 0; i < n; i++)
                {
                    if (p1->number != p3->number)
                        p3 = p3->next;
                    else
                    {
                        //学号如果已经存在,程序报错,返回前面,重新输入
                        printf("学号重复,请重新输入!\n");
                        goto repeat2;
                    }
                }
            }
        }
        //输入学生姓名
        printf("请输入学生姓名:");
        scanf("%s", p1->name);
        //输入学生语文成绩及作出判断
        printf("请输入学生的语文成绩(0~100):");
        scanf("%f", &p1->chinese);
        while (p1->chinese <0 || p1->chinese >100)
        {
            getchar();
            printf("输入错误,请重新输入语文成绩:");
            scanf("%f", &p1->chinese);
        }
        //输入学生数学成绩及作出判断
        printf("请输入学生的数学成绩(0~100)");
        scanf("%f", &p1->mathmatic);
        while (p1->mathmatic < 0 || p1->mathmatic > 100)
        {
            getchar();
            printf("输入错误,请重新输入数学成绩:");
            scanf("%f", &p1->mathmatic);
        }
        //输入英语成绩并作出判断
        printf("请输入学生的英语成绩(0~100):");
        scanf("%f", &p1->english);
        while (p1->english < 0 || p1->english > 100)
        {
            getchar();
            printf("输入错误,请重新输入英语的成绩:");
            scanf("%f", p1->english);
        }
    end: p1 = head;
        p3 = p1;
        for (i = 1; i < n; i++)
        {
            for (j = i + 1; j <= n; j++)
            {
                max = p1;
                p1 = p1->next;
                if (max->number > p1->number)
                {
                    k = max->number;
                    max->number = p1->number;
                    p1->number = k;
                    strcpy(t, max->name);
                    strcpy(max->name, p1->name);
                    strcpy(p1->name, t);
                    fen = max->chinese;
                    max->chinese = p1->chinese;
                    p1->chinese = fen;
                    fen = max->mathmatic;
                    max->mathmatic = p1->mathmatic;
                    p1->mathmatic = fen;
                    fen = max->english;
                    max->english = p1->english;
                    p1->english = fen;
                }
            }
            max = head;
            p1 = head;
        }
        p2->next = NULL;
        printf("输入学生的个数为:%d个!\n", n);
        return (head);
    }
}

图片说明

1个回答

// Q703209.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct score
{
    int number;
    char name[10];
    float chinese;
    float english;
    float mathmatic;
    score * next;
};

score *creatlink()
{
    int n = 0;
    char choice = '1';
    int LEN = sizeof(score);
    score *head, *p1 /*input*/ , *p2 /*current*/, *p3 /*found*/;
    int i, j;
    head = p2 = (score *)malloc(LEN);
    head->next = NULL;
    while (true)
    {
        printf("请输入学生资料,输入0退出!");
        fflush(stdin);
        scanf("%c", &choice);
        if (choice == '0') break;
        printf("请输入学生学号(学号应该大于0):");
        p1 = (score *)malloc(LEN);
        p1->next = NULL;
        scanf("%d", &p1->number);
        //判断学生输入的学号是否小于0
        while (p1->number < 0)
        {
            getchar();
            printf("输入错误,请重新输入学生学号:");
            scanf("%d", &p1->number);
        }
        //学号为0时直接转到结尾,链表结束
        if (p1->number == 0)
            break;
        else
        {
            p3 = head->next;
            int found = 0;
            while (p3 != NULL)
            {
                    if (p1->number == p3->number)
                    {
                        printf("学号重复,请重新输入!\n");
                        found = 1;
                        break;
                    }
                    p3 = p3->next;
            }
            if (found) continue;
        }
        //输入学生姓名
        printf("请输入学生姓名:");
        scanf("%s", p1->name);
        //输入学生语文成绩及作出判断
        printf("请输入学生的语文成绩(0~100):");
        scanf("%f", &p1->chinese);
        while (p1->chinese <0 || p1->chinese >100)
        {
            getchar();
            printf("输入错误,请重新输入语文成绩:");
            scanf("%f", &p1->chinese);
        }
        //输入学生数学成绩及作出判断
        printf("请输入学生的数学成绩(0~100):");
        scanf("%f", &p1->mathmatic);
        while (p1->mathmatic < 0 || p1->mathmatic > 100)
        {
            getchar();
            printf("输入错误,请重新输入数学成绩:");
            scanf("%f", &p1->mathmatic);
        }
        //输入英语成绩并作出判断
        printf("请输入学生的英语成绩(0~100):");
        scanf("%f", &p1->english);
        while (p1->english < 0 || p1->english > 100)
        {
            getchar();
            printf("输入错误,请重新输入英语的成绩:");
            scanf("%f", &p1->english);
        }

        p2->next = p1;
        p2 = p2->next;
        n += 1;
    }
    printf("输入学生的个数为:%d个!\n", n);
    score *maxc , *maxe , *maxm;
    maxc = maxe = maxm = p3 = head->next;
    while (p3 != NULL)
    {
        if (maxc->chinese < p3->chinese) maxc = p3;
        if (maxe->english < p3->english) maxe = p3;
        if (maxm->mathmatic < p3->mathmatic) maxm = p3;
        p3 = p3->next;
    }
    printf("语文最高分,姓名%s,成绩%f\n", maxc->name, maxc->chinese);
    printf("数学最高分,姓名%s,成绩%f\n", maxm->name, maxm->mathmatic);
    printf("英语最高分,姓名%s,成绩%f\n", maxe->name, maxc->english);
    return (head);
}
int main()
{
    creatlink();
}

图片说明

问题如果解决,请点下我回答右边的采纳,谢谢

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问