应用实验 利用线性表实现一个通讯录管理,通讯录的数据格式如下: struct DataType int ID; //编号 char name[10]; //姓名 char ch; //性别 char phone[13]; //电话 char addr[31]; //地址 要求: ·实现通讯录的建立、增加、删除、修改、查询等功能; ·能够实现简单的菜单交互,即可以根据用户输入的命令,选择不同的操作; ·能够保存每次更新的数据(选做); ·能够进行通讯录分类,比如班级类、好友类、黑名单等(选做); ·编写main()函数测试线性表的正确性。
时间: 2023-05-19 11:03:42 浏览: 63
可以使用以下代码实现通讯录管理:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
struct DataType {
int ID;
char name[10];
char ch;
char phone[13];
char addr[31];
};
typedef struct {
DataType data[MAX_SIZE];
int length;
} List;
void initList(List *list) {
list->length = 0;
}
int add(List *list, DataType data) {
if (list->length >= MAX_SIZE) {
return 0;
}
list->data[list->length] = data;
list->length++;
return 1;
}
int removeById(List *list, int id) {
int i, j;
for (i = 0; i < list->length; i++) {
if (list->data[i].ID == id) {
for (j = i; j < list->length - 1; j++) {
list->data[j] = list->data[j + 1];
}
list->length--;
return 1;
}
}
return 0;
}
int updateById(List *list, int id, DataType newData) {
int i;
for (i = 0; i < list->length; i++) {
if (list->data[i].ID == id) {
list->data[i] = newData;
return 1;
}
}
return 0;
}
int searchById(List *list, int id, DataType *result) {
int i;
for (i = 0; i < list->length; i++) {
if (list->data[i].ID == id) {
*result = list->data[i];
return 1;
}
}
return 0;
}
void printList(List *list) {
int i;
for (i = 0; i < list->length; i++) {
printf("%d %s %c %s %s\n", list->data[i].ID, list->data[i].name, list->data[i].ch, list->data[i].phone, list->data[i].addr);
}
}
void menu() {
printf("1. Add\n");
printf("2. Remove\n");
printf("3. Update\n");
printf("4. Search\n");
printf("5. Print\n");
printf("6. Exit\n");
}
int main() {
List list;
initList(&list);
int choice;
do {
menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
DataType data;
printf("Enter ID: ");
scanf("%d", &data.ID);
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter gender: ");
scanf(" %c", &data.ch);
printf("Enter phone: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
if (add(&list, data)) {
printf("Add success!\n");
} else {
printf("Add failed! The list is full.\n");
}
break;
}
case 2: {
int id;
printf("Enter ID: ");
scanf("%d", &id);
if (removeById(&list, id)) {
printf("Remove success!\n");
} else {
printf("Remove failed! The ID does not exist.\n");
}
break;
}
case 3: {
int id;
printf("Enter ID: ");
scanf("%d", &id);
DataType newData;
printf("Enter new name: ");
scanf("%s", newData.name);
printf("Enter new gender: ");
scanf(" %c", &newData.ch);
printf("Enter new phone: ");
scanf("%s", newData.phone);
printf("Enter new address: ");
scanf("%s", newData.addr);
if (updateById(&list, id, newData)) {
printf("Update success!\n");
} else {
printf("Update failed! The ID does not exist.\n");
}
break;
}
case 4: {
int id;
printf("Enter ID: ");
scanf("%d", &id);
DataType result;
if (searchById(&list, id, &result)) {
printf("%d %s %c %s %s\n", result.ID, result.name, result.ch, result.phone, result.addr);
} else {
printf("Search failed! The ID does not exist.\n");
}
break;
}
case 5: {
printList(&list);
break;
}
case 6: {
printf("Goodbye!\n");
break;
}
default: {
printf("Invalid choice!\n");
break;
}
}
} while (choice != 6);
return 0;
}
阅读全文