void display(){ if(num == 0){//若没有学生,则不能输出成绩 printf("没有任何学生信息!\n"); return; } printf("-------------------------------------------------------------------------------\n"); printf("|%5s |%-8s |%8s |%8s |%8s |%10s |%6s |%8s|\n","ID","姓名", "英语成绩","数学成绩","语文成绩","程序设计成绩","总成绩","平均成绩");//输出成绩信息提示 printf("-------------------------------------------------------------------------------\n"); for(int i=0;i<num;i++){ printf("|%5s |%-8s |%8.1f |%8.1f |%8.1f |%12.1f |%6.1f |%8.1f|\n", students[i].ID,students[i].Name, students[i].Scores[0],students[i].Scores[1],students[i].Scores[2],students[i].Scores[3], students[i].All,students[i].Average); } printf("-------------------------------------------------------------------------------\n"); }
时间: 2023-08-15 17:42:02 浏览: 49
好的,下面是代码注释:
```
void display(){ // 声明一个无返回值的函数 display
if(num == 0){//若没有学生,则不能输出成绩
printf("没有任何学生信息!\n"); // 输出提示信息
return; // 直接返回,不进行后续操作
}
printf("-------------------------------------------------------------------------------\n");
// 输出成绩信息的表头
printf("|%5s |%-8s |%8s |%8s |%8s |%10s |%6s |%8s|\n","ID","姓名",
"英语成绩","数学成绩","语文成绩","程序设计成绩","总成绩","平均成绩");
printf("-------------------------------------------------------------------------------\n");
// 依次输出每个学生的成绩信息
for(int i=0;i<num;i++){
printf("|%5s |%-8s |%8.1f |%8.1f |%8.1f |%12.1f |%6.1f |%8.1f|\n",
students[i].ID,students[i].Name,
students[i].Scores[0],students[i].Scores[1],students[i].Scores[2],students[i].Scores[3],
students[i].All,students[i].Average);
}
printf("-------------------------------------------------------------------------------\n");
}
```
这段代码定义了一个函数 `display`,它用于输出学生的成绩信息。在函数中,首先判断学生数量是否为 0,如果是则输出提示信息并直接返回。如果学生数量不为 0,则使用 `printf` 函数输出成绩信息的表头。接着使用 `for` 循环依次输出每个学生的成绩信息,其中使用了格式化输出。最后,再次使用 `printf` 函数输出成绩信息的表尾,以横线分隔。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)