**通讯录说明文档
语言:c语言
格式: 编号 姓 名 住址 电话 住宅电话
201701 * xxxx 183****5668 八位电话号码
要求:使用结构体形式对数据存储
功能:使用链表实现增加(在增加人员的过程中有一个自动排序功能,比如按姓名排序)、删除、修改、查找(比如:工号查找、电话查找)的功能;
(1)添加用户信息(号码长度 号码是否重复)
(2)列出好友信息(按姓名排序)
(3)查找好友信息(按姓名查找)
(4)删除好友
(5)退出
注意事项:在增、删、改、查过程中,如果姓名相同怎么进行选择操作。**
这里先附上同学作品的一部分内容(continue语句用的非常好!!!是我学习的榜样!!!):
思路:在输入手机号的时候,先判断是否为11为号码,然后在判断是否重复;
看起来比较正确,其实不然。。。。。。。。。。
测验:在输入与之前相同的11位手机号码后,系统提示:号码重复,请重新输入
然后习惯性的输入了不同的11位手机号码,这下应该可以了吧???
是吗?????????
似乎又漏掉了什么??
在系统提示号码重复时,如果输入了号码长度不是11位,此时也能避免被系统检测出重名,却检测不出此时的号
码已然不满足11位的要求了!!!(这个问题一直困扰着我,蓝瘦香菇)
printf("\t\t\t\t\t*请输入用户手机号:\t");
scanf("%s", n->tel);
getchar();
while(strlen(n->tel) != 11)
{
printf(color_red"\t\t\t\t\t**号码长度错误,请重新输入: "color_none);
scanf("%s", n->tel);
getchar();
}
while(p != NULL)
{
if(strcmp(p->tel, n->tel) == 0)
{
printf(color_red"\t\t\t\t\t**号码重复,请重新输入: "color_none);
scanf("%s", n->tel);
getchar();
continue;
}
p = p->next;
}
这里附上我的程序(未能解决以上问题,此程序依然存在许多问题,期待日后能够解决!!!)
#include "tongxun.h"
int main()
{
char choice[10] = {0};
int ret;
Address head_node = (Address)malloc(sizeof(ADDRESS));
if(NULL == head_node)
{
return;
}
head_node->next = NULL;
showing();
while(1)
{
show();
scanf("%s",choice);
switch(atoi(&choice[0]))
{
case 1:
init_list(head_node);
system("clear");
printf("*********************************************************************\n\n");
printf("success to create contact!\n\n");
printf("*********************************************************************\n\n");
break;
case 2:
system("clear");
print(head_node);
break;
case 3:
system("clear");
search(head_node);
break;
case 4:
system("clear");
ret=delete(head_node);
if(ret == 0)
{
printf("delete success\n");
}
else
{
printf("delete fail\n");
}
printf("\n");
break;
case 5: system("clear");
alter( head_node);
break;
case 6:
system("clear");
list(head_node);
break;
case 7:
exit(1);
break;
default :
printf("Unknow input !\n");
}
}
return 0;
}
#include "tongxun.h"
void showing()
{
system("clear");
printf("**************************************************************************************\n\n");
printf("***********************WELCOME TO CONTACT SYSTEM********************************\n\n");
printf("**************************************************************************************\n\n");
sleep(2);
system("clear");
}
void show()
{
printf("**********************************************************************************\n\n");
printf("** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\n\n");
printf("*** 1. add information of friends 2.show friends all information ***\n");
printf("*** 3. search for the friend 4.delete the friend ***\n");
printf("*** 5. change the friend information 6.list information by id ***\n");
printf("*** 7. quit ***\n\n");
printf("** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\n");
printf("**********************************************************************************\n\n");
printf("*************************** please input your choice:***************************\n");
}
int init_list(Address h)
{
int ret;
Address p1 = h->next;
Address q1 = h;
// if(NULL == h)
// {
// return error;
// }
Address p =(Address)malloc(sizeof(ADDRESS));
if(NULL == p)
{
return 0;
}
system("clear");
printf("Please input id :\n");
scanf("%ld",&(p->id));
printf("please input name:\n");
scanf("%s",p->name);
while(p1)
{
if (strcmp(p->name, p1->name) > 0)
{
q1 = p1;
p1 = p1->next;
}
else
{
break;
}
}
p->next = p1;
q1->next = p;
printf("please input telelphone :\n");
scanf("%s",p->tel);
while(strlen(p->tel) != 11)
{
printf("please input 11 number:\n");
scanf("%s",p->tel);
}
/*ret = compare(h,p->tel);
while (ret != 1)
{printf("please input another 11 number:");
scanf("%s",p->tel);
ret = compare(h,p->tel);
}*/
printf("please input the address:\n");
scanf("%s",p->address);
printf("please input the homephone:\n");
scanf("%s",p->homephone);
while(strlen(p->homephone) != 8)
{
printf("please input 8 number:");
scanf("%s",p->homephone);
}
return 0;
}
void print(Address h)
{
if(NULL == h)
{
return;
}
if(h->next == NULL)
{
printf("\n\n there is no friend!\n");
}
Address temp = h->next;
while(temp)
{
printf("num:%d name:%s tel:%s address:%s homephone:%s\n",temp->id,temp->name,temp->tel,temp->address,temp->homephone);
temp = temp->next;
}
printf("\n");
}
/*int compare(Address h,char tel[20])
{
Address s = h->next;
if (!s)
{
return 1;
}
while((s != NULL) && (strcmp(tel,s->tel) != 0))
{
s = s->next;
}
if (!s->next)
{
return 1;
}
else if (s)
{
return 0;
}
}*/
int delete(Address h)
{
char name[20] = {0};
printf("please input the name you want delete:");
scanf("%s",name);
Address f = h;
Address p;
if(f == NULL)
{
return -1;
}
while(f->next)
{
if(strcmp(name,f->next->name)==0)
{
p = f->next;
f->next = p->next;
free(p);
return 0;
}
f = f->next;
}
if (strcmp(name,f->name) == 0)
{
free(f);
free(p);
return 0;
}
free(f);
free(p);
return -1;
}
int search(Address h)
{
char name[20] = {0};
printf("please input the name you want to search for:");
scanf("%s",name);
Address q = h;
if(!q)
{
exit(1);
}
while(q->next)
{
q = q->next;
if(strcmp(name,q->name) == 0)
{
printf("num:%d name:%s tel:%s address:%s homephone:%s\n",q->id,q->name,q->tel,q->address,q->homephone);
return 0;
}
}
if (strcmp(name,q->name) != 0)
{
printf("Not Find!\n");
}
return 0;
}
int alter(Address h)
{
char name[20] = {0};
printf("please input the name you want to alter:");
scanf("%s",name);
Address j = h->next;
if(!j)
{
return 0;
}
while(j)
{
while(strcmp(name,j->name) == 0)
{
printf("************************CHANGEING INFORMATION**************************\n\n");
printf("***********1.ID 2.NAME ***\n");
printf("***********3.ADDRESS 4.TELEPHONE ***\n");
printf("***********5.homephone 6.QUIT ***\n\n");
printf("************************PLEASE INPUT YOUR CHOICE*************************\n");
char choice[10] = {0};
scanf("%s",choice);
switch(atoi(&choice[0]))
{
case 1:
printf("Please input new id :\n");
scanf("%ld",&(j->id));
printf("num:%d name:%s tel:%s address:%s homephone:%s\n",j->id,j->name,j->tel,j->address,j->homephone);
break;
case 2:
printf("Please input new name :\n");
scanf("%s",j->name);
printf("num:%d name:%s tel:%s address:%s homephone:%s\n",j->id,j->name,j->tel,j->address,j->homephone);
return;
case 3:
printf("Please input new address :\n");
scanf("%s",j->address);
printf("num:%d name:%s tel:%s address:%s homephone:%s\n",j->id,j->name,j->tel,j->address,j->homephone);
break;
case 4:
printf("Please input new telephone :\n");
scanf("%s",j->tel);
printf("num:%d name:%s tel:%s address:%s homephone:%s\n",j->id,j->name,j->tel,j->address,j->homephone);
break;
case 5:
printf("Please input new telephone :\n");
scanf("%s",j->tel);
printf("num:%d name:%s tel:%s address:%s homephone:%s\n",j->id,j->name,j->tel,j->address,j->homephone);
case 6:
return;
break;
default :
printf("Unkown input!\n");
break;
}
}
j = j->next;
}
if (NULL == j)
{
printf("Not Find!\n");
}
return;
}
int list(Address h)
{
int i =0 , j ,count = 0;
int id[100] = {0};
int t;
Address k = h->next;
if(!k)
{
return 0;
}
while(k)
{
id[i]= k->id;
k = k->next;
count++;
i++;
}
for(i = 0;i < count;i++)
{
for(j = 0;j if(id[i] < id[j])
{
t = id[i];
id[i] = id[j];
id[j] = t;
}
}
}
for(i = 0;i < count; i++)
{
k=h->next;
while(k)
{
if(id[i] = k->id)
{
printf(" num:%d name:%s tel:%s address:%s homephone:%s\n",k->id,k->name,k->tel,k->address,k->homephone);
}
k =k->next;
}
}
return 0;
}
#ifndef _CONTACT_H_
#define _CONTACT_H_
#include
#include
#include
#define error 100001
#define SUCCESS 100002
typedef struct node{
long int id;
char name[20];
char tel[20];
char address[20];
char homephone[20];
struct node *next;
}ADDRESS;
typedef ADDRESS *Address;
extern int init_list(Address h);
extern void print(Address h);
extern int compare(Address h ,char tel[20]);
extern int delete(Address h);
extern int search(Address h);
extern int alter(Address h);
extern int list(Address h);
这里写代码片