设计一个简单的文本编辑器,用C语言实现 20

 我来答
建筑信息通
2007-10-08 · TA获得超过1733个赞
知道小有建树答主
回答量:277
采纳率:100%
帮助的人:84.6万
展开全部
#include <conio.h>
#include <sing.h>
#include "keyscode.h"
#include "menu.h"

suct line{
int charnum;
char *base;
};

typedef suct line SequListElemType;

#include "sequlist.h"

#define MAXLINES 1000

int key;
int cureditline=0;
int curscrtopline=0;
int cureditcol=0;
int funckey;
int editwinx=2;
int editwiny=3;
int editwinlines=20;
int mainover=0;
FILE *curfile;

char filename[255]="test.txt";

SequList *Document;

char ascii(int k);
prinoc(int startfileline,int startscrline);
readdoc(FILE *fp);

editline(int curline,int maxcolnum)
{
char s[2000];
setscrcolor(BLUE,YELLOW);
scpy(s,Document->elem[curline].base);
getsing(editwinx,editwiny curline-curscrtopline,s,maxcolnum);
free(Document->elem[curline].base);
Document->elem[curline].base=(char *)malloc(slen(s) 1);
scpy(Document->elem[curline].base,s);
}

prinoc(int startfileline,int startscrline)
{
int i;
int j;
cursor_hide();
i=startscrline;
j=startfileline;
setscrcolor(BLUE,YELLOW);
gotoxy(editwinx,editwiny startscrline);
while(i<=editwinlines && i<Document->length)
{
clearscrline(editwinx,editwiny i,78);
putsxy(editwinx,editwiny i ,Document->elem[j -1].base);
}
while(i<=editwinlines)
clearscrline(editwinx,editwiny i ,78);
cursor_show();
}

getline(FILE *fp,char *l)
{
int i=0;
while(!feof(fp))
if((l[i ]=fgetc(fp))=='\n')
break;
l[i-1]='\0';
}

closedoc(FILE *fp)
{

int i;
fclose(fp);
for(i=0;i<Document->length;i )
free(Document->elem[i].base);
SequListDesoy(Document);
}

readdoc(FILE *fp)
{
char *newline;
char s[20000];
int linenum=0;
suct line curline;
if(!fp) return;
while(!feof(fp))
{
getline(fp,s);
linenum ;
newline=(char *)malloc(slen(s) 1);
scpy(newline,s);
curline.base=newline;
curline.charnum=slen(newline);
SequListInsert(Document,linenum,curline);
}
}

char ascii(int k)
{
char c;
c=(k<<8)>>8;
if(c>=32&&c<127)
return c;
else
return 0;
}

insertchar(char *s,char c,int pos)
{
int i;
for(i=slen(s) 1;i>pos;i--)
s[i]=s[i-1];
s[i]=c;
}
deletechar(char *s,int pos)
{
int i;
int j;
j=slen(s);
for(i=pos;i<=j;i )
s[i]=s[i 1];
}
clearscrline(int x,int y,int num)
{
int i;
for(i=0;i<num;i )
putcxy(x i,y,'');
}
getsindiag(int x,int y,int w,char *title,char *s)
{
char buf[2000];
gettext(x,y,x w 2,y 2,buf);
setscrcolor(LIGHTGRAY,BLACK);
box(x,y,w 2,3);
putsxy(x 3,y,title);
getsing(x 1,y 1,s,w);
puttext(x,y,x w 2,y 2,buf);
}

insertnewline(int linenum)
{
suct line l;
l.base=(char *)malloc(80);
l.charnum=0;
l.base[0]='\0';
SequListInsert(Document,linenum,l);
}
deleteline(int linenum)
{
SequListDelete(Document,linenum);
}

getsing(int x,int y,char *s,int num)
{
int k;
int n;
char c;
int curpos=0;
int thisover=0;
cursor_show();
/* putsxy(x,y,s); */
gotoxy(x,y);
while(!thisover)
{
if(k=bioskey(1))
{ switch(k){
case KEYEsc:
case KEYReturn:
case KEYUp:
case KEYDown:
case KEYAlt_x:
case KEYCl_y:
return;
break;
default:
bioskey(0);
}
}
else
continue;
switch(k){
case KEYHome:
curpos=0;
gotoxy(x curpos,y);
break;
case KEYEnd:
curpos=slen(s);
gotoxy(x curpos,y);
break;
case KEYReturn:
thisover=1;
break;
case KEYLeft:
if(curpos) curpos--;
gotoxy(x curpos,y);
break;
case KEYRight:
if(curpos<num) curpos ;
gotoxy(x curpos,y);
break;
case KEYDelete:
if(curpos<slen(s))
{
deletechar(s,curpos);
cursor_hide();
clearscrline(x,y,num);
putsxy(x,y,s);
gotoxy(x curpos,y);
cursor_show();
}
break;
case KEYBackspace:
if(curpos)
{
deletechar(s,curpos-1);
curpos--;
cursor_hide();
clearscrline(x,y,num);
putsxy(x,y,s);
gotoxy(x curpos,y);
cursor_show();
}
break;
case KEYEsc:
return;
default:
c=ascii(k);
if (c&&slen(s)<num)
{
insertchar(s,c,curpos);
cursor_hide();
clearscrline(x,y,num);
curpos ;
putsxy(x,y,s);
gotoxy(x curp os,y);
cursor_show();
}
break;
}
}
}

dofunc(int itemno)
{
switch(itemno)
{
case 0:
break;
case 1: /*Open file*/
getsindiag(3,4,50,"Open file Name",filename);
if(curfile=fopen(filename,"r "))
{
readdoc(curfile);
prinoc(1,0);
}
break;
case 3:
mainover=1;
break;
case 21:
showmessagew();
default:
break;
}
}

main()
{ int i;
char c;

char *mainmenuitem[10]={"File","Edit ","Help"};
char *submenu1[10]={"New","Open","Save","Exit to system"};
char *submenu2[10]={"Copy","Cut","Paste "};
char *submenu3[20]={"HelpContent","About"};

initmenu(MAINMENU_V,3,&mainmenu,mainmenuitem);
initmenu(SUBMENU_H,4,&submenu[0],submenu1);
initmenu(SUBMENU_H,3,&submenu[1],submenu2);
initmenu(SUBMENU_H,2,&submenu[2],submenu3);
for(i=0;i<3;i )
addsubmenu(&mainmenu,i,&submenu[i]);
definemenupos(3,1,&mainmenu);
cursor_hide();
showmenu(&mainmenu);
showmainw(1,2,80,23);
showmessagew();

SequListInit(&Document,MAXLINES);
insertnewline(1);
setscrcolor(BLUE,YELLOW);
while(!mainover)
{
if(key=bioskey(1))
{if(!ascii(key))
bioskey(0);
}
else
continue;
switch(key){
case KEYBackspace:
case KEYDelete:
case KEYHome:
case KEYEnd:
case KEYReturn:
if(cureditline-curscrtopline<editwinlines)
cureditline ;
else
{
curscrtopline ;
prinoc(curscrtopline 1,editwiny);
}
insertnewline(cureditline 1);
prinoc(curscrtopline 1,0);
gotoxy(editwinx,editwiny cureditline);
editline(cureditline,78);
break;
case KEYUp:
if (cureditline>0)
{
cureditline--;
gotoxy(editwinx,editwiny cureditline);
editline(cureditline,78);
}
break;
case KEYDown:
if (cureditline<Document->length-1)
{
cureditline ;
gotoxy(editwinx,editwiny cureditline);
editline(cureditline,78);
}
break;
case KEYCl_y:
deleteline(cureditline 1);
if(Document->length==0)
insertnewline(cureditline 1);
if(cureditline>=Document->length)
insertnewline(cureditline 1);
prinoc(cureditline 1,cureditline-curscrtopline);
gotoxy(editwinx,editwiny cureditline);
editline(cureditline,78);
break;
case KEYLeft:
case KEYRight:
break;
case KEYCl_s:
break;
case ESC:
cursor_hide();
dofunc(getmenuitem());
break;
case KEYAlt_x:
mainover=1;
break;
default:
gotoxy(editwinx,editwiny cureditline-curscrtopline);
editline(cureditline,78);
break;
}
}
closedoc(curfile);
setscrcolor(BLACK,WHITE);
clrscr();
cursor_show();
}
博思aippt
2024-07-20 广告
博思AIPPT是基于ai制作PPT的智能在线工具,它提供了4种AI制作PPT的方式,包括AI生成大纲、AI直接生成PPT、文本生成PPT、AI提炼文档生成PPT,一站式集成多种AI生成PPT的方式,可满足办公用户的不同需求和使用场景。ai生... 点击进入详情页
本回答由博思aippt提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式