用c语言写一个学生成绩管理系统
时间: 2023-05-20 15:05:09 浏览: 44
好的,以下是一个简单的学生成绩管理系统的 C 语言代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[50];
int id;
float grade;
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
return;
}
struct 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[num_students] = s;
num_students++;
}
void print_students() {
printf("Name\tID\tGrade\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].grade);
}
}
int main() {
while (1) {
printf("Enter command (add, print, quit): ");
char command[10];
scanf("%s", command);
if (strcmp(command, "add") == 0) {
add_student();
} else if (strcmp(command, "print") == 0) {
print_students();
} else if (strcmp(command, "quit") == 0) {
break;
} else {
printf("Error: invalid command\n");
}
}
return 0;
}
这个程序可以让用户添加学生信息,包括姓名、学号和成绩,并且可以打印出所有学生的信息。
阅读全文