You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
8.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <windows.h>
#define M 50
typedef struct abc
{
char name[20];
char address[13];
char phone1[15];
char phone2[15];
char email[11];
struct abc *next;
}LINKMAN;
int menu();
int input(LINKMAN a[]);
void list(LINKMAN a[],int b);
void sort(LINKMAN a[],int b);
int search(LINKMAN a[],int b);
int add(LINKMAN a[],int b);
int delet(LINKMAN a[],int b);
void update(LINKMAN a[],int b);
void save(LINKMAN a[],int b);
int load();
void copy();
int main()
{
int sum=0;
LINKMAN adr[M];
while(1)
{
switch(menu())
{
case 1:sum=input(adr);break;
case 2:list(adr,sum);break;
case 3:search(adr,sum);break;
case 4:update(adr,sum);break;
case 5:sum=add(adr,sum);break;
case 6:sum=delet(adr,sum);break;
case 7:save(adr,sum);break;
case 8:load();break;
case 9:sort(adr,sum);break;
case 10:copy();break;
case 11:exit(0);
}
}
return 0;
}
int menu()
{
int n;
system("cls");
printf("&&&&&&&&&&&&&&&&& Menu &&&&&&&&&&&&&&&&&\n");
printf(" 1.input\n");
printf(" 2.list\n");
printf(" 3.search\n");
printf(" 4.update\n");
printf(" 5.add\n");
printf(" 6.delet\n");
printf(" 7.save\n");
printf(" 8.load\n");
printf(" 9.sort\n");
printf(" 10.copy\n");
printf(" 11.exit\n");
printf("***** ***** ***** *****\n");
scanf("%d",&n);
return n;
}
int input(LINKMAN a[])
{
int i,n;
system("cls");
printf("********* Input *********\n");
printf("ÇëÊäÈë¼Ç¼Êý£º\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s%s%s%s%s",a[i].name,a[i].address,a[i].phone1,a[i].phone2,a[i].email);
printf("***** ***** ***** *****\n");
}
system("pause");
return n;
}
void list(LINKMAN a[],int b)
{
int i;
system("cls");
printf("********* List *********\n");
printf("name address phone1 phone2 e-mail \n");
printf("---------------------------------------------------------------\n");
for(i=0;i<b;i++)
{
printf("%-12s%-12s%-14s%-14s%-10s\n",a[i].name,a[i].address,a[i].phone1,a[i].phone2,a[i].email);
printf("---------------------------------------------------------------\n");
}
printf("********* End *********\n");
system("pause");
}
void sort(LINKMAN a[],int b)
{
int i,j;
LINKMAN c;
system("cls");
printf("********* Sort *********\n");
for(i=0;i<b-1;i++)
for(j=i+1;j<b;j++)
{
if(strcmp(a[i].name,a[j].name)>0)
{
c=a[i];
a[i]=a[j];
a[j]=c;
}
}
list(a,b);
printf("\n");
system("pause");
}
int search(LINKMAN a[],int b)
{
system("cls");
printf("********* Search *********\n");
char c[12];
printf("ÇëÊäÈëÏëÒª²éÕÒµÄÁªÏµÈËÐÕÃû£º\n");
scanf("%s",c);
int front=0;
int end=b-1;
int mid;
mid=(end-front)/2;
while(front<=end)
{
if(strcmp(a[mid].name,c)>0)
{
end=mid-1;
mid=(end+front)/2;
}
else if(strcmp(a[mid].name,c)<0)
{
front=mid+1;
mid=(end+front)/2;
}
else
{
printf("%s\n%s\n%s\n%s\n%s\n",a[mid].name,a[mid].address,a[mid].phone1,a[mid].phone2,a[mid].email);
break;
}
}
printf("********* End *********\n");
if(front>end)
{
printf("Sorry,the linkman is not existed!!!\n");
system("pause");
return -1;
}
else
{
system("pause");
return mid;
}
}
int add(LINKMAN a[],int b)
{
system("cls");
LINKMAN *head,*p,*q;
int i;
head=q=(LINKMAN *)malloc(sizeof(LINKMAN));
strcpy(head->name,a[0].name);
strcpy(head->address,a[0].address);
strcpy(head->phone1,a[0].phone1);
strcpy(head->phone2,a[0].phone2);
strcpy(head->email,a[0].email);
head->next=NULL;
for(i=1;i<b;i++)
{
p=(LINKMAN *)malloc(sizeof(LINKMAN));
strcpy(p->name,a[i].name);
strcpy(p->address,a[i].address);
strcpy(p->phone1,a[i].phone1);
strcpy(p->phone2,a[i].phone2);
strcpy(p->email,a[i].email);
p->next=NULL;
q->next=p;
q=p;
}
LINKMAN c;
LINKMAN *p1,*p2,*p3,*p4;
p1=(LINKMAN *)malloc(sizeof(LINKMAN));
p2=(LINKMAN *)malloc(sizeof(LINKMAN));
p3=(LINKMAN *)malloc(sizeof(LINKMAN));
p4=(LINKMAN *)malloc(sizeof(LINKMAN));
int z,j=1;
printf("************ Add ************\n");
printf("ÇëÊäÈë²åÈëµÄÁªÏµÈËÐÅÏ¢£º\n");
scanf("%s%s%s%s%s",c.name,c.address,c.phone1,c.phone2,c.email);
p1=&c;
printf("ÇëÊäÈë²åÈëµÄλÖãº\n");
scanf("%d",&z);
if(z>1&&z<=b)
{
p2=head;
while(j<z-1)
{
p2=p2->next;
j++;
}
p1->next=p2->next;
p2->next=p1;
}
if(z==1)
{
p1->next=head;
head=p1;
}
if(z==b+1)
{
p2=head;
while(j<b)
{
p2=p2->next;
j++;
}
p2->next=p1;
p1->next=NULL;
}
printf("name address phone1 phone2 e-mail \n");
printf("---------------------------------------------------------------\n");
for(p4=head,i=0;p4!=NULL;p4=p4->next,i++)
{
strcpy(a[i].name,p4->name);
strcpy(a[i].address,p4->address);
strcpy(a[i].phone1,p4->phone1);
strcpy(a[i].phone2,p4->phone2);
strcpy(a[i].email,p4->email);
printf("%-12s%-12s%-14s%-14s%-10s\n",p4->name,p4->address,p4->phone1,p4->phone2,p4->email);
printf("---------------------------------------------------------------\n");
}
b++;
system("pause");
return b;
}
int delet(LINKMAN a[],int b)
{
system("cls");
char c[10];
int i,j,n,d;
printf("********* Delet *********\n");
printf("ÇëÊäÈëÏëҪɾ³ýµÄÁªÏµÈËÐÕÃû£º\n");
scanf("%s",c);
printf("ÇëÔÙ´ÎÊäÈëÏëҪɾ³ýµÄÁªÏµÈËÐÕÃû£º\n");
system("pause");
n=search(a,b);
printf("********* Delet *********\n");
if(n<0)
printf("Sorry,the linkman is not existed!!!\n");
else
{
printf("ÊÇ·ñÈ·ÈÏɾ³ý£¿ÊÇ£¬Çë°´1£»·ñ£¬Çë°´0.\n");
scanf("%d",&d);
if(d==1)
{
for(i=n;i<b;i++)
a[i]=a[i+1];
b--;
}
}
list(a,b);
printf("********* End *********\n");
system("pause");
return b;
}
void update(LINKMAN a[],int b)
{
system("cls");
printf("************ Update ************\n");
printf("ÇëÊäÈëÏëÒªÐ޸ĵÄÁªÏµÈËÐÕÃû£º\n");
char c[12];
LINKMAN d;
int n;
scanf("%s",c);
n=search(a,b);
printf("************ Update ************\n");
printf("ÇëÊäÈëеÄÁªÏµÈËÐÅÏ¢£º\n");
scanf("%s%s%s%s%s",d.name,d.address,d.phone1,d.phone2,d.email);
a[n]=d;
printf("********** End **********\n");
system("pause");
}
void save(LINKMAN a[],int b)
{
system("cls");
int i;
FILE *fp;
printf("************ Save ************\n");
fp=fopen("record10.txt","r+");
if(fp==NULL)
{
fp=fopen("record10.txt","w+");
if(fp==NULL)
{
printf("ÎÞ·¨´ò¿ªÎļþ£¡£¡£¡\n");
exit(0);
}
}
fseek(fp,0,SEEK_END);
for(i=0;i<b;i++)
{
fprintf(fp,"%-12s%-12s%-14s%-14s%-10s",a[i].name,a[i].address,a[i].phone1,a[i].phone2,a[i].email);
fflush(stdin);
}
if(fclose(fp))
{
printf("Can not close the file!!!\n");
exit(0);
}
printf("Îļþ±£´æ³É¹¦£¡\n");
printf("********** End **********\n");
system("pause");
}
int load()
{
system("cls");
printf("************ Load ************\n");
FILE *fp;
LINKMAN a;
if((fp=fopen("record10.txt","rb"))==NULL)
{
printf("ÎÞ·¨´ò¿ªÎļþ£¡£¡£¡\n");
exit(0);
}
rewind(fp);
printf("name address phone1 phone2 e-mail \n");
printf("---------------------------------------------------------------\n");
fgets(a.name,13,fp);
fgets(a.address,13,fp);
fgets(a.phone1,15,fp);
fgets(a.phone2,15,fp);
fgets(a.email,11,fp);
while(!feof(fp))
{
printf("%-12s%-12s%-14s%-14s%-10s\n",a.name,a.address,a.phone1,a.phone2,a.email);
printf("---------------------------------------------------------------\n");
fgets(a.name,13,fp);
fgets(a.address,13,fp);
fgets(a.phone1,15,fp);
fgets(a.phone2,15,fp);
fgets(a.email,11,fp);
}
if(fclose(fp))
{
printf("Can not close the file!!!\n");
exit(0);
}
printf("********** End **********\n");
system("pause");
return 0;
}
void copy()
{
system("cls");
printf("************ Copy ************\n");
LINKMAN d;
FILE *fp,*cp;
if((fp=fopen("record10.txt","rb"))==NULL)
{
printf("ÎÞ·¨´ò¿ªÎļþ£¡£¡£¡\n");
exit(0);
}
rewind(fp);
cp=fopen("copyfile","rb+");
if(cp==NULL)
{
cp=fopen("copyfile","wb+");
if(cp==NULL)
{
printf("Can not open the file!!!\n");
exit(0);
}
}
fgets(d.name,13,fp);
fgets(d.address,13,fp);
fgets(d.phone1,15,fp);
fgets(d.phone2,15,fp);
fgets(d.email,11,fp);
while(!feof(fp))
{
fprintf(cp,"%-12s%-12s%-14s%-14s%-10s",d.name,d.address,d.phone1,d.phone2,d.email);
fgets(d.name,13,fp);
fgets(d.address,13,fp);
fgets(d.phone1,15,fp);
fgets(d.phone2,15,fp);
fgets(d.email,11,fp);
}
fclose(fp);
fclose(cp);
printf("¿½±´³É¹¦£¡£¡£¡\n");
printf("********** End **********\n");
system("pause");
}