#include <stdio.h> #include <stdlib.h> struct student { int num; char name[10]; int c,oop,db; float avg; }; void main() { int i; struct student s,s1,s2; for(i=1;i<3;i++) { printf("请输入第%d个学生的信息:\n",i); scanf("%d%s%d%d%d",&s.num,s.name,&s.c,&s.oop,&s.db); s.avg = (float)(s.c+s.oop+s.db)/3; if(i=1) s1=s; else s2=s; } if(s1.avg<s2.avg) { s=s1; s1=s2; s2=s; } printf("\n学号 姓名 c语言 OOP 数据库 平均成绩\n"); printf("%-10d%-10s%-8d%-8d%-8d%-6.1f\n",s1.num,s1.name,s1.c,s1.oop,s1.db,s1.avg); printf("%-10d%-10s%-6d%-6d%-6d%-6.1f\n",s2.num,s2.name,s2.c,s2.oop,s2.db,s2.avg); }哪里错了
时间: 2024-03-05 22:50:57 浏览: 91
在 if 语句中,判断 s1 和 s2 平均成绩大小的时候,应该使用比较运算符“<”,而不是赋值运算符“=”,所以程序会出现逻辑错误。正确的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
struct student {
int num;
char name[10];
int c,oop,db;
float avg;
};
void main() {
int i;
struct student s,s1,s2;
for(i=1;i<3;i++)
{
printf("请输入第%d个学生的信息:\n",i);
scanf("%d%s%d%d%d",&s.num,s.name,&s.c,&s.oop,&s.db);
s.avg = (float)(s.c+s.oop+s.db)/3;
if(i==1)
s1=s;
else
s2=s;
}
if(s1.avg<s2.avg)
{
s=s1;
s1=s2;
s2=s;
}
printf("\n学号 姓名 c语言 OOP 数据库 平均成绩\n");
printf("%-10d%-10s%-8d%-8d%-8d%-6.1f\n",s1.num,s1.name,s1.c,s1.oop,s1.db,s1.avg);
printf("%-10d%-10s%-6d%-6d%-6d%-6.1f\n",s2.num,s2.name,s2.c,s2.oop,s2.db,s2.avg);
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)