题目:
创建动态数组,顺序存放输入的一批学生数据,然后输出这批学生中分数最高的学生信息。要求定义函数分别完成动态数组的创建,学生数据输入输出和查找。
#include
#include
#include
typedef struct date
{
int year;
int month;
int day;
}DATE;
typedef struct student
{
int num;
char name[20];
char sex;
DATE birthday;
float score;
}STUDENT;
void input(STUDENT *s)
{
scanf("%d",&(s->num));
scanf("%s", s->name);
scanf(" %c",&( (*s) . sex));
scanf("%d%d%d",&s->birthday.year, &s->birthday.month, &s-> birthday.day);
scanf("%f",&(s-> score));
}
void output(STUDENT s)
{
printf("学号:%d\t姓名:%s\t性别:%c\t", s.num,s.name,s.sex);
printf("出生日期:%d-%d-%d \t", s.birthday.year,s.birthday.month, s.birthday.day);
printf("成绩:%.1f\n", s.score);
}
void createarr(STUDENT **s,int *n)
{
scanf("%d",n);
*s=( STUDENT *) malloc( (*n) * sizeof (STUDENT) );
if( *s == NULL)
{
printf("不能成功分配存储空间。\n");
exit(1);
}
}
void inputarr(STUDENT *s, int n)
{
int i;
for(i=0;iinput(*s);
}
}
int maxscore(STUDENT *s, int n)
{
int i,j;
int max,t;
max=-1000;
for(i=0;iif(s[i].score>max)
{
max=s[i].score;
t=i;
}
}
}
void outputarr(STUDENT *s, int n)
{
int i;
for(i=0;ioutput(s[i]);
}
}
int main()
{
int i,max,n;
STUDENT *ps;
createarr(&ps,&n);
inputarr (ps,n);
printf("本班共%d个学生学生信息:\n",n);
outputarr (ps,n);
max= maxscore(ps, n);
printf("本班%d个学生中的分数最高的学生是第%d个学生:\n",n,max+1);
output(ps[max]);
printf("\n");
free(ps);
}
上面是我的代码,一直不对,各种不对。
运行编译出错,哪位可以帮忙修改一下,我写这块有点懵,谢谢。
修改如下,供参考:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct date
{
int year;
int month;
int day;
}DATE;
typedef struct student
{
int num;
char name[20];
char sex;
DATE birthday;
float score;
}STUDENT;
void input(STUDENT* s)
{
scanf("%d", &s->num); //修改
scanf("%s", s->name); //修改
scanf(" %c", &s->sex);//修改
scanf("%d%d%d", &s->birthday.year, &s->birthday.month, &s->birthday.day);
scanf("%f", &s->score);
}
void output(STUDENT s)
{
printf("学号:%d\t姓名:%s\t性别:%c\t", s.num, s.name, s.sex);
printf("出生日期:%d-%d-%d \t", s.birthday.year, s.birthday.month, s.birthday.day);
printf("成绩:%.1f\n", s.score);
}
void createarr(STUDENT** s, int* n)
{
scanf("%d", n);
*s = (STUDENT*)malloc((*n) * sizeof(STUDENT));
if (*s == NULL)
{
printf("不能成功分配存储空间。\n");
exit(1);
}
}
void inputarr(STUDENT* s, int n)
{
int i;
for (i = 0; i < n; i++)
{
input(s+i); //修改
}
}
int maxscore(STUDENT* s, int n)
{
int i, j;
int max, t=0; //修改
max = -1000;
for (i = 0; i < n; i++)
{
if (s[i].score > max)
{
max = s[i].score;
t = i;
}
}
return t; //修改
}
void outputarr(STUDENT* s, int n)
{
int i;
for (i = 0; i < n; i++)
{
output(s[i]);
}
}
int main()
{
int i, max, n;
STUDENT* ps;
createarr(&ps, &n);
inputarr(ps, n);
printf("本班共%d个学生学生信息:\n", n);
outputarr(ps, n);
max = maxscore(ps, n);
printf("本班%d个学生中的分数最高的学生是第%d个学生:\n", n, max + 1);
output(ps[max]);
printf("\n");
free(ps);
}
typedef struct date
{
int year;
int month;
int day;
} DATE;
typedef struct student
{
int num;
char name[20];
char sex;
DATE birthday;
float score;
} STUDENT;
void input(STUDENT *s)
{
scanf("%d", &(s->num));
scanf("%s", s->name);
scanf(" %c", &((*s).sex));
// scanf("%d%d%d", &s->birthday.year, &s->birthday.month, &s->birthday.day);
scanf("%d%d%d", &(s->birthday.year), &(s->birthday.month), &(s->birthday.day));
scanf("%f", &(s->score));
}
void output(STUDENT s)
{
printf("学号:%d\t姓名:%s\t性别:%c\t", s.num, s.name, s.sex);
printf("出生日期:%d-%d-%d \t", s.birthday.year, s.birthday.month, s.birthday.day);
printf("成绩:%.1f\n", s.score);
}
void createarr(STUDENT **s, int *n)
{
scanf("%d", n);
*s = (STUDENT *)malloc((*n) * sizeof(STUDENT));
if (*s == NULL)
{
printf("不能成功分配存储空间。\n");
exit(1);
}
}
void inputarr(STUDENT *s, int n)
{
int i;
for (i = 0; i < n; i++)
{
// input(*s);
input(s + i);
}
}
int maxscore(STUDENT *s, int n)
{
int i, j;
int max, t;
max = -1000;
for (i = 0; i < n; i++)
{
if (s[i].score > max)
{
max = s[i].score;
t = i;
}
}
return t; //
}
void outputarr(STUDENT *s, int n)
{
int i;
for (i = 0; i < n; i++)
{
output(s[i]);
}
}
int main()
{
int i, max, n;
STUDENT *ps;
createarr(&ps, &n);
inputarr(ps, n);
printf("本班共%d个学生学生信息:\n", n);
outputarr(ps, n);
max = maxscore(ps, n);
printf("本班%d个学生中的分数最高的学生是第%d个学生:\n", n, max + 1);
output(ps[max]);
printf("\n");
free(ps);
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct date
{
int year;
int month;
int day;
}DATE;
typedef struct student
{
int num;
char name[20];
char sex;
DATE birthday;
float score;
}STUDENT;
void input(STUDENT* s)
{
scanf("%d", &(s->num));
scanf("%s", s->name);
scanf(" %c", &((*s).sex));
scanf("%d%d%d", &s->birthday.year, &s->birthday.month, &s->birthday.day);
scanf("%f", &(s->score));
}
void output(STUDENT s)
{
printf("学号:%d\t姓名:%s\t性别:%c\t", s.num, s.name, s.sex);
printf("出生日期:%d-%d-%d \t", s.birthday.year, s.birthday.month, s.birthday.day);
printf("成绩:%.1f\n", s.score);
}
STUDENT* createarr( int *n)
{
scanf("%d", n);
STUDENT* s =(STUDENT*)malloc((*n) * sizeof(STUDENT));
if (s == NULL)
{
printf("不能成功分配存储空间。\n");
exit(1);
}
}
void inputarr(STUDENT* s, int n)
{
int i;
for (i = 0; i < n; i++)
{
input(s+i); //输入的时候指针记得移动
}
}
int maxscore(STUDENT* s, int n)
{
int i, j;
int max, t;
max = -1000;
for (i = 0; i < n; i++)
{
if (s[i].score > max)
{
max = s[i].score;
t = i;
}
}
return t; //返回分数最高的学生的编号
}
void outputarr(STUDENT* s, int n)
{
int i;
for (i = 0; i < n; i++)
{
output(s[i]);
}
}
int main()
{
int i, max, n = 0 ;
STUDENT* ps = createarr(&n); //直接设置了返回值为一个STUDENT*
inputarr(ps, n);
printf("本班共%d个学生学生信息:\n", n);
outputarr(ps, n);
max = maxscore(ps, n);
printf("本班%d个学生中的分数最高的学生是第%d个学生:\n", n, max + 1);
output(ps[max]);
printf("\n");
free(ps);
}