这几天都在写通讯录程序。。。第一次写这么长的程序,不管从思路上还是从技术上我都有很多的不足之处,今天把大致草稿写好了,但还有一些要求没有实现,明天继续完善。。。
通讯录说明文档(一)
语言:c语言
格式: 编号 姓 名 住址 电话
201701 *** xxxx 183****5668
要求:使用结构体形式对数据存储
功能:使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找(比如:工号查找、电话查找)的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)修改
(6)退出
注意事项:在增、删、改、查过程中,如果姓名相同怎么进行选择操作。
本来以为第一项增加功能最难,后来才发现改的过程最复杂,既要调序还要保证改的内容合法。。。。。真心头疼,明天把改动模块分成五块来实现,应该会好做一些
txl.h文件
#ifndef _CONTACT_H_
#define _CONTACT_H_
#define _CONTACT_H_
#define SUCCESS 10000001
#define FAILURE 10000002
#define FAILURE 10000002
struct contact
{
char id[10];
char name[10];
char address[20];
char tele[12];
char htele[9];
struct contact *next;
};
typedef struct contact CONT;
typedef CONT *LinkList;
{
char id[10];
char name[10];
char address[20];
char tele[12];
char htele[9];
struct contact *next;
};
typedef struct contact CONT;
typedef CONT *LinkList;
void show();
void PrintInfo();
int ListInit(LinkList *L);
int InsertInfo(LinkList *L);
int ShowInfo(LinkList L);
int SearchInfo(LinkList L);
int DeleteInfo(LinkList L,char s[10]);
int ChangeInfo(LinkList L,char s[10]);
void PrintInfo();
int ListInit(LinkList *L);
int InsertInfo(LinkList *L);
int ShowInfo(LinkList L);
int SearchInfo(LinkList L);
int DeleteInfo(LinkList L,char s[10]);
int ChangeInfo(LinkList L,char s[10]);
#endif
txl.c文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "txl.h"
#include <stdlib.h>
#include <string.h>
#include "txl.h"
extern num;
void show()
{
system("clear");
printf("========================================================\n");
printf("*********** WELCOME TO CONTACTS SYSTEM *************\n");
printf("========================================================\n");
sleep(1);
}
void show()
{
system("clear");
printf("========================================================\n");
printf("*********** WELCOME TO CONTACTS SYSTEM *************\n");
printf("========================================================\n");
sleep(1);
}
void PrintInfo()
{
printf("========================================================\n");
printf("******** 1.InsertInfo 2.ShowInfo ********\n");
printf("******** 3.SearchInfo 4.DeleteInfo ********\n");
printf("******** 5.ChangeInfo 6.Quit ********\n");
printf("========================================================\n");
sleep(1);
printf("******** Please input your choice: ********\n");
printf("========================================================\n");
}
{
printf("========================================================\n");
printf("******** 1.InsertInfo 2.ShowInfo ********\n");
printf("******** 3.SearchInfo 4.DeleteInfo ********\n");
printf("******** 5.ChangeInfo 6.Quit ********\n");
printf("========================================================\n");
sleep(1);
printf("******** Please input your choice: ********\n");
printf("========================================================\n");
}
int ListInit(LinkList *L)
{
(*L)=(LinkList)malloc(sizeof(CONT));
if(NULL == (*L))
{
return FAILURE;
}
(*L)->next = NULL;
{
(*L)=(LinkList)malloc(sizeof(CONT));
if(NULL == (*L))
{
return FAILURE;
}
(*L)->next = NULL;
return SUCCESS;
}
}
int InsertInfo(LinkList *L)
{
LinkList p = *L;
LinkList s;
{
LinkList p = *L;
LinkList s;
printf("==== Please input: id name address tele htele ====\n");
s = (LinkList)malloc(sizeof(CONT));
scanf("%s%s%s%s%s",s->id,s->name,s->address,s->tele,s->htele);
if(strlen(s->tele) != 11)
{
printf("=============== The tele is error! =============");
return 0;
}
if(strlen(s->htele) != 8)
{
printf("============== The htele is error! =============");
return 0;
}
scanf("%s%s%s%s%s",s->id,s->name,s->address,s->tele,s->htele);
if(strlen(s->tele) != 11)
{
printf("=============== The tele is error! =============");
return 0;
}
if(strlen(s->htele) != 8)
{
printf("============== The htele is error! =============");
return 0;
}
while(p->next)
{
if(strcmp((p->next)->tele,s->tele) == 0)
{
printf("============= The tele is existed! =============");
return 0;
}
p= p->next;
}
{
if(strcmp((p->next)->tele,s->tele) == 0)
{
printf("============= The tele is existed! =============");
return 0;
}
p= p->next;
}
p=*L;
if(p->next == NULL)
{
s->next = p ->next;
p->next = s;
}
else
{
while(p->next)
{
if(strcmp((p->next)->name,s->name) > 0)
{
if(p->next == NULL)
{
s->next = p ->next;
p->next = s;
}
else
{
while(p->next)
{
if(strcmp((p->next)->name,s->name) > 0)
{
s->next = p->next;
p->next = s;
break;
}
p = p->next;
}
if(p->next == NULL)
p->next = s;
break;
}
p = p->next;
}
if(p->next == NULL)
{
s->next = p->next;
p->next = s;
}
}
s->next = p->next;
p->next = s;
}
}
}
int ShowInfo(LinkList L)
{
if(NULL == L)
{
return FAILURE;
}
if(num == 0)
{
printf("=============== The contact is empty! ==================\n");
return 0;
}
{
if(NULL == L)
{
return FAILURE;
}
if(num == 0)
{
printf("=============== The contact is empty! ==================\n");
return 0;
}
LinkList p = L->next;
sleep(1);
while(p)
{
printf("========================================================\n");
printf("ID %s\n",p->id);
printf("NAME %s\n",p->name);
printf("ADDRESS %s\n",p->address);
printf("TELE %s\n",p->tele);
printf("HTELE %s\n",p->htele);
p = p->next;
sleep(1);
}
sleep(1);
while(p)
{
printf("========================================================\n");
printf("ID %s\n",p->id);
printf("NAME %s\n",p->name);
printf("ADDRESS %s\n",p->address);
printf("TELE %s\n",p->tele);
printf("HTELE %s\n",p->htele);
p = p->next;
sleep(1);
}
return SUCCESS;
}
}
int SearchInfo(LinkList L)
{
int a;
char s[10];
if(NULL == L)
{
return FAILURE;
}
LinkList p = L->next;
printf("========================================================\n");
printf("************ 1.name 2.address 3.tele ************\n");
printf("========================================================\n");
printf("************ Please input your choice! ************\n");
printf("========================================================\n");
scanf("%d",&a);
{
int a;
char s[10];
if(NULL == L)
{
return FAILURE;
}
LinkList p = L->next;
printf("========================================================\n");
printf("************ 1.name 2.address 3.tele ************\n");
printf("========================================================\n");
printf("************ Please input your choice! ************\n");
printf("========================================================\n");
scanf("%d",&a);
if(a == 1)
{
printf("============= Please input the name! =================\n");
scanf("%s",s);
while(p)
{
if(strcmp(s,p->name) == 0)
{
printf("============= The contact information is ==============\n");
printf("========================================================\n");
printf("*** ID %s \n",p->id);
printf("*** NAME %s \n",p->name);
printf("*** ADDRESS %s \n",p->address);
printf("*** TELE %s \n",p->tele);
printf("*** HTELE %s \n",p->htele);
sleep(1);
return SUCCESS;
}
else
{
p = p->next;
}
}
return 0;
}
else if(a == 2)
{
printf("============ Please input the address! ================\n");
scanf("%s",s);
while(p)
{
if(strcmp(s,p->address) == 0)
{
printf("============= The contact information is ==============\n");
printf("========================================================\n");
printf("*** ID %s \n",p->id);
printf("*** NAME %s \n",p->name);
printf("*** ADDRESS %s \n",p->address);
printf("*** TELE %s \n",p->tele);
printf("*** HTELE %s \n",p->htele);
sleep(1);
return SUCCESS;
}
else
{
p = p->next;
}
}
return 0;
}
{
printf("============= Please input the name! =================\n");
scanf("%s",s);
while(p)
{
if(strcmp(s,p->name) == 0)
{
printf("============= The contact information is ==============\n");
printf("========================================================\n");
printf("*** ID %s \n",p->id);
printf("*** NAME %s \n",p->name);
printf("*** ADDRESS %s \n",p->address);
printf("*** TELE %s \n",p->tele);
printf("*** HTELE %s \n",p->htele);
sleep(1);
return SUCCESS;
}
else
{
p = p->next;
}
}
return 0;
}
else if(a == 2)
{
printf("============ Please input the address! ================\n");
scanf("%s",s);
while(p)
{
if(strcmp(s,p->address) == 0)
{
printf("============= The contact information is ==============\n");
printf("========================================================\n");
printf("*** ID %s \n",p->id);
printf("*** NAME %s \n",p->name);
printf("*** ADDRESS %s \n",p->address);
printf("*** TELE %s \n",p->tele);
printf("*** HTELE %s \n",p->htele);
sleep(1);
return SUCCESS;
}
else
{
p = p->next;
}
}
return 0;
}
else if(a == 3)
{
while(p)
{
printf("============== Please input the tele! =================\n");
scanf("%s",s);
if(strcmp(s,p->tele) == 0)
{
printf("============= The contact information is ==============\n");
printf("========================================================\n");
printf("*** ID %s \n",p->id);
printf("*** NAME %s \n",p->name);
printf("*** ADDRESS %s \n",p->address);
printf("*** TELE %s \n",p->tele);
printf("*** HTELE %s \n",p->htele);
sleep(1);
return SUCCESS;
}
else
{
p = p->next;
}
}
return 0;
}
}
{
while(p)
{
printf("============== Please input the tele! =================\n");
scanf("%s",s);
if(strcmp(s,p->tele) == 0)
{
printf("============= The contact information is ==============\n");
printf("========================================================\n");
printf("*** ID %s \n",p->id);
printf("*** NAME %s \n",p->name);
printf("*** ADDRESS %s \n",p->address);
printf("*** TELE %s \n",p->tele);
printf("*** HTELE %s \n",p->htele);
sleep(1);
return SUCCESS;
}
else
{
p = p->next;
}
}
return 0;
}
}
int DeleteInfo(LinkList L,char s[10])
{
if(NULL == L)
{
return FAILURE;
}
{
if(NULL == L)
{
return FAILURE;
}
LinkList tmp = L;
LinkList p = L->next;
while(p)
{
if(strcmp(s,p->name) == 0)
{
tmp->next = p->next;
free(p);
num--;
sleep(1);
return SUCCESS;
}
else
{
tmp = p;
p = p->next;
}
}
LinkList p = L->next;
while(p)
{
if(strcmp(s,p->name) == 0)
{
tmp->next = p->next;
free(p);
num--;
sleep(1);
return SUCCESS;
}
else
{
tmp = p;
p = p->next;
}
}
return 0;
}
}
int ChangeInfo(LinkList L,char s[10])
{
if(NULL == L)
{
return FAILURE;
}
{
if(NULL == L)
{
return FAILURE;
}
LinkList p = L->next;
LinkList tmp;
LinkList tmp;
while(p)
{
if(strcmp(s,p->name) == 0)
{
printf("Please input new information\n");
LinkList n =(LinkList)malloc(sizeof(CONT));
printf("Please input:ID\n");
scanf("%s",n->id);
{
if(strcmp(s,p->name) == 0)
{
printf("Please input new information\n");
LinkList n =(LinkList)malloc(sizeof(CONT));
printf("Please input:ID\n");
scanf("%s",n->id);
printf("Please input:NAME\n");
scanf("%s",n->name);
while(p)
{
if(strcmp(n->name,p->name) == 1)
{
tmp = p;
p = p->next;
}
else
{
break;
}
}
n->next = tmp->next;
tmp->next = n;
num++;
scanf("%s",n->name);
while(p)
{
if(strcmp(n->name,p->name) == 1)
{
tmp = p;
p = p->next;
}
else
{
break;
}
}
n->next = tmp->next;
tmp->next = n;
num++;
printf("Please input:ADDRESS\n");
scanf("%s",n->address);
printf("Please input:TELE\n");
scanf("%s",n->tele);
scanf("%s",n->address);
printf("Please input:TELE\n");
scanf("%s",n->tele);
while(strlen(n->tele) != 11)
{
printf("Input Failure! Please input again!\n");
scanf("%s",n->tele);
}
printf("Input Success!\n");
{
printf("Input Failure! Please input again!\n");
scanf("%s",n->tele);
}
printf("Input Success!\n");
printf("Please input:HTELE\n");
scanf("%s",n->htele);
scanf("%s",n->htele);
while(strlen(n->htele) != 8)
{
printf("Input Failure! Please input again!\n");
scanf("%s",n->htele);
}
printf("Input Success!\n");
{
printf("Input Failure! Please input again!\n");
scanf("%s",n->htele);
}
printf("Input Success!\n");
sleep(1);
return SUCCESS;
}
else
{
p = p->next;
}
}
return SUCCESS;
}
else
{
p = p->next;
}
}
return 0;
}
}
main.c文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "txl.h"
#include <stdlib.h>
#include <string.h>
#include "txl.h"
int num=0;
int main()
{
int ret;
char target[10];
LinkList list;
char choice[10];
show();
ret=ListInit(&list);
if(FAILURE == ret)
{
printf("=================== Init Failure! ====================\n");
}
while(1)
{
PrintInfo();
int main()
{
int ret;
char target[10];
LinkList list;
char choice[10];
show();
ret=ListInit(&list);
if(FAILURE == ret)
{
printf("=================== Init Failure! ====================\n");
}
while(1)
{
PrintInfo();
scanf("%s",choice);
switch(atoi(&choice[0]))
{
case 1:
ret=InsertInfo(&list);
break;
case 2:
ret=ShowInfo(list);
break;
case 3:
if(num == 0)
{
printf("=================== NO CONTACTS! =====================\n");
sleep(1);
break;
}
ret=SearchInfo(list);
if(FAILURE == ret)
{
printf("================== Search Failure! ===================\n ");
}
else if(SUCCESS == ret)
{
printf("================== Search Success! ===================\n");
sleep(1);
}
else
{
printf("============== NOT FIND THE CONTACT! ===============\n");
sleep(1);
}
break;
case 4:
if(num == 0)
{
printf("===================== NO CONTACTS! ===================\n");
sleep(1);
break;
}
printf("============== Please input the name: ================\n");
scanf("%s",target);
ret = DeleteInfo(list,target);
if(FAILURE == ret)
{
printf("=================== Delete Failure! ==================\n");
sleep(1);
}
else if(SUCCESS == ret)
{
printf("=================== Delete Success! ==================\n");
sleep(1);
}
else
{
printf("================ NOT FIND THE CONTACT! ==============\n");
sleep(1);
}
break;
case 5:
if(num == 0)
{
printf("================== NO CONTACTS! ======================\n");
sleep(1);
break;
}
printf("=============== Please input the name: =================\n");
scanf("%s",target);
ret=ChangeInfo(list,target);
if(FAILURE == ret)
{
printf("================== Change Failure! ===================\n");
}
else if(SUCCESS == ret)
{
printf("=================== Change Success! ====================\n");
sleep(1);
}
else
{
printf("=============== NOT FIND THE CONTACT! ================\n");
sleep(1);
}
break;
case 6:
system("clear");
exit(1);
default:
printf("================= Unknow choice! ===================\n");
sleep(1);
break;
}
}
return 0;
}
switch(atoi(&choice[0]))
{
case 1:
ret=InsertInfo(&list);
break;
case 2:
ret=ShowInfo(list);
break;
case 3:
if(num == 0)
{
printf("=================== NO CONTACTS! =====================\n");
sleep(1);
break;
}
ret=SearchInfo(list);
if(FAILURE == ret)
{
printf("================== Search Failure! ===================\n ");
}
else if(SUCCESS == ret)
{
printf("================== Search Success! ===================\n");
sleep(1);
}
else
{
printf("============== NOT FIND THE CONTACT! ===============\n");
sleep(1);
}
break;
case 4:
if(num == 0)
{
printf("===================== NO CONTACTS! ===================\n");
sleep(1);
break;
}
printf("============== Please input the name: ================\n");
scanf("%s",target);
ret = DeleteInfo(list,target);
if(FAILURE == ret)
{
printf("=================== Delete Failure! ==================\n");
sleep(1);
}
else if(SUCCESS == ret)
{
printf("=================== Delete Success! ==================\n");
sleep(1);
}
else
{
printf("================ NOT FIND THE CONTACT! ==============\n");
sleep(1);
}
break;
case 5:
if(num == 0)
{
printf("================== NO CONTACTS! ======================\n");
sleep(1);
break;
}
printf("=============== Please input the name: =================\n");
scanf("%s",target);
ret=ChangeInfo(list,target);
if(FAILURE == ret)
{
printf("================== Change Failure! ===================\n");
}
else if(SUCCESS == ret)
{
printf("=================== Change Success! ====================\n");
sleep(1);
}
else
{
printf("=============== NOT FIND THE CONTACT! ================\n");
sleep(1);
}
break;
case 6:
system("clear");
exit(1);
default:
printf("================= Unknow choice! ===================\n");
sleep(1);
break;
}
}
return 0;
}