用c++编写一个能管理多条学生信息的程序。
时间: 2023-05-29 13:01:35 浏览: 112
C++编程实现学生基本信息管理程序
抱歉,我是一名语言模型,无法提供您所需的功能。您可以参考以下代码实现一个简单的学生信息管理系统:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student {
char name[20];
int age;
char gender;
struct Student * next;
} Student;
void print_students(Student * head);
void add_student(Student ** head);
void delete_student(Student ** head, char * name);
void free_students(Student * head);
int main() {
Student * head = NULL;
int cmd;
while (1) {
printf("1. Print Students\n");
printf("2. Add Student\n");
printf("3. Delete Student\n");
printf("4. Quit\n");
printf("Enter command: ");
scanf("%d", &cmd);
switch (cmd) {
case 1:
print_students(head);
break;
case 2:
add_student(&head);
break;
case 3:
{
char name[20];
printf("Enter name to delete: ");
scanf("%s", name);
delete_student(&head, name);
}
break;
case 4:
free_students(head);
printf("Goodbye!\n");
return 0;
default:
printf("Invalid command!\n");
}
}
}
void print_students(Student * head) {
if (head == NULL) {
printf("No students found!\n");
return;
}
printf("Name\tAge\tGender\n");
while (head != NULL) {
printf("%s\t%d\t%c\n", head->name, head->age, head->gender);
head = head->next;
}
}
void add_student(Student ** head) {
Student * new_student = (Student *)malloc(sizeof(Student));
printf("Enter name: ");
scanf("%s", new_student->name);
printf("Enter age: ");
scanf("%d", &(new_student->age));
printf("Enter gender (M/F): ");
scanf(" %c", &(new_student->gender));
new_student->next = *head;
*head = new_student;
}
void delete_student(Student ** head, char * name) {
if (*head == NULL) {
printf("No students found!\n");
return;
}
Student * current = *head;
Student * previous = NULL;
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
printf("%s has been deleted.\n", name);
free(current);
return;
}
previous = current;
current = current->next;
}
printf("%s not found!\n", name);
}
void free_students(Student * head) {
while (head != NULL) {
Student * to_free = head;
head = head->next;
free(to_free);
}
}
```
这是一个简单的链表实现,实现了打印、添加、删除和退出功能。您可以根据自己的需要进行修改和扩展。
阅读全文