|
|
|
@ -1,3 +1,132 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
char num[20]; // 学号
|
|
|
|
|
char name[10]; // 姓名
|
|
|
|
|
int math_score; // 高数成绩
|
|
|
|
|
int datastruct_score; // 数据结构成绩
|
|
|
|
|
int English_score; // 英语成绩
|
|
|
|
|
int physics_score; // 物理成绩
|
|
|
|
|
float average; // 平均分
|
|
|
|
|
} Student;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
Student *data;
|
|
|
|
|
int length;
|
|
|
|
|
} funlist;
|
|
|
|
|
|
|
|
|
|
// 初始化线性表
|
|
|
|
|
void initlist(funlist *l) {
|
|
|
|
|
l->data = (Student *)malloc(100 * sizeof(Student));
|
|
|
|
|
l->length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断是否为空表
|
|
|
|
|
int isempty(funlist *l) {
|
|
|
|
|
return l->length == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取值
|
|
|
|
|
int getelem(funlist l, int i, Student *e) {
|
|
|
|
|
if (i < 1 || i > l.length) {
|
|
|
|
|
printf("位置不合法\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
*e = l.data[i - 1];
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算平均分
|
|
|
|
|
float calculate_average(Student *s) {
|
|
|
|
|
return (s->math_score + s->datastruct_score + s->English_score + s->physics_score) / 4.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 打印操作
|
|
|
|
|
void printlist(funlist l) {
|
|
|
|
|
int i;
|
|
|
|
|
printf("成绩表为:\n");
|
|
|
|
|
for (i = 0; i < l.length; i++) {
|
|
|
|
|
printf("学号:%s 姓名:%s 高数:%d 数据结构:%d 英语:%d 物理:%d 平均分:%.2f\n",
|
|
|
|
|
l.data[i].num, l.data[i].name, l.data[i].math_score,
|
|
|
|
|
l.data[i].datastruct_score, l.data[i].English_score,
|
|
|
|
|
l.data[i].physics_score, l.data[i].average);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找操作(按学号查找)
|
|
|
|
|
int locateelem1(funlist l, char *num) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < l.length; i++) {
|
|
|
|
|
if (strcmp(l.data[i].num, num) == 0)
|
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找操作(按姓名查找)
|
|
|
|
|
int locateelem2(funlist l, char *name) {
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < l.length; i++) {
|
|
|
|
|
if (strcmp(l.data[i].name, name) == 0)
|
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除操作
|
|
|
|
|
int deleteelem(funlist *l, int i) {
|
|
|
|
|
int j;
|
|
|
|
|
if (i < 1 || i > l->length) {
|
|
|
|
|
printf("位置不合法\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
for (j = i; j < l->length; j++) {
|
|
|
|
|
l->data[j - 1] = l->data[j];
|
|
|
|
|
}
|
|
|
|
|
l->length--;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入操作
|
|
|
|
|
int listinsert(funlist *l, int i, Student e) {
|
|
|
|
|
if (i < 1 || i > l->length + 1) {
|
|
|
|
|
printf("位置不合法\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (l->length >= 100) {
|
|
|
|
|
printf("线性表已满\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int j;
|
|
|
|
|
for (j = l->length - 1; j >= i - 1; j--) {
|
|
|
|
|
l->data[j + 1] = l->data[j];
|
|
|
|
|
}
|
|
|
|
|
// 计算并保存平均分
|
|
|
|
|
e.average = calculate_average(&e);
|
|
|
|
|
l->data[i - 1] = e;
|
|
|
|
|
l->length++;
|
|
|
|
|
return 1; // 返回1表示成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按平均分排序
|
|
|
|
|
void sort_by_average(funlist *l) {
|
|
|
|
|
if (l->length <= 1) return; // 无需排序
|
|
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
|
for (i = 0; i < l->length - 1; i++) {
|
|
|
|
|
for (j = 0; j < l->length - i - 1; j++) {
|
|
|
|
|
if (l->data[j].average < l->data[j + 1].average) {
|
|
|
|
|
Student temp = l->data[j];
|
|
|
|
|
l->data[j] = l->data[j + 1];
|
|
|
|
|
l->data[j + 1] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("学生已按平均分降序排列\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
funlist L;
|
|
|
|
|
initlist(&L);
|
|
|
|
@ -120,4 +249,4 @@ int main() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|