C语言编写学生管理系统
时间: 2023-05-20 19:04:53 浏览: 153
C语言编写的学生管理系统
非常感谢您的提问。以下是一个简单的 C 语言代码示例,用于编写学生管理系统:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[50];
int id;
float gpa;
};
int main() {
struct student students[MAX_STUDENTS];
int num_students = 0;
while (1) {
printf("Enter a command (add, list, quit): ");
char command[10];
scanf("%s", command);
if (strcmp(command, "add") == 0) {
if (num_students == MAX_STUDENTS) {
printf("Error: too many students\n");
continue;
}
struct student new_student;
printf("Enter name: ");
scanf("%s", new_student.name);
printf("Enter ID: ");
scanf("%d", &new_student.id);
printf("Enter GPA: ");
scanf("%f", &new_student.gpa);
students[num_students] = new_student;
num_students++;
} else if (strcmp(command, "list") == 0) {
printf("Name\tID\tGPA\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].gpa);
}
} else if (strcmp(command, "quit") == 0) {
break;
} else {
printf("Error: invalid command\n");
}
}
return 0;
}
希望这个示例能够帮助您编写学生管理系统。如果您有任何其他问题,请随时问我。
阅读全文