C语言编程练习:建立一个同学通讯录,输入学号、姓名、电话号码、住址等信息,然后 对通信录进行显示、查找、添加、修改。 编写一个菜单驱动的通讯录,其功能要求: (1)通讯录的每一条信息包括学号、姓名、家庭住址、电话、分类(如 同事、朋友、同学、家人等)、EMAIL、QQ等。 (2)输入功能:可以一次完成若干条信息的输入。 (3)显示功能:完成全部通讯录信息的显示。(4)查找功能:可以按姓名或按类别等多种方式查找通讯信息 (5)修改功能:根据需要完成通讯录信息的更新、修改 (6)排序功能:可按照所有学生的学号从小到大进行排序; (7)通讯录数据要能够保存,可采用文件读写技术存放于专门的数据文 件如xxx.txt,在程序开始运行时,可以通过读取该文件获得记录数据。
时间: 2023-07-10 13:34:58 浏览: 218
这是一个比较完整的C语言编程练习,需要综合运用结构体、文件读写、字符串处理、菜单驱动等知识点。以下是一个简单的实现,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 1000
#define MAX_LENGTH 50
typedef struct {
char id[MAX_LENGTH];
char name[MAX_LENGTH];
char address[MAX_LENGTH];
char phone[MAX_LENGTH];
char type[MAX_LENGTH];
char email[MAX_LENGTH];
char qq[MAX_LENGTH];
} Record;
Record records[MAX_RECORDS];
int num_records = 0;
void load_records();
void save_records();
void add_record();
void show_records();
void search_records();
void modify_record();
void sort_records();
int main() {
load_records();
while (1) {
printf("请选择操作:\n");
printf("1. 输入记录\n");
printf("2. 显示记录\n");
printf("3. 查找记录\n");
printf("4. 修改记录\n");
printf("5. 排序记录\n");
printf("6. 保存并退出\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_record();
break;
case 2:
show_records();
break;
case 3:
search_records();
break;
case 4:
modify_record();
break;
case 5:
sort_records();
break;
case 6:
save_records();
return 0;
default:
printf("无效的选择!\n");
}
}
}
void load_records() {
FILE *fp = fopen("records.txt", "r");
if (fp == NULL) {
printf("无法打开记录文件!\n");
return;
}
num_records = 0;
while (num_records < MAX_RECORDS && fscanf(fp, "%s %s %s %s %s %s %s",
records[num_records].id,
records[num_records].name,
records[num_records].address,
records[num_records].phone,
records[num_records].type,
records[num_records].email,
records[num_records].qq) != EOF) {
num_records++;
}
fclose(fp);
printf("成功加载 %d 条记录。\n", num_records);
}
void save_records() {
FILE *fp = fopen("records.txt", "w");
if (fp == NULL) {
printf("无法创建记录文件!\n");
return;
}
for (int i = 0; i < num_records; i++) {
fprintf(fp, "%s %s %s %s %s %s %s\n", records[i].id,
records[i].name,
records[i].address,
records[i].phone,
records[i].type,
records[i].email,
records[i].qq);
}
fclose(fp);
printf("成功保存 %d 条记录。\n", num_records);
}
void add_record() {
printf("请输入记录(格式:学号 姓名 家庭住址 电话 分类 EMAIL QQ):\n");
while (num_records < MAX_RECORDS && scanf("%s %s %s %s %s %s %s",
records[num_records].id,
records[num_records].name,
records[num_records].address,
records[num_records].phone,
records[num_records].type,
records[num_records].email,
records[num_records].qq) == 7) {
num_records++;
}
printf("成功输入 %d 条记录。\n", num_records);
}
void show_records() {
printf("所有记录如下:\n");
for (int i = 0; i < num_records; i++) {
printf("%s %s %s %s %s %s %s\n", records[i].id,
records[i].name,
records[i].address,
records[i].phone,
records[i].type,
records[i].email,
records[i].qq);
}
}
void search_records() {
printf("请选择查找方式:\n");
printf("1. 按姓名查找\n");
printf("2. 按分类查找\n");
int choice;
scanf("%d", &choice);
if (choice == 1) {
printf("请输入姓名:\n");
char name[MAX_LENGTH];
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_records; i++) {
if (strcmp(records[i].name, name) == 0) {
printf("%s %s %s %s %s %s %s\n", records[i].id,
records[i].name,
records[i].address,
records[i].phone,
records[i].type,
records[i].email,
records[i].qq);
found = 1;
}
}
if (!found) {
printf("未找到记录!\n");
}
} else if (choice == 2) {
printf("请输入分类:\n");
char type[MAX_LENGTH];
scanf("%s", type);
int found = 0;
for (int i = 0; i < num_records; i++) {
if (strcmp(records[i].type, type) == 0) {
printf("%s %s %s %s %s %s %s\n", records[i].id,
records[i].name,
records[i].address,
records[i].phone,
records[i].type,
records[i].email,
records[i].qq);
found = 1;
}
}
if (!found) {
printf("未找到记录!\n");
}
} else {
printf("无效的选择!\n");
}
}
void modify_record() {
printf("请输入要修改的记录的学号:\n");
char id[MAX_LENGTH];
scanf("%s", id);
int found = 0;
for (int i = 0; i < num_records; i++) {
if (strcmp(records[i].id, id) == 0) {
printf("请输入新的记录(格式:姓名 家庭住址 电话 分类 EMAIL QQ):\n");
scanf("%s %s %s %s %s %s",
records[i].name,
records[i].address,
records[i].phone,
records[i].type,
records[i].email,
records[i].qq);
found = 1;
break;
}
}
if (found) {
printf("成功修改记录!\n");
} else {
printf("未找到记录!\n");
}
}
void sort_records() {
for (int i = 0; i < num_records - 1; i++) {
for (int j = 0; j < num_records - i - 1; j++) {
if (strcmp(records[j].id, records[j+1].id) > 0) {
Record temp = records[j];
records[j] = records[j+1];
records[j+1] = temp;
}
}
}
printf("成功排序记录!\n");
}
```
在这个实现中,所有记录存储在一个全局数组中,最多可以存储1000条记录。load_records()和save_records()函数用来实现文件读写,可以在程序开始运行时自动加载之前保存的记录,也可以在程序结束时自动保存当前的记录。add_record()、show_records()、search_records()、modify_record()和sort_records()分别对应了题目中要求的各种功能。菜单驱动则使用了一个while循环和switch语句来实现。
阅读全文