#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct linker)
typedef struct linker
{ char xuehao[21];
char name[20];
char banji[21];
char tel[12];
char addr[11];
struct linker *next;
}link;
void main()
{ FILE *fp;
void welcome(); //欢迎界面
void menu(); //菜单界面
link *creat(); //创建一个链表
link *print(link *); //输出全部记录
link *insert(link *); //添加一条新记录
link *change(link *); //改变记录中的信息
void search(link *); //查记录,支持模糊查
link *delet(link *); //删除一条记录
link *SortNum(link *head);
void saves(link *); //将记录保存到文件中
char identify;
// char fullname[10];
link *head,*p,*p1,*p2,*p3;
int choose;
char absorb;
int i;
head=NULL;
welcome();
do //确定输入是否为y或Y或n或N
{
scanf("%c",&identify);
scanf("%c",&absorb);
if(identify=='y'||identify=='Y')
printf("Thank your use\n");
else
if(identify=='n'||identify=='N')
exit(0);
else
printf(" 请输入(Y/N):");
}while(identify!='y'&&identify!='Y'&&identify!='n'&&identify!='N');
while(1)
{
menu();
scanf("%d",&choose);
scanf("%c",&absorb);
switch(choose)
{ case 1: head=creat(); //创建链表,并将链表中的信息输入到文件中
saves(head);
break;
case 2: i=0;
if((fp=fopen("","a+"))==NULL)
{ printf(" 无法打开文件~~~\n");
exit(0);
} //打开文件,并将文件中内容复制到链表中,若文件中无记录,则链表头指针为空
if(!feof(fp))
{ i++;
p2=p1;
p1=p2=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
head=p1;
}
while(!feof(fp))
{ i++;
p2=p1;
p1=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
p2->next=p1;
}
if(i==1)
head=NULL;
p2->next=NULL;
fclose(fp);
search(head);
break;
case 3: i=0;
if((fp=fopen("","a+"))==NULL)
{ printf(" 无法打开文件~~~\n");
exit(0);
}
if(!feof(fp))
{ i++;
p2=p1;
p1=p2=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
head=p1;
}
while(!feof(fp))
{ i++;
p2=p1;
p1=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
p2->next=p1;
}
if(i==1)
head=NULL;
p2->next=NULL;
fclose(fp);
if(head==NULL)
{
printf("\n");
printf(" 通讯录中无记录,请返回~~~");
}
else
change(head);
saves(head);
break;
case 4: i=0;
if((fp=fopen("","a+"))==NULL)
{ printf(" 无法打开文件~~~\n");
exit(0);
}
if(!feof(fp))
{ i++;
p2=p1;
p1=p2=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
head=p1;
}
while(!feof(fp))
{ i++;
p2=p1;
p1=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
p2->next=p1;
}
if(i==1)
head=NULL;
p2->next=NULL;
fclose(fp);
if(head==NULL)
{ printf("\n");
printf(" 通讯录中无记录,请创建~~~");
}
else
{ head=insert(head);
saves(head);
}
break;
case 5: i=0;
if((fp=fopen("","a+"))==NULL)
{ printf(" 无法打开文件~~~\n");
exit(0);
}
if(!feof(fp))
{ i++;
p2=p1;
p1=p2=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
head=p1;
}
while(!feof(fp))
{ i++;
p2=p1;
p1=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
p2->next=p1;
}
if(i==1)
head=NULL;
p2->next=NULL;
fclose(fp);
if(head==NULL)
{
printf("\n");
printf(" 通讯录中无记录,请返回~~~");
}
else
head=delet(head);
saves(head);
break;
case 6: i=0;
if((fp=fopen("","a+"))==NULL)
{ printf(" 无法打开文件~~~\n");
exit(0);
}
if(!feof(fp))
{ i++;
p2=p1;
p1=p2=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
head=p1;
}
while(!feof(fp))
{ i++;
p2=p1;
p1=(link *)malloc(LEN);
fread(p1,LEN,1,fp);
p2->next=p1;
}
if(i==1)
head=NULL;
p2->next=NULL;
fclose(fp);
if(head==NULL)
{
printf("\n");
printf(" 通讯录中无记录,请返回~~~");
}
else
head=print(head);
printf("\n");
// head=SortNum(head);
printf(" 按任意键返回~~~");
saves(head);
break;
case 7: printf("\
n");
printf(" 确认要退出吗?(Y/N):");
do
{
scanf("%c",&identify);
scanf("%c",&absorb);
if(identify=='y'||identify=='Y')
{ p=p3=head;//释放链表占用的内存
while(p!=NULL)
{ p3=p3->next;
p=p3;
free(p3);
}
exit(0);
}
else
if(identify=='n'||identify=='N')
printf("Please go on\n");
else
printf(" 请输入(Y/N):");
} while(identify!='y'&&identify!='Y'&&identify!='n'&&identify!='N');
break;
default: printf("\n");
printf("\n");
printf(" 请输入正确数字(1-7)!");
}
}
}
void welcome()
{ printf("\n");
printf("\n");
printf("\n");
c语言写入txt文件printf("\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" * 制作人:宫源,隋丽*\n");
printf(" * *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * 欢迎使用:通讯录 *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" 请按提示操作:\n");
printf(" 是否进入(Y/N):");
}
void menu()
{ printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf(" * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *\n");
printf(" * *\n");
printf(" * *\n");
printf(" * [1] 新建: * \n");
printf(" * [2] 查
询: * \n");
printf(" * [3] 修改: * \n");
printf(" * [4] 添加: * \n");
printf(" * [5] 删除: * \n");
printf(" * [6] 排序: * \n");
printf(" * [7] 退出: *\n");
printf(" * *\n");
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * \n");
printf(" 请输入数字(1-7):");
}
link *creat()
{ link *head;
link *p1,*p2;
char identify,absorb;
p1=p2=(link *)malloc(LEN);
head=p1;
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf(" 学号:");
gets(p1->xuehao);
printf(" 姓名:");
gets(p1->name);
printf(" 班级:");
gets(p1->banji);
printf(" 电话:");
gets(p1->tel);
printf(" 地址:");
gets(p1->addr);
printf("\n");
printf("\n");
printf(" 内容已保存,是否继续?(Y/N):");
scanf("%c",&identify);
scanf("%c",&absorb);
while(identify!='y'&&identify!='Y'&&identify!='n'&&identify!='N')
{ printf(" 请输入(Y/N):");
scanf("%c",&identify);
scanf("%c",&absorb);
}
while(identify=='y'||identify=='Y')
{
p2->next=p1;
p2=p1;
p1=(link*)malloc(LEN);
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf(" 学号:");
gets(p1->xuehao);
printf(" 姓名:");
gets(p1->name);
printf(" 班级:");
gets(p1->banji);
printf(" 电话:");
gets(p1->tel);
printf(" 地址:");
gets(p1->addr);
printf("\n");
printf("\n");
printf(" 内容已保存,是否继续?(Y/N):");
scanf("%c",&identify);
scanf("%c",&absorb);
while(identify!='y'&&identify!='Y'&&identify!='n'&&identify!='N')
{ printf(" 请输
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论