本文实例为大家分享了C语言实现文本编辑器系统的具体代码,供大家参考,具体内容如下
#include#include #include #include #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 nextl; p=q->next; for(i=1;i 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 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 nextl; p1=q->next; for(i=1;i 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 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 nextl; p1=q->next; for(i=1;i 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;iopen 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(" 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;i0) 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;i0) 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;i0) for(i=0;i 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



