C語言通訊錄管理系統【沒看】

實現了通訊錄的錄入信息、保存信息、插入、刪除、排序、查找、單個顯示等功能。。

完整的代碼如下:


#include <stdio.h>
#include <malloc.h>  //得到指向大小爲Size的內存區域的首字節的指針//
#include <string.h>
#include <stdlib.h>  //標準庫函數// 
#define NULL 0
#define LEN sizeof(struct txlproject)  //計算字節//
int n;
struct txlproject
{
	char name[30];     //名字
	char work[30];     //職業
	char handset[30];  //手機
	char email[30];    //電子郵件
	char address[30];  //通訊地址
	struct txlproject *next;
};
struct txlproject *shifang(struct txlproject *head); // 釋放內存函數聲明
//創建函數,不帶頭結點的鏈表
struct txlproject *creat(void)       
{
	struct txlproject *head,*p1,*p2;
	char name[20];
	n=0;
	p1=(struct txlproject *)malloc(LEN);
	p2=p1;   //強制內存轉換
	printf("請輸入通訊錄的內容!/n姓名輸入爲0時表示創建完畢!/n");
	printf("請輸入姓名:");
	gets(name);
	if(strcmp(name,"0")!=0)
	{
		strcpy(p1->name,name);
		printf("請輸入職業:");     gets(p1->work);
		printf("請輸入手機:");     gets(p1->handset);
		printf("請輸入電子郵件:"); gets(p1->email);
		printf("請輸入通訊地址:");  gets(p1->address);
		head=NULL;
		while(1)
		{
			n=n+1;   //記錄通訊錄人數個數
			if(n==1)
				head=p1;
			else
				p2->next=p1;
			p2=p1;
			printf("請輸入姓名:");
			gets(name);
			if(strcmp(name,"0")==0)
			{
				break;
			}
			else
			{
				p1=(struct txlproject *)malloc(LEN);
				strcpy(p1->name,name);
				printf("請輸入職業:"); gets(p1->work);
				printf("請輸入手機:"); gets(p1->handset);
				printf("請輸入電子郵件:"); gets(p1->email);
				printf("請輸入通訊地址:");  gets(p1->address);
			}
		}
		p2->next=NULL;
		return head;
	}
	else
		return 0;
}
//輸出函數
void print(struct txlproject *head)   
{
	struct txlproject *p;
	if(head!=NULL)
	{
		p=head;
		printf("本通訊錄現在共有%d人:/n",n);
		printf("---姓名-------職業--------手機-------Email-------通訊地址/n");
		printf("==================================/n");
		do
		{
			printf("== %s",p->name); printf("       ");
			printf("%s",p->work); printf("       ");
			printf("%s",p->handset); printf("       ");
			printf("%s",p->email); printf("       ");
			printf("%s",p->address); printf("       /n");
			p=p->next;
		}while(p!=NULL);
		printf("==================================/n");
	}
	else
		printf("通訊錄爲空,無法輸出!/n");
}
//增加函數
struct txlproject *insert(struct txlproject *head) 
{
	struct txlproject *p0,*p1,*p2;
	char name[20];
	p1=head;
	printf("請輸入增加的內容:/n");
	printf("請輸入姓名:"); gets(name);
	if(strcmp(name,"0")==0)
	{
		printf("姓名不能爲0,增加失敗!/n");
		return(head);
	}
	else
	{
		p0=(struct txlproject *)malloc(LEN);
		strcpy(p0->name,name);
		printf("請輸入職業:"); gets(p0->work);
		printf("請輸入手機:"); gets(p0->handset);
		printf("請輸入電子郵件:"); gets(p0->email);
		printf("請輸入通訊地址:");  gets(p0->address);
		n=n+1;
		if(head==NULL)
		{
			head=p0;
			p0->next=NULL;
			return head;
		}
		else
		{
			while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL))
			{
				p2=p1;
				p1=p1->next;
			}
			if(strcmp(p0->name,p1->name)<0 || strcmp(p0->name,p1->name)==0)
			{
				if(head==p1)
				{
					head=p0;
				}
				else
				{
					p2->next=p0;
				}
				p0->next=p1;
			}
			else
			{
				p1->next=p0;
				p0->next=NULL;
			}
			return head;
		}
	}
}
struct txlproject *delete(struct txlproject *head)
{
	struct txlproject *p,*q;
	char name[30];
	if(head==NULL)
	{
		printf("通訊錄爲空,無法顯示!/n");
		return head;
	}
	p=head;
	printf("請輸入需要刪除的人的姓名:");
	gets(name);
	if(strcmp(head->name,name)==0)
	{
		head=head->next;
		free(p);
		printf("刪除操作成功!/n");
		return head;
	}
	else
	{
		q=head,p=head->next;
		while(p!=NULL)
		{
			if(strcmp(p->name,name)==0)
			{
				q->next=p->next;
				free(p);
				printf("刪除操作成功!/n");
				return head;
			}
			p=p->next;
			q=q->next;
		}
	}
}
//顯示函數
struct txlproject *display(struct txlproject *head)
{
	struct txlproject *p1,*p2;
	char name[30];
	int m;
	if(head==NULL)
	{
		printf("通訊錄爲空,無法顯示!/n");
		return head;
	}
	p1=head;
	m=0;
	printf("請輸入需要顯示人的姓名:");
	gets(name);
	while(p1!=NULL)
	{
		while((strcmp(p1->name,name))!=0 && p1->next!=NULL)
		{
			p2=p1;
			p1=p1->next;
		}
		if(strcmp(p1->name,name)==0)
		{
			m++;
			printf("%s的通訊內容如下:/n",name);
			printf("---姓名--------職業--------手機-------Email------通訊地址/n");
			printf("==================================/n");
			printf("== %s",p1->name);printf("       ");
			printf("%s",p1->work);printf("       ");
			printf("%s",p1->handset);printf("       ");
			printf("%s",p1->email);printf("       ");
			printf("%s",p1->address); printf("       /n");
			printf("==================================/n");
		}
		p1=p1->next;
	}
	if(m==0)
	{
		printf("此人未在本通訊錄中!/n");
	}
	return(head);
}

//排序函數
struct txlproject *paixu(struct txlproject *head)
{
	struct txlproject *p1,*p2;
	int i,j;
	struct txlproject1
	{
		char name[30];
		char work[30];
		char handset[30];
		char email[30];
		char address[30];
	};
	struct txlproject1 px[200];
	struct txlproject1 temp;
	if(head==NULL)
	{
		printf("通訊錄爲空,無法排序!/n");
		return(head);
	}
	p1=head;
	for(i=0;i<n,p1!=NULL;i++)
	{
		strcpy(px[i].name,p1->name);
		strcpy(px[i].work,p1->work);
		strcpy(px[i].handset,p1->handset);
		strcpy(px[i].email,p1->email);
		strcpy(px[i].address,p1->address);
		p2=p1;
		p1=p1->next;
	}
	head=shifang(head);
	for(j=0;j<n-1;j++)
	{
		for(i=j+1;i<n;i++)
		{
			if(strcmp(px[i].name,px[j].name)<0)
			{
				temp=px[i];
				px[i]=px[j];
				px[j]=temp;
			}
		}
	}
	p1=(struct txlproject *)malloc(LEN);
	p2=p1;
	strcpy(p1->name,px[0].name);
	strcpy(p1->work,px[0].work);
	strcpy(p1->handset,px[0].handset);
	strcpy(p1->email,px[0].email);
	strcpy(p1->address,px[0].address);

	head=p1;
	for(i=1;i<n;i++)
	{
		p1=(struct txlproject *)malloc(LEN);
		strcpy(p1->name,px[i].name);
		strcpy(p1->work,px[i].work);
		strcpy(p1->handset,px[i].handset);
		strcpy(p1->email,px[i].email);
		strcpy(p1->address,px[i].address);
		p2->next=p1;
		p2=p1;
	}
	p2->next=NULL;
	printf("按姓名排序後爲:/n");
	print(head);
	return(head);
}
//姓名查找函數
struct txlproject *search(struct txlproject *head)
{
	struct txlproject *p1,*p2;
	int m;
	char name[30];
	if(head==NULL)
	{
		printf("通訊錄爲空,無法分類查找!/n");
		return(head);
	}
	p1=head;
	printf("********************/n");
	printf("**  請輸入需要查找的姓名  **/n");
	printf("********************/n");
	m=0;
	gets(name);
	while(p1!=NULL)
	{
		while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
		{
			p2=p1;
			p1=p1->next;
		}
		if(strcmp(p1->name,name)==0)
		{
			m++;
			printf("你查找的內容是:/n");
			printf("+++++++++++++++++++++++++++++++++++/n");
			printf("++ %s        %s       %s       %s        %s/n",p1->name,p1->work,p1->handset,p1->email,p1->address);
			printf("+++++++++++++++++++++++++++++++++++/n");
		}
		p1=p1->next;

		if(m==0)
		{
			printf("此人未在本通訊錄中!/n");
		}
		break;
	}

	return(head);
}

//釋放內存函數
struct txlproject *shifang(struct txlproject *head)
{
	struct txlproject *p1;
	while(head!=NULL)
	{
		p1=head;
		head=head->next;
		free(p1);
	}
	return(head);
}

//文件寫入函數
void save(struct txlproject *head)
{
	FILE *fp;
	struct txlproject *p1;
	char tong[30];
	if(head==NULL)
	{
		printf("通訊錄爲空,無法存儲!/n");
		return;
	}
	printf("請輸入保存後的文件名:");
	gets(tong);
	fp=fopen("(tong).txt","w");
	if(fp==NULL)
	{
		printf("cannot open file/n");
		return;
	}
	p1=head;
	fprintf(fp,"姓名    職業      手機     Email     通訊地址/n");
	for(;p1!=NULL;) 
	{
		fprintf(fp,"%s       %s       %s        %s       %s/n",p1->name,p1->work,p1->handset,p1->email,p1->address);
		p1=p1->next;
	}
	printf("保存完畢!/n");
	fclose(fp);
}

//文件讀出函數
struct txlproject *load(struct txlproject *head)
{
	FILE *fp;
	char tong[30];
	struct txlproject *p1,*p2;
	printf("請輸入要輸出的文件名:");
	gets(tong);
	fp=fopen("(tong).txt","r");
	if(fp==NULL)
	{
		printf("此通訊錄名不存在,無法輸出!/n");
		return(head);
	}
	else
	{
		head=shifang(head);
	}
	p1=(struct txlproject *)malloc(LEN);
	fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
	if(feof(fp)!=0)
	{
		printf("文件爲空,無法打開!/n");
		return(head);
	}
	else
	{
		rewind(fp);
		p2=p1;
		head=p1;
		n=0;
		while(feof(fp)==0)
		{
			fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address);
			if(feof(fp)!=0)
				break;
			p2->next=p1;
			p2=p1;
			p1=(struct txlproject *)malloc(LEN);
			n=n+1;
		}
		p2->next=NULL;
		p1=head;
		head=head->next;
		n=n-1;
		free(p1);
		print(head);
		printf("打開完畢!/n");
		return(head);
	}
	fclose(fp);
}

//綜合操作函數
struct txlproject *menu(struct txlproject *head)
{
	char num[10];
	while(1)
	{
		printf("*********************/n");
		printf("*** 1 姓名查找      ****/n");
		printf("*** 2 單個顯示      ****/n");
		printf("*** 3 增加          ****/n");
		printf("*** 4 退出          ****/n");
		printf("*********************/n");
		printf("請輸入您選擇的操作:");
		gets(num);
		switch(*num)
		{
		case '1':
			{
				head=search(head);                          //姓名查找
				print(head);
			}
			break;
		case '2':
			{
				head=display(head);                          //顯示
			}
			break;
		case '3':
			{
				head=insert(head);                           //增加
				print(head);
			}
			break;
		case '4':
			return head;
		default:
			printf("操作錯誤,此項不存在!/n");
			break;
		}
		if(strcmp(num,"6")==0)
			break;
	}
	return head;
}
//主函數
void main()
{
	struct txlproject *head=NULL;
	char num[10];
	printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/n");
	printf("*=*               程序說明                *=*/n");
	printf("*=*    請及時保存創建完畢的通訊錄內容!    *=*/n");
	printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/n");
	while(1)
	{
		printf("************************/n");
		printf("***     1 創建通訊錄      ****/n");
		printf("***     2 按名字排序      ****/n");
		printf("***     3 綜合操作        ****/n");
		printf("***     4 保存            ****/n");
		printf("***     5 打開            ****/n"); 
		printf("***     6 刪除            ****/n");
		printf("***     7 退出            ****/n");
		printf("************************/n");
		printf("請輸入您選擇的操作:");
		gets(num);
		switch(*num)
		{
		case '1':
			{
				if(head==NULL)
				{
					head=creat();                                //創建
					print(head);
				}
				else
				{
					head=shifang(head);
					head=creat();                                //重新創建
					print(head);
				}
			}
			break;
		case '2':
			{
				head=paixu(head);                               //排序
			}
			break;
		case '3':
			{
				head=menu(head);                              //綜合操作
			}
			break;
		case '4':
			{
				save(head);                                   //文件保存
				print(head);
			}
			break;
		case '5':
			{
				head=load(head);                              //文件輸出
			}
			break;
		case '6':
			{
				head=delete(head);                           //刪除
				print(head);
			}
			break;
		case '7':
			head=shifang(head);
			break;
		default:
			printf("操作錯誤,此項不存在!/n");
			break;
		}
		if(strcmp(num,"7")==0)
			break;
	}
}


發佈了5 篇原創文章 · 獲贊 12 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章