链表应用

#include "stdio.h"  
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
#define M 50 
typedef struct
{
   char name[20];
   char
units[30]; 
   char
tele[10]; 
}ADDRESS;

int enter(ADDRESS t[]);
void list(ADDRESS t[],int n);
void search(ADDRESS t[],int n);
int delete(ADDRESS t[],int n);
int  add(ADDRESS t[],int n);
void save(ADDRESS t[],int n);
int load(ADDRESS t[]); 
void display(ADDRESS t[]);
void sort(ADDRESS t[],int n);
void qseek(ADDRESS t[],int n);
void copy(); 
void print(ADDRESS temp);
int find(ADDRESS t[],int n,char *s) ;
int menu_select(); 

main()
{
   int i;
   ADDRESS
adr[M]; 
   int
length; 
  
clrscr(); 
   for(;;)
   {
     
switch(menu_select())  
     
{
    
case 0:length=enter(adr);break;
    
case 1:list(adr,length);break;
    
case 2:search(adr,length);break;
    
case 3:length=delete(adr,length);break;
    
case 4:length=add(adr,length); 
break;  
    
case 5:save(adr,length);break;
    
case 6:length=load(adr); break;
    
case 7:display(adr);break; 
    
case 8:sort(adr,length);break;
    
case 9:qseek(adr,length);break;
    
case 10:copy();break;
    
case 11:exit(0);
     
}
   }
}

menu_select()
{
   char s[80];
   int c;
   gotoxy(1,25);
   printf("press any key enter
menu......\n");
   getch();
   clrscr();
   gotoxy(1,1);
  
printf("********************MENU*********************\n\n");
  
printf("    
0. Enter record\n");
  
printf("    
1. List the file\n");
  
printf("    
2. Search record on name\n");
  
printf("    
3. Delete a record\n");
  
printf("    
4. add record \n");
  
printf("    
5. Save the file\n");
  
printf("    
6. Load the file\n");
  
printf("    
7. display record on order\n");
  
printf("        
8. sort to make new file\n");
  
printf("        
9. Quick seek record\n");
  
printf("    
10. copy the file to new file\n");
  
printf("        
11. Quit\n");
  
printf("***********************************************\n");
   do{
     
printf("\n    
Enter you choice(0~11):");
     
scanf("%s",s);
     
c=atoi(s);
  
}while(c<0||c>11);
   return c;
}

int  enter(ADDRESS t[])
{
   int i,n;
   char *s;
   clrscr();
   printf("\nplease input num
\n");
  
scanf("%d",&n);
   printf("please input record
\n");
  
printf("name            
unit                    
telephone\n");
  
printf("------------------------------------------------\n");
  
for(i=0;i<n;i++)
   {
     
scanf("%s%s%s",t[i].name,t[i].units,t[i].tele); 

     
printf("----------------------------------------------\n");
   }
   return
n; 
}

void list(ADDRESS t[],int n)
{
   int i;
   clrscr();
  
printf("\n\n*******************ADDRESS******************\n");
  
printf("name              
unit                    
telephone\n");
  
printf("------------------------------------------------\n");
  
for(i=0;i<n;i++)
  
printf("%-20s%-30s%-10s\n",t[i].name,t[i].units,t[i].tele);
  
if((i+1)%10==0)  
   {
     
printf("Press any key continue...\n");
     
getch(); 
   }
  
printf("************************end*******************\n");
}

void search(ADDRESS t[],int n)
{
   char
s[20];  
   int
i;  
  
clrscr();  
   printf("please search
name\n");
   scanf("%s",s);
   i=find(t,n,s);
  
if(i>n-1) 
     
printf("not found\n");
   else
     
print(t[i]); 
}

void print(ADDRESS temp)
{
   clrscr();
  
printf("\n\n********************************************\n");
  
printf("name               
unit                     
telephone\n");
  
printf("------------------------------------------------\n");
  
printf("%-20s%-30s%-10s\n",temp.name,temp.units,temp.tele);
  
printf("**********************end***********************\n");
}

int find(ADDRESS t[],int n,char *s)
{
   int i;
  
for(i=0;i<n;i++)
   {
     
if(strcmp(s,t[i].name)==0) 
     
return i;  
   }
   return
i; 
}

int delete(ADDRESS t[],int n)
{
   char
s[20]; 
   int ch=0;
   int i,j;
   printf("please deleted
name\n");
   scanf("%s",s);
   i=find(t,n,s);
  
if(i>n-1) 
     
printf("no found not deleted\n");
   else
   {
     
print(t[i]);
     
printf("Are you sure delete it(1/0)\n"); 
     
scanf("%d",&ch); 
     
if(ch==1) 
     
{
    
for(j=i+1;j<n;j++) 
    
{
       
strcpy(t[j-1].name,t[j].name);
       
strcpy(t[j-1].units,t[j].units);
       
strcpy(t[j-1].tele,t[j].tele);
    
}
    
n--; 
     
}
   }
   return
n; 
}

int add(ADDRESS t[],int n)
{
   ADDRESS
temp; 
   int i,j;
   char s[20];
   printf("please input
record\n");
  
printf("************************************************\n");
  
printf("name               
unit                     
telephone\n");
  
printf("--------------------------------------------------\n");

  
scanf("%s%s%s",temp.name,temp.units,temp.tele);
  
printf("------------------------------------------------\n");
   printf("please input locate
name \n");
   scanf("%s",s);
  
i=find(t,n,s); 
  
for(j=n-1;j>=i;j--)  

   {
     
strcpy(t[j+1].name,t[j].name);
     
strcpy(t[j+1].units,t[j].units);
     
strcpy(t[j+1].tele,t[j].tele);
   }
  
strcpy(t[i].name,temp.name);
  
strcpy(t[i].units,temp.units);
  
strcpy(t[i].tele,temp.tele);
  
n++;  
   return n;
}

void save(ADDRESS t[],int n)
{
   int i;
   FILE
*fp; 
  
if((fp=fopen("record.txt","wb"))==NULL) 
   {
     
printf("can not open file\n");
     
exit(1); 
   }
   printf("\nSaving
file\n");
  
fprintf(fp,"%d",n); 
  
fprintf(fp,"\r\n"); 
  
for(i=0;i<n;i++)
   {
     
fprintf(fp,"%-20s%-30s%-10s",t[i].name,t[i].units,t[i].tele);
     
fprintf(fp,"\r\n");
   }
   fclose(fp);
   printf("****save
success***\n");
}

int load(ADDRESS t[])
{
   int i,n;
   FILE *fp;
  
if((fp=fopen("record.txt","rb"))==NULL)
   {
     
printf("can not open file\n"); 
     
exit(1); 
   }
  
fscanf(fp,"%d",&n);
  
for(i=0;i<n;i++)
     
fscanf(fp,"%20s%30s%10s",t[i].name,t[i].units,t[i].tele);
  
fclose(fp); 
   printf("You have success read
data from file!!!\n");
   return n;
}

void display(ADDRESS t[])
{
   int id,n;
   FILE *fp;
  
if((fp=fopen("record.txt","rb"))==NULL)
   {
     
printf("can not open file\n");
     
exit(1); 
   }
   printf("Enter order
number...\n");
  
scanf("%d",&id); 
  
fscanf(fp,"%d",&n);
  
if(id>=0&&id<n)

   {
     
fseek(fp,(id-1)*sizeof(ADDRESS),1);
     
print(t[id]);
     
printf("\r\n");
   }
   else
     
printf("no %d number record!!!\n ",id);
  
fclose(fp); 
}

void sort(ADDRESS t[],int n)
{
   int i,j,flag;
   ADDRESS temp;
  
for(i=0;i<n;i++)
   {
     
flag=0; 
     
for(j=0;j<n-1;j++)
     
if((strcmp(t[j].name,t[j+1].name))>0)
     
{
    
flag=1;
    
strcpy(temp.name,t[j].name); 
    
strcpy(temp.units,t[j].units);
    
strcpy(temp.tele,t[j].tele);
    
strcpy(t[j].name,t[j+1].name);
    
strcpy(t[j].units,t[j+1].units);
    
strcpy(t[j].tele,t[j+1].tele);
    
strcpy(t[j+1].name,temp.name);
    
strcpy(t[j+1].units,temp.units);
    
strcpy(t[j+1].tele,temp.tele);
     
}
     
if(flag==0)break; 
   }
   printf("sort
sucess!!!\n");
}

void qseek(ADDRESS t[],int n)
{
   char s[20];
   int l,r,m;
  
printf("\nPlease  sort before qseek!\n");
   printf("please
enter  name for qseek\n");
   scanf("%s",s);
  
l=0;r=n-1; 
  
while(l<=r)
   {
     
m=(l+r)/2;
     
if(strcmp(t[m].name,s)==0)
     
{
    
print(t[m]);
    
return ;
     
}
     
if(strcmp(t[m].name,s)<0) 
    
l=m+1; 
     
else
    
r=m-1;
   }
  
if(l>r)  
     
printf("not found\n");
}

void copy()
{
   char outfile[20];
   int i,n;
   ADDRESS
temp[M]; 
   FILE *sfp,*tfp;
   clrscr();
  
if((sfp=fopen("record.txt","rb"))==NULL)
   {
     
printf("can not open file\n");
     
exit(1);
   }
   printf("Enter outfile name,for
example c:\\f1\\te.txt:\n");
   scanf("%s",outfile);
  
if((tfp=fopen(outfile,"wb"))==NULL)
   {
     
printf("can not open file\n");
     
exit(1);
   }
  
fscanf(sfp,"%d",&n);
   fprintf(tfp,"%d",n);
   fprintf(tfp,"\r\n");
  
for(i=0;i<n;i++)
   {
     
fscanf(sfp,"%20s%30s%10s\n",temp[i].name,temp[i].units,
   
temp[i].tele);
     
fprintf(tfp,"%-20s%-30s%-10s\n",temp[i].name,
   
temp[i].units,temp[i].tele);
     
fprintf(tfp,"\r\n");
   }
   fclose(sfp);
   fclose(tfp);
   printf("you have success
copy  file!!!\n");
}