主题:[讨论]求高手帮我改下程序·~我是刚学的·~是在做不下去啦~~跪求高手的帮忙
#include "ctype.h"
#include "stdlib.h"
#define MaxCol 81
#define MaxRow 100
struct buf
{
char text[MaxRow][MaxCol];
int col;
int row;
int len[MaxRow];
};
FILE *in;
FILE *out;
typedef struct buf *pbuf;
pbuf newbuf(void);
void freebuf(pbuf bp);
void moveforward(pbuf bp); /* F */
void movebackward(pbuf bp);/* B */
void moveup(pbuf bp);/* U */
void movelow(pbuf bp);/* L */
void movetostart(pbuf bp);/* J */
void movetoend(pbuf bp);/* E */
void movetoleft(pbuf bp);/* A */
void insertchar(pbuf bp,char ch);/* I */
void deletechar(pbuf bp);/* D */
void dispbuf(pbuf bp);
void readfile(pbuf bp);
void writefile(pbuf bp);
static void execcomd(pbuf bp,char *line);
static void helpcomd(void);
char *getline(void);
char line[MaxCol];
void main(void)
{
pbuf bufp;
bufp=newbuf();
while(1)
{
printf(">Generates a help message----H\n");
printf(">\n");
printf(">");
execcomd(bufp,getline());
dispbuf(bufp);
}
freebuf(bufp);
}
static void execcomd(pbuf bp,char *line)
{
int i;
switch(toupper(line[0]))
{
case 'I':
for(i=1;line[i]!='\0';i++)
insertchar(bp,line[i]);
break;
case 'D':
deletechar(bp);
break;
case 'F':
moveforward(bp);
break;
case 'B':
movebackward(bp);
break;
case 'U':
moveup(bp);
break;
case 'L':
movelow(bp);
break;
case 'A':
movetoleft(bp);
break;
case 'J':
movetostart(bp);
break;
case 'E':
movetoend(bp);
break;
case 'H':
helpcomd();
break;
case 'R':
readfile(bp);
break;
case 'W':
writefile(bp);
break;
case 'Q':
exit(0);
default:
printf("illegal command!\n");
break;
}
}
static void helpcomd(void)
{
printf("Use the following command to edit a text file!\n");
printf(" I...\t Insert text in current cursor.\n");
printf(" D\t delete the next character.\n");
printf(" J\t Jump to the beginning of the text.\n");
printf(" E\t Jump to the end of the text.\n");
printf(" F\t Move forward a character.\n");
printf(" B\t Move backward a character.\n");
printf(" U\t Move up a line.\n");
printf(" L\t Move down a line.\n");
printf(" A\t Move to the beginning of current line.\n");
printf(" R...\t read a file from the disk.For example, rc:\\abc.txt\n");
printf(" W...\t write a file to the disk.For example, wc:\\abc.txt\n");
printf(" H\t Generates a help message.\n");
printf(" Q\t Quits the program.\n");
}
char *getline(void)
{
int i=0;
while((line[i]=getchar())!='\n' && i<MaxCol-1)
i++;
line[i]='\0';
return line;
}
pbuf newbuf(void)
{
pbuf bufp;
int i;
bufp=(pbuf)malloc(sizeof(struct buf));
bufp->col=bufp->row=0;
for(i=0;i<MaxRow;i++)
bufp->len[i]=0;
return bufp;
}
void freebuf(pbuf bp)
{
free(bp);
}
void moveforward(pbuf bp)
{
if(bp->col<MaxCol)
bp->col++;
}
void movebackward(pbuf bp)
{
if(bp->col>0)
bp->col--;
}
void moveup(pbuf bp)
{
if(bp->row>0)
bp->row--;
}
void movelow(pbuf bp)
{
if(bp->row<MaxRow)
bp->row++;
}
void movetostart(pbuf bp)
{
bp->row=bp->col=0;
}
void movetoleft(pbuf bp)
{
bp->col=0;
}
void movetoend(pbuf bp)
{
int i=MaxRow-1;
while(i>=0)
{
if(bp->len[i]!=0)
{
bp->row=i;
break;
}
i--;
}
bp->col=bp->len[i];
}
void insertchar(pbuf bp,char ch)
{
int i;
if(bp->len[bp->row]==MaxCol-1)
printf("this row is full, please move to next row!\n");
for(i=bp->len[bp->row];i>bp->col;i--)
bp->text[bp->row][i]=bp->text[bp->row][i-1];
bp->text[bp->row][bp->col]=ch;
bp->len[bp->row]++;
bp->col++;
}
void deletechar(pbuf bp)
{
int i;
if(bp->col < bp->len[bp->row])
{
for(i=bp->col+1;i<bp->len[bp->row];i++)
bp->text[bp->row][i-1]=bp->text[bp->row][i];
bp->len[bp->row]--;
}
}
void dispbuf(pbuf bp)
{
int i,j;
for(j=0;j<=bp->row;j++)
{
if(bp->len[j]>0)
{
for(i=0;i<bp->len[j];i++)
if(bp->text[j][i]!='\n')
printf("%c",bp->text[j][i]);
}
printf("\n");
}
for(i=0;i<bp->col;i++)
printf(" ");
printf("-\n");
}
void readfile(pbuf bp)
{
char *filename=&line[1];
int i;
in=fopen(filename,"r+");
for(bp->row=0;fgets(bp->text[bp->row],MaxCol,in)!=NULL;bp->row++)
{
i=0;
bp->len[bp->row]=0;
while(bp->text[bp->row][i++]!='\0')
bp->len[bp->row]++;
}
fclose(in);
}
void writefile(pbuf bp)
{
char *filename=&line[1];
int i,len;
out=fopen(filename,"w+");
for(i=0;i<MaxRow;i++)
if((len=bp->len[i])!=0)
{
bp->text[i][len]='\n';
bp->text[i][len+1]='\0';
fputs(bp->text[i],out);
}
fclose(out);
}
这个是普通文本编辑器的程序~~ 求高手帮我把光标的控制改成用方向键控制的·~~小弟在这里跪求大家啦。