#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct line
{
char text[81];
int num;
struct line *next;
struct line *prior;
}txtLine;
txtLine *start;
txtLine *last;
txtLine *find(int linenum)
{
txtLine *info;
info=start;
while (info)
{
if (linenum==info->num)
{
return(info);
}
info=info->next;
}
return(NULL);
}
void patchup(int n,int incr)
{
txtLine *i;
i=find(n);
while(i)
{
i->num=i->num+incr;
i=i->next;
}
}
txtLine *insert_Line(txtLine *i)
{
txtLine *old,*p;
if (last==NULL)
{
i->next=NULL;
i->prior=NULL;
last=i;
return(i);
}
p=start;
old=NULL;
while(p)
{
if (p->num<i->num)
{
old=p;
p=p->next;
}
else
{
if (p->prior)
{
p->prior->next=i;
i->next=p;
p->prior=i;
return start;
}
i->next=p;
i->prior=NULL;
p->prior=i;
return(i);
}
}
old->next=i;
i->next=NULL;
i->prior=old;
last=i;
return start;
}
int enter(int linenum)
{
txtLine *info;
for (;;)
{
info=(txtLine *)malloc(sizeof(txtLine));
if (!info)
{
printf("\t!内存不足!\n");
return(NULL);
}
printf("%d:",linenum);
gets(info->text);
info->num=linenum;
if (*info->text)
{
if (find(linenum))
{
patchup(linenum,1);
}
if (*info->text)
{
start=insert_Line(info);
}
}
else
{
break;
}
linenum++;
}
return(linenum);
}
void delete_text()
{
txtLine *info;
char s[80];
int linenum;
printf("\t行号:");
gets(s);
linenum=atoi(s);
info=find(linenum);
if (info)
{
if (start==info)
{
start=info->next;
if (start)
{
start->prior=NULL;
}
else
{
last=NULL;
}
}
else
{
info->prior->next=info->next;
if (info!=last)
{
info->next->prior=info->prior;
}
else
{
last=info->prior;
}
}
free(info);
patchup(linenum+1,-1);
}
}
void list()
{
txtLine *info;
info=start;
while (info)
{
printf("%d:%s\n",info->num,info->text);
info=info->next;
}
printf("\n\n");
}
void wordnum()
{
line *p;
char keyword[80];
printf("请输入你要统计的单词:");
gets(keyword);
char *key=keyword,*q,*r;
int len=strlen(key),i=0;
p=start;
do
{
q=p->text;
q--;
do
{
if (q=strstr(++q,key))
{
r=q;
if (!(((*r-1)>='a'&&*(r-1)<='z')||(*(r-1)>='A'&&*(r-1)<='Z')||((*(r+len)>='a'&&*(r+len)<='z')||(*(r+len)>='A'&&*(r+len)<='Z'))))
{
i++;
}
}
} while (q!=NULL);
p=p->next;
} while (p);
printf("你输入的单词在本文中出现的次数为:%d\n",i);
}
void wordfind()
{
txtLine *p;
char keyword[80];
printf("请输入你要查找的单词:");
gets(keyword);
char *key=keyword,*q,*r;
int len=strlen(key),i;
p=start;
do
{
q=p->text;
q--;
do
{
i=1;
if (q=strstr(++q,key))
{
r=q;
if (!(((*r-1)>='a'&&*(r-1)<='z')||(*(r-1)>='A'&&*(r-1)<='Z')||((*(r+len)>='a'&&*(r+len)<='z')||(*(r+len)>='A'&&*(r+len)<='Z'))))
{
for (r=p->text;r!=q;r++)
{
if (!(*r>='a'&&*r<='z'||*r>='A'&&*r<='Z'))
{
i++;
}
}
printf("你查找的单词在第%d行第%d个\n",p->num,i);
printf("继续下一个查找输入'y'回车将停止查找:");
char s;
fflush(stdin);
scanf("%c",&s);
switch (s)
{
case 'y':
case 'Y':
continue;
default:
printf("\n查找已停止!\n");
return;
}
}
}
}while (q!=NULL);
p=p->next;
}while (p);
printf("查找完毕!");
}
void save(char *fname)
{
txtLine *info;
char *p;
FILE *fp;
if ((fp=fopen("text.txt","w"))==NULL)
{
printf("\t文件打不开!\n");
exit(0);
}
printf("\t正在存入文件:\n");
info=start;
while(info)
{
p=info->text;
while(*p)
{
putc(*p++,fp);
}
putc('\n',fp);
info=info->next;
}
fclose(fp);
}
void load(char *fname)
{
txtLine *info,*temp;
char *p;
FILE *fp;
int size,inct;
if ((fp=fopen("text.txt","r"))==NULL)
{
printf("\t文件打不开!\n");
exit(0);
}
while(start)
{
temp=start;
start=start->next;
free(temp);
}
printf("\n\t正在装入文件!\n");
size=sizeof(txtLine);
start=(txtLine *)malloc(size);
if (!start)
{
printf("\n\t内存已经用完!");
return;
}
info=start;
p=info->text;
inct=1;
while ((*p=getc(fp))!=EOF)
{
p++;
while ((*p=getc(fp))!='\n')
{
p++;
}
//getc(fp); //丢掉'\n'
*p='\0';
info->num=inct++;
info->next=(txtLine *)malloc(size);
if (!info->next)
{
printf("\n\t内存已经用完!");
return;
}
info->prior=temp;
temp=info;
info=info->next;
p=info->text;
}
temp->next=NULL;
last=temp;
free(info);
start->prior=NULL;
fclose(fp);
}
int menu_select()
{
char s[80];
int c;
printf("\t\t1.输入\n");
printf("\t\t2.删除一行\n");
printf("\t\t3.显示全部\n");
printf("\t\t4.单词统计\n");
printf("\t\t5.查找定位单词\n");
printf("\t\t6.定行位置插入\n");
printf("\t\t7.文件存盘\n");
printf("\t\t8.装入文件\n");
printf("\t\t9.退出\n");
do
{
printf("\n\n\t请按数字选择:");
gets(s);
c=atoi(s);
} while (c<1||c>9);
return(c);
}
void main()
{
char s[80],choice,fname[80];
int linenum=1;
start=NULL;
last=NULL;
do
{
choice=menu_select();
switch (choice)
{
case 1:
printf("\t行号:");
gets(s);
linenum=atoi(s);
enter(linenum);
break;
case 2:
delete_text();
list();
break;
case 3:
list();
break;
case 4:
wordnum();
printf("回车返回主菜单!");
getchar();
break;
case 5:
wordfind();
printf("回车返回主菜单!");
getchar();
break;
case 6:
printf("\t插入的行号:");
gets(s);
linenum=atoi(s);
enter(linenum);
list();
break;
case 7:
printf("\t文件名:");
gets(fname);
save(fname);
break;
case 8:
printf("\t文件名:");
gets(fname);
load(fname);
break;
case 9:
exit(0);
}
} while (1);
}