利用学生成绩表的特点,分析逻辑结构、存储方式,实现学生成绩表的建立、利用学生成绩表的特点,分析逻辑结构、存储方式,实现学生成绩表的建立、插入、删除、查找、总分的排序等基本操作的实现。。代码
时间: 2023-12-20 22:05:56 浏览: 74
aa.zip_学生成绩管理系统 c语言_学生成绩链表
5星 · 资源好评率100%
以下是用C语言实现学生成绩表的基本操作的示例代码,用的是链表方式存储:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
char name[20]; // 学生姓名
int id; // 学号
int score1; // 第一个科目成绩
int score2; // 第二个科目成绩
int score3; // 第三个科目成绩
int total; // 总分
struct node* next;
} Node;
// 创建链表
Node* create_list() {
Node* head = (Node*)malloc(sizeof(Node));
head->next = NULL;
return head;
}
// 插入成绩记录
void insert_record(Node* head, char* name, int id, int score1, int score2, int score3) {
Node* p = head;
while (p->next != NULL) {
p = p->next;
}
Node* new_node = (Node*)malloc(sizeof(Node));
strcpy(new_node->name, name);
new_node->id = id;
new_node->score1 = score1;
new_node->score2 = score2;
new_node->score3 = score3;
new_node->total = score1 + score2 + score3;
new_node->next = NULL;
p->next = new_node;
}
// 删除成绩记录
void delete_record(Node* head, int id) {
Node* p = head;
while (p->next != NULL) {
if (p->next->id == id) {
Node* q = p->next;
p->next = q->next;
free(q);
return;
}
p = p->next;
}
printf("No such student!\n");
}
// 查找成绩记录
void find_record(Node* head, int id) {
Node* p = head->next;
while (p != NULL) {
if (p->id == id) {
printf("Name: %s\n", p->name);
printf("ID: %d\n", p->id);
printf("Score1: %d\n", p->score1);
printf("Score2: %d\n", p->score2);
printf("Score3: %d\n", p->score3);
printf("Total: %d\n", p->total);
return;
}
p = p->next;
}
printf("No such student!\n");
}
// 总分排序
void sort_record(Node* head) {
Node *p, *q, *tail = NULL;
int swapped;
do {
swapped = 0;
p = head->next;
while (p->next != tail) {
q = p->next;
if (p->total < q->total) {
// 交换p和q的位置
char temp_name[20];
int temp_id, temp_score1, temp_score2, temp_score3, temp_total;
strcpy(temp_name, p->name);
temp_id = p->id;
temp_score1 = p->score1;
temp_score2 = p->score2;
temp_score3 = p->score3;
temp_total = p->total;
strcpy(p->name, q->name);
p->id = q->id;
p->score1 = q->score1;
p->score2 = q->score2;
p->score3 = q->score3;
p->total = q->total;
strcpy(q->name, temp_name);
q->id = temp_id;
q->score1 = temp_score1;
q->score2 = temp_score2;
q->score3 = temp_score3;
q->total = temp_total;
swapped = 1;
}
p = p->next;
}
tail = p;
} while (swapped);
}
// 打印所有成绩记录
void print_records(Node* head) {
Node* p = head->next;
while (p != NULL) {
printf("Name: %s\n", p->name);
printf("ID: %d\n", p->id);
printf("Score1: %d\n", p->score1);
printf("Score2: %d\n", p->score2);
printf("Score3: %d\n", p->score3);
printf("Total: %d\n", p->total);
printf("\n");
p = p->next;
}
}
int main() {
Node* head = create_list();
insert_record(head, "Alice", 1001, 80, 85, 90);
insert_record(head, "Bob", 1002, 90, 85, 95);
insert_record(head, "Charlie", 1003, 95, 90, 80);
insert_record(head, "David", 1004, 85, 80, 70);
print_records(head);
printf("After deleting record of student with ID 1002:\n");
delete_record(head, 1002);
print_records(head);
printf("After finding record of student with ID 1003:\n");
find_record(head, 1003);
printf("After sorting by total score:\n");
sort_record(head);
print_records(head);
return 0;
}
```
注意:以上代码只是示例,实际应用中还需要考虑输入输出格式、错误处理等问题。
阅读全文