头发 发表于 2020-11-16 19:47:25

为啥会出现一堆乱码?

本帖最后由 头发 于 2020-11-16 19:58 编辑

#include <stdio.h>
#define N 16
typedef struct
{ char num;
int s;
} STREC;
int fun( STREC *a, STREC *b )
{
        int max=0,s=0;                                                //要求编写一个函数
                                                                       //功能:把分数最高的学生数据放在b所指的数组中。注意:分数最高的学生可能不止一个,函数返                                                                                                                                          
      for(int i=0;i<N;i++)                                          回分数最高的学生的人数。
       if(a.s>max) max=a.s;
        for(int i=0;i<N;i++)
       if(a.s==max) s++;
        for(int i=0;i<s;i++)
       b.s=max;
        return s;
}
main()
{ STREC s={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},
{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87},
{"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},
{"GA011",77},{"GA017",64},{"GA018",64},{"GA016",72}};
STREC h;
int i,n;
n=fun( s,h );
printf("The %d highest score :\n",n);
for(i=0;i<n; i++)
printf("%s %4d\n",h.num,h.s);
printf("\n");
}

结果对了 看到了仨91 ,其他都是乱码。。。咋改?求解

xieglt 发表于 2020-11-16 20:18:25

#include <stdio.h>
#define N 16
typedef struct
{        char num;
        int s;
} STREC;

int fun( STREC *a, STREC *b )
{
      int max=0,s=0;                                                //要求编写一个函数
                                                                     //功能:把分数最高的学生数据放在b所指的数组中。注意:分数最高的学生可能不止一个,函数返                                                                                                                                          
      for(int i=0;i<N;i++)                                          //回分数最高的学生的人数。
                        if(a.s>max) max=a.s;
               
                        //你光返回了成绩,没有返回学号
                for(i=0;i<N;i++)
                {
                        if(a.s==max)
                        {
                                b = a;
                        }
                }


      return s;
}
int main()
{
        STREC s={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},
                                {"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87},
                                {"GA015",85},{"GA013",91},{"GA012",64},{"GA014",91},
                                {"GA011",77},{"GA017",64},{"GA018",64},{"GA016",72}};
        STREC h;
        int i,n;
        n=fun( s,h );
        printf("The %d highest score :\n",n);
        for(i=0;i<n; i++)
                printf("%s %4d\n",h.num,h.s);
        printf("\n");
        return 0;
}

页: [1]
查看完整版本: 为啥会出现一堆乱码?