📄 editor.c
字号:
scanf("%s",filename); /*输入文件名*/
fp=fopen(filename,"w");
if(fp==NULL) /*打开文件失败*/
{
printf("\n=====>open file error!\n");
getchar();
return ;
}
do{
p=q->next; /*指向node类型的数据*/
while(p!=NULL)
{ if((int)p->ch==13)
{
fputc('\n',fp);p=p->next; count++;
}
else
{fputc(p->ch, fp);
p=p->next;
count++;}
}
q=q->nextl;
}while(q!=NULL);
fclose(fp); /*关闭此文件*/
return ;
}
/*文件另存为:将head所指的行单链表中所指的各个列单链表中的数据域的值写入文件,文件路径和文件名由用户指定*/
void saveas(Hnode *head)
{
FILE* fp;
Hnode *q;
node *p;
int count=0,x,y;
char filename[10]; /*保存文件名*/
q=head;
clrscr();/*清屏*/
printf("Enter infile name,for example [c:\\wb.txt]:");/*输入文件名格式*/
scanf("%s",filename); /*输入文件名*/
fp=fopen(filename,"w");
if(fp==NULL) /*打开文件失败*/
{
printf("\n=====>open file error!\n");
getchar();
return ;
}
do{
p=q->next; /*指向node类型的数据*/
while(p!=NULL)
{ if((int)p->ch==13)
{
fputc('\n',fp);p=p->next; count++;
}
else
{fputc(p->ch, fp);
p=p->next;
count++;}
}
q=q->nextl;
}while(q!=NULL);
fclose(fp); /*关闭此文件*/
return ;
}
/*从任意文本文件中读取文件内容,保存至行单链表和列单链表形式的数据结构中*/
void opens(Hnode *Hp)
{
FILE* fp;
Hnode *q11,*q22;
node *p11,*p22,*hp;
char temp;
int count=0,flags=1;
char filename[10]; /*保存文件名*/
clrscr();/*清屏*/
printf("Enter infile name,for example [c:\\wb.txt]:");/*输入文件名格式*/
scanf("%s",filename); /*输入文件名*/
fp=fopen(filename,"r");/*以只读方式打开文件,filename必须要存在*/
if(fp==NULL)/*打开文件失败*/
{ textbackground(2);
textcolor(13);
cprintf("open file error!");
getchar();
exit(0) ;
}
q11=Hp;
while(!feof(fp))
{ count=0;flags=1;
q22=(Hnode *)malloc(sizeof(Hnode));/*新建一个行单链表中的节点*/
p11=(node *)malloc(sizeof(node)); /*新建一个列单链表中的节点*/
while((temp=fgetc(fp))!=10&&count<=76&&!feof(fp)) /*循环结束,表示在单链表中一行处理完毕,开始新行*/
{ p22=(node *)malloc(sizeof(node));/*新建一个列单链表中的节点*/
if(flags==1) {hp=p22;flags=0;} /*hp保存列单链表中的首节点的地址*/
p22->ch=temp; p22->next=NULL;
p11->next=p22; p11=p22;
count++;
}
if(temp==10){ /*若为换行符,将其转换为回车符,因为在程序中,是按回车符处理的*/
p22=(node *)malloc(sizeof(node));p22->ch=13; p22->next=NULL;
p11->next=p22; p11=p22;
}
if(!feof(fp))/*若没此条件,文件最后一行会处理两次.*/
{q22->next=hp;q22->nextl=NULL; /*将存储了字符的新列单链表与行单链表中的新节点建立关联*/
q11->nextl=q22;q11=q22;}
}
fclose(fp);
Hp=Hp->nextl;/*因为Hp的所在节点的数据域为空,所以Hp=Hp->nextl*/
return ;
}
void main()
{
char a;
int i,A,x,y,flag=0,b;
Hnode *Hhead,*q;
node *p1,*p2;
Hhead=(Hnode *)malloc(sizeof(Hnode)); /*为行单链表中首节点分配内存空间*/
q=Hhead; Hhead->nextl=NULL;
p1=p2=q->next=(node *)malloc(sizeof(node)); /*为列单链表中首节点分配内存空间*/
p1->ch=13; p1->next=NULL;
drawmain(); /*显示主窗口*/
window(2,2,79,23);
textbackground(9);
for(i=0;i<24;i++)
insline();
window(3,3,78,23);
textcolor(10);
while(1)
{
while(bioskey(1)==0) continue; /*等待用户按键*/
a=A=bioskey(0); /*返回输入的字符的键值*/
if(a>=32&&a<127) /*若输入为常规字符或回车键*/
{
if(check(Hhead,wherey(),wherex())<=0)/*当前位置没有字符且输入是常规字符,则执行添加字符操作*/
{
NUM++;
p2->ch=a;
putch(a);
if(NUM==76) /*连续输入满行,分别生成一个新的行单链表和列单链表节点*/
{
p2->next=NULL;
q->nextl=(Hnode *)malloc(sizeof(Hnode));
q=q->nextl; q->nextl=NULL; q->next=NULL;
p1=p2=q->next=(node *)malloc(sizeof(node));
p1->ch=13; p1->next=NULL;
NUM=0;
}
else /*连续输入未满一行,生成一个新的列单链表节点*/
{
p2->next=(node *)malloc(sizeof(node));
p2=p2->next;
p2->ch=13;
p2->next=NULL;
}
}
else /*当前位置有字符且输入是常规字符,则执行插入字符操作*/
{
x=wherex(); y=wherey();
insert(Hhead,wherey(),wherex(),a);
NUM++;
view(Hhead);
gotoxy(x,y);
}
}
/*若输入为回车键*/
if(a==13)
{
gotoxy(1,wherey()+1);
q->nextl=(Hnode *)malloc(sizeof(Hnode));
q=q->nextl; q->nextl=NULL; q->next=NULL;
p1=p2=q->next=(node *)malloc(sizeof(node));
p1->ch=13; p1->next=NULL;
NUM=0;
}
x=wherex(); y=wherey();
/*文本窗口中左移,当前光标不在窗口的第1列*/
if((A==LEFT)&&(x!=1)) gotoxy(wherex()-1,wherey());
/*文本窗口中左移,当前光标在窗口的第1列*/
if((A==LEFT)&&(x==1)) gotoxy(abs(judge(Hhead,wherey()-1)),wherey()-1);
/*文本窗口中右移,若当前光标的右边一位有字符*/
if((A==RIGHT)&&check(Hhead,wherey(),wherex())>0) gotoxy(wherex()+1,wherey());
/*文本窗口中右移至下行的第1列,若当前光标位置没有字符且下行的第1列有字符*/
if((A==RIGHT)&&check(Hhead,wherey()+1,1)!=0&&check(Hhead,y,x)<=0) gotoxy(1,wherey()+1);
/*右移*/
if((A==RIGHT)&&x==76) gotoxy(1,wherey()+1);
/*上移*/
if((A==UP)&&check(Hhead,wherey()-1,wherex())!=0) gotoxy(wherex(),wherey()-1);
/*上移*/
if((A==UP)&&check(Hhead,wherey()-1,wherex())<=0)
{
if(judge(Hhead,wherey()-1)==0)
gotoxy(-judge(Hhead,wherey()-1)+1,wherey()-1);
else
gotoxy(-judge(Hhead,wherey()-1),wherey()-1);
}
/*下移*/
if((A==DOWN)&&check(Hhead,wherey()+1,wherex())!=0)
gotoxy(wherex(),wherey()+1);
/*处理BackSpace键*/
if(A==BACK) /*处理BackSpace键*/
{
flag=del(Hhead,wherey(),wherex()-1);
x=wherex()-1; y=wherey();
view(Hhead);
if(flag==0)
{
if(x!=0) gotoxy(x,y);
else gotoxy(x+1,y);
}
if(flag==1)
{
gotoxy(x+1,y);
flag=0;
}
}
/*处理菜单按键F1 F2 F3*/
if((A==F1)||(A==F2)||(A==F3)||(a<32||a>127))
{ A=menuctrl(Hhead,A);
if(A==100){main();} /*新建文件*/
if(A==101){ /*打开文件*/
Hhead=(Hnode *)malloc(sizeof(Hnode));
opens(Hhead);
getchar();clrscr();gotoxy(3,3);view(Hhead);
}
/*保存文件*/
if(A==102){save(Hhead);clrscr();cprintf("save successfully!");getch();gotoxy(3,3);view(Hhead);}
/*文件另存为*/
if(A==103){saveas(Hhead);clrscr();cprintf("save as successfully!");getch();gotoxy(3,3);view(Hhead);}
/*帮助*/
if(A==120){clrscr();cprintf("<Help> F1:File F2:Edit F3:Help ");
getch();gotoxy(3,3);view(Hhead);}
if(A==121){clrscr();cprintf("Abort:Version 2.0 Tel:XXXXXXXXXX");getch();gotoxy(3,3);view(Hhead);}
}
/*处理DEL键,删除当前位置的单个字符*/
if(A==DEL)
{
x=wherex(); y=wherey();
del(Hhead,wherey(),wherex());
view(Hhead);
gotoxy(x,y);
}
/*处理已经选定文本字符后,按DEL键的情况*/
if(A==DEL&&value!=0)
{
if(value>0)
x=wherex(), y=wherey();
else
x=r[0].col, y=r[0].line;
for(i=0;i<abs(value);i++)
{
if(value>0)
del(Hhead,r[i].line,r[i].col);
if(value<0)
del(Hhead,r[abs(value)-1-i].line,r[abs(value)-1-i].col);
}
value=0; /*此value为全局变量*/
view(Hhead);
gotoxy(x,y);
}
/*处理Ctrl+x按键*/
if(A==Cx&&value!=0)
{
if(value>0)
x=wherex(), y=wherey();
else
x=r[0].col, y=r[i].line;
for(i=0;i<abs(value);i++)
{
if(value>0)
del(Hhead,r[i].line,r[i].col);
if(value<0)
del(Hhead,r[abs(value)-1-i].line,r[abs(value)-1-i].col);
}
backup=value; /*保存r数组的有值元素的最大下标值*/
value=0; /*此value为全局变量*/
view(Hhead);
gotoxy(x,y);
}
/*处理Ctrl+c按键*/
if(A==Cc&&value!=0)
{
x=wherex(); y=wherey();
backup=value; value=0; /*此value为全局变量*/
view(Hhead);
gotoxy(x,y);
}
/*处理Ctrl+v按键*/
if(A==Cv&&backup!=0)
{
x=wherex(); y=wherey();
if(backup<0) /*Ctrl+右移键选定的文本,贴切此当前位置*/
for(i=0;i<abs(backup);i++)
insert(Hhead,y,x+i,r[i].ch);/*逐个插入*/
if(backup>0) /*Ctrl+左移键选定的文本,贴切此当前位置*/
for(i=0;i<backup;i++)
insert(Hhead,y,x+i,r[backup-1-i].ch);
view(Hhead);
gotoxy(x,y);
}
/*快速预览*/
if(A==F10)
{
qview(Hhead);
view(Hhead);
gotoxy(x,y);
}
/*处理Ctrl+左移键或右移键*/
if(A==CL||A==CR) control(A,Hhead);
/*显示当前行列号*/
x=wherex(); y=wherey();
window(1,1,80,25);
textcolor(0);
textbackground(7);
gotoxy(10,25); /*第25行,第10列,输出当前行号wherey()*/
cprintf("%-3d",y);
gotoxy(24,25); /*第25行,第24列*/
cprintf("%-3d",x);
window(3,3,78,23);
textcolor(10);
gotoxy(x,y);
textcolor(10);
textbackground(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -