回顾并谈一谈之前写过的通讯录小项目

本文介绍了一个使用单链表实现的通讯录系统,包括增删改查等基本功能,并利用文件IO技术实现了信息的持久化存储。系统提供友好的用户交互界面,方便用户进行各种操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通讯录项目我是用单链表写的,这个项目整体比较简单,除了一些基本的增、删、改、查等功能外,由于后面学到了文件IO和标准IO方面的知识

我又添加了保存信息和下载信息两个功能通过fopen函数创建文件,fwrite将信息写入文件,fread从文件中读取信息输出打印

头文件代码:

#ifndef   _STRUCT_H_
#define   _STRUCT_H_

#define ERROR        1000001
#define MALLOC_ERROR 1000002
#define OK           1000003

typedef char ElemType;

struct address
{
	int id;
	ElemType name[10];
	ElemType homeadd[20];
	ElemType telephone[20];
	ElemType homephone[20];

	struct address *next;
};

typedef struct address Add;
typedef Add *add;


int Add_contact(add L);
void List_contact(add L);
add Find_contact(add L,ElemType *a);
int Delete_contact(add L,ElemType *b);
void Change_contact(add L,ElemType *c);
void Save_contact(add L);
void Load_contact(add L);
#endif

主函数文件代码:

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



int main()
{

	char choice[10] = {0};
	char name[10] = {0};

	show();
    
	add user = (add)malloc(sizeof(Add));
    if(user == NULL)
	{
		return ERROR;
	}
	user->next = NULL; //设置空链表
	
    while(1)
	{   PrintInfo();
		scanf("%s",choice);

		switch(atoi(&choice[0]))
		{
			case 1:
				printf("\n");
				printf("Please input friends infomation:\n");
				if(Add_contact(user) != OK)
				{
					return ERROR;
				
				}
				printf("\n Friends infomation add finish!\n");
				break;
			case 2:
				List_contact(user);
				break;
			case 3:
				printf("Please input the name you want to find:\n ");
				scanf("%s",name);
			
				add P = Find_contact(user,name);
				if (NULL == P)
				{
					printf("Sorry,there is no this friend!\n");
				}
				else
				{
					printf("id:%d  name:%s  homeadd:%s  telephone:%s  homephone:%s",P->id,P->name,P->homeadd,P->telephone,P->homephone);
				}

				break;
			case 4:
				printf("Please input the name you want to delete:\n");
				scanf("%s",name);
			if(	Delete_contact(user,name) != OK)
			{
				printf("DELETE ERROR!\n");
				return ERROR;
			}
			printf("DELETE SUCCESS!\n");
			List_contact(user);
				break;
			case 5:
				printf("Please input the whose information you want to change:\n");
				scanf("%s",name);
				Change_contact(user,name);
				break;
			case 6:
				Save_contact(user);
				printf("Save Success!\n");
				break;
			case 7:
				Load_contact(user);
				printf("Load Success!\n");
				break;
			case 8:
				exit(0);
			default:
				printf("unkown input!\n");
				break;
		}

	}


	return 0;
}

接口函数文件代码:

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

void show()
{
	system("clear");
	printf("**************************************************\n\n");
	printf("********WELCOME TO ADDRESS LIST SYSTEM************\n\n");
	printf("**************************************************\n\n");
	sleep(2);
	system("clear");
}


void PrintInfo()
{
	printf("****************************************************************\n\n");
	printf("****1.Add user infomation        2.List friends infomation******\n");
	printf("****3.Find friends infomation    4.Delete friends infomation****\n");
	printf("****5.Change friends infomation  6.Save the information*********\n");
	printf("****7.Load the information       8.exit*************************\n");
	printf("****************************************************************\n\n");
	printf("Please input your choice :\n");
}

int Add_contact(add L)
{
	add p1 = L->next;
	add temp = L;
	if(L == NULL)
	{
		return ERROR;
	}
	add p = (add)malloc(sizeof(Add));
	if (p == NULL)
	{
		return MALLOC_ERROR;
	}

	printf("Please input id :\n");
	scanf("%d",&p->id);
	printf("Please input name:\n");
	scanf("%s",p->name);
	while(p1)
	{
		if(strcmp(p->name,p1->name) > 0)
		{
			temp = p1;
			p1 = p1->next;
		}
		else
		{
			break;
		}
	}
	p->next = p1;
	temp->next = p;
		
	printf("Please input homeadd:\n");
	scanf("%s",p->homeadd);
	printf("Please input telephone:\n");
	scanf("%s",p->telephone);
	while(strlen(p->telephone) != 11)
	{
		printf("Please input 11 numbers:\n");
		scanf("%s",p->telephone);
	}

	printf("Please input homephone:\n");
	scanf("%s",p->homephone);
	while(strlen(p->homephone) != 8)
	{
		printf("Please input 8 numbers:\n");
		scanf("%s",p->homephone);
	}

	
	return OK;
}

void List_contact(add L)
{
	if(L == NULL)
	{
		printf("List ERROR!\n");
		return ;
	}

	add p = L->next;
	while(p)
	{
		printf("id:%d  name:%s  homeadd:%s  telephone:%s  homephone:%s\n",p->id,p->name,p->homeadd,p->telephone,p->homephone);
		p = p->next;
	}
}

add Find_contact(add L,ElemType *a)
{
	if (NULL == L)
	{
		printf("Find ERROR!\n");
		return NULL ;
	}
	
	add p = L->next;
        while(p)
	{
		if(strcmp(p->name,a) == 0)
		{
			return p;
		}
		p = p->next;
	}
		return NULL;
	

}
int Delete_contact(add L, ElemType *b)
{
	add temp;
	if (NULL == L)
	{
		return ERROR;
	}
    
	add p = L;
	
	while(p->next)
	{
		if (strcmp(p->next->name,b) == 0)
		{
			temp = p->next;
			p->next = temp->next;
			free(temp);
			return OK;
		}
		p = p->next;
	}	

		return ERROR;

}

void Change_contact(add L,ElemType *c)
{
	if(L == NULL)
	{
		printf("CHANGE ERROR");
		return ;
	}

	add p = L->next;
	while(p)
	{
		if (strcmp(p->name,c) == 0)
		{
			printf("id : ");
			scanf("%d",&p->id);
			printf("name : ");
			scanf("%s",p->name);
			printf("homeadd : ");
			scanf("%s",p->homeadd);
			printf("telephone : ");
			scanf("%s",p->telephone);
			printf("homephone : ");
			scanf("%s",p->homephone);
			
		}
		p = p->next;
	}

}

void Save_contact(add L)
{
	FILE *fp;
	fp = fopen("contact.txt","w");
	if (fp == NULL)
	{
		perror("fopen");
		exit(1);
	}
	add p = L->next;
	while(p)
	{
		fwrite(p,1,sizeof(Add),fp);
		p = p->next;
	}
	getchar();
	fclose(fp);
}

void Load_contact(add L)
{
	add p = L;
	L->next = NULL;
	add temp = L;

	FILE *fp;
	fp  = fopen("contact.txt","r");
	if (fp == NULL)
	{
		perror("fopen");
		exit(1);
	}

	while(feof(fp) == 0)
	{
		p = (add)malloc(sizeof(Add));
		if (p == NULL)
		{
			printf("Malloc Failure!\n");
			return ;
		}
		if (fread(p,1,sizeof(Add),fp) == 0)
		{
			
			break;
		}
		else
		{
			p->next = NULL;
			temp->next = p;
			temp = p;

		}


	}
	getchar();
	fclose(fp);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值