c语言的记事本源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
#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("tongxunlu.txt","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("tongxunlu.txt","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("tongxunlu.txt","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("tongxunlu.txt","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("tongxunlu.txt","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");
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(" 请输

入(Y/N):");
scanf("%c",&identify);
scanf("%c",&absorb);
}
}
p2->next=p1;
p2=p1;
p2->next=NULL;
return (head);
}

link *print(link *head)
{ link *p1,*p2,*p3;
char xuehao[21];
char name[20];
char banji[21];
char tel[12];
char addr[11];
p3=head;
p1=head;
p2=head;
int n=0;

while(p2!=NULL) //将链表中记录按学号顺序排列
{ p1=p2;
while(p1!=NULL)
{ if(strcmp(p2->name,p1->name)>0)
{
strcpy(xuehao,p2->xuehao);
strcpy(p2->xuehao,p1->xuehao);
strcpy(p1->xuehao,xuehao);

strcpy(name,p2->name);
strcpy(p2->name,p1->name);
strcpy(p1->name,name);

strcpy(banji,p2->banji);
strcpy(p2->banji,p1->banji);
strcpy(p1->banji,banji);

strcpy(tel,p2->tel);
strcpy(p2->tel,p1->tel);
strcpy(p1->tel,tel);

strcpy(addr,p2->addr);
strcpy(p2->addr,p1->addr);
strcpy(p1->addr,addr);
}
p1=p1->next;
}
p2=p2->next;
}

printf("\n");
printf("\n");
printf("序号\t学号\t\t姓名\t\t\t班级\t\t\t电话\t\t地址\n");
//p3=head;

do //输出链表中信息
{
n++;
printf(" %d\t%-10s\t%-15s\t\t%-15s\t\t%-15s\t%-15s\n",n,p3->xuehao,p3->name,p3->banji,p3->tel,p3->addr);
p3=p3->next;
}while(p3!=NULL);

return (head);
}

link *insert(link *head)
{ link *pp1=NULL,*pp2=NULL;
char identify,absorb;
do
{
pp1=head;
pp2=(link *)malloc(LEN);
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf(" 学号:");
gets(pp2->xuehao);
printf(" 姓名:");
gets(pp2->name);
printf(" 班级:");
gets(pp2->banji);
printf(" 电话:");
gets(pp2->tel);
p

rintf(" 地址:");
gets(pp2->addr);
while(pp1->next!=NULL)
pp1=pp1->next;
pp1->next=pp2;
pp2->next=NULL;
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!='n'&&identify!='N');
return (head);
}

link *change(link *head)
{ link *p1,*p3;
p1=p3=head;
print(p3);
int choose,i,n=0;
char _choose,absorb,identify;
char input[21];
printf("\n");
printf("\n");
printf(" 请输入需修改内容所在行序列号:");
scanf("%d",&choose);
scanf("%c",&absorb);

while(p1!=NULL) //计算链表中记录数目
{ n++;
p1=p1->next;
}
p1=head;

while(choose<0||choose>n)
{ printf(" 查无此内容,请确认后重新输入:");
scanf("%d",&choose);
scanf("%c",&absorb);
}

printf("\n");
for(i=1;ip1=p1->next;

printf("a.学号 b.姓名 c.班级 d.电话 e.地址\n");
printf(" 请输入修改内容类型:");
scanf("%c",&_choose);
scanf("%c",&absorb);
printf("\n");
printf(" 请输入修改内容:");
gets(input);
printf("\n");
printf(" 确认要替换吗?(Y/N):");
scanf("%c",&identify);
scanf("%c",&absorb);
printf("\n");

while(identify!='y'&&identify!='Y'&&identify!='n'&&identify!='N')
{ printf(" 请输入(Y/N):");
scanf("%c",&identify);

}

if(identify=='y'||identify=='Y')
{ switch(_choose)
{ case 'a': strcpy(p1->xuehao,input);
printf(" 内容已修改,按任意键返回。");
break;
case 'b': strcpy(p1->name,input);
printf(" 内容已修改,按任意键返回。");
break;
case 'c': strcpy(p1->banji,input);
printf(" 内容已修改,按任意键返回。");

break;
case 'd': strcpy(p1->tel,input);
printf(" 内容已修改,按任意键返回。");
break;
case 'e': strcpy(p1->addr,input);
printf(" 内容已修改,按任意键返回。");
break;
}
}
getchar();
return (head);
}

void search(link *head)
{ char fullname[9];
printf("\n");
printf("\n");
printf(" 请输入姓名:");
gets(fullname);
link *p1;
char _name[9]="";
int n=0;
p1=head;
printf("\n");
printf("序号\t学号\t\t姓名\t\t\t班级\t\t\t电话\t\t地址\n");

while(p1!=NULL)
{ strncpy(_name,p1->name,strlen(fullname)); //按输入姓名字符数截取每个姓名,和输入姓名比较
if(strcmp(fullname,_name)==0)
{ n++; //计算链表中记录的序号
printf(" %-5d\t%-10s\t%-15s\t\t%-15s\t\t%-15s\t%-15s\n",n,p1->xuehao,p1->name,p1->banji,p1->tel,p1->addr);
p1=p1->next;
}
else
p1=p1->next;
}

printf("\n");
printf(" 共找到%d条信息,按任意键返回。",n);
getchar();
}

link *delet(link *head)
{
print(head);
link *p1,*p2,*pp;
char absorb,identify;
int i,choose,n=0;
p1=p2=pp=head;
printf("\n");
printf(" 请选择要删除的内容所在行的序号:");
scanf("%d",&choose);
scanf("%c",&absorb);

while(p1!=NULL) //计算链表中的记录数目
{ n++;
p1=p1->next;
}
p1=head;

while(choose<0||choose>n)
{ printf(" 查无此内容,请确认后重新输入:");
scanf("%d",&choose);
scanf("%c",&absorb);
}

printf(" 确认要删除吗?(Y/N)");
scanf("%c",&identify);

while(identify!='y'&&identify!='Y'&&identify!='n'&&identify!='N')
{ printf(" 请输入(Y/N):");
scanf("%c",&identify);
}

if(identify=='y'||identify=='Y')
{ if(choose==1)
{ p1=p1->next;
head=p1;
free(pp); //释放删除结点所占内存
}
else
{ for(i=1;i{
p2=p1;
p1=p1->next;

}
p2->next=p1->next;
free(p1); ////释放删除结点所占内存
}
printf(" 已成功删除,按任意键返回。");
getchar();
}

return (head);
}

void saves(link *p) //将原文件中内容清空,将链表中的内容存入文件
{ FILE *fp;
if((fp=fopen("tongxunlu.txt","w"))==NULL)
{ printf(" 无法打开文件~~~\n");
exit(0);
}
while(p!=NULL)
{ fwrite(p,LEN,1,fp);
p=p->next;
}
fclose(fp);
}


//排序
link *SortNum(link *head)
{
link *front,*node,*temp,*p,*end=NULL,message;
if(head==NULL)
{
printf("没有信息!\n");
return NULL;
}
for(node=head;node->next!=NULL;node=node->next)
{
for(front=head,temp=head->next;temp!=end;temp=temp->next,front=front->next)
if(front->xuehao>temp->xuehao)
{
message=*front;
*front=*temp;
*temp=message;
p=front->next;
front->next=temp->next;
temp->next=p;
}
end=front;
}
return head;
}





相关文档
最新文档