用C语言写一个学生成绩管理系统
时间: 2023-12-08 20:05:20 浏览: 62
好的,一个简单的学生成绩管理系统可以包含以下功能:
1. 添加学生信息
2. 删除学生信息
3. 修改学生信息
4. 查询学生成绩
5. 显示所有学生信息
以下是一个使用C语言实现的示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
int id;
char name[50];
double score;
};
struct student students[MAX_STUDENTS];
int num_students = 0;
int add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
return 0;
}
struct student s;
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student score: ");
scanf("%lf", &s.score);
students[num_students++] = s;
printf("Student added successfully\n");
return 1;
}
int find_student(int id) {
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
return i;
}
}
return -1;
}
int delete_student() {
int id;
printf("Enter student ID to delete: ");
scanf("%d", &id);
int index = find_student(id);
if (index < 0) {
printf("Error: student not found\n");
return 0;
}
for (int i = index; i < num_students - 1; i++) {
students[i] = students[i + 1];
}
num_students--;
printf("Student deleted successfully\n");
return 1;
}
int update_student() {
int id;
printf("Enter student ID to update: ");
scanf("%d", &id);
int index = find_student(id);
if (index < 0) {
printf("Error: student not found\n");
return 0;
}
struct student s;
printf("Enter new student name (or press Enter to keep current name): ");
scanf("%s", s.name);
if (strlen(s.name) > 0) {
strcpy(students[index].name, s.name);
}
printf("Enter new student score (or press Enter to keep current score): ");
char score_input[10];
scanf("%s", score_input);
if (strlen(score_input) > 0) {
sscanf(score_input, "%lf", &s.score);
students[index].score = s.score;
}
printf("Student updated successfully\n");
return 1;
}
void print_student(struct student s) {
printf("%d\t%s\t%.2lf\n", s.id, s.name, s.score);
}
void print_all_students() {
printf("ID\tName\tScore\n");
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
}
int main() {
int choice = 0;
while (choice != 6) {
printf("\nStudent Score Management System\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Update student\n");
printf("4. Query student score\n");
printf("5. Show all students\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: add_student(); break;
case 2: delete_student(); break;
case 3: update_student(); break;
case 4:
{
int id;
printf("Enter student ID to query: ");
scanf("%d", &id);
int index = find_student(id);
if (index < 0) {
printf("Error: student not found\n");
} else {
print_student(students[index]);
}
break;
}
case 5: print_all_students(); break;
case 6: break;
default: printf("Error: invalid choice\n"); break;
}
}
return 0;
}
```
这个示例代码实现了一个简单的学生成绩管理系统,可以添加、删除、修改和查询学生信息,以及显示所有学生的信息。请注意,这只是一个示例,实际的学生成绩管理系统可能需要更多的功能和更复杂的算法。
阅读全文