时间:2021-05-20
本文实例为大家分享了C语言实现文本编辑器系统的具体代码,供大家参考,具体内容如下
#include <stdio.h>#include <conio.h>#include <bios.h>#include <math.h>#define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define ESC 0x011b #define ENTER 0x1c0d #define DEL 21248 #define BACK 3592 #define CL 29440 #define CR 29696 #define Cc 11779 #define Cv 12054 #define Cx 11544 #define F1 15104 #define F2 15360 #define F3 15616 #define F10 17408 int value,backup,NUM;typedef struct record { char ch; int col, line; }record;record r[500]; typedef struct node { char ch; struct node *next; }node; typedef struct Hnode { node *next; struct Hnode *nextl; }Hnode;void drawmain() { int i,j; gotoxy(1,1); textbackground(7); textcolor(0); insline(); for(i=1;i<=24;i++) { gotoxy(1,1+i); cprintf("%c",196); gotoxy(80,1+i); cprintf("%c",196); } for(i=1;i<=79;i++) { gotoxy(1+i,2); cprintf("%c",196); gotoxy(1+i,25); cprintf("%c",196); } gotoxy(1,1); cprintf("%c",196); gotoxy(1,24); cprintf("%c",196); gotoxy(80,1); cprintf("%c",196); gotoxy(80,24); cprintf("%c",196); gotoxy(7,1); cprintf("%c %c File %c %c",179,17,16,179); gotoxy(27,1); cprintf("%c %c Edit %c %c",179,17,16,179); gotoxy(47,1); cprintf("%c %c Help %c %c",179,17,16,179); gotoxy(5,25); textcolor(1); cprintf(" Row:1 Col:1"); gotoxy(68,25); cprintf("Version 2.0"); }void qview(Hnode *q) /*快速预览文本:开头:#,回车:* */ { void view(Hnode *q); node *p; int i; window(1,1,80,25); clrscr(); do{ p=q->next; cprintf("#"); while(p!=NULL) { if(p->ch==13) putch('*'); /*若为回车键,打印出*号*/ else putch(p->ch); p=p->next; } q=q->nextl; printf("\n"); }while(q!=NULL); getch(); clrscr(); drawmain(); window(2,2,79,23); textbackground(9); for(i=0;i<24;i++) insline(); window(3,3,78,23); textcolor(10); }void view(Hnode *q) { node *p; clrscr(); do{ p=q->next; while(p!=NULL&&p->ch>=32&&p->ch<127&&p->ch!=13&&p->ch!=-1) { putch(p->ch); p=p->next; } q=q->nextl; if((p->ch==13||p->ch==-1)&&q!=NULL) gotoxy(1,wherey()+1); }while(q!=NULL); } int check(Hnode *Hhead,int m,int n) { int i; Hnode *q; node *p; q=Hhead; for(i=1;i<m;i++) q=q->nextl; p=q->next; for(i=1;i<n;i++) p=p->next; if(p->ch==13) return -1; if(p->ch>=32&&p->ch<127) return p->ch; else return 0; }int judge(Hnode *Hhead,int m) { Hnode *q; node *p; int i,num=0; q=Hhead; for(i=1;i<m;i++) q=q->nextl; if(q==NULL) return -1; p=q->next; while(p->next!=NULL) { p=p->next; num++; } if(p->ch==13&&num==0) return 0; if(p->ch>=32&&p->ch<127) return num+1; if(p->ch==13&&num!=0) return num; else return 1; } int del(Hnode *Hhead,int m,int n) { Hnode *q,*q1; node *p1,*p2,*tail; int i,num=0,j,flag=0; q=Hhead; if(n==0&&m==1) return; if(n==0&&m>1) { n=76; m=m-1; gotoxy(n,m); flag=1; } for(i=1;i<m;i++) q=q->nextl; p1=q->next; for(i=1;i<n-1;i++) p1=p1->next; p2=p1->next; if(n==1) { q->next=p1->next; free(p1); } else { p1->next=p2->next; free(p2); } while((num=judge(Hhead,m++))>0) { p1=q->next; q1=q; if(p1!=NULL) { while(p1->next!=NULL) p1=p1->next; tail=p1; q=q->nextl; p1=p2=q->next; tail->next=p1; } else { q=q->nextl; p1=p2=q->next; q1->next=p1; } for(i=0;i<76-num;i++) { p1=p2; p2=p2->next; if(p2->ch==13) break; } q->next=p2; p1->next=NULL; } return flag; } int test(Hnode *Hhead,int n) { int i=0,num1=1; node *p1,*p2,*tail,*temp1,*temp2; Hnode *q; q=Hhead; for(i=1;i<n;i++) q=q->nextl; tail=p1=q->next; if(p1==NULL) return; while(tail->next!=NULL) tail=tail->next; for(i=0;i<75;i++) { if(p1->ch==13||p1->next==NULL) break; p1=p1->next; } p2=p1->next; p1->next=NULL; if(tail->ch!=13) { if(p1->ch==13&&q->nextl==NULL) { q->nextl=(Hnode *)malloc(sizeof(Hnode)); q->nextl->nextl=NULL; tail->next=(node *)malloc(sizeof(node)); tail->next->ch=13; tail->next->next=NULL; q->nextl->next=p2; } else { q=q->nextl; tail->next=q->next; q->next=p2; if(q!=NULL) test(Hhead,++n); } } else { temp2=p2; while(q!=NULL&&p2!=NULL) { if((q->nextl==NULL)&&(p1!=tail||p2!=NULL)&&(num1==1)) { num1++; q->nextl=(Hnode *)malloc(sizeof(Hnode)); q->nextl->nextl=NULL; q->nextl->next=NULL; } q=q->nextl; temp1=q->next; q->next=temp2; temp2=temp1; } } }void insert(Hnode *Hhead,int m,int n, char a) { int i; Hnode *q; node *p,*p1,*p2; q=Hhead; for(i=1;i<m;i++) q=q->nextl; p1=q->next; for(i=1;i<n-1;i++) p1=p1->next; p=(node *)malloc(sizeof(node)); p->ch=a; if(n==1) { p->next=q->next; q->next=p; } else { p->next=p1->next; p1->next=p; } test(Hhead,m); }void control(int A, Hnode *Hhead) { void colorview(Hnode *,int,int); int x,y,flag=0; x=wherex(); y=wherey(); if((A==CL)&&(x!=1)) gotoxy(wherex()-1,wherey()); if((A==CL)&&(x==1)) gotoxy(abs(judge(Hhead,wherey()-1)),wherey()-1); if((A==CR)&&check(Hhead,wherey(),wherex())>0) { flag=1; gotoxy(wherex()+1,wherey()); } if((A==CR)&&check(Hhead,wherey()+1,1)>0&&check(Hhead,y,x)==0) { flag=1; gotoxy(1,wherey()+1); } if((A==CR)&&x==76) { flag=1; gotoxy(1,wherey()+1); } if(A==CR&&flag==1) { r[abs(value)].col=wherex(); r[abs(value)].line=wherey(); r[abs(value)].ch=check(Hhead,r[abs(value)].line,r[abs(value)].col); if(r[abs(value)].ch==-1) r[abs(value)].ch=13; value--; } if(A==CL&&(x!=1||y!=1)) { r[abs(value)].col=wherex(); r[abs(value)].line=wherey(); r[abs(value)].ch=check(Hhead,r[abs(value)].line,r[abs(value)].col); value++; } colorview(Hhead,wherex(),wherey()); }void colorview(Hnode *Hhead,int x,int y) { int i; view(Hhead); for(i=0;i<abs(value);i++) { gotoxy(r[i].col,r[i].line); textbackground(7); textcolor(0); if(r[i].ch!=13&&r[i].ch!=-1) cprintf("%c",r[i].ch); if(r[i].ch==13||r[i].ch==-1) cprintf(" "); } gotoxy(x,y); }void drawmenu(int m,int n) { int i; if(m%3==0) { window(8,2,19,9); textcolor(0); textbackground(7); for(i=0;i<7;i++) { gotoxy(1,1+i); insline(); } window(1,1,80,25); gotoxy(7,1); for(i=1;i<=7;i++) { gotoxy(8,1+i); cprintf("%c",179); gotoxy(19,1+i); cprintf("%c",179); } for(i=1;i<=11;i++) { gotoxy(8+i,2); cprintf("%c",196); gotoxy(8+i,9); cprintf("%c",196); } textbackground(0); gotoxy(10,10); cprintf(" "); for(i=0;i<9;i++) { gotoxy(20,2+i); cprintf(" "); } textbackground(7); gotoxy(8,2); cprintf("%c",218); gotoxy(8,9); cprintf("%c",192); gotoxy(19,2); cprintf("%c",191); gotoxy(19,9); cprintf("%c",217); gotoxy(9,3); cprintf(" New "); gotoxy(9,4); cprintf(" Open "); gotoxy(9,5); cprintf(" Save "); gotoxy(9,6); cprintf(" Save as"); for(i=1;i<=10;i++) { gotoxy(8+i,7); cprintf("%c",196); } gotoxy(9,8); cprintf(" Exit"); textcolor(15); textbackground(0); gotoxy(7,1); cprintf("%c %c File %c %c",179,17,16,179); switch(n%5) { case 0:gotoxy(9,3); cprintf(" New "); break; case 1:gotoxy(9,4); cprintf(" Open "); break; case 2:gotoxy(9,5); cprintf(" Save "); break; case 3:gotoxy(9,6); cprintf(" Save as "); break; case 4:gotoxy(9,8); cprintf(" Exit "); break; } } /********************************************************/ if(m%3==1) { window(28,2,38,7); textcolor(0); textbackground(7); for(i=0;i<5;i++) { gotoxy(1,1+i); insline(); } window(1,1,80,25); gotoxy(27,1); for(i=1;i<=5;i++) { gotoxy(28,1+i); cprintf("%c",179); gotoxy(39,1+i); cprintf("%c",179); } for(i=1;i<=11;i++) { gotoxy(28+i,2); cprintf("%c",196); gotoxy(28+i,7); cprintf("%c",196); } textbackground(0); gotoxy(30,8); cprintf(" "); for(i=0;i<7;i++) { gotoxy(40,2+i); cprintf(" "); } textbackground(7); gotoxy(28,2); cprintf("%c",218); gotoxy(28,7); cprintf("%c",192); gotoxy(39,2); cprintf("%c",191); gotoxy(39,7); cprintf("%c",217); gotoxy(29,3); cprintf(" Cut "); gotoxy(29,4); cprintf(" Copy "); gotoxy(29,5); cprintf(" Paste "); gotoxy(29,6); cprintf(" Clear "); textcolor(15); textbackground(0); gotoxy(27,1); cprintf("%c %c Edit %c %c",179,17,16,179); switch(n%4) { case 0:gotoxy(29,3); cprintf(" Cut "); break; case 1:gotoxy(29,4); cprintf(" Copy "); break; case 2:gotoxy(29,5); cprintf(" Paste "); break; case 3:gotoxy(29,6); cprintf(" Clear "); break; } } /*********************************************************/ if(m%3==2) { window(48,2,48,6); textcolor(0); textbackground(7); for(i=0;i<3;i++) { gotoxy(1,1+i); insline(); } window(1,1,80,25); gotoxy(47,1); for(i=1;i<=5;i++) { gotoxy(48,1+i); cprintf("%c",179); gotoxy(59,1+i); cprintf("%c",179); } for(i=1;i<=11;i++) { gotoxy(48+i,2); cprintf("%c",196); gotoxy(48+i,6); cprintf("%c",196); } textbackground(0); gotoxy(50,7); cprintf(" "); for(i=0;i<6;i++) { gotoxy(60,2+i); cprintf(" "); } textbackground(7); gotoxy(48,2); cprintf("%c",218); gotoxy(48,6); cprintf("%c",192); gotoxy(59,2); cprintf("%c",191); gotoxy(59,6); cprintf("%c",217); gotoxy(49,3); cprintf("Help... "); gotoxy(49,5); cprintf("About... "); for(i=1;i<=10;i++) { gotoxy(48+i,4); cprintf("%c",196); } textcolor(15); textbackground(0); gotoxy(47,1); cprintf("%c %c Help %c %c",179,17,16,179); switch(n%2) { case 0:gotoxy(49,3); cprintf("Help... "); break; case 1:gotoxy(49,5); cprintf("About... "); break; } } }int menuctrl(Hnode *Hhead,int A) { int x,y,i,B,value,flag=100,a,b; x=wherex(); y=wherey(); if(A==F1) { drawmenu(0,flag); value=300; } if(A==F2) { drawmenu(1,flag); value=301; } if(A==F3) { drawmenu(2,flag); value=302; } if(A==F1||A==F2||A==F3) { while((B=bioskey(0))!=ESC) { if(flag==0) flag=100; if(value==0) value=300; if(B==UP) drawmenu(value,--flag); if(B==DOWN) drawmenu(value,++flag); if(B==LEFT) { flag=100; drawmain(); window(2,2,79,23); textbackground(9); for(i=0;i<24;i++) insline(); window(3,3,78,23); textcolor(10); view(Hhead); drawmenu(--value,flag); } if(B==RIGHT) { flag=100; drawmain(); window(2,2,79,23); textbackground(9); for(i=0;i<24;i++) insline(); window(3,3,78,23); textcolor(10); view(Hhead); drawmenu(++value,flag); } if(B==ENTER) { if(value%3==0) b=5; if(value%3==1) b=4; if(value%3==2) b=2; a=(value%3)*10+flag%b; drawmain(); window(2,2,79,23); textbackground(9); for(i=0;i<24;i++) insline(); window(3,3,78,23); textcolor(10); view(Hhead); gotoxy(x,y); if(a==0) return 100; if(a==1) return 101; if(a==2) return 102; if(a==3) return 103; if(a==4) exit(0); if(a==10) return Cx; if(a==11) return Cc; if(a==12) return Cv; if(a==13) return DEL; if(a==20) return 120; if(a==21) return 121; } gotoxy(x+2,y+2); } drawmain(); window(2,2,79,23); textbackground(9); for(i=0;i<24;i++) insline(); window(3,3,78,23); textcolor(10); view(Hhead); gotoxy(x,y); } return A; }void save(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; 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 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; 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");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;} 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;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(); if((A==LEFT)&&(x!=1)) gotoxy(wherex()-1,wherey()); 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()); 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); if(A==BACK) { 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; } } 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);} } if(A==DEL) { x=wherex(); y=wherey(); del(Hhead,wherey(),wherex()); view(Hhead); gotoxy(x,y); } 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; view(Hhead); gotoxy(x,y); } 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; value=0; view(Hhead); gotoxy(x,y); } if(A==Cc&&value!=0) { x=wherex(); y=wherey(); backup=value; value=0; view(Hhead); gotoxy(x,y); } if(A==Cv&&backup!=0) { x=wherex(); y=wherey(); if(backup<0) for(i=0;i<abs(backup);i++) insert(Hhead,y,x+i,r[i].ch); if(backup>0) 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); } if(A==CL||A==CR) control(A,Hhead); x=wherex(); y=wherey(); window(1,1,80,25); textcolor(0); textbackground(7); gotoxy(10,25); cprintf("%-3d",y); gotoxy(24,25); cprintf("%-3d",x); window(3,3,78,23); textcolor(10); gotoxy(x,y); textcolor(10); textbackground(1); } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
如果您想要设置C++语言环境,您需要确保电脑上有以下两款可用的软件,文本编辑器和C++编译器。文本编辑器这将用于输入您的程序。文本编辑器包括WindowsNot
C环境设置本地环境设置如果您想要设置C语言环境,您需要确保电脑上有以下两款可用的软件,文本编辑器和C编译器。文本编辑器这将用于输入您的程序。文本编辑器包括Win
vim文本编辑器是由vi发展而来的,拥有丰富的编辑功能,Linux系统经常会用到vim文本编辑器。在Ubuntu系统中,vim文本编辑器存在无安装的问题,遇
文本编辑器系列软件最新版本下载4.20MBEditPlus(文本编辑器)立即下载2.88MBQuickEdit文本编辑器立即下载软件名称:MacromediaD
本地环境设置在这里我们介绍设置Go编程语言环境,需要在你的计算机上的准备以下两个软件,(A)文本编辑器和(B)Go编译器。文本编辑器这将用来键入程序。一些编辑器