c語言實現通訊錄,包括增加名字,刪除資訊,查詢,修改,排序
阿新 • • 發佈:2019-01-30
c語言可以實現的簡單程式,很好的運用了結構體,結構體陣列,結構體指標,函式的實現
//contact.h #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <string.h> #include <stdlib.h> #ifndef __CONTACT_H__ #define __CONTACT_H__ #define MAX 1000 #define MAX_NAME 20 #define MAX_SEX 5 #define MAX_TEL 12 #define MAX_ADDR 20 typedef struct PeoInfor { char name[MAX_NAME]; char sex[MAX_SEX]; int age; char tel[MAX_TEL]; char addr[MAX_ADDR]; }PeoInfor; typedef struct Contact { PeoInfor contact [MAX]; int count; }Contact,*pContact; void InitContact(pContact pcon); void AddContact(pContact pcon); void DelContact(pContact pcon); void SearchContact(pContact pcon); void ModifyContact(pContact pcon); void ShowContact(pContact pcon); void SortContact(pContact pcon); #endif // //contact.c #define _CRT_SECURE_NO_WARNINGS 1 #include "contact.h" void InitContact(pContact pcon) { pcon ->count = 0; memset(pcon->contact,0,MAX*sizeof(PeoInfor)); } void AddContact(pContact pcon) { if (pcon->count >=MAX) { printf ("電話本滿了。\n"); return ; } printf ("請輸入姓名>:"); scanf ("%s",pcon->contact[pcon->count].name); printf ("請輸入年齡>:"); scanf ("%d",&(pcon->contact [pcon->count].age)); printf ("請輸入性別>:"); scanf ("%s",pcon->contact [pcon->count].sex); printf ("請輸入電話>:"); scanf ("%s",pcon->contact [pcon->count].tel); printf ("請輸入地址>:"); scanf ("%s",pcon->contact [pcon->count].addr); pcon->count ++; printf("新增成功\n"); } int find_entry(pContact pcon,char name[]) { int i = 0; for (i=0;i<pcon->count;i++) { if (strcmp(name,pcon->contact[i].name)==0) return i; } return -1; } void DelContact(pContact pcon) { char name[MAX_NAME] = {0}; int pos = 0; int i = 0; if (pcon->count==0) { printf ("電話本為空,無法刪除\n"); return ; } printf ("請輸入要刪除的人名字>:"); scanf ("%s",name); pos = find_entry(pcon,name); if (pos==-1) { printf ("要刪的人不存在\n"); return ; } for (i=pos;i<pcon->count;i++) { pcon->contact[i] = pcon->contact[i+1]; } pcon->count--; printf ("刪除成功\n"); } void SearchContact(pContact pcon) { int pos = 0; char name[MAX_NAME] = {0}; printf ("請輸入你要查詢的人名字\n"); scanf ("%s",name); pos = find_entry(pcon,name); if (pos==-1) { printf ("沒有這個人\n"); return ; } else { printf ("%10s%5s%6s%7s%8s\n", "name","age","sex","tel","addr" ); printf ("%10s%5d%6s%7s%8s\n", pcon->contact[pos].name, pcon->contact[pos].age, pcon->contact[pos].sex, pcon->contact[pos].tel, pcon->contact[pos].addr); } } void ModifyContact(pContact pcon) { int pos = 0; char name[MAX_NAME] = {0}; int age = 0; char sex[MAX_SEX] = {0}; char tel[MAX_TEL] = {0}; char addr[MAX_ADDR] = {0}; printf ("請輸入你要查詢的人名字\n"); scanf ("%s",name); pos = find_entry(pcon,name); if (pos==-1) { printf ("沒有這個人\n"); return ; } printf ("請輸入你要改為的名字\n"); scanf ("%s",name); printf ("請輸入你要改為的年齡\n"); scanf ("%d",&age); printf ("請輸入你要改為的性別\n"); scanf ("%s",sex); printf ("請輸入你要改為的電話\n"); scanf ("%s",tel); printf ("請輸入你要改為的地址\n"); scanf ("%s",addr); printf ("%10s%5d%6s%7s%8s\n", strcpy(pcon->contact[pos].name,name), age, strcpy(pcon->contact[pos].sex,sex), strcpy(pcon->contact[pos].tel,tel), strcpy(pcon->contact[pos].addr,addr)); } void ShowContact(pContact pcon) { int i = 0; printf ("%10s%5s%6s%7s%8s\n","name","age","sex","tel","addr" ); for (i=0;i<pcon->count;i++) { printf ("%10s%5d%6s%7s%8s\n", pcon->contact[i].name, pcon->contact[i].age, pcon->contact[i].sex, pcon->contact[i].tel, pcon->contact[i].addr); } } static int cmp(const void *elem1,const void *elem2) { return strcmp ((*(PeoInfor*)elem1).name ,(*(PeoInfor*)elem2).name ); } void SortContact(pContact pcon) { int i = 0; int sz = sizeof(pcon->contact)/sizeof(pcon->contact[0]); qsort (pcon->contact,pcon->count,sizeof (pcon->contact[0]),&cmp); for (i=0;i<pcon->count;i++) { printf ("%s %d %s %s %s\n", pcon->contact[i].name, pcon->contact[i].age, pcon->contact[i].sex, pcon->contact[i].tel, pcon->contact[i].addr); } } //test.c #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include "contact.h" enum Option { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT, }; void menu () { printf ("***************1.ADD *******\n"); printf ("***************2.DEL *******\n"); printf ("***************3.SEARCH *******\n"); printf ("***************4.MODIFY *******\n"); printf ("***************5.SHOW *******\n"); printf ("***************6.SORT *******\n"); printf ("***************0.EXIT *******\n"); } void test () { int input = 0; Contact con = {0} ; InitContact(&con); do { menu(); printf ("請選擇>:"); scanf ("%d",&input); switch (input) { case ADD: AddContact(&con); break; case DEL: DelContact(&con); break; case SEARCH: SearchContact(&con); break; case MODIFY: ModifyContact(&con); break; case SHOW: ShowContact(&con); break; case SORT: SortContact(&con); break; case EXIT: break; default : printf ("輸入有誤,請重新輸入!\n"); break; } }while (input); } int main () { test(); return 0; }