用c语音写一个学生通讯录系统
时间: 2023-12-09 21:06:26 浏览: 36
好的,以下是一个简单的学生通讯录系统的C语言代码。主要功能包括添加学生信息、查找学生信息、删除学生信息和显示所有学生信息。
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[50];
char phone[20];
char email[50];
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
return;
}
struct student new_student;
printf("Enter name: ");
scanf("%s", new_student.name);
printf("Enter phone number: ");
scanf("%s", new_student.phone);
printf("Enter email address: ");
scanf("%s", new_student.email);
students[num_students] = new_student;
num_students++;
printf("Student added successfully.\n");
}
void find_student() {
char name[50];
printf("Enter name to search: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(name, students[i].name) == 0) {
printf("Name: %s\n", students[i].name);
printf("Phone: %s\n", students[i].phone);
printf("Email: %s\n", students[i].email);
found = 1;
break;
}
}
if (!found) {
printf("Student not found.\n");
}
}
void delete_student() {
char name[50];
printf("Enter name to delete: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(name, students[i].name) == 0) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
printf("Student deleted successfully.\n");
found = 1;
break;
}
}
if (!found) {
printf("Student not found.\n");
}
}
void display_students() {
for (int i = 0; i < num_students; i++) {
printf("Name: %s\n", students[i].name);
printf("Phone: %s\n", students[i].phone);
printf("Email: %s\n", students[i].email);
printf("\n");
}
}
int main() {
int choice;
while (1) {
printf("1. Add student\n");
printf("2. Find student\n");
printf("3. Delete student\n");
printf("4. Display all students\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
find_student();
break;
case 3:
delete_student();
break;
case 4:
display_students();
break;
case 5:
return 0;
default:
printf("Invalid choice. Try again.\n");
}
}
}
```
该代码定义了一个结构体`student`,其中包括学生的姓名、电话和电子邮件。`students`数组存储了所有学生的信息,`num_students`变量表示当前学生数量。
函数`add_student`用于添加新的学生信息,它会从用户输入中获取学生的姓名、电话和电子邮件,并将其添加到`students`数组中。
函数`find_student`用于查找学生信息,它会从用户输入中获取学生的姓名,并在`students`数组中查找相应的学生信息。
函数`delete_student`用于删除学生信息,它会从用户输入中获取学生的姓名,并在`students`数组中删除相应的学生信息。
函数`display_students`用于显示所有学生信息。
主函数中包含一个循环,允许用户选择执行哪个操作。用户选择退出时,程序结束。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)