|
题目:要求用两个自定义函数input和max起输入编号,姓名,成绩数据和求成绩最大值的功能,然后在主函数输出该学生信息。 #include <stdio.h>
#define N 10
struct student{
int num;
char name[10];
float point;
};
void input(struct student *p)
{
for (; (*p).num !=""\0""; p++)
{
printf("Input num:");
scanf("%d", &(*p).num);
printf("Input name:");
scanf("%s", &(*p).name);
printf("Input point");
scanf("%lf", &(*p).point);
}
}
struct student *max(struct student *p)
{
for (; (*p).point!= ""\0""; p++)
{
if ((*p).point > (*(p + 1)).point)
{
(*(p + 1)).point = (*p).point;
}
}
return p;
}
int main()
{
struct student *p;
struct student stu[N];
input(stu);
max(stu);
p = max(stu);
printf("%d%s%f", &(*p).num, &(*p).name, &(*p).point);
return 0;
}
|
|
| 20分 |
#include <stdio.h>
#define N 10
struct student{
int num;
char name[10];
float point;
};
void input(struct student *p)
{
for (int i=0;i<N;i++)
{
printf("%d/%d\n",i+1,N);
printf("Input num:");fflush(stdout);
scanf("%d", &p[i].num);
printf("Input name:");fflush(stdout);
scanf("%9s", p[i].name);
printf("Input point");fflush(stdout);
scanf("%f", &p[i].point);
}
}
struct student *max(struct student *p)
{
struct student *v;
v=p;
for (int i=1;i<N;i++)
{
if (p[i].point > v->point)
{
v=p+i;
}
}
return v;
}
int main()
{
struct student *p;
struct student stu[N];
input(stu);
p = max(stu);
printf("%d %s %f", p->num, p->name, p->point);
return 0;
}
|
| 15分 |
1:边界值N应该传入参数,输入的个数可以通过传出参数控制。
2:输出多了&,注意 scanf和printf区别。 scanf(“%lf”, &(*p).point); printf(“%d%s%f”, &(*p).num, &(*p).name, &(*p).point); 此外注意: |
|
非常感谢,不过我想问如果在input函数里用p++这样的操作能实现吗? |
|
| 15分 |
使用p++;可如下:
#include <stdio.h>
#define N 10
struct student {
int num;
char name[10];
float point;
};
void input(struct student *p)
{
struct student *q = p;
for (; p != q + N; p++)
{
printf("Input num:");
scanf("%d", &(*p).num);
printf("Input name:");
scanf("%s", &(*p).name);
printf("Input point");
scanf("%f", &(*p).point);
}
}
struct student *imax(struct student *p)
{
struct student *q, *r;
q = r = p;
for (p++; p != q + N; p++)
{
if ((*p).point > (*r).point) r = p;
}
return r;
}
int main()
{
struct student *p;
struct student stu[N];
input(stu);
imax(stu);
p = imax(stu);
printf("%d %s %f", (*p).num, (*p).name, (*p).point);
return 0;
}
|