void sort() { int i, j, m = 0; int ch; FILE *fp; struct student temp; if ((fp = fopen("C:\Users\裴浩华\Desktop\新建文件夹(1)\stu.txt", "rb")) == NULL) { printf("文件打开失败\n"); return; } while (!feof(fp)) { if (fread(&stu[m], LEN, 1, fp) == 1) { m++; } } fclose(fp); if (m == 0) { printf("文件中没有内容\n"); } printf("1:学号升序,2:英语成绩升序,3:C语言升序,4:总成绩升序,5:平均成绩升序\n"); printf("请选择显示形式:"); scanf("%d", &ch); switch (ch) { case 1: for (i = 0; i < m; i++) { for (j = i + 1; j < m; j++) { if (stu[i].num > stu[j].num) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } break; case 2: for (i = 0; i < m; i++) { for (j = i + 1; j < m; j++) { if (stu[i].Eng_score > stu[j].Eng_score) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } break; case 3: for (i = 0; i < m; i++) { for (j = i + 1; j < m; j++) { if (stu[i].C_score > stu[j].C_score) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } break; case 4: for (i = 0; i < m; i++) { for (j = i + 1; j < m; j++) { if (stu[i].total > stu[j].total) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } break; case 5: for (i = 0; i < m; i++) { for (j = i + 1; j < m; j++) { if (stu[i].aver > stu[j].aver) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } break; } printf("学号\t姓名\t班级\t性别\t英语成绩\tC语言成绩\t总成绩\t平均成绩\n"); for (i = 0; i < m; i++) { printf("%3d\t%s\t%s\t%s\t%.2f\t\t%.2f\t\t%.2f\t %.2f\n", stu[i].num, stu[i].name, stu[i].clas, stu[i].sex, stu[i].Eng_score, stu[i].C_score, stu[i].total, stu[i].aver); } } 画流程图
时间: 2023-02-06 15:21:32 浏览: 93
这是一段 C 语言的代码,它的功能是将文件中的学生成绩按照学号从小到大的顺序进行排序。
具体来说,代码首先打开了一个文件 "C:\Users\裴浩华\Desktop\新建文件夹(1)\stu.txt",如果打开失败就输出 "文件打开失败" 并退出函数。然后,代码使用 fread 函数从文件中读取学生成绩的信息,每次读取一个结构体 student 类型的数据。如果读取成功,就将读取到的学生成绩存储在一个数组 student_array 中,并将 m 变量加 1。代码使用双重循环来排序,每次比较两个学生的学号,如果前者的学号大于后者的学号,就交换两个学生的信息。最后,代码使用 fwrite 函数将排序后的学生成绩写回文件中。
相关问题
int (*open) (struct inode *, struct file *);
`int (*open) (struct inode *, struct file *);` 是一个函数指针,指向一个函数,该函数有两个参数,分别是`struct inode *`和`struct file *`,返回类型为`int`。
这个函数指针通常用于字符设备驱动中的`open`函数。在字符设备驱动中,`open`函数用于打开设备文件,并进行一些初始化操作。
下面是一个示例代码,演示了如何使用函数指针`open`:
```c
#include <linux/module.h>
#include <linux/fs.h>
int my_open(struct inode *inode, struct file *file)
{
// 打开设备文件时的初始化操作
// ...
return 0;
}
static int __init my_init(void)
{
struct file_operations fops = {
.open = my_open,
// 其他操作函数
};
// 注册字符设备驱动
// ...
return 0;
}
static void __exit my_exit(void)
{
// 注销字符设备驱动
// ...
}
module_init(my_init);
module_exit(my_exit);
```
在上面的示例代码中,`my_open`函数是一个自定义的`open`函数,它会在设备文件被打开时被调用。通过将`my_open`函数赋值给函数指针`open`,可以实现在设备文件打开时调用`my_open`函数。
int SortList(struct student *head) // 按总分成绩排序学生信息(降序) { // 冒泡排序 int count = 0; struct student *p, *q, *temp; temp = (struct student *)malloc(sizeof(struct student)); int length = 0; p = head->next; for ( p != NULL; p = p->next) { length++; //记录表长 } for (int i = 0; i < length - 1; i++) { for (p = head->next; p->next != NULL; p = p->next) { q = p->next; if ((p->sum) < (q->sum)) //排序 { *temp = *p; *p = *q; *q = *temp; } } } free(temp); printf("按总成绩降序排行:\n"); TraverseList(head); }
在这段代码中,有一个错误:在第10行的 for 循环中,判断条件写错了,应该是 `p != NULL` 而不是 `for ( p != NULL; p = p->next)`。下面是修改后的代码:
```
int SortList(struct student *head) // 按总分成绩排序学生信息(降序)
{
// 冒泡排序
int count = 0;
struct student *p, *q, *temp;
temp = (struct student *)malloc(sizeof(struct student));
int length = 0;
p = head->next;
while (p != NULL) //修改此处循环条件
{
length++; //记录表长
p = p->next;
}
for (int i = 0; i < length - 1; i++)
{
for (p = head->next; p->next != NULL; p = p->next)
{
q = p->next;
if ((p->sum) < (q->sum)) //排序
{
*temp = *p;
*p = *q;
*q = *temp;
}
}
}
free(temp);
printf("按总成绩降序排行:\n");
TraverseList(head);
}
```
另外,建议在函数结束时加上 return 语句,虽然这个函数返回类型是 void,在函数结尾加上 `return 0;` 可以让代码更规范。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)