#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<bios.h>
#include<GdiPlusMem.h>
#define MAXROW 300
#define MAXCOL 80
#define MAXCMDTIMES 300
#define BackSpace 8
#define CR 13
#define ESC 277
#define UP 72
#define PGUP 73
#define LEFT 75
#define RIGHT 77
#define DOWN 80
#define PGDN 81
#define CTRL_B 2
#define CTRL_F 6
#define MIN(a,b) a>b ? b:a
#define MAX(a,b) a>b ? a:b
void beep(void);
int init(char *);
int editfun(char *);
void beep(void);
int write_file(char *);
void refresh_scr(void);
int input_str(int,int,char *,int);
int display(int,char *);
int append(void);
int search(char *);
typedef struct{
int allrec;
int scry;
int shinex,shiney;
unsigned char info[MAXROW][MAXCOL+1];
}EDIT;
EDIT edit;
void main(int argc,char *argv[])
{
if(argc!=2){
printf("Command error!\nUsage %s filename\n",argv[0]);
return;
}
if(init(argv[1])<0)
return;
editfun(argv[1]);
clrscr();
exit(0);
}
int init(char *filename)
{
int i,j;
FILE *fp;
if((fp=fopen(filename,"r")) == NULL){
printf("File %s is not exist! Create now!\n",filename);
if((fp=fopen(filename,"w")) == NULL){
printf("Can not create file %s\n",filename);
return -1;
}
fprintf(fp,"\n");
fclose(fp);
fp = fopen(filename,"r");
}
memset(edit.info,0,sizeof(edit.info));
for(i=0;i<MAXROW;i++){
fgets(edit.info[i],MAXCOL + 1,fp);
if(feof(fp))
break;
j=strlen(edit.info[i])-1;
if(edit.info[i][j] == '\n')
edit.info[i][j] = '\0';
}
edit.allrec = i;
edit.scry = 0;
edit.shinex = 1;
edit.shiney = 1;
fclose(fp);
return 0;
}
int editfun(char *filename)
{
int editflag=0;
int nr;
int i;
int in_ch;
int cmdtimes;
int rolllines;
int len;
char command[81];
char queryinfo[81];
refresh_scr();
memset(queryinfo,0,sizeof(queryinfo));
while(1){
if(editflag == 1 && edit.allrec == 0)
edit.allrec = 1;
if(edit.shiney + edit.scry>edit.allrec)
edit.shiney = edit.allrec - edit.scry;
nr = edit.shiney - 1 + edit.scry;
len = strlen(edit.info[nr]);
if(edit.shinex>=len + 1)
edit.shinex = (editflag == 1||len == 0)?len+1:len;
gotoxy(edit.shinex,edit.shiney);
in_ch = getch();
if(editflag){
switch(in_ch){
case ESC:
editflag = 0;
if(edit.shinex>1)
edit.shinex--;
break;
case BackSpace:
if(edit.shinex>1){
for(i = edit.shinex-1;i<MAXCOL-1;i++)
edit.info[nr][i] = edit.info[nr][i+1];
if(edit.shinex>1)
edit.shinex--;
display(edit.shiney,edit.info[nr]);
}
break;
case CR:
append();
break;
default:
for(i = MAXCOL - 1;i>=edit.shinex;i--)
edit.info[nr][i] = edit.info[nr][i-1];
edit.info[nr][i] = (unsigned char)in_ch;
if(edit.shinex<MAXCOL)
edit.shinex++;
display(edit.shiney,edit.info[nr]);
break;
}
}
else{
memset(command,0,sizeof(command));
cmdtimes = 1;
if(in_ch>='1' && in_ch<='9'){
for(i=0;in_ch>='0' && in_ch<='9';i++){
command[i] = (char)in_ch;
in_ch = getch();
}
cmdtimes = atoi(command);
if(cmdtimes<=0||cmdtimes>=MAXCMDTIMES){
beep();
continue;
}
}
if(in_ch == 0)
in_ch = getch();
switch(in_ch){
case 'i':
editflag = 1;
break;
case 'a':
edit.shinex++;
editflag = 1;
break;
case 'A':
edit.shinex = len + 1;
editflag = 1;
break;
case 'o':
append();
editflag = 1;
break;
case ':':
gotoxy(1,25);
clreol();
printf(":");
if(input_str(2,25,command,sizeof(command)-1) == 0){
if(strcmp(command,"x") == 0){
write_file(filename);
return 0;
}
if(strcmp(command,"q") == 0)
return 1;
if(strcmp(command,"w") == 0)
write_file(filename);
}
break;
case '/':
gotoxy(1,25);
clreol();
clreol();
printf("/");
if(input_str(2,25,command,sizeof(command)-1) == 0)
strcpy(queryinfo,command);
else
break;
case 'n':
if(search(queryinfo))
beep();
break;
case 'x':
if(len){
if(cmdtimes>len-edit.shinex+1)
cmdtimes = len - edit.shinex + 1;
for(i=0;i<len-edit.shinex+1-cmdtimes;i++)
edit.info[nr][edit.shinex+i-1]=
edit.info[nr][edit.shinex+i+cmdtimes-1];
memset(edit.info[nr] + len - cmdtimes,0,cmdtimes);
display(edit.shiney,edit.info[nr]);
}
else
beep();
break;
case 'd':
in_ch = getch();
if(in_ch == 'd'&&edit.allrec>0){
if(cmdtimes>=edit.allrec - edit.shiney - edit.scry + 1)
cmdtimes = edit.allrec - edit.shiney - edit.scry + 1;
for(i = edit.scry + edit.shiney - 1;i<edit.allrec - cmdtimes;i++)
memcpy(edit.info[i],edit.info[i+cmdtimes],MAXCOL+1);
memset(edit.info[i],0,(MAXCOL+1) * cmdtimes);
if(edit.shiney>1)
edit.shiney--;
if(edit.scry>0)
edit.scry--;
edit.allrec -= cmdtimes;
refresh_scr();
}
else
beep();
break;
case '0':
edit.shinex = 1;
break;
case '$':
edit.shinex = 1;
break;
case LEFT:
case 'h':
edit.shinex -= cmdtimes;
if(edit.shinex<1)
edit.shinex = 1;
break;
case RIGHT:
case 'l':
edit.shinex += cmdtimes;
if(edit.shinex>len)
edit.shinex = len;
break;
case DOWN:
case 'j':
rolllines = MAX(0,edit.shiney + cmdtimes - 24);
if(rolllines>edit.allrec - 24 - edit.scry)
rolllines = MAX(0,edit.allrec - 24 - edit.scry);
if(rolllines){
edit.scry += rolllines;
refresh_scr();
}
edit.shinex = MIN(edit.shiney + cmdtimes,24);
break;
case UP:
case 'k':
rolllines = MAX(0,cmdtimes - edit.shiney + 1);
if(rolllines>edit.scry)
rolllines = edit.scry;
if(rolllines){
edit.scry -= rolllines;
refresh_scr();
}
edit.shiney = MAX(edit.shiney - cmdtimes,1);
break;
case PGDN:
case CTRL_F:
rolllines = MIN(24 * cmdtimes,edit.allrec - 24 - edit.scry);
if(rolllines>0){
edit.scry += rolllines;
edit.shiney = 1;
refresh_scr();
}
break;
case PGUP:
case CTRL_B:
rolllines = MIN(24 * cmdtimes,edit.scry);
if(rolllines>0){
edit.scry -= rolllines;
edit.shiney = 1;
refresh_scr();
}
break;
default:
beep();
break;
}
}
}
}
void beep()
{
sound(800);
delay(100);
nosound();
}
int write_file(char *filename)
{
int i;
FILE *fp;
if((fp = fopen(filename,"w")) == NULL){
printf("Can not create file %s\n",filename);
return -1;
}
for(i=0;i<edit.allrec;i++)
fprintf(fp,"%s\n",edit.info[i]);
fclose(fp);
return 0;
}
void refresh_scr()
{
int i;
for(i=0;i<24;i++){
gotoxy(1,i+1);
clreol();
if(edit.scry + i<edit.allrec)
printf("%s\n",edit.info[edit.scry+i]);
}
gotoxy(edit.shinex,edit.shiney);
}
int input_str(int x,int y,char *str,int len)
{
int i;
int in_ch;
memset(str,0,len);
for(i=0;i<len-1&&(in_ch=getch())!=CR;){
gotoxy(x,y);
if(in_ch == BackSpace){
if(i == 0)
return -1;
str[i] = '\0';
i--;
x--;
}else{
str[i] = (char)in_ch;
printf(str+1);
fflush(stdout);
i++;
x++;
}
}
return 0;
}
int display(int y,char *str)
{
clreol();
gotoxy(1,y);
printf(str);
fflush(stdout);
return 0;
}
int append()
{
int i;
int nr;
nr = edit.shiney - 1 + edit.scry;
if(edit.allrec >= MAXROW){
display(25,"Too many lines");
}
for(i = edit.allrec;i>nr+1;i--)
memcpy(edit.info[i],edit.info[i-1],MAXCOL+1);
memset(edit.info[nr+1],0,MAXCOL+1);
edit.allrec++;
if(edit.shiney>=24){
edit.shiney = 24;
edit.scry++;
}else
edit.shiney++;
refresh_scr();
return 0;
}
int search(char *str)
{
int i,j,len,cmplen;
len = strlen(str);
if(len == 0)
return -1;
j = edit.shinex;
for(i = edit.scry + edit.shiney - 1;i<edit.allrec;i++){
cmplen = strlen(edit.info[i]) - len;
for(;j<=cmplen;j++)
if(strncmp(edit.info[i] + j,str,len) == 0)
break;
j = 0;
}
if(i == edit.allrec)
return -1;
if(i>=edit.scry&&i<edit.scry + 24){
edit.shinex = j + 1;
edit.shiney = i + 1 - edit.scry;
gotoxy(edit.shinex,edit.shiney);
}else{
edit.shinex = j + 1;
edit.shiney = 1;
edit.scry = i;
refresh_scr();
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/60198.html
標籤:C語言
上一篇:求教!!求各位大佬幫我看看這段代碼,就Order_Call的最后一個形參好像參考錯誤了,不知道該怎么改(這個是模擬爐石傳說的實踐代碼)
下一篇:c語言請教大佬
