用C++写一个学生信息管理系统程序
时间: 2023-10-17 08:05:32 浏览: 77
好的,我可以回答这个问题。以下是一个简单的学生信息管理系统的 C 代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
struct Student {
char name[20];
int id;
float grade;
};
int main() {
struct Student students[MAX_SIZE];
int n = 0;
while (1) {
int option;
printf("\n1. Add student\n2. Remove student\n3. Show all students\n4. Exit\n");
printf("Enter option: ");
scanf("%d", &option);
switch(option) {
case 1: {
struct Student student;
printf("Enter name: ");
scanf("%s", student.name);
printf("Enter id: ");
scanf("%d", &student.id);
printf("Enter grade: ");
scanf("%f", &student.grade);
students[n] = student;
n++;
printf("Added student successfully.\n");
break;
}
case 2: {
int id;
printf("Enter id of student to remove: ");
scanf("%d", &id);
int found = 0;
for (int i = 0; i < n; i++) {
if (students[i].id == id) {
found = 1;
for (int j = i; j < n - 1; j++) {
students[j] = students[j+1];
}
n--;
printf("Removed student successfully.\n");
break;
}
}
if (!found) {
printf("Student with id %d not found.\n", id);
}
break;
}
case 3: {
printf("Name\tID\tGrade\n");
for (int i = 0; i < n; i++) {
printf("%s\t%d\t%.2f\n", students[i].name, students[i].id, students[i].grade);
}
break;
}
case 4: {
printf("Exiting program...\n");
exit(0);
}
default: {
printf("Invalid option, please try again.\n");
break;
}
}
}
return 0;
}
```
请注意,这只是一个非常简单的示例程序,可能需要进一步的测试和开发才能满足真正的需求。
阅读全文