(1)用C语言实现系统; (2)设计一个信息管理系统,比如设计一学生信息管理系统, 学生信息包括:学号,姓名,性别,出生年月,电话,英语,数学,总成绩等。使之能提供以下功能: 1) 系统以菜单方式工作 2) 学生信息录入功能(学生信息用文件保存)---输入 3) 学生信息浏览功能---输出 4) 根据输入英语、数学成绩,计算总成绩。 5) 按学号查询 6) 按姓名查询 7) 按照总成绩进行排序 8) 学生信息的删除与修改(可选项)生成代码
时间: 2024-03-11 08:48:14 浏览: 68
很抱歉,我无法提供编写代码的服务,但是我可以给你提供一个参考的代码实现。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_STUDENT_NUM 100
typedef struct {
char id[10];
char name[MAX_NAME_LEN];
char gender[5];
char birth[12];
char phone[20];
int english;
int math;
int total;
} Student;
Student students[MAX_STUDENT_NUM];
int student_count = 0;
int load_data(const char* file_name) {
FILE* fp = fopen(file_name, "r");
if (!fp) {
return 0;
}
while (1) {
Student* student = &students[student_count];
int ret = fscanf(fp, "%s %s %s %s %s %d %d %d\n", student->id, student->name, student->gender, student->birth, student->phone, &student->english, &student->math, &student->total);
if (ret != 8) {
break;
}
student_count++;
}
fclose(fp);
return 1;
}
int save_data(const char* file_name) {
FILE* fp = fopen(file_name, "w");
if (!fp) {
return 0;
}
for (int i = 0; i < student_count; i++) {
Student* student = &students[i];
fprintf(fp, "%s %s %s %s %s %d %d %d\n", student->id, student->name, student->gender, student->birth, student->phone, student->english, student->math, student->total);
}
fclose(fp);
return 1;
}
void add_student() {
if (student_count >= MAX_STUDENT_NUM) {
printf("Error: Student list is full!\n");
return;
}
Student* student = &students[student_count];
printf("Please input student ID: ");
scanf("%s", student->id);
printf("Please input student name: ");
scanf("%s", student->name);
printf("Please input student gender: ");
scanf("%s", student->gender);
printf("Please input student birth: ");
scanf("%s", student->birth);
printf("Please input student phone: ");
scanf("%s", student->phone);
printf("Please input student English score: ");
scanf("%d", &student->english);
printf("Please input student math score: ");
scanf("%d", &student->math);
student->total = student->english + student->math;
student_count++;
}
void print_student(const Student* student) {
printf("%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\n", student->id, student->name, student->gender, student->birth, student->phone, student->english, student->math, student->total);
}
void browse_students() {
printf("ID\tName\tGender\tBirth\tPhone\tEnglish\tMath\tTotal\n");
for (int i = 0; i < student_count; i++) {
print_student(&students[i]);
}
}
void calculate_total() {
int english, math;
printf("Please input English score: ");
scanf("%d", &english);
printf("Please input math score: ");
scanf("%d", &math);
printf("ID\tName\tGender\tBirth\tPhone\tEnglish\tMath\tTotal\n");
for (int i = 0; i < student_count; i++) {
Student* student = &students[i];
if (student->english == english && student->math == math) {
print_student(student);
}
}
}
void search_student_by_id() {
char id[10];
printf("Please input student ID: ");
scanf("%s", id);
printf("ID\tName\tGender\tBirth\tPhone\tEnglish\tMath\tTotal\n");
for (int i = 0; i < student_count; i++) {
Student* student = &students[i];
if (strcmp(student->id, id) == 0) {
print_student(student);
return;
}
}
printf("Error: Student not found!\n");
}
void search_student_by_name() {
char name[MAX_NAME_LEN];
printf("Please input student name: ");
scanf("%s", name);
printf("ID\tName\tGender\tBirth\tPhone\tEnglish\tMath\tTotal\n");
for (int i = 0; i < student_count; i++) {
Student* student = &students[i];
if (strcmp(student->name, name) == 0) {
print_student(student);
return;
}
}
printf("Error: Student not found!\n");
}
void sort_students_by_total() {
for (int i = 0; i < student_count - 1; i++) {
for (int j = i + 1; j < student_count; j++) {
if (students[i].total < students[j].total) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void delete_student_by_id() {
char id[10];
printf("Please input student ID: ");
scanf("%s", id);
for (int i = 0; i < student_count; i++) {
Student* student = &students[i];
if (strcmp(student->id, id) == 0) {
for (int j = i + 1; j < student_count; j++) {
students[j - 1] = students[j];
}
student_count--;
printf("Student %s deleted!\n", id);
return;
}
}
printf("Error: Student not found!\n");
}
void modify_student_by_id() {
char id[10];
printf("Please input student ID: ");
scanf("%s", id);
for (int i = 0; i < student_count; i++) {
Student* student = &students[i];
if (strcmp(student->id, id) == 0) {
printf("Please input student name: ");
scanf("%s", student->name);
printf("Please input student gender: ");
scanf("%s", student->gender);
printf("Please input student birth: ");
scanf("%s", student->birth);
printf("Please input student phone: ");
scanf("%s", student->phone);
printf("Please input student English score: ");
scanf("%d", &student->english);
printf("Please input student math score: ");
scanf("%d", &student->math);
student->total = student->english + student->math;
printf("Student %s modified!\n", id);
return;
}
}
printf("Error: Student not found!\n");
}
void show_menu() {
printf("****************************************\n");
printf("* Student Information System *\n");
printf("****************************************\n");
printf("* 1. Add student *\n");
printf("* 2. Browse students *\n");
printf("* 3. Calculate total score *\n");
printf("* 4. Search student by ID *\n");
printf("* 5. Search student by name *\n");
printf("* 6. Sort students by total score *\n");
printf("* 7. Delete student by ID *\n");
printf("* 8. Modify student by ID *\n");
printf("* 0. Exit *\n");
printf("****************************************\n");
printf("Please input your choice: ");
}
int main() {
if (!load_data("students.txt")) {
printf("Error: Failed to load student data!\n");
return 1;
}
while (1) {
show_menu();
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
save_data("students.txt");
break;
case 2:
browse_students();
break;
case 3:
calculate_total();
break;
case 4:
search_student_by_id();
break;
case 5:
search_student_by_name();
break;
case 6:
sort_students_by_total();
browse_students();
break;
case 7:
delete_student_by_id();
save_data("students.txt");
break;
case 8:
modify_student_by_id();
save_data("students.txt");
break;
case 0:
save_data("students.txt");
return 0;
default:
printf("Error: Invalid choice!\n");
break;
}
}
return 0;
}
```
上述代码实现了学生信息管理系统的所有功能,包括学生信息录入、浏览、查询、排序、删除和修改等。其中,学生信息使用文件保存,可以实现系统数据的持久化。
阅读全文