鱼C论坛

 找回密码
 立即注册
查看: 563|回复: 3

[已解决]c 语言结构体

[复制链接]
发表于 2022-3-14 11:38:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请教各位大佬!!!

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

struct Student
{
    char name[10];
    char hao[10];
    int score;
    struct Student *next;
}student;

void getInput(struct Student *student);
void addStudent(struct Student **student,int n);
struct Student *maxScore(struct Student *student);
struct Student *minScore(struct Student *student);
void printStudent(struct Student *student);

void getInput(struct Student *student)
{
    struct Student *temp;
    temp=student;

    scanf("%s ",temp->name);
    scanf("%s ",temp->hao);
    scanf("%d",&temp->score);
}

void addStudent(struct Student **student,int n)
{
    struct Student *temp;
    struct Student *p;
    while(n--)
    {
        p=(struct Student *)malloc(sizeof(struct Student));
        if(p==NULL)
        {
            exit(1);
}
        getInput(p);
        if(*student!=NULL)
        {
            temp=*student;
            *student=p;
            p->next=temp;
}
        else
        {
            *student=p;
            p->next=NULL;
}
}
}

struct Student *maxScore(struct Student *student)
{
    struct Student *maxScore=student;
    while(student!=NULL)
    {
        if(student->score > maxScore->score)
        {
            maxScore=student;
}
        student=student->next;
    }
    return maxScore;
}

struct Student *minScore(struct Student *student)
{
    struct Student *minScore=student;
    while(student!=NULL)
    {
        if(student->score < minScore->score)
        {
            minScore=student;
        }
        student=student->next;
}
    return minScore;
}

void printStudent(struct Student *student)
{
    printf("%s ",student->name);
    printf("%s",student->hao);
}

int main(void)
{
    struct Student *student=NULL;
    struct Student *max;
    struct Student *min;
    int n;

    scanf("%d",&n);
    addStudent(&student,n);
    max=maxScore(student);
    min=minScore(student);

    printStudent(max);
    printf("\n");
    printStudent(min);

    return 0;
}
最佳答案
2022-3-14 11:48:10
char name[10];
    char hao[10];
空间太小

问题描述

问题描述

为什么按照上面的输入最后会有个Y呢

为什么按照上面的输入最后会有个Y呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-14 11:41:24 | 显示全部楼层
为什么最后结果回多打印一个Y呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-14 11:48:10 | 显示全部楼层    本楼为最佳答案   
char name[10];
    char hao[10];
空间太小
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-14 12:09:56 | 显示全部楼层
ba21 发表于 2022-3-14 11:48
char name[10];
    char hao[10];
空间太小

懂了懂了  感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 00:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表