c語言實現手機通訊錄
int del_contacts(People head);//刪除聯絡人
int seek_contacts(People head);//查詢聯絡人
int amend_contacts(People head);//修改聯絡人
int show_contacts(People head);//顯示聯絡人
int empty_contacts(People head);//清空聯絡人
主要程式碼:
contacts.h
contacts.cpp#include<stdio.h> #include<stdlib.h> #include<string.h> #define OK 1 #define ERROR -1 #define MAXSIZE 15 typedef struct Contacts { char name[MAXSIZE]; //姓名 char sex[MAXSIZE]; //性別 char phone[MAXSIZE]; //電話號碼 struct Contacts *next; }Contacts ,*People; //主要功能函式 void add_contacts(People head);//增加聯絡人 int del_contacts(People head);//刪除聯絡人 int seek_contacts(People head);//查詢聯絡人 int amend_contacts(People head);//修改聯絡人 int show_contacts(People head);//顯示聯絡人 int empty_contacts(People head);//清空聯絡人
test.cpp#include"contacts.h" void add_contacts(People head)//增加聯絡人(每次只能增加一個) { People p; p=(People)malloc(sizeof(Contacts));//頭插法 fflush(stdin);//清空快取區。 char a=0; printf("whether add to contacts?(y/n)。\n"); a=getchar(); if(a=='y'||a=='Y') { printf("please enter the name of contact.\n"); scanf("%s",p->name); printf("please enter the gender of contact.\n"); scanf("%s",p->sex); printf("please enter your contact number.\n"); scanf("%s",p->phone); p->next=head->next; head->next = p; printf("The added contact information:\n"); printf("The name of contact: %s\n",head ->next ->name); printf("The gender of contact information: %s\n",head ->next ->sex); printf("Your contact number: %s\n",head ->next->phone); } } int del_contacts(People head)//刪除聯絡人 { int n=0; char ch=0; char name[15]; char phone[15]; People p1,p2; p1=head; if(head->next==NULL) { printf("Linkedlist is empty !\n"); return 0; } else { printf("Please choose the right way .\n"); printf("1.Delete by name.\n"); printf("2.Delete by phone .\n"); printf("Please choice 1 or 2 . \n"); scanf("%d",&n); } switch(n) { case 1: printf("Please enter the name you want to find .\n"); fflush(stdin); gets(name); while(p1->next!=NULL) { if(strcmp(p1->next->name,name)==0) { printf("The user exists .\n"); printf("%s , %s ,%s \n",p1->next->name,p1->next->sex,p1->next->phone); p2=p1->next; p1->next=p2->next; free(p2); } else p1=p1->next; } return OK;break; case 2: printf("Please ent that phone you want to find .\n"); fflush(stdin); gets(phone); while(p1->next!=NULL) { if(strcmp(p1->next->phone,phone)==0) { printf("The user exists .\n"); printf("%s , %s ,%s \n",p1->next->name,p1->next->sex,p1->next->phone); p2=p1->next; p1->next=p2->next; free(p2); } else p1=p1->next; } return OK;break; } } int seek_contacts(People head)//查詢聯絡人 { People p; p=head; int ch=0; char name[15]; char phone[15]; if(head->next==NULL) { printf("Linkedlist is empty !\n"); } else { printf("1.Find by name .\n"); printf("2.Find by phone .\n"); printf("Please choice 1 or 2 . \n"); scanf("%d",&ch); } switch(ch) { case 1: printf("Please enter the name you want to find .\n"); fflush(stdin); gets(name); while(p->next!=NULL) { p=p->next; if(strcmp(p->name,name)==0) { printf("The user exists .\n"); printf("%s , %s ,%s \n",p->name,p->phone,p->sex); } } return OK;break; case 2: printf("Please ent that phone you want to find .\n"); fflush(stdin); gets(phone); while(p->next!=NULL) { p=p->next; if(strcmp(p->phone,phone)==0) printf("%s , %s ,%s \n",p->name,p->phone,p->sex); } return OK;break; } return 0; } int amend_contacts(People head)//修改聯絡人 { People p; p=head; int n=0; char sex[15]; char name[15]; char phone[15]; if(head->next==NULL) { printf("Linledlist is empty!\n"); printf("\n"); } printf("1.Find by name.\n"); printf("2.Find by phone.\n"); printf("piease choice 1 or 2 . \n"); scanf("%d",&n); fflush(stdin); switch(n) { case 1: printf("please enter a name to modify .\n"); gets(name); while(p->next!=NULL) { p=p->next; if(strcmp(p->name,name)==0) { printf("%s , %s ,%s \n",p->name,p->phone,p->sex); printf("Please enter the information you want to modify.<name ,phone,gender>\n"); scanf("%s %s %s",name,phone,sex); strcpy(p->name,name); strcpy(p->phone,phone); strcpy(p->sex,sex); printf("Modified information:"); printf("%s , %s , %s\n",p->name,p->phone,p->sex); } } break; case 2: printf("Please enter the phone to modify . \n"); gets(name); while(p->next!=NULL) { p=p->next; if(strcmp(p->phone,phone)==0) { printf("%s , %s ,%s \n",p->name,p->phone,p->sex); printf("Please enter the information you want to modify.<name ,phone,gender>\n"); scanf("%s %s %s",name,phone,sex); strcpy(p->name,name); strcpy(p->phone,phone); strcpy(p->sex,sex); printf("%s , %s , %s\n",p->name,p->phone,p->sex); } break; } } return 0; } int show_contacts(People head)//顯示聯絡人 { People p; if(head->next==NULL) printf("LinkedList is empty !"); else { p=head; printf("That is all contacts message:\n"); while(p->next!=NULL) { p=p->next; printf("name:%s people number:%s gender:%s\n",p->name,p->phone,p->sex); } } return 0; } int empty_contacts(People head) //清空聯絡人 { People p1,p2; p1=head; p2=head; while(head->next!=NULL) { p1=head->next; p2=p1->next; head->next=p1->next; free(p1); } if(head->next==NULL) printf("All contacts were delete !\n"); return 0; }
#include"contacts.h"
int menu(People head)
{
char str1[]=" ";
char str2[]="***********";
int n=0;
system("color 4e");
printf("\n");
printf(" ");
printf("%s手機通訊錄%s\n",str2,str2);
printf("%s請選擇以下功能:\n",str1);
printf("%s1.增加聯絡人。\n",str1);
printf("%s2.刪除聯絡人。\n",str1);
printf("%s3.查詢聯絡人。\n",str1);
printf("%s4.修改聯絡人。\n",str1);
printf("%s5.顯示聯絡人。\n",str1);
printf("%s6.清空聯絡人。\n",str1);
printf("%s7.退出。\n",str1);
scanf("%d",&n);
switch(n)
{
case 1: add_contacts(head);break;
case 2: del_contacts(head);break;
case 3: seek_contacts(head);break;
case 4: amend_contacts(head);break;
case 5: show_contacts(head);break;
case 6: empty_contacts(head);break;
case 7: return 0;break;
}
system("pause");
system("cls");
return 1;
}
int main()
{
People head; //定義一個頭節點
head=(People)malloc(sizeof(Contacts));
head->next=NULL;
while(menu(head));
return 0;
}
測試時遇到主要問題解決總結:
1.在修改void add_contacts(People head) 時發現如下的情況,仔細檢查程式碼並無大的錯誤。
具體程式碼:
void add_contacts(People head)//增加聯絡人(每次只能增加一個)
{
People p;
p=(People)malloc(sizeof(Contacts));//頭插法
char a=0;
printf("whether add to contacts?(y/n)。\n");
a=getchar();
if(a=='y'||a=='Y')
{
printf("please enter the name of contact.\n");
scanf("%s",p->name);
printf("please enter the gender of contact.\n");
scanf("%s",p->sex);
printf("please enter your contact number.\n");
scanf("%s",p->phone);
p->next=head->next;
head->next = p;
printf("The added contact information:\n");
printf("The name of contact: %s\n",p->name);
printf("The gender of contact information: %s\n",p->sex);
printf("Your contact number: %s\n",p->phone);
}
}
解決:
通過網路查詢發現:是快取區接收了兩個字元的原因,接收了一個 1(選單選擇中的1)和回車程式選擇不是 y或 Y ,if()判斷進入了 NO, 跳出程式,所以只要清除快取區的回車鍵即可。
C語言如何清空緩衝區
方法一:
fflush(stdin);
fflush(stdin)在VC上可以使用,但是其他編譯器不能保證對fflush的實現。
方法二:
setbuf(stdin, NULL);
setbuf(stdin, NULL);是使stdin輸入流由預設緩衝區轉為無緩衝區。但緩衝區沒有了。
方法三:
char ch;while((ch = getchar()) != '\n' && ch != EOF);
這種方法是最好的方法,因為它使用的是C語言的基本語法,在什麼情況都是支援的
2.
int menu()
{
People head; //定義一個頭節點
head=(People)malloc(sizeof(Contacts));
head->next=NULL;
char str1[]=" ";
char str2[]="***********";
int n=0;
system("color 4e");
printf("\n");
printf(" ");
printf("%s手機通訊錄%s\n",str2,str2);
printf("%s請選擇以下功能:\n",str1);
printf("%s1.增加聯絡人。\n",str1);
printf("%s2.刪除聯絡人。\n",str1);
printf("%s3.查詢聯絡人。\n",str1);
printf("%s4.修改聯絡人。\n",str1);
printf("%s5.顯示聯絡人。\n",str1);
printf("%s6.清空聯絡人。\n",str1);
printf("%s7.退出。\n",str1);
scanf("%d",&n);
switch(n)
{
case 1: add_contacts(head);break;
case 2: del_contacts(head);break;
case 3: seek_contacts(head);break;
case 4: amend_contacts(head);break;
case 5: show_contacts(head);break;
case 6: empty_contacts(head);break;
case 7: return 0;break;
}
system("pause");
system("cls");
return 1;
}
int main()
{
while(menu(head));
return 0;
}
問題: 每次執行顯示 連結串列為空!
解決:
在測試程式碼中 紅字的部分寫在了menu()中導致head->next 每次都為空,放在int main()很好的解決了這個問題。
switch(n)
{
case 1: printf("please enter phone:\n");
gets(phone);
while(p1->next!=NULL)
{
p1=p1->next;
if(strcmp(p1->phone,phone)==0)
{
printf("%s %s\n",p1->phone,p1->name);
printf("The confirmation is to be deleted?<y\n>?\n");
ch=getchar();
fflush(stdin);
if(ch=='y'||'Y')
{
p2=p1->next;
p1->next=p2->next;
free(p2);
}
}
}break;
case 2: printf("please enter name:\n");
gets(name);
while(p1->next!=NULL)
{
p1=p1->next;
if(strcmp(p1->name,name)==0)
{
printf("%s %s\n",p1->phone,p1->name);
printf("The confirmation is to be deleted?<y\n>?\n");
ch=getchar();
if(ch=='y'||'Y')
{
p2=p1->next;
p1->next=p2->next;
free(p2);
return OK;
}
else
return ERROR ;
}
}break;
}
return 0;