学生信息管理系统c语言
时间: 2023-08-24 10:39:46 浏览: 38
好的,你需要一个学生信息管理系统的C语言代码是吗?那我可以为你提供一些参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student {
char name[20];
int id;
int age;
float score;
} Student;
void showMenu();
void addStudent(Student *students, int *count);
void showAllStudents(Student *students, int count);
void findStudentById(Student *students, int count);
void modifyStudentById(Student *students, int count);
void deleteStudentById(Student *students, int *count);
int main() {
int choice;
int count = 0;
Student students[100];
while (1) {
showMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &count);
break;
case 2:
showAllStudents(students, count);
break;
case 3:
findStudentById(students, count);
break;
case 4:
modifyStudentById(students, count);
break;
case 5:
deleteStudentById(students, &count);
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
void showMenu() {
printf("\n");
printf("********** MENU **********\n");
printf("1. Add student\n");
printf("2. Show all students\n");
printf("3. Find student by id\n");
printf("4. Modify student by id\n");
printf("5. Delete student by id\n");
printf("6. Exit\n");
printf("****************************\n");
printf("Enter your choice: ");
}
void addStudent(Student *students, int *count) {
Student student;
printf("\nEnter student name: ");
scanf("%s", student.name);
printf("Enter student id: ");
scanf("%d", &student.id);
printf("Enter student age: ");
scanf("%d", &student.age);
printf("Enter student score: ");
scanf("%f", &student.score);
students[*count] = student;
(*count)++;
printf("Student added successfully!\n");
}
void showAllStudents(Student *students, int count) {
if (count == 0) {
printf("\nNo students found!\n");
return;
}
printf("\n%-20s%-10s%-10s%-10s\n", "Name", "ID", "Age", "Score");
for (int i = 0; i < count; i++) {
printf("%-20s%-10d%-10d%-10.2f\n", students[i].name, students[i].id, students[i].age, students[i].score);
}
}
void findStudentById(Student *students, int count) {
int id;
printf("\nEnter student id: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
printf("\n%-20s%-10s%-10s%-10s\n", "Name", "ID", "Age", "Score");
printf("%-20s%-10d%-10d%-10.2f\n", students[i].name, students[i].id, students[i].age, students[i].score);
return;
}
}
printf("\nStudent not found!\n");
}
void modifyStudentById(Student *students, int count) {
int id;
printf("\nEnter student id: ");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (students[i].id == id) {
printf("\nCurrent student:\n");
printf("%-20s%-10s%-10s%-10s\n", "Name", "ID", "Age", "Score");
printf("%-20s%-10d%-10d%-10.2f\n", students[i].name, students[i].id, students[i].age, students[i].score);
printf("\nEnter new name (press Enter to keep current value): ");
char name[20];
fgets(name, 20, stdin);
fgets(name, 20, stdin);
if (strcmp(name, "\n") != 0) {
strcpy(students[i].name, name);
}
printf("Enter new id (press 0 to keep current value): ");
int newId;
scanf("%d", &newId);
if (newId != 0) {
students[i].id = newId;
}
printf("Enter new age (press 0 to keep current value): ");
int age;
scanf("%d", &age);
if (age != 0) {
students[i].age = age;
}
printf("Enter new score (press 0.0 to keep current value): ");
float score;
scanf("%f", &score);
if (score != 0.0) {
students[i].score = score;
}
printf("Student modified successfully!\n");
return;
}
}
printf("\nStudent not found!\n");
}
void deleteStudentById(Student *students, int *count) {
int id;
printf("\nEnter student id: ");
scanf("%d", &id);
for (int i = 0; i < *count; i++) {
if (students[i].id == id) {
for (int j = i; j < *count - 1; j++) {
students[j] = students[j + 1];
}
(*count)--;
printf("Student deleted successfully!\n");
return;
}
}
printf("\nStudent not found!\n");
}
```
这是一个简单的学生信息管理系统,包括添加学生、显示所有学生、根据学号查找学生、修改学生信息和删除学生等功能。希望对你有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)