用结构体实现通讯录

问题描述:

实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址

提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息

6. 清空所有联系人信息

头文件:"contacts.h"

#ifndef __CONTACT_H__//防止内容的重复
#define __CONTACT_H__
//工程中用到的函数库
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//关于字符串的各种宏替换
#define MAX 1000
#define NAME_SIZE 20
#define SEX_SIZE 10
#define TELE_SIZE 11
#define ADDR_SIZE 20
//描述通讯录信息的结构体
struct personinfor
{
	char name[NAME_SIZE];
	char sex[SEX_SIZE];
	int age;
	char tele[TELE_SIZE];
	char addr[ADDR_SIZE];
};

typedef struct personinfor person;

struct contacts
{
	person person[MAX];            //指向信息结构体的指针
	int contact_count;             //统计通讯录中的元素个数
};

typedef struct contacts * pcontacts;

int add_contact(pcontacts pcon);   //添加联系人
int delete_contact(pcontacts pcon);//删除指定联系人
int find_contact(pcontacts pcon);  //查找指定联系人
int modify_contact(pcontacts pcon);//修改指定联系人
int clear_contact(pcontacts pcon); //清空所有联系人
void show_contact(pcontacts pcon); //显示所有联系人

#endif<span style="color:#ff0000;">

</span>
具体函数实现“contacts.c”
#include"contacts.h"
/*
寻找指定联系人的位置:
如果找到:返回该联系人的在数组中的下标
没有找到:返回-1;
*/
int find_entry(pcontacts pcon)
{
	char name[NAME_SIZE];
	int i = 0;

	if(pcon->contact_count == 0)
	{
		printf("the contacts is empty!\n");
		return -1;
	}

	printf("please input a name:");
	scanf("%s",name);

	for(i = 0;i<pcon->contact_count; ++i)
	{
		if(strcmp(name,pcon->person[i].name) == 0)
		{
			return i;
		}
	}
	return -1;
}
//增加联系人
int add_contact(pcontacts pcon)
{
	int input = 1;
	if(pcon->contact_count == MAX)
	{
		printf("the contacts is full!\n");
		return -1;
	}
		printf("please input a person's information according the rompt:\n");
		printf("name:");
		scanf("%s",pcon->person[pcon->contact_count].name);

		printf("sex:");
		scanf("%s",pcon->person[pcon->contact_count].sex);

		printf("age:");
		scanf("%d",&pcon->person[pcon->contact_count].age);

		printf("tele:");
		scanf("%s",pcon->person[pcon->contact_count].tele);

		printf("addr:");
		scanf("%s",pcon->person[pcon->contact_count].addr);
		pcon->contact_count++;

//		printf("do you want to continue ? 1:yes  0:no");
//		scanf("%d",&input);

//	}
}

int delete_contact(pcontacts pcon)
{
	int ret = find_entry(pcon);
	int i = 0;

	if(pcon->contact_count == 0)
	{
		printf("the contacts is empty!\n");
		return -1;
	}
	if(ret == -1)
	{
		printf("the contact is not exsit!\n");
	}

	for(i=ret;i<pcon->contact_count-1;++i)//注意i < (pcon->contact_count-1)而不是(pcon->contact_count)
	{
		pcon->person[i]=pcon->person[i+1];//否则越界
	}
	pcon->contact_count--;
	return 1;

}
int find_contact(pcontacts pcon)
{
	int ret = find_entry(pcon);
	if(ret!= -1)
	{
		printf("%s\t%s\t%d\t%s\t%s\n",pcon->person[ret].name,pcon->person[ret].sex,pcon->person[ret].age,pcon->person[ret].tele,pcon->person[ret].addr);
		return 1;
	}
	else
	{
		printf("not exsit\n");
		return 0;
	}

}
//修改指定联系人
int modify_contact(pcontacts pcon)
{
	int ret = find_entry(pcon);
	if(ret == -1)
	{
		printf("the contact is not exsit!\n");
		return 0;
	}
	printf("please input the new infor according the rompt:\n");
	printf("name:");
	scanf("%s",pcon->person[ret].name);
	printf("sex:");
	scanf("%s",pcon->person[ret].sex);
	printf("age:");
	scanf("%d",&pcon->person[ret].age);
	printf("tele:");
	scanf("%s",pcon->person[ret].tele);
	printf("addr:");
	scanf("%s",pcon->person[ret].addr);
	return 1;
}
//清空联系人
int clear_contact(pcontacts pcon)
{
	pcon->contact_count = 0;
	return 1;
}
//显示所有联系人
void show_contact(pcontacts pcon)
{
	int i = 0;
	if(pcon->contact_count == 0)
	{
		printf("the contacts is empty!\n");
		return -1;
	}

	printf("the contacts' infor are as following:\n ");
	for(i=0;i<pcon->contact_count;++i)
	{
		printf("%10s%5s%10d%10s%10s\n",pcon->person[i].name,pcon->person[i].sex,pcon->person[i].age,pcon->person[i].tele,pcon->person[i].addr);
		
	}
}
//菜单函数:使上述各个功能能够多次调用
int meau()
{
	int n;
	printf("*******欢迎使用通讯录管理系统********\n");
	printf("                               实现者:zyh_helen\n");
	printf("1.添加联系人\n");
	printf("2.删除指定联系人\n");
	printf("3.查找指定联系人\n");
	printf("4.修改指定联系人\n");
	printf("5.显示所有联系人\n");
	printf("6.清空联系人\n");
	printf("7.退出\n");
	printf("选择您所需要的功能序号1~7:\n");
	scanf("%d",&n);
	return n;
	<span style="color:#ff0000;">

}</span>
主函数:main.c
#include"contacts.h"
int main()
{
	struct contacts cont;
	pcontacts p=&cont;
	int n = 0;
	p->contact_count = 0;
	while(1)
	{
		n=meau();
		switch(n)
		{
			case 7:
				exit(0);
			case 1:
				add_contact(p);
				break;
			case 2:
				delete_contact(p);
				break;
			case 3:
				find_contact(p);
				break;
			case 4:
				modify_contact(p);
				break;
			case 5:
				show_contact(p);				
				break;
			case 6:
				clear_contact(p);				
				break;
			default:
				break;
		}

	}
	return 0;
}
调用结果显示



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。下面是示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 20 #define MAX_PHONE_LEN 15 // 定义通讯录结构 typedef struct _Contact { char name[MAX_NAME_LEN]; char phone[MAX_PHONE_LEN]; struct _Contact* next; } Contact; // 定义通讯录链表头 Contact* head = NULL; // 函数声明 void add_contact(); void delete_contact(); void display_contact(); void search_contact(); void save_contact(); void load_contact(); int main() { int choice; do { printf("\n"); printf("1. Add contact\n"); printf("2. Delete contact\n"); printf("3. Display contacts\n"); printf("4. Search contact\n"); printf("5. Save contacts to file\n"); printf("6. Load contacts from file\n"); printf("0. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: add_contact(); break; case 2: delete_contact(); break; case 3: display_contact(); break; case 4: search_contact(); break; case 5: save_contact(); break; case 6: load_contact(); break; case 0: break; default: printf("Invalid choice!\n"); break; } } while (choice != 0); return 0; } // 添加通讯录记录 void add_contact() { Contact* new_contact = (Contact*) malloc(sizeof(Contact)); printf("Enter name: "); scanf("%s", new_contact->name); printf("Enter phone number: "); scanf("%s", new_contact->phone); new_contact->next = head; head = new_contact; printf("Contact added successfully!\n"); } // 删除通讯录记录 void delete_contact() { char name[MAX_NAME_LEN]; printf("Enter name to delete: "); scanf("%s", name); Contact* curr_contact = head; Contact* prev_contact = NULL; while (curr_contact != NULL && strcmp(curr_contact->name, name) != 0) { prev_contact = curr_contact; curr_contact = curr_contact->next; } if (curr_contact == NULL) { printf("Contact not found!\n"); } else { if (prev_contact == NULL) { head = curr_contact->next; } else { prev_contact->next = curr_contact->next; } free(curr_contact); printf("Contact deleted successfully!\n"); } } // 显示所有通讯录记录 void display_contact() { Contact* curr_contact = head; if (curr_contact == NULL) { printf("No contacts found!\n"); } else { while (curr_contact != NULL) { printf("%s\t%s\n", curr_contact->name, curr_contact->phone); curr_contact = curr_contact->next; } } } // 搜索通讯录记录 void search_contact() { char name[MAX_NAME_LEN]; printf("Enter name to search: "); scanf("%s", name); Contact* curr_contact = head; while (curr_contact != NULL && strcmp(curr_contact->name, name) != 0) { curr_contact = curr_contact->next; } if (curr_contact == NULL) { printf("Contact not found!\n"); } else { printf("%s\t%s\n", curr_contact->name, curr_contact->phone); } } // 将通讯录记录保存到文件中 void save_contact() { char filename[MAX_NAME_LEN]; printf("Enter filename to save: "); scanf("%s", filename); FILE* fp = fopen(filename, "w"); if (fp == NULL) { printf("File open failed!\n"); return; } Contact* curr_contact = head; while (curr_contact != NULL) { fprintf(fp, "%s\t%s\n", curr_contact->name, curr_contact->phone); curr_contact = curr_contact->next; } fclose(fp); printf("Contacts saved successfully!\n"); } // 从文件中加载通讯录记录 void load_contact() { char filename[MAX_NAME_LEN]; printf("Enter filename to load: "); scanf("%s", filename); FILE* fp = fopen(filename, "r"); if (fp == NULL) { printf("File open failed!\n"); return; } char name[MAX_NAME_LEN]; char phone[MAX_PHONE_LEN]; while (fscanf(fp, "%s\t%s\n", name, phone) != EOF) { Contact* new_contact = (Contact*) malloc(sizeof(Contact)); strcpy(new_contact->name, name); strcpy(new_contact->phone, phone); new_contact->next = head; head = new_contact; } fclose(fp); printf("Contacts loaded successfully!\n"); } ``` 这是一个基本的通讯录程序,使用了结构存储通讯录记录信息,使用链表实现通讯录信息的增删及查询显示,使用文件储存通讯录。你可以根据需要进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值