投入就放过 发表于 2022-3-14 11:38:32

c 语言结构体

请教各位大佬!!!

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

struct Student
{
    char name;
    char hao;
    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:41:24

为什么最后结果回多打印一个Y呢

ba21 发表于 2022-3-14 11:48:10

char name;
    char hao;
空间太小

投入就放过 发表于 2022-3-14 12:09:56

ba21 发表于 2022-3-14 11:48
char name;
    char hao;
空间太小

懂了懂了感谢!
页: [1]
查看完整版本: c 语言结构体