c语言的通讯录

来源:互联网 发布:部落冲突数据大全2016 编辑:程序博客网 时间:2024/06/05 16:27

使用单链写的通讯录,还是有些小问题,比如编号删除的时候会留下缺口。需改进


#include <stdio.h>

#include <stdlib.h>
#include <string.h>




#define T 1
#define F -1


typedef char type;
typedef struct contact* Node;
typedef int status;


struct contact
{
int id;
char name[20];
char tel[20];
struct contact* next;
};


void print(Node head);
status init(Node* head);
status intail(Node head, int id, type name[], type tel[]);
status inhead(Node head, int id, type name[], type tel[]);
status inmid(Node head, int x, int id, type name[], type tel[]);
status length(Node head);
void findid(Node head, int id);
void findname(Node head, type name[]);
void findtel(Node head, type tel[]);
void deltel(Node head, type tel[]);
void delid(Node head, int x);
void delname(Node head, type name[]);
void updateid(Node head, int x, type name[], type tel[]);
void updatename(Node head, type x[], type name[], type tel[]);
void updatetel(Node head, type x[], type name[], type tel[]);
void sortid(Node head);
void sorttel(Node head);
void sortname(Node head);


void sortid(Node head)
{
int j = length(head);
int i, k;


for (; j > 1; j--)
{
Node temp = head;
for (i = 1; i < j; i++)
{
if (temp->next->id > temp->next->next->id)
{
Node asd = temp->next->next->next;
Node dsa = temp->next->next;
temp->next->next->next = temp->next;
temp->next->next = asd;
temp->next = dsa;
}
temp = temp->next;
}
}
}




void sortname(Node head)
{
int j = length(head);
int i, k;


for (; j > 1; j--)
{
Node temp = head;
for (i = 1; i < j; i++)
{
if (strcmp(temp->next->name,temp->next->next->name) == 1)
{
Node asd = temp->next->next->next;
Node dsa = temp->next->next;
temp->next->next->next = temp->next;
temp->next->next = asd;
temp->next = dsa;
}
temp = temp->next;
}
}
}
void sorttel(Node head)
{
int j = length(head);
int i, k;


for (; j > 1; j--)
{
Node temp = head;
for (i = 1; i < j; i++)
{
if (strcmp(temp->next->tel,temp->next->next->tel) == 1)
{
Node asd = temp->next->next->next;
Node dsa = temp->next->next;
temp->next->next->next = temp->next;
temp->next->next = asd;
temp->next = dsa;
}
temp = temp->next;
}
}
}




int js = 1;




int main()
{
    int i;
int id;
type name[20];
type tel[20];
type x[20];


Node head = NULL;
init(&head);
int back = 1;


while(back)
{   


begin();
i = 0;
fflush(stdin);
scanf("%d", &i);


switch(i)
{
case 1:
{
printf("Give the Name, Telephone.(Name Telephone)\n");
fflush(stdin);
scanf("%s %s", name, tel);
intail(head, js++, name, tel);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
case 2:
{
printf("What do you want to delete by?\n1.ID\n2.Name\n3.Telephone\n");
fflush(stdin);
scanf("%d", &i);
switch(i)
{
case 1:
{
printf("ID is :\n");
fflush(stdin);
scanf("%d",&id);
delid(head, id);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
case 2:
{
printf("Name is :\n");
fflush(stdin);
scanf("%s",name);
delname(head, name);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
case 3:
{
printf("Telephone is :\n");
fflush(stdin);
scanf("%s", tel);
deltel(head, tel);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
default:printf("Wrong\n");back = 0;
}
break;
}
case 3:
{
printf("What do you want to search by?\n1.ID\n2.Name\n3.Telephone\n");
fflush(stdin);
scanf("%d", &i);
switch(i)
{
case 1:
{
printf("ID is :\n");
fflush(stdin);
scanf("%d",&id);
printf("***************************************\n");
findid(head, id);
printf("***************************************\n");
break;
}
case 2:
{
printf("Name is :\n");
fflush(stdin);
scanf("%s",name);
printf("***************************************\n");
findname(head, name);
printf("***************************************\n");
break;
}
case 3:
{
printf("Telephone is :\n");
fflush(stdin);
scanf("%s", tel);
printf("***************************************\n");
findtel(head, tel);
printf("***************************************\n");
break;
}
default:printf("Wrong\n");back = 0;
}
break;
}
case 4:
{
printf("What do you want to Change by?\n1.ID\n2.Name\n3.Telephone\n");
fflush(stdin);
scanf("%d", &i);
switch(i)
{
case 1:
{
printf("ID is :\n");
fflush(stdin);
scanf("%d",&i);
printf("Changing the Name, Telephone.(Name Telephone)\n");
fflush(stdin);
scanf("%s %s", name, tel);
updateid(head, i, name, tel);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
case 2:
{
printf("Name is :\n");
fflush(stdin);
scanf("%s",x);
printf("Changing the Name, Telephone.(Name Telephone)\n");
fflush(stdin);
scanf("%s %s", name, tel);
updatename(head, x, name, tel);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
case 3:
{
printf("Telephone is :\n");
fflush(stdin);
scanf("%s", x);
printf("Changing the Name, Telephone.(Name Telephone)\n");
fflush(stdin);
scanf("%s %s", name, tel);
updatetel(head, x, name, tel);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
default:printf("Wrong\n");back = 0;
}
break;
}
case 5:
{
printf("What do you want to sort contacts by?\n1.ID\n2.Name\n3.Telephone\n");
fflush(stdin);
scanf("%d", &i);
switch(i)
{
case 1:
{
sortid(head);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
case 2:
{
sortname(head);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
case 3:
{
sorttel(head);
printf("***************************************\n");
print(head);
printf("***************************************\n");
break;
}
default:printf("Wrong.\n");back = 0;
}
break;
}
case 6:printf("Thanks for using.\n");
  return 0;
default:printf("Wrong.\n");back = 0;

}
}




return 0;


}


int begin()
{
printf("***********************\n");
printf("*1.Add a contact      *\n");
printf("*2.delete a contact   *\n");
printf("*3.search a contac    *\n");
printf("*4.Change a contact   *\n");
printf("*5.Print contact(s)   *\n");
printf("*6.End the process    *\n");
printf("***********************\n");


printf("Choose one option,please.(like: 1)\n");


return 0;
}




void print(Node head)
{
Node temp = head;
while (temp->next != NULL)
{
printf("%d %s %s\n",temp->next->id,temp->next->name,temp->next->tel);
temp = temp->next;
}
printf("\n");
}






status intail(Node head, int id, type name[], type tel[])
{


while (head->next != NULL)
{
head = head->next;
}
Node new = (Node)malloc(sizeof(struct contact));
if (NULL == new)
{
return F;
}
new->next = NULL;
new->id = id;
strcpy(new->name, name);
strcpy(new->tel, tel);
head->next = new;







    return T;
}





status init(Node* head)
{
Node new = (Node)malloc(sizeof(struct contact));
if (NULL == new)
{
return F;
}
new->next = NULL;
new->id = 0;
strcpy(new->tel, "0");
strcpy(new->name, "0");


*(head) = new;


    return T;
}


status inhead(Node head, int id, char name[],type tel[])
{
Node temp = head->next;
Node newnode = (Node)malloc(sizeof(struct contact));
if (NULL == newnode)
{
return F;
}
newnode->next = temp;
newnode->id = id;
strcpy(newnode->name,name);
strcpy(newnode->tel,tel);
head->next = newnode;


  return T;
}


status length(Node head)
{
int c = 0;
Node temp = head;
while (temp->next != NULL)
{
temp = temp->next;
c++;
}
return c;
}


status inmid(Node head, int x, int id, type name[], type tel[])
{
if (x < 0 || x > length(head))
{
printf("Sorry,out of range.\n");
return F;
}
int i;


Node new = (Node)malloc(sizeof(struct contact));
Node temp = head;


for (i = 0; i < x; i++)
{
temp = temp->next;
}
new->next = temp->next;
temp->next = new;
new->id = id;
strcpy(new->name, name);
strcpy(new->tel, tel);


return T;
}




void delid(Node head, int x)
{
int i, j = length(head);
Node temp = head;


while (temp->next->id != x)
{
temp = temp->next;
}
Node temp2 = temp->next->next;
free(temp->next);
temp->next = temp2;


}


void deltel(Node head, type tel[])
{
int i, j = length(head);
Node temp = head;


for (i = 0; i < j; i++)
{
if (strcmp(temp->next->tel, tel) == 0)
{
Node temp2 = temp->next->next;
free(temp->next);
temp->next = temp2;
}
else
{
temp = temp->next;
}
}
}


void delname(Node head, type name[])
{
int i, j = length(head);
Node temp = head;


for (i = 0; i < j; i++)
{
if (strcmp(temp->next->name, name) == 0)
{
Node temp2 = temp->next->next;
free(temp->next);
temp->next = temp2;
}
else
{
temp = temp->next;
}
}
}




void findid(Node head, int id)
{


Node temp = head;


if (id < js)
{
while (temp->next->id != id)
{
temp = temp->next;
}

printf("ID %d is %s,the telephone is %s.\n", id, temp->next->name, temp->next->tel);
}
else
{
printf("Sorry,not found.\n");
}
}


void findname(Node head, type name[])
{
int j = 0;
Node temp = head;


while(temp->next != NULL)
{
if (strcmp(temp->next->name, name) == 0)
{
printf("%s's ID is %d,the telephone is %s.\n", temp->next->name,temp->next->id, temp->next->tel);
j++;
}
temp = temp->next;
}
if (j == 0)
{
printf("Sorry,not found.\n");


}
}


void findtel(Node head, type tel[])
{
int j = 0;
Node temp = head;


while(temp->next != NULL)
{
if (strcmp(temp->next->tel, tel) == 0)
{
printf("%s is %s's telephone, the ID is %d.\n", temp->next->tel, temp->next->name, temp->next->id);
j++;
}
temp = temp->next;
}
if (j == 0)
{
printf("Sorry,not found.\n");


}
}




void updateid(Node head, int x, type name[], type tel[])
{
Node temp = head;
 
if (x < js)
{
while (temp->next->id != x)
{
temp = temp->next;
}
printf("ID:%d Name:%s telephone:%s.\n",temp->next->id, temp->next->name, temp->next->tel);


strcpy(temp->next->name, name);
strcpy(temp->next->tel, tel);

printf("After changed: ID:%d Name:%s telephone:%s.\n", x, temp->next->name, temp->next->tel);
}
else
{
printf("Sorry,not found.\n");


}
}


void updatename(Node head, type x[], type name[], type tel[])
{
Node temp = head;
int i = 0;


while(temp->next != NULL)
{
if (strcmp(temp->next->name,x) == 0)
{
printf("ID:%d Name:%s telephone:%s.\n",temp->next->id, temp->next->name, temp->next->tel);


strcpy(temp->next->name, name);
strcpy(temp->next->tel, tel);

printf("After changed: ID:%d Name:%s telephone:%s.\n",temp->next->id, temp->next->name, temp->next->tel);
i++;
}
temp = temp->next;
}
if (i == 0)
{
printf("Sorry,not found.\n");


}
}


void updatetel(Node head, type x[], type name[], type tel[])
{
Node temp = head;
int i = 0;


while(temp->next != NULL)
{
if (strcmp(temp->next->tel,x) == 0)
{
printf("ID:%d Name:%s telephone:%s.\n",temp->next->id, temp->next->name, temp->next->tel);


strcpy(temp->next->name, name);
strcpy(temp->next->tel, tel);

printf("After changed: ID:%d Name:%s telephone:%s.\n",temp->next->id, temp->next->name, temp->next->tel);
i++;
}
temp = temp->next;
}
if (i == 0)
{
printf("Sorry,not found.\n");


}
}