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 14:20:36 浏览: 69
这是一个使用 C 语言写的函数,它的作用是对一个文件中的学生信息进行排序。
首先,它声明了几个变量,包括文件指针 fp,和一个临时变量 temp。
然后,它使用 fopen 函数打开文件 "C:\\Users\\裴浩华\\Desktop\\新建文件夹(1)\\stu.txt",如果打开失败,就输出 "文件打开失败" 并退出函数。
接着,它使用一个 while 循环来读取文件中的每一个学生信息。如果已经到达文件的末尾,就退出循环。否则,使用 fread 函数读取一个学生信息到 temp 中。
在循环中,它还使用了一个 for 循环和一个 if 语句来实现学生信息的排序。
最后,它使用 fclose 函数关闭文件。
相关问题
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;` 可以让代码更规范。