请写一段以C语言为基础的通讯录管理系统要求如下⑴ 添加和删除通讯录中的记录信息; ⑵ 浏览通讯录信息; ⑶ 根据姓名进行查询。 ⑷ 统计排序,统计某籍贯的所有学生数量并排序。 ⑸ 结果格式化显示。并且可以在vs2010中运行
时间: 2024-02-12 22:09:44 浏览: 68
C语言通讯录管理系统实现添加,查询,修改,删除等功能。
4星 · 用户满意度95%
好的,以下是一个简单的通讯录管理系统的C语言代码,可以在VS2010中运行:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_ADDRESS_LEN 50
#define MAX_PHONE_LEN 15
#define MAX_STUDENT_NUM 100
// 定义学生结构体
typedef struct {
char name[MAX_NAME_LEN];
char address[MAX_ADDRESS_LEN];
char phone[MAX_PHONE_LEN];
} Student;
// 定义通讯录结构体
typedef struct {
Student students[MAX_STUDENT_NUM];
int num_students;
} AddressBook;
// 添加学生信息
void add_student(AddressBook *book) {
if (book->num_students >= MAX_STUDENT_NUM) {
printf("通讯录已满,无法添加学生信息!\n");
return;
}
Student *new_student = &(book->students[book->num_students]);
printf("请输入学生姓名:");
scanf("%s", new_student->name);
printf("请输入学生地址:");
scanf("%s", new_student->address);
printf("请输入学生电话:");
scanf("%s", new_student->phone);
book->num_students++;
printf("学生信息添加成功!\n");
}
// 删除学生信息
void remove_student(AddressBook *book) {
if (book->num_students == 0) {
printf("通讯录中没有学生信息!\n");
return;
}
char name[MAX_NAME_LEN];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
int i = 0;
for (; i < book->num_students; i++) {
if (strcmp(name, book->students[i].name) == 0) {
break;
}
}
if (i == book->num_students) {
printf("通讯录中没有该学生信息!\n");
return;
}
book->num_students--;
for (; i < book->num_students; i++) {
book->students[i] = book->students[i+1];
}
printf("学生信息删除成功!\n");
}
// 浏览学生信息
void browse_students(AddressBook *book) {
if (book->num_students == 0) {
printf("通讯录中没有学生信息!\n");
return;
}
printf("姓名\t地址\t电话\n");
for (int i = 0; i < book->num_students; i++) {
printf("%s\t%s\t%s\n", book->students[i].name, book->students[i].address, book->students[i].phone);
}
}
// 根据姓名查询学生信息
void find_student(AddressBook *book) {
if (book->num_students == 0) {
printf("通讯录中没有学生信息!\n");
return;
}
char name[MAX_NAME_LEN];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
for (int i = 0; i < book->num_students; i++) {
if (strcmp(name, book->students[i].name) == 0) {
printf("姓名\t地址\t电话\n");
printf("%s\t%s\t%s\n", book->students[i].name, book->students[i].address, book->students[i].phone);
return;
}
}
printf("通讯录中没有该学生信息!\n");
}
// 统计排序学生信息
void sort_students(AddressBook *book) {
if (book->num_students == 0) {
printf("通讯录中没有学生信息!\n");
return;
}
char address[MAX_ADDRESS_LEN];
printf("请输入要统计排序的籍贯:");
scanf("%s", address);
int count = 0;
Student temp;
for (int i = 0; i < book->num_students; i++) {
if (strcmp(address, book->students[i].address) == 0) {
count++;
for (int j = i+1; j < book->num_students; j++) {
if (strcmp(address, book->students[j].address) == 0) {
if (strcmp(book->students[i].name, book->students[j].name) > 0) {
temp = book->students[i];
book->students[i] = book->students[j];
book->students[j] = temp;
}
}
}
}
}
printf("共计%d个%s籍贯的学生,按姓名排序结果如下:\n", count, address);
printf("姓名\t地址\t电话\n");
for (int i = 0; i < book->num_students; i++) {
if (strcmp(address, book->students[i].address) == 0) {
printf("%s\t%s\t%s\n", book->students[i].name, book->students[i].address, book->students[i].phone);
}
}
}
// 格式化显示学生信息
void format_students(AddressBook *book) {
if (book->num_students == 0) {
printf("通讯录中没有学生信息!\n");
return;
}
printf("姓名\t\t地址\t\t\t电话\n");
for (int i = 0; i < book->num_students; i++) {
printf("%-20s%-30s%-15s\n", book->students[i].name, book->students[i].address, book->students[i].phone);
}
}
int main() {
AddressBook book;
book.num_students = 0;
int choice = 0;
while (1) {
printf("请选择操作:\n");
printf("1. 添加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 浏览学生信息\n");
printf("4. 根据姓名查询学生信息\n");
printf("5. 统计排序学生信息\n");
printf("6. 格式化显示学生信息\n");
printf("7. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(&book);
break;
case 2:
remove_student(&book);
break;
case 3:
browse_students(&book);
break;
case 4:
find_student(&book);
break;
case 5:
sort_students(&book);
break;
case 6:
format_students(&book);
break;
case 7:
printf("谢谢使用!\n");
exit(0);
default:
printf("输入错误,请重新输入!\n");
}
}
return 0;
}
```
以上就是一个简单的以C语言为基础的通讯录管理系统的代码,可以实现添加、删除、浏览、查询、统计排序和格式化显示学生信息的功能。
阅读全文