|
基于C語言的通訊錄設計
0.png (66.7 KB, 下載次數: 122)
下載附件
2017-3-1 23:26 上傳
完整代碼下載:
個人通訊錄系統.rar
(237.77 KB, 下載次數: 19)
2017-3-1 13:39 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
部分源碼預覽:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- struct person
- {
- char name[15];
- char type[15];
- int nianling;
- long int youbian;
- char addr[50];
- char celephone[22];
- char homephone[22];
- char company[20];
- char email[40];
- char QQ[22];
- }communication[500];
- int num=0;//*外部變量num為文件中的紀錄數*//
- FILE *fp;
- void mainmenu();//*主菜單*//
- void newrecord();//新添紀錄*//
- void xiugaimenu();//*修改菜單*//
- void xiugaibyname();//*按姓名修改*//
- void xiugaibycelephone();//*按手機號碼修改*//
- void xiugaixiangmu(int a);//*修改項目*//
- void searchmenu();//*查詢菜單*//
- void searchbyname();//*按姓名查詢*//
- void searchbycelephone();//*按手機號碼查詢*//
- void searchbyhomephone();//*按固定電話號碼查詢*//
- void searchbyqq();//*按QQ查詢*//
- void deletemenu();//*刪除菜單*//
- void deleteall();//*刪除所有信 息*//
- void deleteone();//*刪除單個*//
- void showall();//*輸 出 全 部 信 息*//
- void readfromfile();//*讀取文件*//
- void writetofile();//*寫入文件*// /
- void deletebycelephone(); //*按手機號碼刪除*//
- void deletebyname();//*按姓名刪除*/
- void listbycelephone();//*按手機號碼排序*//
- void listbyname();//*按姓名排序*//
- void listmenu();//*排序菜單*//
-
- void main()//*主函數*//
- {
- system("cls");
- readfromfile();
- while (1)
- {
- mainmenu();
- }
- } //*主函數結束*//
- void mainmenu()//*主菜單函數*//
- {
- int a;
- system("cls");
- system("color 2a");
- printf("\n **************您 好! 歡 迎 使 用 通 訊 錄************\n");
- printf(" * 1: 添 加 通 訊 錄 信 息 *\n");
- printf(" * 2: 查 詢 通 訊 錄 信 息 *\n");
- printf(" * 3: 刪 除 通 訊 錄 信 息 *\n");
- printf(" * 4: 修 改 通 訊 錄 信 息 *\n");
- printf(" * 5: 通 訊 錄 信 息 排 序 *\n");
- printf(" * 6: 輸 出 通 訊 錄 信 息 *\n");
- printf(" * 7: 保存文件(添加后必選) *\n");
- printf(" * 8: 讀 取 通 訊 錄 文 件 *\n");
- printf(" * 9: 刪 除 所 有 聯 系 人 *\n");
- printf(" * 0: 退 出 通 訊 錄 系 統 *\n");
- printf(" *****************************************************\n");
- printf("\n 請輸入您的數字選擇: ");
- loop1: scanf("%d",&a);
- switch (a)
- {
- case 1:newrecord();break;
- case 2:searchmenu();break;
- case 3:deletemenu();break;
- case 4:xiugaimenu();break;
- case 5:listmenu();break;
- case 6:showall();break;
- case 7:writetofile();break;
- case 8:readfromfile();break;
- case 9:deleteall();break;
- case 0:exit(0);
- default:printf("對不起!您的輸入有誤!請重新輸入: ");goto loop1;
- }
- }
- void newrecord()//*添加紀錄*//
- {
- int a;
- system("cls");
- system("color 2a");
- loop: printf("\n\n\n\n\t\t 請輸入您要添加的聯系人的信息: \n");
- printf("\n請輸入姓名(小于15個漢字):\n");
- name: scanf("%s",&communication[num].name);
- if(strlen(communication[num].name)>15)
- {
- printf("對不起!您的輸入有誤!請重新輸入");
- goto name;
- }
- printf("\n請輸入該聯系人類別:\n");
- type: scanf("%s",&communication[num].type);
- printf("\n請輸入年齡(0-110之間):\n");
- nianling: scanf("%d",&communication[num].nianling);
- if(communication[num].nianling<0||communication[num].nianling>110)
- {
- printf("對不起!您的輸入有誤!請重新輸入");
- goto nianling;
- }
- printf("\n請輸入郵編(5-6個數字):\n");
- youbian: scanf("%d",&communication[num].youbian);
- if(communication[num].youbian<10000||communication[num].youbian>999999)
- {
- printf("對不起!您的輸入有誤!請重新輸入");
- goto youbian;
- }
- printf("\n請輸入地址(0-50個漢字):\n");
- addr: scanf("%s",&communication[num].addr);
- if(strlen(communication[num].addr)>50||strlen(communication[num].addr)<0)
- {
- printf("對不起!您的輸入有誤!請重新輸入:\n");
- goto addr;
- }
- printf("\n請輸入手機號碼(11個數字):\n");
- celephone: scanf("%s",&communication[num].celephone);
- if(strlen(communication[num].celephone)!=11)
- {
- printf("對不起!您的輸入有誤!請重新輸入\n");
- goto celephone;
- }
- printf("\n請輸入固定電話(8-11個數字):\n");
- homephone: scanf("%s",&communication[num].homephone);
- printf("\n請輸入公司名稱:\n");
- company: scanf("%s",&communication[num].company);
- printf("\n輸入電子郵箱(4-30個字符):\n");
- email: scanf("%s",&communication[num].email);
- if(strlen(communication[num].email)>30||strlen(communication[num].email)<4)
- {
- printf("對不起!您的輸入有誤!請重新輸入");
- goto email;
- }
- printf("\n輸入QQ號碼:\n");
- QQ: scanf("%s",&communication[num].QQ);
- num++;
- printf("\n是否繼續添加?\n");
- printf("\n請按1和2(1代表繼續;2代表不繼續)\n");
- printf("請輸入您的選擇 ");
- scanf("%d",&a);
- if(a==1)
- {
- goto loop;
- }
- else
- {
- return;
- }
- }
- void searchmenu()//*查詢菜單*//
- {
- int a;
- system("cls");
- system("color 2a");
- printf("\n\t********歡 迎 使 用 查 詢 菜 單********");
- printf("\n\t\t >>> 1-按手機號碼查詢");
- printf("\n\t\t >>> 2-按姓名查詢");
- printf("\n\t\t >>> 3-按固定電話查詢");
- printf("\n\t\t >>> 4-按QQ查詢");
- printf("\n\t\t >>> 5-返回主菜單");
- printf("\n\t\t");
- printf(" >>> 請輸入您的選擇:");
- scanf("%d",&a);
- switch (a)
- {
- case 1:searchbycelephone();break;
- case 2:searchbyname();break;
- case 3:searchbyhomephone();break;
- case 4:searchbyqq();break;
- case 5:mainmenu();break;
- }
- }
- void searchbycelephone()//*按手機號碼查詢*//
- {
- int mark=0;
- int i,j;
- int a=0;
- char celephone[11];
- printf("\n 按手機號碼查找 \n");
- printf("\n請輸入手機號碼:\n");
- scanf("%s",celephone);
- for(i=0;i<num;i++)
- {
- if (strcmp(communication[i].celephone,celephone)==0)
- {
- printf("\n*****************以下是您查找的聯系人的信息***************");
- printf("\n________________________________");
- printf("\n 名字: %s",communication[i].name);
- printf("\n 類別: %s",communication[i].type);
- printf("\n 年齡: %d",communication[i].nianling);
- printf("\n 郵編: %ld",communication[i].youbian);
- printf("\n 地址: %s",communication[i].addr);
- printf("\n 手機號碼:%s",communication[i].celephone);
- printf("\n 固定電話:%s",communication[i].homephone);
- printf("\n 公司名稱:%s",communication[i].company);
- printf("\n 電子郵件:%s",communication[i].email);
- printf("\n QQ號碼:%s",communication[i].QQ);
- printf("\n________________________________");
- printf("\n請按任意數字鍵并回車返回主菜單");
- mark++;
- scanf("%d",&j);
- return;
- }
- }
- if (mark==0)
- {
- printf("\n對不起!沒有該聯系人的信息!");
- printf("\n請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- }
- void searchbyname()//*按姓名查詢*//
- {
- int mark=0;
- int i,j,findmark=0;
- int a=0;
- char name[15];
- printf("\n ***********************按 姓 名 查 找***********************");
- printf("\n請輸入姓名:");
- scanf("%s",name);
- for(i=a;i<num;i++)
- {
- if (strcmp(communication[i].name,name)==0)
- {
- findmark++;
- printf("\n\t\t************以下是您查找的聯系人的信息************");
- printf("\n\t\t________________________________");
- printf("\n\t\t 名字: %s",communication[i].name);
- printf("\n\t\t 類別: %s",communication[i].type);
- printf("\n\t\t 年齡: %d",communication[i].nianling);
- printf("\n\t\t 郵編: %ld",communication[i].youbian);
- printf("\n\t\t 地址: %s",communication[i].addr);
- printf("\n\t\t 手機號碼:%s",communication[i].celephone);
- printf("\n\t\t 固定電話:%s",communication[i].homephone);
- printf("\n\t\t 公司名稱:%s",communication[i].company);
- printf("\n\t\t 電子郵件:%s",communication[i].email);
- printf("\n\t\t QQ號碼:%s",communication[i].QQ);
- printf("\n\t\t________________________________");
- if ((i+1)<num)
- {
- printf("\n是否繼續查找相同名字的聯系人的信息?(1代表繼續;2代表不繼續)");
- scanf("%d",&j);
- if (j==1)
- {
- a=i;
- mark++;
- continue;
- }
- else
- break;
- }
- else
- {
- printf("\n請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- }
- }
- if(mark!=0)
- {
- printf("\n對不起! 沒有相同名字的該聯系人的信息!!");
- printf("\n請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- else if(findmark==0)
- {
- printf("\n對不起!沒有該聯系人的信息!");
- printf("\n請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- }
- void searchbyhomephone()//*按固定電話查詢*//
- {
- int mark=0;
- int i,j;
- int a=0;
- char homephone[11];
- printf("\n\t ******************按 固 定 電 話 查 找******************");
- printf("\n請輸入固定電話:");
- scanf("%s",homephone);
- for(i=0;i<num;i++)
- {
- if (strcmp(communication[i].homephone,homephone)==0)
- {
- printf("\n\t\t**********以下是您要查找的聯系人的信息**********");
- printf("\n\t\t________________________________");
- printf("\n\t\t 名字: %s",communication[i].name);
- printf("\n\t\t 類別: %s",communication[i].type);
- printf("\n\t\t 年齡: %d",communication[i].nianling);
- printf("\n\t\t 郵編: %ld",communication[i].youbian);
- printf("\n\t\t 地址: %s",communication[i].addr);
- printf("\n\t\t 手機號碼:%s",communication[i].celephone);
- printf("\n\t\t 固定電話:%s",communication[i].homephone);
- printf("\n\t\t 公司名稱:%s",communication[i].company);
- printf("\n\t\t 電子郵件:%s",communication[i].email);
- printf("\n\t\t QQ號碼: %s",communication[i].QQ);
- printf("\n\t\t________________________________");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- mark++;
- scanf("%d",&j);
- return;
- }
- }
- if (mark==0)
- {
- printf("\n\t\t對不起!沒有該聯系人的信息!");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- }
- void searchbyqq()//*按QQ查詢*//
- {
- int mark=0;
- int i,j;
- int a=0;
- char QQ[22];
- printf("\n\t**********************按 QQ 號 碼 查 找***********************\n");
- printf("\n請輸入QQ號碼:");
- scanf("%s",&QQ);
- for(i=0;i<num;i++)
- {
- if (strcmp(communication[i].QQ,QQ)==0)
- {
- printf("\n\t***********以下是您查找的聯系人的信息**********");
- printf("\n\t\t________________________________");
- printf("\n\t\t 名字: %s",communication[i].name);
- printf("\n\t\t 類別: %s",communication[i].type);
- printf("\n\t\t 年齡: %d",communication[i].nianling);
- printf("\n\t\t 郵編: %ld",communication[i].youbian);
- printf("\n\t\t 地址: %s",communication[i].addr);
- printf("\n\t\t 手機號碼:%s",communication[i].celephone);
- printf("\n\t\t 固定電話:%s",communication[i].homephone);
- printf("\n\t\t 公司名稱:%s",communication[i].company);
- printf("\n\t\t 電子郵件:%s",communication[i].email);
- printf("\n\t\t QQ號碼: %s",communication[i].QQ);
- printf("\n\t\t________________________________");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- mark++;
- scanf("%d",&j);
- return;
- }
- }
- if (mark==0)
- {
- printf("\n\t\t對不起!沒有該聯系人的信息!");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- }
-
- void deletemenu()//*刪除菜單*//
- {
- int i,j;
- system("cls");
- system("color 3b");
- if(num==0)
- {
- printf("\n\t\t對不起!文件中無任何紀錄!");
- printf("\n\t\t請先添加個人信息!");
- printf("\n\t\t請按任意數字鍵并回車返回主菜單");
- scanf("%d",&j);
- return;
- }
- else
- {
- printf("\n\t\t 刪 除 菜 單 ");
- printf("\n\t\t >>> 1-刪除單個 ");
- printf("\n\t\t >>> 2-返回主菜單 ");
- printf("\n\t\t ");
- printf("\n\t\t請輸入您的數字選擇: ");
- loop4: scanf("%d",&i);
- switch (i)
- {
- case 1:deleteone();break;
- case 2:mainmenu();break;
- default:printf("對不起!您的輸入有誤!請重新輸入: ");goto loop4;
- }
- }
- }
- void deleteone()//*刪除單個*//
- {
- int i;
- printf("\n\t\t1-按手機號碼刪除 2-按姓名刪除");
- printf("\n\t\t請輸入您的選擇:");
- loop5:scanf("%d",&i);;
- switch (i)
- {
- case 1:deletebycelephone();break;
- case 2:deletebyname();break;
- default:printf("對不起!您的輸入有誤!請重新輸入: ");goto loop5;
- }
- }
- void deletebyname()//姓名刪除*//
- {
- int a=0;
- int findmark=0;
- int i,j,k;
- int deletemark=0;
- char name[15];
- printf("\n\t\t請輸入您要刪除的聯系人的姓名:");
- scanf("%s",name);
- for (i=a;i<num;i++)
- {
- if (strcmp(communication[i].name,name)==0)
- {
- printf("\n\t\t以下是您要刪除的聯系人的紀錄:");
- findmark++;
- printf("\n\t\t________________________________");
- printf("\n\t\t 名字: %s",communication[i].name);
- printf("\n\t\t 類別: %s",communication[i].type);
- printf("\n\t\t 年齡: %d",communication[i].nianling);
- printf("\n\t\t 郵編: %ld",communication[i].youbian);
- printf("\n\t\t 地址: %s",communication[i].addr);
- printf("\n\t\t 手機號碼:%s",communication[i].celephone);
- printf("\n\t\t 固定電話:%s",communication[i].homephone);
- printf("\n\t\t 公司名稱:%s",communication[i].company);
- printf("\n\t\t 電子郵件:%s",communication[i].email);
- printf("\n\t\t QQ號碼: %s",communication[i].QQ);
- printf("\n\t\t________________________________");
- printf("\n\t\t是否刪除?(1代表是;2代表否)\n");
- printf("\n\t\t請輸入您的數字選擇: ");
- scanf("%d",&k);
- if(k==1)
- {
- for (j=i;j<num-1;j++) //*紀錄移動,從communication數組中刪除之*//
- communication[j]=communication[j+1];
- num--;
- deletemark++;
- printf("\n\t\t刪除成功");
- if((i+1)<num)
- {
- printf("\n\t\t是否繼續刪除相同姓名的聯系人的信息?(1代表刪除;2代表不刪除)");
- printf("\n\t\t請輸入您的數字選擇: ");
- scanf("%d",&k);
- if(k=1)
- {
- a=i;
- findmark++;
- continue;
- }
- }
- printf("\n\t\t是否繼續刪除?(1代表繼續;2代表不繼續)");
- printf("\n\t\t請輸入您的數字選擇: ");
- scanf("%d",&k);
- if(k==1)
- deleteone();
- else
- break;
- }
- }
- else
- continue;
- }
- if((deletemark==0)&&(findmark==0))
- {
- printf("\n\t\t對不起!沒有該聯系人的紀錄!");
- printf("\n\t\t是否繼續刪除?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&k);
- if(k==1)
- deleteone();
- }
- else if(findmark!=0)
- {
- printf("\n\t\t對不起!沒有重名信息!");
- printf("\n\t\t是否繼續刪除?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&k);
- if(k==1)
- deleteone();
- }
- }
- void deletebycelephone()//*按手機號碼刪除*//
- {
- int i,j,h,m;
- int deletemark=0;
- char celephone[11];
- printf("\n\t\t請輸入要刪除的聯系人的手機號碼:");
- scanf("%s",celephone);
- for(i=0;i<num;i++)
- {
- if(strcmp(communication[i].celephone,celephone)==0)
- {
- deletemark++;
- printf("\n\t\t以下是您要刪除的聯系人的紀錄:");
- printf("\n\t\t________________________________");
- printf("\n\t\t 名字: %s",communication[i].name);
- printf("\n\t\t 類別: %s",communication[i].type);
- printf("\n\t\t 年齡: %d",communication[i].nianling);
- printf("\n\t\t 郵編: %ld",communication[i].youbian);
- printf("\n\t\t 地址: %s",communication[i].addr);
- printf("\n\t\t 手機號碼:%s",communication[i].celephone);
- printf("\n\t\t 固定電話:%s",communication[i].homephone);
- printf("\n\t\t 公司名稱:%s",communication[i].company);
- printf("\n\t\t 電子郵件:%s",communication[i].email);
- printf("\n\t\t QQ號碼: %s",communication[i].QQ);
- printf("\n\t\t________________________________");
- printf("\n\t\t是否刪除?(1代表是;2代表否)\n");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&h);
- if(h==1)
- {
- for (j=i;j<num-1;j++) /*紀錄移動,從communication數組中刪除之*/
- communication[j]=communication[j+1];
- num--;
- printf("\n\t\t刪除成功");
- printf("\n是否繼續刪除?(1代表繼續;2代表不繼續)\n");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&h);
- if(h==1)
- deletebycelephone();
- }
- }
- }
- if(deletemark==0)
- {
- printf("\n\t\t對不起!沒有該聯系人的紀錄!");
- printf("\n\t\t是否繼續刪除?(1代表繼續;2代表不繼續)\n");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&m);
- if(m==1)
- deletebycelephone();
- }
- }
- void xiugaimenu()//*修改菜單*//
- {
- int i,j;
- system("cls");
- system("color 2a");
- if(num==0)
- {
- printf("\n\t\t對不起!文件中無任何紀錄!");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- else
- {
- printf("\n\t\t 1-按姓名修改 2-按手機號碼修改\n");
- printf(" 請輸入您的數字選擇: ");
- loop6:scanf("%d",&i);
- switch (i)
- {
- case 1:xiugaibyname();break;
- case 2:xiugaibycelephone();break;
- default:printf("對不起!您的輸入有誤!請重新輸入: ");goto loop6;
- }
- }
- }
- void xiugaibyname()//*按姓名修改*//
- {
- int a=0;
- int findmark=0;
- int xiugaimark=0;
- int i,j;
- char name[15];
- printf("\n\t\t請輸入要修改聯系人的姓名:");
- scanf("%s",name);
- for (i=a;i<num;i++)
- {
- if (strcmp(communication[i].name,name)==0)
- {
- printf("\n\t\t以下是您要修改的聯系人的紀錄:");
- findmark++;
- printf("\n\t\t________________________________");
- printf("\n\t\t 名字: %s",communication[i].name);
- printf("\n\t\t 類別: %s",communication[i].type);
- printf("\n\t\t 年齡: %d",communication[i].nianling);
- printf("\n\t\t 郵編: %ld",communication[i].youbian);
- printf("\n\t\t 地址: %s",communication[i].addr);
- printf("\n\t\t 手機號碼:%s",communication[i].celephone);
- printf("\n\t\t 固定電話:%s",communication[i].homephone);
- printf("\n\t\t 公司名稱:%s",communication[i].company);
- printf("\n\t\t 電子郵件:%s",communication[i].email);
- printf("\n\t\t QQ號碼: %s",communication[i].QQ);
- printf("\n\t\t________________________________");
- printf("\n\t\t是否修改?(1代表是;2代表否)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&j);
- if (j==1)
- {
- xiugaixiangmu(i);
- xiugaimark++;
- printf("\n\t\t修改成功");
- if((i+1)<num)
- {
- printf("\n\t\t是否繼續修改相同姓名的聯系人的信息?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&j);
- if (j==1)
- {
- a=i;
- findmark++;
- continue;
- }
- }
- printf("\n\t\t是否繼續修改?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&j);
- if (j==1)
- xiugaibyname();
- return;
- }
- }
- }
- if ((xiugaimark==0)&&(findmark==0))
- {
- printf("\n\t\t沒有該聯系人的紀錄");
- printf("\n\t\t是否繼續修改?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&j);
- if (j==1)
- xiugaibyname();
- return;
- }
- else if (findmark!=0)
- {
- printf("\n\t\t沒有重名信息");
- printf("\n\t\t是否繼續修改?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&j);
- if (j==1)
- xiugaibyname();
- return;
- }
- }
- void xiugaibycelephone()//*按手機號碼修改*//
- {
- int findmark=0;
- int i,j;
- char celephone[11];
- printf("\n\t\t請輸入要修改聯系人的手機號碼:");
- scanf("%s",celephone);
- for (i=0;i<num;i++)
- {
- if (strcmp(communication[i].celephone,celephone)==0)
- {
- printf("\n\t\t以下是您要修改的聯系人的紀錄:");
- findmark++;
- printf("\n\t\t________________________________");
- printf("\n\t\t 名字: %s",communication[i].name);
- printf("\n\t\t 類別: %s",communication[i].type);
- printf("\n\t\t 年齡: %d",communication[i].nianling);
- printf("\n\t\t 郵編: %ld",communication[i].youbian);
- printf("\n\t\t 地址: %s",communication[i].addr);
- printf("\n\t\t 手機號碼:%s",communication[i].celephone);
- printf("\n\t\t 固定電話:%s",communication[i].homephone);
- printf("\n\t\t 公司名稱:%s",communication[i].company);
- printf("\n\t\t 電子郵件:%s",communication[i].email);
- printf("\n\t\t QQ號碼: %s",communication[i].QQ);
- printf("\n\t\t________________________________");
- printf("\n\t\t是否修改?(1代表是;2代表否)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&j);
- if (j==1)
- {
- xiugaixiangmu(i);
- printf("\n\t\t修改成功");
- printf("\n\t\t是否繼續修改?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&j);
- printf(" 請輸入您的數字選擇: ");
- if (j==1)
- xiugaixiangmu(i);
- break;
- }
- }
- }
- if (findmark==0)
- {
- printf("\n\t\t對不起!沒有該聯系人的紀錄!");
- printf("\n\t\t是否繼續修改?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&j);
- if (j==1)
- xiugaibycelephone();
- return;
- }
- }
- void xiugaixiangmu(int a)//*修改項目*//
- {
- int i;
- system("cls");
- system("color c0");
- printf("\n\t\t 請輸入您要修改的項目 ");
- printf("\n\t\t\n");
- printf("\n\t\t 1-手機號碼\n");
- printf("\n\t\t 2-姓名\n");
- printf("\n\t\t 3-固定電話\n");
- printf("\n\t\t 4-QQ號碼\n");
- printf("\n\t\t 5-地址\n");
- printf("\n\t\t 6-郵編\n");
- printf("\n\t\t 7-電子郵件\n");
- printf("\n\t\t 8-回主菜單\n");
- printf("\n\t\t\n");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&i);
- loop9: switch (i)
- {
- case 1:printf("請輸入手機號碼:");
- scanf("%s",&communication[a].celephone);break;
- case 2:printf("請輸入姓名:");
- scanf("%s",&communication[a].name);break;
- case 3:printf("請輸入固定電話:");
- scanf("%s",&communication[a].homephone);break;
- case 4:printf("請輸入QQ號碼:");
- scanf("%s",&communication[a].QQ);break;
- case 5:printf("請輸入地址:");
- scanf("%s",&communication[a].addr);break;
- case 6:printf("請輸入郵編:");
- scanf("%s",&communication[a].youbian);break;
- case 7:printf("請輸入email:");
- scanf("%s",&communication[a].email);break;
- case 8:mainmenu();break;
- default: printf("對不起!您的輸入有誤!請重新輸入: ");goto loop9;
- }
- }
- void listmenu()//*排序菜單*//
- {
- int i,j;
- system("cls");
- system("color b0");
- if(num==0)
- {
- printf("\n\t\t對不起,文件中無任何紀錄");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- else
- {
- system("cls");
- system("color 12");
- printf("\n\t\t 排 序 菜 單 ");
- printf("\n\t\t >>> 1-按姓名排序 ");
- printf("\n\t\t >>> 2-按手機號碼排序 ");
- printf("\n\t\t >>> 3-返回主菜單 ");
- printf(" 請輸入您的數字選擇: ");
- loop10: scanf("%d",&i);
- switch(i)
- {
- case 1:listbyname();break;
- case 2:listbycelephone();break;
- case 3:mainmenu();break;
- default:printf("對不起!您的輸入有誤!請重新輸入: ");goto loop10;
- }
- }
- }
- void listbyname()//*按姓名排序*//
- {
- int i,j,k;
- struct person tmp;
- for (i=1;i<num;i++)
- {
- if(strcmp(communication[i].name,communication[i-1].name)<0)
- {
- tmp=communication[i];
- j=i-1;
- do
- {
- communication[j+1]=communication[j];
- j--;
- }
- while ((strcmp(tmp.name,communication[j].name)<0&&j>=0));
- communication[j+1]=tmp;
- }
- }
- printf("\n\t\t排序成功,是否顯示?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&k);
- if (k==1)
- showall();
- return;
- }
- void listbycelephone()//*按手機號碼排序*//
- {
- int i,j,k;
- struct person tmp;
- for (i=1;i<num;i++)
- {
- if(strcmp(communication[i].celephone,communication[i-1].celephone)<0)
- {
- tmp=communication[i];
- j=i-1;
- do
- {
- communication[j+1]=communication[j];
- j--;
- }while ((strcmp(tmp.celephone,communication[j].celephone)<0)&&j>=0);
- communication[j+1]=tmp;
- }
- }
- printf("\n\t\t排序成功,是否顯示?(1代表繼續;2代表不繼續)");
- printf(" 請輸入您的數字選擇: ");
- scanf("%d",&k);
- if (k==1)
- showall();
- return;
- }
- void showall()//*輸 出 全 部 信 息*//
- {
- int i,j;
- system("cls");
- system("color 3b");
- if(num!=0)
- {
- printf("\n\t\t*************** 以下為通訊錄所有信息************");
- for (i=0;i<num;i++)
- {
- printf("\n\t\t________________________________");
- printf("\n\t\t 名字: %s",communication[i].name);
- printf("\n\t\t 類別: %s",communication[i].type);
- printf("\n\t\t 年齡: %d",communication[i].nianling);
- printf("\n\t\t 郵編: %ld",communication[i].youbian);
- printf("\n\t\t 地址: %s",communication[i].addr);
- printf("\n\t\t 手機號碼:%s",communication[i].celephone);
- printf("\n\t\t 固定電話:%s",communication[i].homephone);
- printf("\n\t\t 公司名稱:%s",communication[i].company);
- printf("\n\t\t 電子郵件:%s",communication[i].email);
- printf("\n\t\t QQ號碼: %s",communication[i].QQ);
- printf("\n\t\t________________________________");
- printf("\t\t");
- if (i+1<num)
- {
- printf("\n\t\t__________________________");
- system("pause");
- }
- }
- printf("\n\t\t************************************************");
- }
- else
- printf("\n\t\t對不起!通訊錄中無任何紀錄!");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- void writetofile()//*寫入文件*//
- {
- int i,k;
- system("cls");
- system("color 84");
- if ((fp=fopen("communication.txt","wb"))==NULL)
- {
- printf("\n\t\t文件打開失敗");
- }
- for (i=0;i<num;i++)
- {
- if (fwrite(&communication[i],sizeof(struct person),1,fp)!=1)
- {
- printf("\n\t\t寫入文件錯誤!\n");
- }
- }
- fclose(fp);
- printf("\n\t\t通訊錄文件已保存");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- scanf("%d",&k);
- return;
- }
-
- void readfromfile()//*讀取文件*//
- {
- int i,j,k;
- system("cls");
- system("color 2b");
- if((fp=fopen("communication.txt","rb"))==NULL)
- {
- printf("\n\t\t ****************************");
- printf("\n\t\t * *");
- printf("\n\t\t * *");
- printf("\n\t\t * 通訊錄文件不存在 *");
- if ((fp=fopen("communication.txt","wb"))==NULL)
- {
- printf("\n * 建立失敗 *");
- printf("\n * *");
- printf("\n * *");
- printf("\n ****************************");
- exit(0);
- }
- else
- {
- printf("\n\t\t * 通訊錄文件已建立 *");
- printf("\n\t\t * *");
- printf("\n\t\t * *");
- printf("\n\t\t ****************************");
- printf("\n\t\t >>> 進入主菜單 ");
- printf("\n\t\t >>> 請輸入任意數字并回車: ");
- scanf("%d",&k);
- return;
- }
- exit(0);
- }
- fseek(fp,0,2); //*文件位置指針移動到文件末尾*//
- if (ftell(fp)>0) //*文件不為空*//
- {
- rewind(fp); //*文件位置指針移動到文件開始位置*//
- for (num=0;!feof(fp) && fread(&communication[num],sizeof(struct person),1,fp);num++);
- printf("\n\t\t ****************************");
- printf("\n\t\t * *");
- printf("\n\t\t * *");
- printf("\n\t\t * 文件導入成功! *");
- printf("\n\t\t * *");
- printf("\n\t\t * *");
- printf("\n\t\t ****************************");
- printf("\n\t\t >>> 按1顯示所有信息,按2回主菜單! ");
- printf("\t\t >>> 請輸入您的數字選擇: ");
- scanf("%d",&j);
- if(j==1)
- showall();
- }
- else
- {
- printf("\n\t\t ****************************");
- printf("\n\t\t * *");
- printf("\n\t\t * *");
- printf("\n\t\t * 文件導入成功! *");
- printf("\n\t\t * 通訊錄文件中無任何紀錄! *");
- printf("\n\t\t * *");
- printf("\n\t\t * *");
- printf("\n\t\t ****************************");
- printf("\n\t\t >>> 請按任意數字鍵返回主菜單");
- scanf("%d",&i);
- return;
- }
- }
- void deleteall()//*刪除所有信息*//
- {
- int i,j;
- system("cls");
- system("color 50");
- printf("\n\t\t確認刪除?\n");
- printf("請按1和2(1代表確認;2代表不刪除)\n ");
- scanf("%d",&i);
- if (i==1)
- {
- fclose(fp);
- if ((fp=fopen("communication.txt","wb"))==NULL)
- {
- printf("\n\t\t不能打開文件,刪除失敗");
- readfromfile();
- }
- num=0;
- printf("\n\t\t紀錄已刪除!");
- printf("\n\t\t請按任意數字鍵返回主菜單");
- scanf("%d",&j);
- return;
- }
- }
復制代碼
|
|