用c语言代码写一个完整的学生信息管理系统
时间: 2023-09-03 19:25:30 浏览: 110
### 回答1:
可以参考以下的代码:
#include <stdio.h>
int main()
{
int choice;
struct student
{
char name[30];
int age;
float marks;
};
struct student s[10];
printf("*** Student Management System ***\n");
while(1)
{
printf("1. Add Student\n");
printf("2. List Students\n");
printf("3. Delete Student\n");
printf("4. Exit\n");
printf("Enter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
// code to add student
break;
case 2:
// code to list student
break;
case 3:
// code to delete student
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
### 回答2:
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 100
typedef struct {
char name[100];
int age;
int id;
} Student;
typedef struct {
Student students[MAX_STUDENTS];
int count;
} StudentDatabase;
void addStudent(StudentDatabase *db, char *name, int age, int id) {
if (db->count == MAX_STUDENTS) {
printf("学生信息已满,无法添加新学生\n");
return;
}
Student newStudent;
strcpy(newStudent.name, name);
newStudent.age = age;
newStudent.id = id;
db->students[db->count] = newStudent;
db->count++;
printf("学生 %s 成功添加到数据库\n", newStudent.name);
}
void removeStudent(StudentDatabase *db, int id) {
for (int i = 0; i < db->count; i++) {
if (db->students[i].id == id) {
printf("学生 %s 成功从数据库中删除\n", db->students[i].name);
for (int j = i; j < db->count - 1; j++) {
db->students[j] = db->students[j + 1];
}
db->count--;
return;
}
}
printf("未找到该学生的信息\n");
}
void displayStudents(StudentDatabase *db) {
printf("数据库中的学生信息如下:\n");
for (int i = 0; i < db->count; i++) {
printf("学生姓名:%s\n", db->students[i].name);
printf("学生年龄:%d\n", db->students[i].age);
printf("学生学号:%d\n", db->students[i].id);
printf("\n");
}
}
int main() {
StudentDatabase db;
db.count = 0;
addStudent(&db, "张三", 20, 10001);
addStudent(&db, "李四", 19, 10002);
addStudent(&db, "王五", 21, 10003);
displayStudents(&db);
removeStudent(&db, 10002);
displayStudents(&db);
return 0;
}
### 回答3:
学生信息管理系统是一个用来管理学生信息的应用程序,通常包括添加学生信息、查找学生信息、删除学生信息以及修改学生信息等功能。下面是一个简单的用C语言编写的学生信息管理系统的示例代码:
```c
#include <stdio.h>
struct Student {
char name[20];
int age;
int score;
};
void printMenu() {
printf("********** 学生信息管理系统 **********\n");
printf("1. 添加学生信息\n");
printf("2. 查找学生信息\n");
printf("3. 删除学生信息\n");
printf("4. 修改学生信息\n");
printf("5. 退出\n");
printf("************************************\n");
}
void addStudent(struct Student students[], int* count) {
printf("请输入学生姓名:");
scanf("%s", students[*count].name);
printf("请输入学生年龄:");
scanf("%d", &students[*count].age);
printf("请输入学生成绩:");
scanf("%d", &students[*count].score);
*count += 1;
printf("添加成功!\n");
}
void findStudent(struct Student students[], int count) {
char name[20];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("姓名:%s\n", students[i].name);
printf("年龄:%d\n", students[i].age);
printf("成绩:%d\n", students[i].score);
return;
}
}
printf("未找到该学生!\n");
}
void deleteStudent(struct Student students[], int* count) {
char name[20];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
for (int i = 0; i < *count; i++) {
if (strcmp(students[i].name, name) == 0) {
for (int j = i; j < *count - 1; j++) {
students[j] = students[j + 1];
}
*count -= 1;
printf("删除成功!\n");
return;
}
}
printf("未找到该学生!\n");
}
void modifyStudent(struct Student students[], int count) {
char name[20];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
for (int i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("请输入学生姓名:");
scanf("%s", students[i].name);
printf("请输入学生年龄:");
scanf("%d", &students[i].age);
printf("请输入学生成绩:");
scanf("%d", &students[i].score);
printf("修改成功!\n");
return;
}
}
printf("未找到该学生!\n");
}
int main() {
struct Student students[100];
int count = 0;
int choice;
while (1) {
printMenu();
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &count);
break;
case 2:
findStudent(students, count);
break;
case 3:
deleteStudent(students, &count);
break;
case 4:
modifyStudent(students, count);
break;
case 5:
return 0;
default:
printf("无效的选择!\n");
}
printf("\n");
}
return 0;
}
```
该示例代码包括了添加学生信息、查找学生信息、删除学生信息以及修改学生信息等功能。用户可以根据菜单选择需要的操作,然后按照提示进行相应的操作。这段代码使用了结构体来存储学生信息,并通过数组来管理多个学生的信息。
阅读全文