复习之 c实现 通讯录

实验成果截图

实验的源码附上:(编译机是vm)

main.c

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

/*void print(e)
{
	printf("%d ",e);
}
*/
int main()
{	
	linklist list;
	int ret,i,j;
//	char name[10];
//	int number,tele,wtele;
//	char address[100];
	int e;
	char buf[100] = {0};
	ret = listinit(&list);//初始化联系人表单
	if(ret == success)
	{
		printf("contacter init successful!\n");
	}
	FILE *fp;
	fp=fopen("cunchu.txt","r");//打开文件,把原来写入的缓存起来
	if(fp != NULL)
	{
	//	fseek(fp,0,SEEK_SET);
		while(!feof(fp))
		{
			linklist new;
			linklist loop = list;
			new=(node *)malloc(sizeof(node));
			while(loop->next != NULL)
			{
				loop=loop->next;
			}

			fgets(buf,sizeof(buf)-1,fp);
			if(feof(fp))
			{
				break;
			}
			new->number=buf[0]-'0';
			memset(buf,'\0',sizeof(buf));

			fgets(buf,sizeof(buf)-1,fp);
			strncpy(new->name,buf,strlen(buf)-1);
			memset(buf,'\0',sizeof(buf));

			fgets(buf,sizeof(buf)-1,fp);
			strncpy(new->tele,buf,strlen(buf)-1);
			memset(buf,'\0',sizeof(buf));

			fgets(buf,sizeof(buf)-1,fp);
			strncpy(new->wtele,buf,strlen(buf)-1);
			memset(buf,'\0',sizeof(buf));

			fgets(buf,sizeof(buf)-1,fp);
			strncpy(new->address,buf,strlen(buf)-1);
			memset(buf,'\0',sizeof(buf));

			loop->next=new;
			new->next=NULL;
		}
		fclose(fp);
	}

	sleep(1);
	system("clear");
	printf("******************************************\n");
	printf("          welcome to you contacter\n");
	printf("******************************************\n");
	sleep(1);
	while(1)
	{
		printf("******************************************\n");
		printf(" 1.input       2.listall    3.find(name)  \n");
		printf(" 4.find(tele)  5.delete     6.modifi(tele)\n");
		printf(" 7.modifi(wtele)            8.quit\n");
		printf("******************************************\n");
		printf("        please choose you function\n");
		printf("******************************************\n");
		scanf("%d",&j);
	
		if(j == 1)//插入
		{
			ret = listinsert(list);
			if(ret == success)
			{
				printf("input successful!~\n");
			}
			else if(ret == -2)
			{
				printf("the number of tele/wtele is wrong!~\n");
			}
			else
			{
				printf("input failed~\n");
			}
			continue;
		}
		else if(j == 2)//遍历
		{
			ret = listtraver(list);
			if( ret == success)
			{
				printf("\n");
				printf("this is all infor!~\n");
			}
			continue;
		}
		else if(j == 3)//查找名字
		{	
			ret = find1(list);
			if( ret == success)
			{
				printf("find it!~\n");
			}
			continue;
		}
		else if(j == 4)//查找电话
		{	
			ret = find2(list);
			if( ret == success)
			{
				printf("find it!~\n");
			}
			continue;
		}
		else if(j == 5)//删除
		{
			ret = listdelete(list);
			if(ret == success)
			{
				printf("delete successful!~\n");
			}
			else
			{
				printf("delete failed!~\n");
			}
			continue;
			
		}
		else if(j == 6)//修改信息
		{
			ret = modifi1(list);
			if(ret == success)
			{
				printf("modifi successful!~\n");
			}
			else if(ret == -2)
			{
				printf("the new tele is wrong!\n");
			}
			else
			{
				printf("modifi failed!~\n");
			}
			continue;
			
		}
		else if(j == 7)
		{
			ret = modifi2(list);
			if(ret == success)
			{
				printf("modifi successful!~\n");
			}
			else if(ret == -2)
			{
				printf("the new wtele is wrong!\n");
			}
			else
			{
				printf("modifi failed!~\n");
			}
			continue;
		}	
		else
		{
		//	quit();
			fp=fopen("cunchu.txt","w");//将信息写入文本文件
			if(fp == NULL)
			{
				perror("fopen");
				exit(1);
			}
			while(list->next != NULL)
			{
				list = list->next;
				sprintf(buf,"%d\n%s\n%s\n%s\n%s\n",list->number,list->name,list->tele,list->wtele,list->address);
				fwrite(buf,1,strlen(buf),fp);
			}
			fclose(fp);
			exit(0);
		}
 	}

/*
	for(i = 0; i < 5; i++)
	{
		ret = listinsert(list,i+1,i);
	}
	
	ret = listlength(list);
	printf("the length of list is: %d\n",ret);
	
	ret = listtraver(list,print);
	if( ret == success)
	{
		printf("\n");
		printf("traver successful!~\n");
	}

	ret = listdelete(list,2,&e);
	if(ret == success)
	{
		printf("delete %d successful!~\n",e);
	}
	else
	{
		printf("delete failed!~\n");
	}

	ret = listtraver(list,print);
	if( ret == success)
	{
		printf("\n");
		printf("traver successful!~\n");
	}

	ret = listdeleteall(list);
	if(ret == success)
	{
		printf("delete all successful!~\n");
	}
	else
	{
		printf("delete failed!~\n");
	}

	ret = listtraver(list,print);
	if( ret == success)
	{
		printf("\n");
		printf("traver successful!~\n");
	}
	
	ret = destory(&list);
	if(ret == success)
	{
		printf("destroy successful!~\n");
	}
	else
	{
		printf("destory failed!~\n");
	}

	ret = listinsert(list,1,1);
	if( ret == success)
	{
		printf("\n");
		printf("insert successful!~\n");
	}

	ret = listtraver(list,print);
	if( ret == success)
	{
		printf("\n");
		printf("traver successful!~\n");
	}*/
	return 0;
}

phone.c

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

node cun[100];
int count = 0;

int listinit(linklist *l)//初始化链表
{
	(*l) = (linklist)malloc(sizeof(node));//c中用malloc分配空间
	if(l == NULL)//这叫啥来着
	{
		return failed;
	}
	(*l)->next = NULL;
/*	FILE *fp;
	fp = fopen("cun.txt" , "r");
	if(fp == NULL)
	{
		perror("fopen");
		exit(1);
	}
	while(fread(buf , 1 , 32 , fp))
	{
		printf("%s",buf);
		memset(buf , 0 , sizeof(buf));
	}

	fclose(fp);*/
	return success;//初始化成功
}

int listinsert(linklist l)//插入功能
{
	char name[10];
	int number;
	char tele[50];
	char wtele[50];
	char address[100];
	linklist p = l;
	int j = 1;
	if(l == NULL)//空表无法插入
	{
		return failed;
	}
	l = (linklist)malloc(sizeof(node));//为它分配结构体大小的空间
	if(l == NULL)
	{
		return failed;
	}

	printf("input number name tele wtele address\n");
	scanf("%d %s %s %s %s",&number,name,tele,wtele,address);
	if(strlen(tele) != 11)
	{
		return -2;
	}
	if(strlen(wtele) != 8)
	{
		return -2;
	}

	l->number = number ;//将所有数据赋值存储进表
	strcpy(l->tele,tele);
	strcpy(l->wtele,wtele);
	strcpy(l->name,name);
	strcpy(l->address,address);
	l->next = p->next;
	p->next = l;
	cun[count].number = number;
	strcpy(cun[count].tele,tele);
	strcpy(cun[count].wtele,wtele);
	strcpy(cun[count].name,name);
	strcpy(cun[count].address,address);
	count++;
	return success;

}
int listtraver(linklist l)//遍历表
{
	linklist p = l;
	if(p == NULL)
	{
		return failed;
	}
	while(p->next)
	{
		p = p->next;
		printf("%d %s %s %s %s %s\n",p->number,p->name,p->tele,p->wtele,p->address);

	}
	return success;
}

int find1(linklist l)//找人
{
	linklist p = l;
	char a[50];
	if(p == NULL)
	{
		return failed;
	}
	printf("input the name of user~\n");
	scanf("%s",a);
	while(p->next)
	{
		p = p->next;
		if(!strcmp(p->name,a))
		{
			printf("number is :%d\n",p->number);
			printf("name is :%s\n",p->name);
			printf("tele is :%s\n",p->tele);
			printf("wtele is :%s\n",p->wtele);
			printf("address is :%s\n",p->address);
			return success;
		}

	}	
	return failed;
}
int find2(linklist l)//找电话
{
	linklist p = l;
	char a[50];
	if(p == NULL)
	{
		return failed;
	}
	printf("input the tele of user~\n");
	scanf("%s",a);
	while(p->next)
	{
		p = p->next;
		if(!strcmp(p->tele,a))
		{
			printf("number is :%d\n",p->number);
			printf("name is :%s\n",p->name);
			printf("tele is :%s\n",p->tele);
			printf("wtele is :%s\n",p->wtele);
			printf("address is :%s\n",p->address);
			return success;
		}
	}
	return failed;
}

int listdelete(linklist l)//删除
{
	int i;
	linklist p = l;
	linklist loop = l;
	if(l == NULL)
	{
		return failed;
	}
	char a[50];
	int count = 0;
	printf("input the name of user~\n");
	scanf("%s",a);
	while(p->next)
	{
		p = p->next;
		count++;
		if(!strcmp(p->name,a))
		{
			for(i = 0; i < count; i++)
			{
				loop->next = p->next;
				free(p);
				return success;
			}
		}
	}
	return failed;
}


int modifi1(linklist l)//改电话
{
	linklist p = l;
	if(l == NULL)
	{
		return failed;
	}
	char a[20];
	char b[20];
	printf("input the tele of user~\n");
	scanf("%s",a);
	while(p->next)
	{
		p = p->next;
		if(!strcmp(p->tele,a))
		{
			printf("input the new tele of user~\n");
			scanf("%s",b);
			if(strlen(b) == 11)
			{
				strcpy(p->tele,b);
				return success;
			}
			else
			{
				return -2;
			}
		}
	}
}
int modifi2(linklist l)//改家庭电话
{
	linklist p = l;
	if(l == NULL)
	{
		return failed;
	}
	char a[20];
	char b[20];
	printf("input the wtele of user~\n");
	scanf("%s",a);
	while(p->next)
	{
		p = p->next;
		if(!strcmp(p->wtele,a))
		{
			printf("input the new wtele of user~\n");
			scanf("%s",b);
			if(strlen(b) == 8)
			{
				strcpy(p->wtele,b);
				return success;
			}
			else
			{
				return -2;
			}
		}
	}
}
void quit()//退出
{
//	int i ;
//	for(i = 0 ; i <= count ; i++)
//	{
		FILE *fp = fopen("cunchu.txt" , "w+");
		if(fp == NULL)
		{
			perror("fopen");
			exit(1);
		}
		fwrite((cun), sizeof(node) , 1 ,fp);
		fclose(fp);
//	}
}
/*int listdeleteall(linklist l)
{
	linklist p = l->next;
	while(p)
	{
		l->next  = p->next;
		free(p);
		p = l->next;
	}
	return success;
}*/

/*int destory(linklist *l)
{
	free(*l);
	*l = NULL;
	return success;
}
int listlength(linklist l)
{
	linklist p = l;
	int i = 0;
	while(p->next)
	{
		p = p->next;
		i++;
	}
	return i;
}*/

phone.h

#ifndef _linklist_
#define _linklist_

#define success 0
#define failed -1


struct Node//题目要求的结构体
{
	int number;//工号
	char tele[50];//电话号码
	char wtele[50];//办公电话
	char name[10];//姓名
	char address[100];//地址
	struct Node *next;//指针
};

typedef struct Node node;
typedef node* linklist;

//所有的函数声明
int listinit(linklist *l);
int listinsert(linklist l);
//int listtraver(linklist l,void(*visit)(char));
int listtraver(linklist l);
int find1(linklist l);
int find2(linklist l);
int listdelete(linklist l);
int modifi1(linklist l);
int modifi2(linklist l);

//int listlength(linklist l);
//int listdeleteall(linklist l);
//int detory(linklist *l);


#endif

就是第一个输出的时候有乱码 不知道是不是\0的问题,啊哈哈

猜你喜欢

转载自blog.csdn.net/qq_38313246/article/details/81235657
今日推荐