C语言写一个学生成绩管理系统
时间: 2023-12-29 20:45:48 浏览: 63
C语言写的学生成绩管理系统
好的,下面是一个简单的学生成绩管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
char name[20];
int id;
float grade;
} student;
void add_student(student students[], int* count);
void print_students(student students[], int count);
void search_student(student students[], int count);
void update_grade(student students[], int count);
int main() {
student students[MAX_STUDENTS];
int count = 0;
int choice;
while (1) {
printf("\nStudent Grade Management System\n");
printf("1. Add Student\n");
printf("2. Print All Students\n");
printf("3. Search Student\n");
printf("4. Update Student Grade\n");
printf("5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &count);
break;
case 2:
print_students(students, count);
break;
case 3:
search_student(students, count);
break;
case 4:
update_grade(students, count);
break;
case 5:
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
}
void add_student(student students[], int* count) {
if (*count >= MAX_STUDENTS) {
printf("Maximum number of students reached.\n");
return;
}
student s;
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student grade: ");
scanf("%f", &s.grade);
students[*count] = s;
(*count)++;
}
void print_students(student students[], int count) {
if (count == 0) {
printf("No students found.\n");
return;
}
printf("Name\tID\tGrade\n");
for (int i = 0; i < count; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].grade);
}
}
void search_student(student students[], int count) {
if (count == 0) {
printf("No students found.\n");
return;
}
int id;
printf("Enter student ID to search: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
printf("Name: %s\n", students[i].name);
printf("ID: %d\n", students[i].id);
printf("Grade: %.2f\n", students[i].grade);
return;
}
}
printf("Student with ID %d not found.\n", id);
}
void update_grade(student students[], int count) {
if (count == 0) {
printf("No students found.\n");
return;
}
int id;
printf("Enter student ID to update grade: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
printf("Enter new grade: ");
scanf("%f", &students[i].grade);
printf("Grade updated successfully.\n");
return;
}
}
printf("Student with ID %d not found.\n", id);
}
```
这个学生成绩管理系统可以进行添加学生、查看所有学生、搜索指定学生、更新学生成绩和退出程序等操作。每个学生包括姓名、学号和成绩。程序使用了结构体来存储学生信息,并使用了函数来完成各种操作。
阅读全文