?? 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 + -