* main_tongxunlu.c
*
* Created on: 2011-6-21
* Author: zhanglujin
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct record
{
char name[20]; //姓名
char phone[12]; //電話
char adress[50]; //地址
char postcode[8]; //郵政編碼
char e_mail[20]; //電子郵件。
}student[100]; //假設最大數為100.
//定義全局變量num,表示已經輸入的人數 。
int num; //這裏使用數組解決通訊錄的問題,實際上使用鏈表更好。
int menu_select()
{
char s[80];
int a;/*定義整形變量*/
system("cls");
printf("\t\t***********歡迎進入通訊管理界面********\n\n");
printf("\t\t\t0. 輸入記錄\n");
printf("\t\t\t1. 顯示記錄\n");
printf("\t\t\t2. 按姓名查找\n");
printf("\t\t\t3. 按電話號碼查找\n");
printf("\t\t\t4. 插入記錄 \n");
printf("\t\t\t5. 按姓名排序\n");
printf("\t\t\t6. 刪除記錄\n");
printf("\t\t\t7. Quit\n");
printf("\t\t***********************************************\n\n");
do{
printf("Enter you choice(0~7):");
scanf("%s",s);
a=atoi(s);
}
while (a<0 || a>7);
return a;
}
int adduser()
{
printf("\t\t\t**************** 請輸入用戶信息 ****************\n");
printf("\t\t\t輸入姓名:\n");
scanf("%s",student[num].name);
printf("\t\t\t輸入電話號碼:\n");
scanf("%s",student[num].phone);
printf("\t\t\t輸入地址:\n");
scanf("%s",student[num].adress);
printf("\t\t\t輸入郵編:\n");
scanf("%s",student[num].postcode);
printf("\t\t\t輸入e-mail:\n");
scanf("%s",student[num].e_mail);
num++;
printf("\t\t\t是否繼續添加?(Y/N):\n");
if(getch()=='y' || getch()=='Y')
adduser();
return(0);
}
void list()
{
int i;
system("cls");
if(num!=0)
{
printf("\t\t\t*************** 以下為通訊錄所有信息************\n");
for (i=0;i<num;i++)
{
printf("\t\t\t姓名:%s\n",student[i].name);
printf("\t\t\t電話:%s\n",student[i].phone);
printf("\t\t\t地址:%s\n",student[i].adress);
printf("\t\t\t郵編:%s\n",student[i].postcode);
printf("\t\t\te-mail:%s\n",student[i].e_mail);
if(i+1<num)
{
system("pause");
}
}
printf("\t\t\t************************************************\n");
}
else
printf("\t\t\t通訊錄中無任何紀錄\n");
printf("\t\t\t按任意鍵返回主菜單:\n");
getch(); //這裏是無回顯的輸入字符,妳輸入的字符不會顯示在屏幕上。
return;
}
int searchbyname()
{
int mark=0;
int i;
printf("\t\t\t***************** 按姓名查找 *******************\n");
char name[20];
printf("\t\t\t請輸入姓名:\n");
scanf("%s",name);
for(i=0;i<num;i++)
{
if (strcmp(student[i].name,name)==0)
{
printf("\t\t\t************* 以下是您查找的用戶信息 ***********\n");
printf("\t\t\t姓名: %s",student[i].name);
printf("\t\t\t電話: %s",student[i].phone);
printf("\t\t\t地址: %s",student[i].adress);
printf("\t\t\te-mail:%s",student[i].e_mail);
printf("\t\t\t************************************************\n");
mark++;
if((i+1)<num)
{
printf("\t\t\t是否繼續查找相同名字的用戶信息:(y/n)\n");
if(getch()=='y' || getch()=='Y')
{
continue;
}
else
return(0);
}
else
{
printf("\t\t\t按任意鍵返回主菜單");
getch();
return(0);
}
}
}
if(mark == 0)
{
printf("\t\t\t沒有相同姓名的用戶紀錄\n");
printf("\t\t\t按任意鍵返回主菜單\n");
getch();
return(0);
}
return 0;
}
int searchbyphone()
{
int mark=0;
int i;
printf("\t\t\t****************** 按電話查找 ******************\n");
char phone[10];
printf("\t\t\t請輸入電話號碼:\n");
scanf("%s",phone);
for(i=0;i<num;i++)
{
if (strcmp(student[i].phone,phone)==0)
{
printf("\t\t\t************** 以下是您查找的用戶信息 **********\n");
printf("\t\t\t姓名: %s",student[i].name);
printf("\t\t\t電話: %s",student[i].phone);
printf("\t\t\t地址: %s",student[i].adress);
printf("\t\t\te-mail:%s",student[i].e_mail);
printf("\t\t\t************************************************\n");
printf("\t\t\t按任意鍵返回主菜單\n");
mark++;
getch();
return(0);
}
}
if (mark==0)
{
printf("\t\t\t沒有改用戶的信息\n");
printf("\t\t\t按任意鍵返回主菜單\n");
getch();
return(0);
}
return(0);
}
void deletebyphone()
{
int i,j;
int deletemark=0;
char phone[20];
printf("\t\t\t請輸入要刪除用戶電話號碼:\n");
scanf("%s",phone);
if(num==0)
{
printf("\t\t\t對不起,文件中無任何紀錄\n");
printf("\t\t\t按任意鍵返回主菜單\n");
getch();
return;
}
for (i=0;i<num;i++)
{
if (strcmp(student[i].phone,phone)==0)
{
printf("\t\t\t以下是您要刪除的用戶紀錄:\n");
printf("\t\t\t姓名: %s",student[i].name);
printf("\t\t\t電話: %s",student[i].phone);
printf("\t\t\t地址: %s",student[i].adress);
printf("\t\t\te-mail:%s",student[i].e_mail);
printf("\t\t\t是否刪除?(y/n)");
if (getch()=='y' || getch()=='Y')
{
for (j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf("\t\t\t刪除成功");
printf("\t\t\t是否繼續刪除?(y/n)");
if (getch()=='y' || getch()=='Y')
deletebyphone();
return;
}
else
return;
}
continue;
}
if (deletemark==0)
{
printf("\t\t\t沒有該用戶的紀錄");
printf("\t\t\t是否繼續刪除?(y/n)");
if(getch()=='y' || getch()=='Y')
deletebyphone();
return;
}
return;
}
void deletebyname()
{
int a=0;
int findmark=0;
int j;
int deletemark=0;
int i;
char name[20];
printf("\t\t\t請輸入要刪除用戶姓名:\n");
scanf("%s",name);
for (i=a;i<num;i++)
{
if(strcmp(student[i].name,name)==0)
{
printf("\t\t\t以下是您要刪除的用戶紀錄:");
findmark++;
printf("\t\t\t________________________________");
printf("\t\t\t姓名: %s",student[i].name);
printf("\t\t\t電話: %s",student[i].phone);
printf("\t\t\t地址: %s",student[i].adress);
printf("\t\t\te-mail:%s",student[i].e_mail);
printf("\t\t\t________________________________");
printf("\t\t\t是否刪除?(y/n)");
if (getch()=='y' || getch() == 'Y')
{
for(j=i;j<num-1;j++)
student[j]=student[j+1];
num--;
deletemark++;
printf("\t\t\t刪除成功");
if((i+1)<num)
{
printf("\t\t\t是否繼續刪除相同姓名的用戶信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
printf("\t\t\t是否繼續刪除?(y/n)");
if (getch()=='y')
deletebyname();
return;
}
if((i+1)<num)
{
printf("\t\t\t是否繼續刪除相同姓名的用戶信息?(y/n)");
if (getch()=='y' || getch() == 'Y')
{
a=i;
continue;
}
}
}
else
continue;
}
if ((deletemark==0)&&(findmark==0))
{
printf("\t\t\t沒有該用戶的紀錄");
printf("\t\t\t是否繼續刪除?(y/n)");
if(getch()=='y' || getch() == 'Y')
deletebyphone();
return;
}
else if (findmark!=0)
{
printf("\t\t\t沒有重名信息");
printf("\t\t\t沒有該用戶的紀錄");
printf("\t\t\t是否繼續刪除?(y/n)");
if(getch()=='y' || getch() == 'Y')
deletebyphone();
return;
}
}
int dele()
{
char choic;
printf("\t\t\t1-按電話號碼刪除 2-按姓名刪除");
printf("\t\t\t請選擇:");
choic=getch();
switch (choic)
{
case '1':deletebyphone();break;
case '2':deletebyname();break;
}
return(0);
}
int sortbyname() //按姓名進行排序
{
int i,j;
struct record tmp;
for (i=1;i<num;i++)
{
if(strcmp(student[i].name,student[i-1].name)<0)
{
tmp=student[i];
j=i-1;
do
{
student[j+1]=student[j];
j--;
}while ((strcmp(tmp.name,student[j].name)<0&&j>=0));
student[j+1]=tmp;
}
}
printf("\t\t\t排序成功,是否顯示?(y/n)");
if (getch()=='y')
list();
return(0);
}
int main()
{
printf("\t\t************************************************\n");
printf("\t\t********welcome to TONGXUNLU *******************\n");
printf("\t\t###########code by XXXXX ###################\n");
printf("\t\t*************************************************\n");
printf("按任意鍵進入主菜單\n");
getch();
int selectnum;
while(1)
{
selectnum = menu_select();
switch(selectnum)
{
case 0:
{
adduser();
break;
}
case 1:
{
list();
break;
}
case 2:
{
searchbyname();
break;
}
case 3:
{
searchbyphone();
break;
}
case 4:
{
adduser(); //這裏插入,應該能指定位置,不過意義不大,所以和添加記錄壹樣了。
break;
}
case 5:
{
sortbyname();
break;
}
case 6:
{
dele();
break;
}
case 7:
{
printf("BYE BYE!\n");
system("pause");
getchar();
exit(0);
}
}
}
getchar();
return 0;
}