项目一——电子通讯录

需求
制作一个电子通讯录,通过该通讯录能存入好友ID号、姓名(英文)、手机号码、家庭住址、公司电话。
模块
 主界面:主要显示软件功能。
A) 添加好友信息。
B) 列表好友信息。(包含排序功能)
C) 搜索好友
D) 删除好友
 添加好友:
用户输入INSERT命令后,让用户输入好友信息。添加成功或失败都需要提示用户
 列表好友:
用户输入DISPLAY命令后,好友信息升序排列。
 搜索好友:
用户输入SEARCH命令后,让用户输入将要搜索好友姓名查询。如果未搜索到请友好提示。如果搜索到,显示出该好友信息。
 删除好友:
用户输入DELETE命令后,让用输入将要删除好友姓名删除,如果存在同名的

多个好友,则列表出,所有同名的好友信息,让用户通过输入ID号删除。提示用户删除成功。


LinkList.h

#ifndef __LINKLIST_H__
#define __LINKLIST_H__

#define FALSE 0
#define TRUE  1

typedef struct _linkdata
{
	int id;
	char name[20];
	char phone_tel[20];
	char address[100];
	char compony_tel[20];
}LinkData;

typedef struct _node
{
	LinkData data;
	struct _node *next;
}Node;

Node *Create_List();

int Insert_Last(Node *h,LinkData data);

int Find_Name(Node *h,char *name);

int Delete_Data(Node* h,char *name);

int Get_Len(Node * h);

void Display(Node *h);

void interface();

void write_data(Node* h);

void read_data(Node* h);

#endif //__LINKLIST_H__

LinkList.c

#include "LinkList.h"
#include <stdio.h>
#include <stdlib.h>

Node *Create_List()
{
	Node *head = (Node *)malloc(sizeof(Node) / sizeof(char));
	
	if(head == NULL)
		return NULL;
	
	head->next = NULL;
	
	return head;
}

int Insert_Last(Node *h,LinkData data)
{
	if(h == NULL)
		return FALSE;
	
	Node *node = (Node *)malloc(sizeof(Node) / sizeof(char));
	
	if (node == NULL)
	{
		return FALSE;
	}
	
	node->data = data;
	node->next = NULL;
	
	Node *tmp = h;
	
	while(tmp->next)
	{
		if(tmp->next->data.id == node->data.id)
		{
			printf("\t\t\tid已存在添加失败\n");
			return FALSE;
		}
		tmp = tmp->next;
	}
	
	tmp->next = node;
	
	printf("\t\t\t添加成功!\n");
	
	return TRUE;
}

int Find_Name(Node *h,char *name)
{
	if(h == NULL || name == NULL)
		return FALSE;
	
	Node *tmp = h->next;
	
	int count = 0;
	
	while(tmp)
	{
		if(strcmp(tmp->data.name,name) == 0)
		{
			printf("\t\t\t查找成功!\n");
			printf("\t\t\tid = %d,name = %s,phone_tel = %s,address = %s,compony_tel = %s\n",tmp->data.id,tmp->data.name,tmp->data.phone_tel,tmp->data.address,tmp->data.compony_tel);
			
			count++;
		}
		tmp = tmp->next;
	}
	
	if(count == 0)
	{
		printf("\t\t\t该好友不存在!\n");
		return FALSE;
	}
}

int Delete_Data(Node* h,char *name)
{
	if(h == NULL)
		return FALSE;
	
	Node *tmp = h;
	
	int count = 0;
	
	while(tmp->next)
	{
		if(strcmp(tmp->next->data.name,name) == 0)
		{
			printf("\t\t\tid = %d,name = %s,phone_tel = %s,address = %s,compony_tel = %s\n",\
			tmp->next->data.id,tmp->next->data.name,tmp->next->data.phone_tel,tmp->next->data.address,tmp->next->data.compony_tel);
			
			count++;			
		}
		tmp = tmp->next;
	}
	
	if(count == 0)
	{
		printf("\t\t\t该好友不存在!\n");
		return FALSE;
	}
	else if(count == 1)
	{
		tmp = h;
		
		while(tmp->next)
		{
			if(strcmp(tmp->next->data.name,name) == 0)
				break;
			tmp = tmp->next;
		}
	
		if(tmp->next == NULL)
			return FALSE;
	
		Node *p = tmp->next;
		tmp->next = p->next;
		free(p);
		
		printf("\t\t\t删除成功!\n");
	}
	else
	{
		int id;
		printf("\t\t\t该名字好友不只一个,请输入id号删除:");
		scanf("%d",&id);
		
		tmp = h;
		
		while(tmp->next)
		{
			if(strcmp(tmp->next->data.name,name) == 0 && tmp->next->data.id == id)
				break;
			tmp = tmp->next;
		}
	
		if(tmp->next == NULL)
			return FALSE;
	
		Node *p = tmp->next;
		tmp->next = p->next;
		free(p);
		
		printf("\t\t\t删除成功!\n");
	}
	
	return TRUE;
}

int Get_Len(Node * h)
{
	if (h == NULL)
		return 0;
	
	Node *tmp = h;
	int count = 0;
	while (tmp->next)
	{
		count++;
		tmp = tmp->next;
	}
	
	return count;
}

void Display(Node *h)
{
	if(h == NULL)
		return;
	
	int len = Get_Len(h);
	int i,j;
	
	Node *temp;
	for(i = 0;i < len - 1;i++ )
	{
		Node *start = h;
		Node *end = h->next;
		
		for(j = 0;j < len - i - 1;j++)
		{
			if(start->next->data.id > end->next->data.id)
			{
				temp = end->next->next;
				end->next->next = start->next;
				start->next = end->next;
				end->next = temp;
				start = end->next;
			}
			else
			{
				start = start->next;
				end = end->next; 
			}
		}
	}
	
	Node *tmp = h->next; 
	
	int count = 0;
	
	while(tmp)
	{
		printf("\t\t\tid = %d,name = %s,phone_tel = %s,address = %s,compony_tel = %s\n",\
		tmp->data.id,tmp->data.name,tmp->data.phone_tel,tmp->data.address,tmp->data.compony_tel);
		
		count++;
		
		tmp = tmp->next;
	}
	
	if(count == 0)
	{
		printf("\t\t\t通讯录好友为空!\n");
	}
}

void write_data(Node* h)
{
	FILE *fp = fopen("people", "wb");
	if (fp == NULL)
	{
		perror ("fopen");
		return;
	}
	
	int count = 0;
	Node *tmp = h->next;
	
	while(tmp)
	{
		count++;
		tmp = tmp->next;
	}
	
	tmp = h->next;
	
	fwrite(&count,sizeof(int),1,fp);
	
	int i;
	for(i = 0;i < count;i++)
	{
		int len = sizeof(tmp->data);
		fwrite(&len,sizeof(int),1,fp);
		
		fwrite(&(tmp->data),sizeof(LinkData),1,fp);
		
		tmp = tmp->next;
	}
	
	fclose(fp);
}

void read_data(Node* h)
{
	FILE *fp = fopen("people", "r");
	if (fp == NULL)
	{
		perror ("fopen");
		return;
	}
	
	int count;
	fread(&count,sizeof(int),1,fp);
	
	int i;
	LinkData tmp;
	
	for(i = 0;i < count;i++)
	{
		int len;
		fread(&len,sizeof(int),1,fp);
		
		fread(&tmp,len,1,fp);
		
		Insert_Last(h,tmp);
	}
	
	fclose(fp);
}

void interface()
{
	system("clear");
	printf("\t\t\t|***********************************************|\n");
	printf("\t\t\t|                                               |\n");
	printf("\t\t\t|                电 子 通 讯 录                 |\n");
	printf("\t\t\t|                                               |\n");
	printf("\t\t\t|***********************************************|\n");
	printf("\t\t\t|    1) 添加好友信息   ||    2) 列表好友信息    |\n");
	printf("\t\t\t|***********************************************|\n");
	printf("\t\t\t|    3) 搜索好友       ||    4) 删除好友        |\n");
	printf("\t\t\t|***********************************************|\n");
	printf("\t\t\t|                    5)退出                     |\n");
	printf("\t\t\t|***********************************************|\n\n");
	printf("\t\t\t请输入你的操作(1-5):");	
}

main.c

#include "LinkList.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	Node *head = Create_List();
	
	if(head == NULL)
	{
		printf("创建失败\n");
		return -1;
	}
	
	read_data(head);
	
	while(1)
	{
		interface();
		
		int choice = 0;
		int num;
		char ch;
						
		scanf("%d",&choice);

		if(choice == 1)//添加联系人
		{
			LinkData data;
			int id;
			char *name = (char *)malloc(sizeof(char)*20);
			char *phone_tel = (char *)malloc(sizeof(char)*20);
			char *address = (char *)malloc(sizeof(char)*20);
			char *compony_tel = (char *)malloc(sizeof(char)*20);
	
			while(1)
			{
				
				printf("\t\t\t联系人id:");
				scanf("%d",&id);
				printf("\t\t\t联系人姓名:");
				scanf("%s",name);
				printf("\t\t\t手机号码:");
				scanf("%s",phone_tel);
				printf("\t\t\t地址:");
				scanf("%s",address);
				printf("\t\t\t公司电话:");
				scanf("%s",compony_tel);
				
				data.id = id;
				strcpy(data.name,name);
				strcpy(data.phone_tel,phone_tel);
				strcpy(data.address,address);
				strcpy(data.compony_tel,compony_tel);
				
				Insert_Last(head,data);
				
				printf("\t\t\t继续添加输入1,返回主界面输入2:");
				scanf("%d",&num);
				
				if(num == 2)
					break;
				else
					continue;
			}
		}
		
		else if(choice == 2)//显示好友
		{
			Display(head);
			
			printf("\t\t\t返回主界面输入2:");
			scanf("%d",&num);
			
			if(num == 2)
				continue;
		}
		
		else if(choice == 3)//查找好友
		{
			while(1)
			{
				char *name = (char *)malloc(sizeof(char)*20);
				
				printf("\t\t\t请输入要查找的姓名:");
				scanf("%s",name);

				Find_Name(head,name);
				
				printf("\t\t\t继续查找输入1,返回主界面输入2:");
				scanf("%d",&num);
				
				if(num == 2)
					break;
				else
					continue;
			}		
		}
		
		else if(choice == 4)//删除好友
		{
			while(1)
			{
				char *name = (char *)malloc(sizeof(char)*20);
				
				printf("\t\t\t请输入要删除的姓名:");
				scanf("%s",name);

				Delete_Data(head,name);
				
				printf("\t\t\t继续删除输入1,返回主界面输入2:");
				scanf("%d",&num);
				
				if(num == 2)
					break;
				else
					continue;
			}		
		}
		
		else if(choice == 5)
		{
			write_data(head);
			break;
		}
		
		else
		{
			printf("\t\t\t输入错误,请输入1-5的数字\n");
			printf("\t\t\t输入回车返回主界面\n");
			scanf("%c",&ch);
			scanf("%c",&ch);
		}
	}
	   
	return 0;
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值