#include #include #include #include #define HEADER1 \"------------------------------telephone book --\\n\" #define HEADER2 \"|-------num-------|--------name--------|----phonenumber-----|---addre------|\\n\" #define HEADER3 \"|-----------------|--------------------|--------------------|----------------|\\n\" #define FORMAT \"|%-10s
|%-10s
|%-15s
|%-16s|\\n\" #define DATA
p->num
,
p->name
,
p->phonenum p->addre #define END
\"----------------\\n\" #define N 100 int saveflag=0;
typedef int Status; typedef struct telebook{
char num[15];
char name[15];
char phonenum[15];
char addre[20]; }TELEBOOK;
void menu() {
system(\"cls\");
textcolor(13);
gotoxy(10,5);
cprintf(\"
The telephone-book Management System
\\n\");
gotoxy(10,8);
cprintf(\"*****************************************************\\n\");
gotoxy(10,9);
cprintf(\"*
1 input record
2 display record
* \\n\");
gotoxy(10,10);
cprintf(\"*
3 delete record
4 search record
* \\n\");
gotoxy(10,11);
cprintf(\"*
5 modify record
6 insert record
* \\n\");
gotoxy(10,12);
cprintf(\"*
7 sort record
8 save record
* \\n\");
gotoxy(10,13);
cprintf(\"*
0 input record
* \\n\");
gotoxy(10,14);
cprintf(\"*****************************************************\\n\"); }
,
void printheader() {
printf(HEADER1);
printf(HEADER2);
printf(HEADER3); }
void printdata(TELEBOOK pp) {
TELEBOOK *p;
p = &pp;
printf(FORMAT,DATA); }
void Disp(TELEBOOK temp[],int n) {
int i;
if(n==0)
{
printf(\"\\n======>Not telephone record!\\n\");
getchar();
return ;
}
printf(\"\\n\\n\");
printheader();
i=0;
while(i
{
printdata(temp[i]);
i++;
printf(HEADER3);
}
getchar(); }
void Wrong() {
printf(\"\\n\\n\\n\\n\\n********Error:input has wrong! pre any key to continue*******\\n\");
getchar(); }
void Nofind() {
printf(\"\\n========>Not find this telephone record!\\n\"); }
Status Locate(TELEBOOK temp[],int n,char findme[],char nameorphonenum[])
/*find the locate*/ {
int i=0;
if(strcmp(nameorphonenum,\"phonenum\")==0)
{
while(i
{
if(strcmp(temp[i].phonenum,findme)==0) return i;
i++;
}
}
else if(strcmp(nameorphonenum,\"name\")==0)
{
while(i
{
if(strcmp(temp[i].name,findme)==0) return i;
i++;
}
}
return -1; }
void stringinput(char *t,int lens,char *notice) {
char n[255];
do{
printf(notice);
scanf(\"%s\",n);
if(strlen(n)>lens) printf(\"\\n exceed the required length!\\n\");
}while(strlen(n)>lens);
strcpy(t,n); }
Status Add(TELEBOOK temp[],int n)
/*increase record*/ {
char ch,num[10];
int i,flag=0;
system(\"cls\");
Disp(temp,n);
while(1)
{
while(1)
{
stringinput(num,10,\"input number(pre \'0\' retrun menu):\");
flag = 0;
if(strcmp(num,\"0\")==0)
{ return n; }
i=0;
while(i
{
if(strcmp(temp[i].num,num)==0)
{
flag = 1;
break;
}
i++;
}
if(flag==1)
{
getchar();
printf(\"==>The number %s is existing,try again?(Y/N):\",num);
scanf(\"%c\",&ch);
if(ch==\'Y\'&&ch==\'y\') continue;
else return n;
}
else break;
}
strcpy(temp[n].num,num);
stringinput(temp[n].name,15,\"Name:\");
stringinput(temp[n].phonenum,15,\"Telephone:\");
stringinput(temp[n].addre,15,\"Adre:\");
saveflag=1;
n++;
}
return n; }
void Qur(TELEBOOK temp[],int n) /*search the record*/ {
int select;
char searchinput[20];
int p = 0;
if(n
{
system(\"cls\");
printf(\"\\n====>No telephone record !\\n\");
getchar();
return ;
}
system(\"cls\");
printf(\"\\n=====>1 Search by name =====>2 Search by telephonenumber!\\n\");
printf(\"
Please choice(1,2):\");
scanf(\"%d\",&select);
if(select==1)
{
stringinput(searchinput,10,\"input the existing name:\");
p = Locate(temp,n,searchinput,\"name\");
if(p!=-1)
{
printheader();
printdata(temp[p]);
printf(END);
printf(\"pre any key to return \");
getchar();
}
else {
Nofind();
getchar();
}
}
else if(select==2)
{
stringinput(searchinput,15,\"input the exiseing telephone number:\");
p = Locate(temp,n,searchinput,\"phonenum\");
if(p!=-1)
{
printheader(); printdata(temp[p]);
printf(END);
printf(\"pre any key to return \");
getchar();
}
else { Nofind(); getchar(); }
}
else Wrong();getchar(); }
Status Del(TELEBOOK temp[],int n) {
int sel;
char findme[20];
int p=0,i=0;
if(n
{
system(\"cls\");
printf(\"\\n======>No telephone !\\n\");
getchar();
return n;
}
system(\"cls\");
Disp(temp,n);
printf(\" ===>1 Name ====>2 Telephone Please choice[1,2]:\");
scanf(\"%d\",&sel);
if(sel==1)
{
stringinput(findme,15,\"input the existing name:\");
p = Locate(temp,n,findme,\"name\");
getchar();
if(p!=-1)
{
for(i=p+1;i
{
strcpy(temp[i-1].num,temp[i].num);
strcpy(temp[i-1].name,temp[i].name);
strcpy(temp[i-1].phonenum,temp[i].phonenum);
strcpy(temp[i-1].addre,temp[i].addre);
}
printf(\"\\n==>delete succe\\n\");
n--;
getchar();
saveflag=1;
}
else
Nofind();
getchar();
}
else if(sel==2)
{
stringinput(findme,15,\"input the existing telephone number:\");
p = Locate(temp,n,findme,\"phonenum\");
getchar();
if(p!=-1)
{
for(i=p+1;i
{
strcpy(temp[i-1].num,temp[i].num);
strcpy(temp[i-1].name,temp[i].name);
strcpy(temp[i-1].phonenum,temp[i].phonenum);
strcpy(temp[i-1].addre,temp[i].addre);
}
printf(\"\\n======>delete succe !\\n\");
n--;
getchar();
saveflag = 1;
}
else
Nofind();
getchar();
}
else
Nofind();
getchar();
return n; }
void Modify( TELEBOOK temp[],int n) {
char findme[20];
int p = 0;
if(n==0)
{
system(\"cls\");
printf(\"\\n=======>No telephone book recorderd\\n\");
getchar();
return ;
}
system(\"cls\");
printf(\"modify telephone book recorder\");
Disp(temp,n);
stringinput(findme,10,\"input the existing name:\");
p = Locate(temp,n,findme,\"name\");
if(p!=-1)
{
printf(\"Number : %s\\n\",temp[p].num);
printf(\"Name : %s\\n\",temp[p].name);
stringinput(temp[p].name,15,\"input new name:\");
printf(\"Telephone:%s \\n\",temp[p].phonenum);
stringinput(temp[p].phonenum,15,\"input new phonenum:\");
printf(\"Addre:%s\\n\",temp[p].addre);
stringinput(temp[p].addre,30,\"input new addre:\");
printf(\"\\n==========>modify succe!\\n\");
getchar();
saveflag = 1;
}
else {
Nofind();
getchar();
}
return ; }
Status Insert(TELEBOOK temp[],int n) {
char ch,num[10],snum[10];
TELEBOOK newinfo;
int flag = 0,i =0,kkk = 0;
system(\"cls\");
Disp(temp,n);
while(1)
{
stringinput(snum,10,\"Please input insert location after the Number:\");
flag = 0;i = 0;
while(i
{
if(strcmp(temp[i].num,snum)==0) {kkk = i;flag = 1;break;}
i++;
}
if(flag==1) break;
else {
getchar();
printf(\"\\n=====>The number %s is not existing,try again?(Y/N)\",snum);
scanf(\"%c\",&ch);
if(ch==\'Y\'||ch==\'y\') continue;
else return n;
}
}
while(1)
{
stringinput(num,10,\"input the new Number:\");
i = 0;flag = 0;
while(i
{
if(strcmp(temp[i].num,num)==0) {flag = 1;break;}
i++;
}
if(flag==1){
getchar();
printf(\"\\n=======>Sorry,The number %s is existing,try again(Y/N)\",num);
scanf(\"%c\",&ch);
if(ch==\'y\'||ch==\'Y\') continue;
else return n;
}
else break;
}
strcpy(newinfo.num,num);
stringinput(newinfo.name,15,\"Name:\");
stringinput(newinfo.phonenum,15,\"Telephone:\");
stringinput(newinfo.addre,15,\"Addre:\");
saveflag = 1;
for(i=n-1;i>kkk;i--)
{
strcpy(temp[i+1].num,temp[i].num);
strcpy(temp[i+1].name,temp[i].name);
strcpy(temp[i+1].phonenum,temp[i].phonenum);
strcpy(temp[i+1].addre,temp[i].addre);
}
strcpy(temp[kkk+1].num,newinfo.num);
strcpy(temp[kkk+1].name,newinfo.name);
strcpy(temp[kkk+1].phonenum,newinfo.phonenum);
strcpy(temp[kkk+1].addre,newinfo.addre);
n++;
Disp(temp,n);
printf(\"\\n\\n\");
getchar();
return n; }
void SelectSort(TELEBOOK temp[],int n) {
int i=0,j=0,indexmin;
char charflag[10];
TELEBOOK newinfo;
if(n
{
system(\"cls\");
printf(\"====>Not telephone record !\\n\");
getchar();
return ;
}
system(\"cls\");
Disp(temp,n);
printf(\"The order is sorted by number:\\n\");
for(i=0;i
{
indexmin = i;
for(j=i+1;j
if(strcmp(temp[i].num,temp[j].num>0))
indexmin = j;
if(indexmin!=i)
{
strcpy(newinfo.num,temp[i].num);
strcpy(newinfo.name,temp[i].name);
strcpy(newinfo.phonenum,temp[i].phonenum);
strcpy(newinfo.addre,temp[i].addre);
strcpy(temp[indexmin].num,newinfo.num);
strcpy(temp[indexmin].name,newinfo.name);
strcpy(temp[indexmin].phonenum,newinfo.phonenum);
strcpy(temp[indexmin].addre,newinfo.addre);
}
}
Disp(temp,n);
saveflag = 1;
printf(\"\\n=======>sort complete!\\n\");
getchar();
return ; }
void Save(TELEBOOK temp[],int n) {
FILE *fp;
int i = 0;
fp = fopen(\"c: elephonebook\",\"w\");
if(fp==NULL)
{
printf(\"\\n=====>open file error !\\n\");
getchar();
return ;
}
for(i=0;i
{
if(fwrite(&temp[i],sizeof(TELEBOOK),1,fp)==1)
continue;
else break;
}
if(i>0)
{
getchar();printf(\"\\n\\n=====>save file comlete,toal save\'s record is save\'s record number is:%d\\n\",i);
getchar();
saveflag = 0;
}
else {
system(\"cls\");
printf(\"the current link is empty , no telephone record is saved!n\") ;
getchar();
fclose(fp);
} }
void main() {
TELEBOOK tele[N];
FILE *fp;
int select ;
char ch;
int count=0;
fp = fopen(\"C: elephonebook\",\"a+\");
if(fp==NULL)
{
printf(\"\\n========>can not open file!\\n\");
exit (0);
}
while(!feof(fp))
{
if(fread(&tele[count],sizeof(TELEBOOK),1,fp)==1)
count++;
}
fclose(fp);
printf(\"\\n==>open file suce, the total records number is :%d.\\n\",count);
getchar();
menu();
while(1)
{
system(\"cls\");
menu();
printf(\"\\n
Please Enter you choice(0-9):\");
scanf(\"%d\",&select);
if(select==0)
{
if(saveflag==1)
{
getchar();
printf(\"\\n==>Wether save the modified record to file?(Y/N):\");
scanf(\"%c\",&ch);
if(ch==\'Y\'||ch==\'y\')
Save(tele,count);
}
printf(\"\\n===>thank you for usene!\");
getchar();
break;
}
switch(select)
{
case 1:count=Add(tele,count);break;
case 2:system(\"cls\");Disp(tele,count);break;
case 3:count = Del(tele,count);break;
case 4:Qur(tele,count);break;
case 5:Modify(tele,count);break;
case 6:count = Insert(tele,count);break;
case 7:SelectSort(tele,count);break;
case 8:Save(tele,count);break;
default :Wrong();getchar();break;
}
}
getch(); }