题目5 通讯录的制作

要求:

1.实现联系人的存储,可选择顺序存储方式或者链式存储方式。

2.对所存储的联系人信息实现添加、删除、查询等基本功能。

3.规定:程序中有合理的提示信息,以合理的形式输入数据、存储数据,并以合理的形式输出程序结果,使用C语言实现程序设计。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define NAME_SIZE 50

#define PHONE_SIZE 20

typedef struct Contact {

char name[NAME_SIZE];

char phone[PHONE_SIZE];

struct Contact* next;

} Contact;

Contact* createContact(const char* name, const char* phone) {

Contact* newContact = (Contact*)malloc(sizeof(Contact));

if (!newContact) {

printf("内存分配失败\n");

exit(1);

}

strncpy(newContact->name, name, NAME_SIZE);

strncpy(newContact->phone, phone, PHONE_SIZE);

newContact->next = NULL;

return newContact;

}

void addContact(Contact** head, const char* name, const char* phone) {

Contact* newContact = createContact(name, phone);

newContact->next = *head;

*head = newContact;

printf("联系人添加成功。\n");

}

void deleteContact(Contact** head, const char* name) {

Contact* temp = *head;

Contact* prev = NULL;

while (temp != NULL && strcmp(temp->name, name) != 0) {

prev = temp;

temp = temp->next;

}

if (temp == NULL) {

printf("未找到联系人。\n");

return;

}

if (prev == NULL) {

*head = temp->next;

} else {

prev->next = temp->next;

}

free(temp);

printf("联系人删除成功。\n");

}

void searchContact(Contact* head, const char* name) {

Contact* temp = head;

while (temp != NULL) {

if (strcmp(temp->name, name) == 0) {

printf("找到联系人:\n");

printf("姓名:%s\n", temp->name);

printf("电话:%s\n", temp->phone);

return;

}

temp = temp->next;

}

printf("未找到联系人。\n");

}

void displayContacts(Contact* head) {

Contact* temp = head;

if (temp == NULL) {

printf("没有联系人可显示。\n");

return;

}

printf("联系人列表:\n");

while (temp != NULL) {

printf("姓名:%s, 电话:%s\n", temp->name, temp->phone);

temp = temp->next;

}

}

void freeContacts(Contact* head) {

Contact* temp;

while (head != NULL) {

temp = head;

head = head->next;

free(temp);

}

}

int main() {

Contact* head = NULL;

int choice;

char name[NAME_SIZE];

char phone[PHONE_SIZE];

while (1) {

printf("\n---- 通讯录管理系统 ----\n");

printf("1. 添加联系人\n");

printf("2. 删除联系人\n");

printf("3. 查询联系人\n");

printf("4. 显示所有联系人\n");

printf("5. 退出\n");

printf("请输入你的选择:");

scanf("%d", &choice);

getchar(); // 清除换行符

switch (choice) {

case 1:

printf("请输入姓名:");

fgets(name, NAME_SIZE, stdin);

name[strcspn(name, "\n")] = '\0'; // 去掉换行符

printf("请输入电话:");

fgets(phone, PHONE_SIZE, stdin);

phone[strcspn(phone, "\n")] = '\0'; // 去掉换行符

addContact(&head, name, phone);

break;

case 2:

printf("请输入要删除的联系人的姓名:");

fgets(name, NAME_SIZE, stdin);

name[strcspn(name, "\n")] = '\0'; // 去掉换行符

deleteContact(&head, name);

break;

case 3:

printf("请输入要查询的联系人的姓名:");

fgets(name, NAME_SIZE, stdin);

name[strcspn(name, "\n")] = '\0'; // 去掉换行符

searchContact(head, name);

break;

case 4:

displayContacts(head);

break;

case 5:

freeContacts(head);

printf("退出程序...\n");

return 0;

default:

printf("无效的选择。请重试。\n");

break;

}

}

return 0;

}#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define NAME_SIZE 50

#define PHONE_SIZE 20

typedef struct Contact {

char name[NAME_SIZE];

char phone[PHONE_SIZE];

struct Contact* next;

} Contact;

Contact* createContact(const char* name, const char* phone) {

Contact* newContact = (Contact*)malloc(sizeof(Contact));

if (!newContact) {

printf("内存分配失败\n");

exit(1);

}

strncpy(newContact->name, name, NAME_SIZE);

strncpy(newContact->phone, phone, PHONE_SIZE);

newContact->next = NULL;

return newContact;

}

void addContact(Contact** head, const char* name, const char* phone) {

Contact* newContact = createContact(name, phone);

newContact->next = *head;

*head = newContact;

printf("联系人添加成功。\n");

}

void deleteContact(Contact** head, const char* name) {

Contact* temp = *head;

Contact* prev = NULL;

while (temp != NULL && strcmp(temp->name, name) != 0) {

prev = temp;

temp = temp->next;

}

if (temp == NULL) {

printf("未找到联系人。\n");

return;

}

if (prev == NULL) {

*head = temp->next;

} else {

prev->next = temp->next;

}

free(temp);

printf("联系人删除成功。\n");

}

void searchContact(Contact* head, const char* name) {

Contact* temp = head;

while (temp != NULL) {

if (strcmp(temp->name, name) == 0) {

printf("找到联系人:\n");

printf("姓名:%s\n", temp->name);

printf("电话:%s\n", temp->phone);

return;

}

temp = temp->next;

}

printf("未找到联系人。\n");

}

void displayContacts(Contact* head) {

Contact* temp = head;

if (temp == NULL) {

printf("没有联系人可显示。\n");

return;

}

printf("联系人列表:\n");

while (temp != NULL) {

printf("姓名:%s, 电话:%s\n", temp->name, temp->phone);

temp = temp->next;

}

}

void freeContacts(Contact* head) {

Contact* temp;

while (head != NULL) {

temp = head;

head = head->next;

free(temp);

}

}

int main() {

Contact* head = NULL;

int choice;

char name[NAME_SIZE];

char phone[PHONE_SIZE];

while (1) {

printf("\n---- 通讯录管理系统 ----\n");

printf("1. 添加联系人\n");

printf("2. 删除联系人\n");

printf("3. 查询联系人\n");

printf("4. 显示所有联系人\n");

printf("5. 退出\n");

printf("请输入你的选择:");

scanf("%d", &choice);

getchar(); // 清除换行符

switch (choice) {

case 1:

printf("请输入姓名:");

fgets(name, NAME_SIZE, stdin);

name[strcspn(name, "\n")] = '\0'; // 去掉换行符

printf("请输入电话:");

fgets(phone, PHONE_SIZE, stdin);

phone[strcspn(phone, "\n")] = '\0'; // 去掉换行符

addContact(&head, name, phone);

break;

case 2:

printf("请输入要删除的联系人的姓名:");

fgets(name, NAME_SIZE, stdin);

name[strcspn(name, "\n")] = '\0'; // 去掉换行符

deleteContact(&head, name);

break;

case 3:

printf("请输入要查询的联系人的姓名:");

fgets(name, NAME_SIZE, stdin);

name[strcspn(name, "\n")] = '\0'; // 去掉换行符

searchContact(head, name);

break;

case 4:

displayContacts(head);

break;

case 5:

freeContacts(head);

printf("退出程序...\n");

return 0;

default:

printf("无效的选择。请重试。\n");

break;

}

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值