刚学习C语言,求大佬指点一下代码哪里有问题 求平均成绩的时候会出现错误
#include <stdio.h>
# define S 60.0
# define N 3
struct student
{
int n;
char name[10];
float score[3];
float ave;
};
int main ()
{
void input (struct student stu[]);
void bujige (struct student stu[]);
void youxiu (struct student stu[]);
struct student stu[N],*p=stu;
input (p);
bujige(p);
youxiu (p);
return 0;
}
void input (struct student stu[])
{
int i;
printf ("请输入各学生的学号姓名及三门课成绩:\n");
for (i=0;i<N;i++)
{
printf ("请输入第%d个学生信息:",i+1);
scanf ("%d-%s-%f-%f-%f",&stu[i].n,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
stu[i].ave=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
printf ("\n");
printf ("%f",stu[i].ave);
}
}
void bujige (struct student stu[])
{
int i,j,k=0;
for (i=0;i<N;i++)
{
for (j=0;j<3;j++)
{
if (stu[i].score[j]<S)
k++;
}
if (k>1)
{
printf ("\n");
printf ("学号:%d",stu[i].n);
for (j=0;j<3;j++)
{
if (stu[i].score[j]<S)
printf ("不及格成绩:%f ",stu[i].score[j]);
}
}
}
}
void youxiu (struct student stu[])
{
int i;
for (i=0;i<N;i++)
{
if (stu[i].ave>=85.0&&stu[i].ave<=90.0)
printf ("学号:%d 姓名:%s",stu[i].n,stu[i].name);
}
}
scanf ("%d-%s-%f-%f-%f",&stu[i].n,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
改为
scanf ("%d-%s",&stu[i].n,&stu[i].name);
fflush(stdin);
scanf (%f-%f-%f",&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
看了一下,有几个小问题,已修改
#include <stdio.h>
# define S 60.0
# define N 3
struct student
{
int n;
char name[10];
float score[3];
float ave;
};
int main()
{
void input(struct student stu[]);
void bujige(struct student stu[]);
void youxiu(struct student stu[]);
struct student stu[N], *p = stu;
input(p);
bujige(p);
youxiu(p);
return 0;
}
void input(struct student stu[])
{
int i;
printf("请输入各学生的学号姓名及三门课成绩:\n");
for (i = 0; i<N; i++)
{
printf("请输入第%d个学生信息:", i + 1);
scanf("%d %s %f %f %f", &stu[i].n, stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
stu[i].ave = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0;
printf("\n");
printf("%f", stu[i].ave);
}
}
void bujige(struct student stu[])
{
int i, j, k = 0;
for (i = 0; i<N; i++)
{
k = 0;
for (j = 0; j<3; j++)
{
if (stu[i].score[j]<S)
k++;
}
if (k>0)
{
printf("\n");
printf("学号:%d", stu[i].n);
for (j = 0; j<3; j++)
{
if (stu[i].score[j]<S)
printf("不及格成绩:%f ", stu[i].score[j]);
}
}
}
}
void youxiu(struct student stu[])
{
int i;
for (i = 0; i<N; i++)
{
if (stu[i].ave >= 85.0&&stu[i].ave <= 90.0)
printf("学号:%d 姓名:%s\n", stu[i].n, stu[i].name);
}
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632