C语言实现学生成绩管理系统
#include <stdio.h> #include <stdlib.h> #include <string.h>
// 定义学生信息结构体 typedef struct { int id; // 学号 char name[20]; // 姓名 float score; // 成绩 } Student;
// 定义成绩管理系统结构体 typedef struct { Student* students; // 学生信息数组 int count; // 学生数量 int capacity; // 学生数组容量 } ScoreManagementSystem;
// 初始化成绩管理系统 void init(ScoreManagementSystem* system) { system->students = NULL; system->count = 0; system->capacity = 0; }
// 创建信息表 void create(ScoreManagementSystem* system, int capacity) { system->students = (Student*)malloc(capacity * sizeof(Student)); system->count = 0; system->capacity = capacity; }
// 向表中插入信息 void insert(ScoreManagementSystem* system, int id, const char* name, float score) { if (system->count >= system->capacity) { printf('Error: The table is full.\n'); return; }
Student student;
student.id = id;
strcpy(student.name, name);
student.score = score;
system->students[system->count++] = student;
printf('Insert success.\n');
}
// 按指定key值查找信息 void search(ScoreManagementSystem* system, int id) { for (int i = 0; i < system->count; i++) { if (system->students[i].id == id) { printf('Student found:\n'); printf('ID: %d\n', system->students[i].id); printf('Name: %s\n', system->students[i].name); printf('Score: %.2f\n', system->students[i].score); return; } }
printf('Student not found.\n');
}
// 按指定key值修改信息 void modify(ScoreManagementSystem* system, int id, const char* name, float score) { for (int i = 0; i < system->count; i++) { if (system->students[i].id == id) { strcpy(system->students[i].name, name); system->students[i].score = score; printf('Modify success.\n'); return; } }
printf('Student not found.\n');
}
// 按指定key值删除信息 void removeStudent(ScoreManagementSystem* system, int id) { for (int i = 0; i < system->count; i++) { if (system->students[i].id == id) { for (int j = i; j < system->count - 1; j++) { system->students[j] = system->students[j + 1]; } system->count--; printf('Remove success.\n'); return; } }
printf('Student not found.\n');
}
// 将系统数据按指定key值排序 void sort(ScoreManagementSystem* system) { for (int i = 0; i < system->count - 1; i++) { for (int j = 0; j < system->count - i - 1; j++) { if (system->students[j].id > system->students[j + 1].id) { Student temp = system->students[j]; system->students[j] = system->students[j + 1]; system->students[j + 1] = temp; } } }
printf('Sort success.\n');
}
// 打印学生信息 void printStudents(ScoreManagementSystem* system) { printf('Student information:\n'); for (int i = 0; i < system->count; i++) { printf('ID: %d\n', system->students[i].id); printf('Name: %s\n', system->students[i].name); printf('Score: %.2f\n', system->students[i].score); printf('--------------------\n'); } }
// 释放成绩管理系统内存 void release(ScoreManagementSystem* system) { free(system->students); system->students = NULL; system->count = 0; system->capacity = 0; }
int main() { ScoreManagementSystem system; init(&system);
int choice;
int id;
char name[20];
float score;
while (1) {
printf('1. Create table\n');
printf('2. Insert information\n');
printf('3. Search information\n');
printf('4. Modify information\n');
printf('5. Remove information\n');
printf('6. Sort information\n');
printf('7. Print information\n');
printf('8. Exit\n');
printf('Enter your choice: ');
scanf('%d', &choice);
switch (choice) {
case 1:
if (system.students != NULL) {
printf('Error: The table is already created.\n');
} else {
int capacity;
printf('Enter the capacity of the table: ');
scanf('%d', &capacity);
create(&system, capacity);
printf('Create table success.\n');
}
break;
case 2:
if (system.students == NULL) {
printf('Error: The table is not created yet.\n');
} else {
printf('Enter student ID: ');
scanf('%d', &id);
printf('Enter student name: ');
scanf('%s', name);
printf('Enter student score: ');
scanf('%f', &score);
insert(&system, id, name, score);
}
break;
case 3:
if (system.students == NULL) {
printf('Error: The table is not created yet.\n');
} else {
printf('Enter student ID to search: ');
scanf('%d', &id);
search(&system, id);
}
break;
case 4:
if (system.students == NULL) {
printf('Error: The table is not created yet.\n');
} else {
printf('Enter student ID to modify: ');
scanf('%d', &id);
printf('Enter new student name: ');
scanf('%s', name);
printf('Enter new student score: ');
scanf('%f', &score);
modify(&system, id, name, score);
}
break;
case 5:
if (system.students == NULL) {
printf('Error: The table is not created yet.\n');
} else {
printf('Enter student ID to remove: ');
scanf('%d', &id);
removeStudent(&system, id);
}
break;
case 6:
if (system.students == NULL) {
printf('Error: The table is not created yet.\n');
} else {
sort(&system);
}
break;
case 7:
if (system.students == NULL) {
printf('Error: The table is not created yet.\n');
} else {
printStudents(&system);
}
break;
case 8:
release(&system);
printf('Exit.\n');
return 0;
default:
printf('Invalid choice.\n');
break;
}
printf('\n');
}
}
原文地址: https://www.cveoy.top/t/topic/lakg 著作权归作者所有。请勿转载和采集!