学生成绩信息包括:学号,姓名,语文成绩、数学成绩、英语成绩。 主要功能: 1、 学生信息录入; 2、 学生信息浏览; 3、 信息查询(a 按学号查 b 按最高分查(查询每门课的最高分的同学信息)) 4、 信息排序(a 按总分排序(从高到低)b 按姓名排序 ) 5、 信息删除(给定学号,删除该学生信息) 6、 信息修改(给定学号,修改该学生信息) 7、 保存信息(把当前的结构体数组内容保存 8、 退出c语言程序设计
时间: 2023-06-25 13:02:30 浏览: 113
这个需求可以通过使用结构体数组来实现。首先需要定义一个包含学号、姓名、语文成绩、数学成绩、英语成绩的结构体,然后再定义一个结构体数组来存储所有学生的信息。
下面是一个简单的示例代码,其中包括上述所有功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
int id;
char name[20];
int chinese;
int math;
int english;
int total;
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Too many students, cannot add more.\n");
return;
}
struct student s;
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter Chinese score: ");
scanf("%d", &s.chinese);
printf("Enter Math score: ");
scanf("%d", &s.math);
printf("Enter English score: ");
scanf("%d", &s.english);
s.total = s.chinese + s.math + s.english;
students[num_students] = s;
num_students++;
printf("Student added.\n");
}
void print_student(struct student s) {
printf("%d\t%s\t%d\t%d\t%d\t%d\n", s.id, s.name, s.chinese, s.math, s.english, s.total);
}
void list_students() {
printf("ID\tName\tChinese\tMath\tEnglish\tTotal\n");
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
}
void find_student_by_id(int id) {
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Found student:\n");
print_student(students[i]);
return;
}
}
printf("Student not found.\n");
}
void find_highest_score() {
int max_chinese = 0;
int max_math = 0;
int max_english = 0;
for (int i = 0; i < num_students; i++) {
if (students[i].chinese > max_chinese) {
max_chinese = students[i].chinese;
}
if (students[i].math > max_math) {
max_math = students[i].math;
}
if (students[i].english > max_english) {
max_english = students[i].english;
}
}
printf("Students with highest scores:\n");
for (int i = 0; i < num_students; i++) {
if (students[i].chinese == max_chinese || students[i].math == max_math || students[i].english == max_english) {
print_student(students[i]);
}
}
}
void sort_by_total() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].total < students[j].total) {
struct student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Students sorted by total score:\n");
list_students();
}
void sort_by_name() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (strcmp(students[i].name, students[j].name) > 0) {
struct student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Students sorted by name:\n");
list_students();
}
void delete_student(int id) {
int index = -1;
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("Student not found.\n");
return;
}
for (int i = index; i < num_students - 1; i++) {
students[i] = students[i + 1];
}
num_students--;
printf("Student deleted.\n");
}
void modify_student(int id) {
int index = -1;
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("Student not found.\n");
return;
}
printf("Enter new Chinese score: ");
scanf("%d", &students[index].chinese);
printf("Enter new Math score: ");
scanf("%d", &students[index].math);
printf("Enter new English score: ");
scanf("%d", &students[index].english);
students[index].total = students[index].chinese + students[index].math + students[index].english;
printf("Student modified.\n");
}
void save_students() {
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
fprintf(fp, "%d\n", num_students);
for (int i = 0; i < num_students; i++) {
fprintf(fp, "%d %s %d %d %d\n", students[i].id, students[i].name, students[i].chinese, students[i].math, students[i].english);
}
fclose(fp);
printf("Students saved to file.\n");
}
void load_students() {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
fscanf(fp, "%d\n", &num_students);
for (int i = 0; i < num_students; i++) {
struct student s;
fscanf(fp, "%d %s %d %d %d\n", &s.id, s.name, &s.chinese, &s.math, &s.english);
s.total = s.chinese + s.math + s.english;
students[i] = s;
}
fclose(fp);
printf("Students loaded from file.\n");
}
int main() {
int choice = 0;
while (choice != 8) {
printf("\n");
printf("1. Add student\n");
printf("2. List students\n");
printf("3. Find student by ID\n");
printf("4. Find highest score\n");
printf("5. Sort by total score\n");
printf("6. Sort by name\n");
printf("7. Delete student\n");
printf("8. Modify student\n");
printf("9. Save students to file\n");
printf("10. Load students from file\n");
printf("11. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
list_students();
break;
case 3:
printf("Enter student ID: ");
int id;
scanf("%d", &id);
find_student_by_id(id);
break;
case 4:
find_highest_score();
break;
case 5:
sort_by_total();
break;
case 6:
sort_by_name();
break;
case 7:
printf("Enter student ID: ");
int id_to_delete;
scanf("%d", &id_to_delete);
delete_student(id_to_delete);
break;
case 8:
printf("Enter student ID: ");
int id_to_modify;
scanf("%d", &id_to_modify);
modify_student(id_to_modify);
break;
case 9:
save_students();
break;
case 10:
load_students();
break;
case 11:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
这个程序使用了一个文件来保存学生信息,这样即使程序退出了,学生信息也不会丢失。在程序启动时,可以尝试从文件中读取学生信息,如果文件不存在或格式不正确,则会忽略文件。在程序退出时,会将当前的学生信息保存到文件中。可以根据需要更改文件名和路径。
阅读全文