c语言通讯录2.0

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>


#define len sizeof(struct tongxunlu)
#define error 0


struct tongxunlu
{
char name[20];
int tel;
char addr[20];
struct tongxunlu *next;
};


typedef struct tongxunlu txl;
txl *init();
txl *create_tail(txl *head, txl *pre);
txl *insert(txl *head, txl *indata, int n);
txl *delete(txl *head, int n);
txl *revise(txl *head, char a[20]);
txl *find(txl *head,  char a[20]);
txl *sort(txl *head);
txl *clear_list(txl *head);
txl *print(txl *head);
txl *out_file(txl *head, char *filename);
txl *inputlxr(txl *pre);
void in_file(txl *head, char *filename);
void menu();


int main()
{
txl *head, *indata, *pre;
int n, m, k, key;

char a[20];
char b[20];
char *filename="txl.txt";


head=init(head);
head=out_file(head,filename);
menu();


while(1)
{
printf("By entering the number to go to the service you need.\n");
scanf("%d",&key);


switch(key)
{

case 1:
pre=inputlxr(pre);
head=create_tail(head,pre);
print(head);
break;


case 2:
indata=(txl *)malloc(len);
printf("the site you want's insert\n");
scanf("%d",&m);
printf("cha ru de shuju\n");
scanf("%s%d%s",indata->name,&indata->tel,indata->addr);
head=insert(head,indata,m);
print(head);
            break; 


case 3:
printf("the num you want delete\n");
scanf("%d",&k);
head=delete(head,k);
print(head);
break;


case 4:
printf("the name you want revise\n");
scanf("%s",&a);
head=revise(head,a);
print(head);
break;


case 5:
printf("the name you want find\n");
scanf("%s",&b);
find(head,b);
break;


case 6:
printf("please sort you txl\n");
head=sort(head);
print(head);
break;

case 7:
exit(0);
break;


case 8:
menu(); 
break;



case 9:
in_file(head,filename); 
break;

case 10:
print(head);   
break;


case 11:
clear_list(head);   
break;

default:
printf("out of range\n");
break;
}

}


return 0;
}


void menu()
{
printf("1 create txl 2 insert lxr\n");
printf("3 delete lxr 4 revise lxr\n");
printf("5 find   lxr 6 sort   lxr\n");
printf("7 exit   txl 8 back  menu\n");
printf("9 save   txl 10 diaplay txl\n");
printf("11 empty  txl             \n");

}




txl *init()
{
txl *head;

head=(txl *)malloc(len);
if(head==NULL)
return error;
head->next=NULL;

return head;
}


txl *inputlxr(txl *pre)
{

pre=(txl *)malloc(len);
if(pre==NULL)
return error;
printf("name:");
scanf("  %s",pre->name);
printf("tel:");
scanf("  %d",&pre->tel);
printf("addr:");
scanf("  %s",pre->addr);

return pre;
}


txl *create_tail(txl *head, txl *pre)
{
txl *after;
after=head;
while(after->next!=NULL)
{
after=after->next;
}
after->next=pre;
pre->next=NULL;

return head;
}


txl *insert(txl *head, txl *indata, int n)
{
txl *look;
int count=0;


if(0==n)
{
indata->next=head->next;
head->next=indata;
}


else
{
look=head;
while((count<n)&&(look!=NULL))
{
count++;
look=look->next;
}
if(count==n)
{
indata->next=look->next;
look->next=indata;
}
else
printf("error\n");
if(indata==NULL)             
printf("out of range\n");
        
}

return head;
}

txl *delete(txl *head, int n)
{
txl *look, *q;
int count=0;

look=head;
while((count<n-1)&&(look!=NULL))
{
count++;
look=look->next;
}
if(count==n-1)
{
q=look->next;
look->next=q->next;
free(q);
}
else
printf("out of range\n");


return head;
}


txl *revise(txl *head, char a[20])
{
txl *look;

look=head;
/* while(look!=NULL)
{
if(strcmp(look->name,a)==0)
{
printf("input the lxr you want revise\n");
scanf("%s%d%s",look->name,&look->tel,look->addr);
break;
}
look=look->next;
}
if(strcmp(look->name,a)!=0)
printf("not find the name\n");
*/


while(1)
{
if( strcmp(look->name,a)==0 )
{
printf("input the lxr you want revise\n");
scanf("%s%d%s",look->name,&look->tel,look->addr);
break;
}
look=look->next;
if(look==NULL)
{
printf("not find the name\n");
break;
}

}


return head;
}


txl *find(txl *head,  char a[20])
{
txl *look;

look=head;
while(look!=NULL)
{
if(strcmp(look->name,a)==0)
{
printf("name: %s   tel: %d   addr:    %s\n",look->name,look->tel,look->addr);
break;
}
look=look->next;
}
if(strcmp(look->name,a)!=0)
printf("not find the name\n");
return head;
}


txl *sort(txl *head)
{
txl *look, *p;
int count=0, n, i, j;
char a[20];
int b[20];


look=head;
while(look!=NULL)
{
count++;
look=look->next;
}
n=count-1;
p=head->next;
for(j=0; j<n-1; j++)
{
p=head->next;
for(i=0; i<n-j-1; i++)
{
if(strcmp(p->name,p->next->name)>0)
{
strcpy(a,p->name);
strcpy(p->name,p->next->name);
strcpy(p->next->name,a);


b[20]=p->tel;
p->tel=p->next->tel;
p->next->tel=b[20];


strcpy(a,p->addr);
strcpy(p->addr,p->next->addr);
strcpy(p->next->addr,a);
}
p=p->next;
}
}
return head;
}


txl *clear_list(txl *head)
{
txl *p, *q;
p=head->next;


while(p)
{
q=p->next;
free(p);
p=q;
}
head->next=NULL;

return head;
}


txl *print(txl *head)
{
txl *pre;
pre=head->next;


if(pre==NULL)
printf("this is a empty\n");
while(pre!=NULL)
{
printf("name: %s   tel: %d   addr:    %s\n",pre->name,pre->tel,pre->addr);
pre=pre->next;
}
printf("\n");
return head;
}




txl *out_file(txl *head, char *filename)
{
txl *tmp, *after;
FILE *fd;
int llen;
after=head;

fd=fopen(filename,"rt");
if(fd==NULL)
{
printf("open file error\n");
}
/*
fseek(fd,0,2);     
llen= ftell(fd);
fseek(fd,0,0);
while(llen != 0)
{
tmp=(txl *)malloc(len);
if(tmp == NULL)
printf("tmp empty\n");
fread(tmp,len,1,fd);
after->next=tmp;
after=tmp;
tmp->next=NULL;
// tmp->next = head->next;    
// head->next = tmp;
llen=llen-len;
}


*/

tmp=(txl *)malloc(len);
while(fread(tmp,len,1,fd)!=0)
{
after->next=tmp;
after=tmp;
tmp=(txl *)malloc(len);
}
tmp->next=NULL;


return head;


fclose(fd);
}


void in_file(txl *head, char *filename)
{


FILE *fd;
txl *tmp;
tmp=head->next;


fd = fopen(filename,"w+");
    if(fd == NULL)
    {
        printf("error");
        exit(-1);
    }
    while(tmp != NULL)
    {
       fwrite(tmp,len,1,fd);
        tmp = tmp->next;
    }
    fclose(fd);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全评估测试题大模型安全评估测试题关键词库生成内容测试题库应拒答测试题库非拒答测试题大模型安全
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值