#include<stdio.h> #include<stdlib.h> #include<string.h> #include<windows.h> struct stu { int id; char name[10]; float grade[11]; float sum; float average; float gpa; struct stu* next; }stu* head;stu *tail;按照这个结构体来续写C语言代码。要求将学生信息保存在硬盘文件中,通过制定文件路径和文件名称来导入学生信息;按照学号对所有学生信息进行升序、降序排列,并输出到屏幕显示或输出到文件
时间: 2023-09-11 15:05:52 浏览: 105
# include <stdio.h>
好的,我会为你续写代码。以下是完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
struct stu {
int id;
char name[10];
float grade[11];
float sum;
float average;
float gpa;
struct stu* next;
};
typedef struct stu stu;
stu* head = NULL;
stu* tail = NULL;
// 读取学生信息
void read_info(char* file_name) {
FILE* fp;
fp = fopen(file_name, "r");
if(fp == NULL) {
printf("Failed to open file %s\n", file_name);
return;
}
while(!feof(fp)) {
stu* s = (stu*)malloc(sizeof(stu));
fscanf(fp, "%d%s%f%f%f", &s->id, s->name, &s->grade[0], &s->grade[1], &s->grade[2]);
s->sum = s->grade[0] + s->grade[1] + s->grade[2];
s->average = s->sum / 3.0;
s->gpa = s->average / 20.0 - 1.5;
if(head == NULL) {
head = s;
tail = s;
tail->next = NULL;
} else {
tail->next = s;
tail = s;
tail->next = NULL;
}
}
fclose(fp);
}
// 写入学生信息
void write_info(char* file_name) {
FILE* fp;
fp = fopen(file_name, "w");
if(fp == NULL) {
printf("Failed to open file %s\n", file_name);
return;
}
stu* p = head;
while(p != NULL) {
fprintf(fp, "%d %s %.2f %.2f %.2f\n", p->id, p->name, p->grade[0], p->grade[1], p->grade[2]);
p = p->next;
}
fclose(fp);
}
// 按照学号升序排序
void sort_by_id_ascending() {
int len = 0;
stu* p = head;
while(p != NULL) {
len++;
p = p->next;
}
p = head;
for(int i = 0; i < len - 1; i++) {
for(int j = 0; j < len - i - 1; j++) {
if(p->id > p->next->id) {
stu* tmp = p->next;
p->next = tmp->next;
tmp->next = p;
if(p == head) {
head = tmp;
} else {
stu* q = head;
while(q->next != p) {
q = q->next;
}
q->next = tmp;
}
p = tmp;
}
p = p->next;
}
p = head;
}
}
// 按照学号降序排序
void sort_by_id_descending() {
int len = 0;
stu* p = head;
while(p != NULL) {
len++;
p = p->next;
}
p = head;
for(int i = 0; i < len - 1; i++) {
for(int j = 0; j < len - i - 1; j++) {
if(p->id < p->next->id) {
stu* tmp = p->next;
p->next = tmp->next;
tmp->next = p;
if(p == head) {
head = tmp;
} else {
stu* q = head;
while(q->next != p) {
q = q->next;
}
q->next = tmp;
}
p = tmp;
}
p = p->next;
}
p = head;
}
}
// 输出学生信息
void print_info() {
stu* p = head;
while(p != NULL) {
printf("%d %s %.2f %.2f %.2f %.2f\n", p->id, p->name, p->grade[0], p->grade[1], p->grade[2], p->gpa);
p = p->next;
}
}
int main() {
char file_name[100];
printf("Please enter the file path and name: ");
scanf("%s", file_name);
read_info(file_name);
printf("Before sorting:\n");
print_info();
sort_by_id_ascending();
printf("After sorting by id in ascending order:\n");
print_info();
sort_by_id_descending();
printf("After sorting by id in descending order:\n");
print_info();
printf("Please enter the file path and name you want to save the sorted information: ");
scanf("%s", file_name);
write_info(file_name);
return 0;
}
```
这个程序可以通过文件读取来导入学生信息,根据学号升序或降序排列学生信息,并输出到屏幕或文件。请注意,在运行程序时,需要输入导入文件的路径和名称,以及希望将排序信息保存到的文件路径和名称。
阅读全文