با این LINE EDITOR می تونید کارهایی مانند ویرایش کلمات حذف insert و یا هر کاری که مربوط به ویرایش است انجام بدید.این برنامه فقط در محیط tc قابل اجراست
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 10
#define SCAPE 27
#define RIGHT 77
#define LEFT 75
#define BACKSPACE 8
#define DELETE 83
#define END 79
#define HOME 71
#define INSERT 82
#define ISCONTROL 0
#define CtrL 12
#define CtrR 18
#define CtrY 25
#define CtrO 15
#define CtrS 19
#define TAB 9
//***********************************************
class LineEditor{
private:
int X,Y,Index,IsInsert,Numchar;
char ch,A[1000],S[12];
void Updatescreen(void){
int i,savepos=wherex()-X;
Peykan();
gotoxy(X,Y);
for(i=Index-savepos;i<Index-savepos+LEN;i++)
if(Numchar>i)
cprintf("%c",A[i]);
else
cprintf(" ");
gotoxy(X+savepos,Y);
}
/************************************************/
void Home (void){
Index=0;
gotoxy(X,Y);
}
/************************************************/
void End (void){
Index=Numchar-1;
if(Numchar<LEN-1){
gotoxy(X+Numchar-1,Y);
}
else{
gotoxy(X+LEN-1,Y);
}
}
/************************************************/
int Left (void){
if(Index==0)
return 0;
Index--;
if(wherex()>X)
gotoxy(wherex()-1,Y);
return 1;
}
/***********************************************/
int Right(void){
if(Index>Numchar-2)
return 0;
Index++;
int k=wherex();
if(k<X+LEN-1)
gotoxy(k+1,Y);
return 1;
}
/************************************************/
void Delete (void){
if(Index<Numchar-1){
for(int i=Index;i<Numchar;i++)
A[i]=A[i+1];
Numchar--;
}
}
/************************************************/
void Insert(char ch){
for(int i=Numchar-1;i>=Index;i--)
A[i+1]=A[i];
A[Index]=ch;
Numchar++;
Right();
}
/************************************************/
void Backspace(void){
if (Left())
Delete();
}
/************************************************/
void Overwrite(char ch){
if(Index==Numchar-1)
Insert(ch);
else{
A[Index]=ch;
Right();
}
}
/************************************************/
void clearleft(void){
int i,savepos=wherex()-X;
for(i=savepos;i>=0;i++)
Backspace();
gotoxy(X,Y);
}
/***********************************************/
void clearright(void){
Index++;
int c=Numchar-Index,i;
for(i=0;i<=c;i++)
Delete();
Index--;
}
/************************************************/
void clear(void){
clearright();
clearleft();
if(Numchar!=1)
Delete();
}
/************************************************/
void save(char S[]){
FILE *fp;
if ((fp=fopen(S,"wb"))==NULL){
gotoxy(X-30,Y+3);
printf("\ncannot open fill...");
getch();
exit(1);
}
fwrite(&A,2*strlen(A),1,fp);
gotoxy(X,Y);
clear();
fclose(fp);
open(S);
}
/************************************************/
void open (char S[]){
FILE *fp;
if ((fp=fopen(S,"rb"))==NULL){
gotoxy(X-30,Y+3);
gotoxy(X,Y);
}
fread(A,sizeof(A),1,fp);
Numchar=strlen(A)+1;
End();
fclose(fp);
}
//**********************************************
void Peykan (void){
int b=wherex();
int a=X+LEN-wherex();
if(Index==0){
gotoxy(X-1,Y);
cprintf(" ");
gotoxy(X,Y);
}
if(Numchar-Index-1>a){
gotoxy(X+LEN,Y);
cprintf("%c",16);
gotoxy(b,Y);
}
else{
gotoxy(X+LEN,Y);
cprintf(" ");
gotoxy(b,Y);
}
if(Index>LEN-1){
gotoxy(X-1,Y);
cprintf("%c",17);
gotoxy(b,Y);
}
if(wherex()-X<Index){
gotoxy(X-1,Y);
cprintf("%c",17);
gotoxy(b,Y);
}
}
//***********************************************
public:
LineEditor(int x,int y,char *s){
X=x;
Y=y;
Numchar=1;
IsInsert=0;
Index=0;
A[Numchar-1]='\0';
strcpy(S,s);
}
//***********************************************
void DetectKeyGo(void){
End();
Updatescreen();
while((ch=getch())!=SCAPE){
if (ch=='\r')
continue;
if((ch==ISCONTROL) || (ch==BACKSPACE) || (ch==CtrL) || (ch==CtrR) || (ch==CtrY) || (ch==CtrS) || (ch==CtrO) || (ch==TAB)){
if(ch==ISCONTROL)
ch=getch();
switch(ch){
case TAB: End();
if(Y<29) gotoxy(X,Y+5);
else gotoxy(X,Y-10);
return;
case END:End(); break;
case LEFT:Left(); break;
case HOME:Home(); break;
case CtrY:clear(); break;
case CtrS:save(S); break;
case CtrO:open(S); break;
case RIGHT:Right(); break;
case DELETE:Delete(); break;
case CtrL:clearleft(); break;
case CtrR:clearright(); break;
case BACKSPACE:Backspace(); break;
case INSERT:IsInsert++;
}}
else if (IsInsert%2==0)
Insert(ch);
else
Overwrite(ch);
Updatescreen();
} exit(0);
}
//************************************************
void Drawline (void){
textbackground(1);
textcolor(4);
gotoxy(X-1,Y);
for(int i=1;i<=LEN+2;i++)
cprintf(" ");
gotoxy(X,Y);
}
};
//***********************************************
void MYNAME(void){
textcolor(7);
gotoxy(26,5);
printf(" << In The Name Of God >>");
gotoxy(25,10);
printf(" << Seyyed Reza Hashemian >>");
}
//***********************************************
int main(){
LineEditor L1(33,20,"LineE1.txt"),L2(33,25,"LineE2.txt"),L3(33,30,"LineE3.txt");
textbackground(0);
clrscr();
MYNAME();
L3.Drawline();
L2.Drawline();
L1.Drawline();
while(1){
L1.DetectKeyGo();
L2.DetectKeyGo();
L3.DetectKeyGo();
}
return 0;
}