有这样:设计一字段编辑器,实现对字段的插入、删除等操作,我设计如下:求各位有何更妙之法,恳求赐教。
#include "conio.h"
#include "bios.h"
#include "mem.h"
#include "string.h"
#include "stdlib.h"
struct field{
int row,col; /*提示信息显示位置*/
char *prompt; /*指向提示信息字串的指针*/
char *ptr;
/*指向要接收输入或被编辑的字符串首位置指针*/
int length; /*编辑字段最大长度*/
char attr1,attr2;
/*提示信息,编辑字段显示属性*/
};
#define FIELDCOUNT 6
#define TEXTCOLOR WHITE+(BLUE<<4) /*提示信息显示属性*/
#define MESSCOLOR WHITE+(RED<<4) /*编辑字段显示属性*/
struct field buff[FIELDCOUNT+1]={
7,30,"SNumber:",NULL,8,MESSCOLOR,TEXTCOLOR,
9,30,"SName:",NULL,8,MESSCOLOR,TEXTCOLOR,
11,30,"Sex:",NULL,2,MESSCOLOR,TEXTCOLOR,
13,30,"Maths:",NULL,6,MESSCOLOR,TEXTCOLOR,
15,30,"Englpossh:",NULL,6,MESSCOLOR,TEXTCOLOR,
17,30,"Computer:",NULL,6,MESSCOLOR,TEXTCOLOR,
0
};
#define BS 8
#define CR 13
#define ESC 27
#define HOME 327
#define END 335
#define UP 328
#define DOWN 336
#define LEFT 331
#define RIGHT 333
#define DEL 339
#define INS 338
#define F2 316
#define CTRL_X 24
#define TRUE 1
#define FALSE 0
int insert=TRUE;
main()
{ void screen(void);
void edit_scr(struct field scr[]);
void disp_screen(struct field scr[]);
void setcolor(int color);
void init(struct field scr[]);
void unload(struct field scr[]);
int edit_string(int row,int col,char *s,char length,int color);
void write_f(int row,int col,char *s,char width,int color);
edit_scr(buff);
}
/****管理多字段编辑界面******/
void edit_scr(struct field scr[])
{ int pos=0;
int ret_code;
screen();
setcolor(WHITE+(BLACK<<4));
init(scr);
changeinsmode(insert);
disp_screen(scr);
for(pos=0;ret_code!=CTRL_X;)
{gotoxy(1,1);
switch(pos)
{case 0:printf("please input your name :");break;
case 1:printf("please input your num :");break;
case 2:printf("please input your sex :");break;
case 3:printf("please input your score:");break;
case 4:printf("please input your score:");break;
case 5:printf("please input your score:");break;
}
ret_code=edit_string(scr[pos].row,scr[pos].col+11,scr[pos].ptr,scr[pos].length,scr[pos].attr2);
switch(ret_code) {
case UP :if(pos) pos--;
else pos=FIELDCOUNT-1; break;
case DOWN:
case CR :if(pos==FIELDCOUNT-1)pos=0;
else pos++;
break;
}
}
unload(scr);
}
/***********************************
*在指定位置处显示结构体数组元素内容*
***********************************/
void disp_screen(struct field scr[])
{ int pos;
for(pos=0;pos<FIELDCOUNT+1;pos++)
{ gotoxy(scr[pos].col,buff[pos].row);
setcolor(scr[pos].attr1);
cprintf(scr[pos].prompt);
}
for(pos=0;pos<FIELDCOUNT+1;pos++)
{ window(scr[pos].col+11,scr[pos].row,scr[pos].col+11+8,scr[pos].row);
textbackground(BLUE);
clrscr();
}
window(1,1,80,25);
}
/*****************
****字段的编辑****
******************/
int edit_string(int row,int col,char *s,char length,int color)
{char len=strlen(s),pos=0;
int key,err;
char *pstr;
pstr=(char *)malloc(length+1);
strcpy(pstr,s);
do{
write_f(row,col,s,length,color);
gotoxy(col+pos,row);
key=getkey();
switch(key){
case HOME :pos=0; break;
case END :pos=len; break;
case INS :insert=!insert;changeinsmode(insert);break;
case LEFT :if(pos) pos--;break;
case RIGHT:if(pos<len) pos++;break;
case DEL :if(pos<len) {memmove(&s[pos],&s[pos+1],len-pos);
len--;}
break;
case BS :if(pos) {memmove(&s[pos-1],&s[pos],len-pos+1);
pos--; len--; }
break;
case ESC
:strcpy(s,pstr);write_f(row,col,s,length,color);pos=0;len=strlen(s);break;
case F2 :
case CR :
case CTRL_X:
case UP :
case DOWN :free(pstr);return key;
default :if((key>' ')&&(key<'~'))
{ err=FALSE;
if(insert)
/*插入模式*/
if(len<length){memmove(&s[pos+1],&s[pos],len-pos+1),
len++;}
else err=TRUE;
else if(pos>=len) {if(len<length) len++;
/*覆盖模式*/
else err=TRUE;}
if(err) printf("%c",7);
else s[pos++]=key;
}
}
}while(1);
}
/***********************************************
**指定位置以指定属性显示字符串s,最大长度为width**
************************************************/
void write_f(int row,int col,char *s,char width,int color)
{char *str;
int len;
str=(char *)malloc(width+1);
strcpy(str,s);
if((len=strlen(str))<width) setmem(&str[len],width-len,' ');
str[width]=0;
setcolor(color);
gotoxy(col,row);
cprintf(str);
free(str);
}
/***********************************
******设置文本的显示属性**********
************************************/
void setcolor(int color)
{ textattr(color); }
/*****初始化数组******/
void init(struct field scr[])
{ int pos;
for(pos=0;pos<FIELDCOUNT+1;pos++)
scr[pos].ptr=(char*)calloc(2*(scr[pos].length+1),1);
}
/******释放结构体动态空间******/
void unload(struct field scr[])
{ int pos;
for(pos=0;pos<FIELDCOUNT+1;pos++) free(scr[pos].ptr);
}
/******显示编辑状态*****/
int changeinsmode(int mode)
{ gotoxy(75,24);
if(mode) printf("INSER");
else printf("COVER");
}
/******键盘接口********/
int getkey(void)
{ int key,hi,lo;
key=bioskey(0);
lo=key&0x00ff;
hi=(key&0xff00)>>8;
return (lo==0?(hi+256):lo);
}
void screen(void){
int key;
window(1,1,80,24);
textbackground(2);
clrscr();
gotoxy(34,13);
textcolor(BLACK);
cprintf("2005--2006\n\b\b\b\b\b\b\b\b\b\b\b\bstudents' score!");
window(1,1,80,25);
gotoxy(50,25);
setcolor(WHITE+(BLUE<<4));
cprintf("PRESS 'ENTER' TO ENTER.....");
do{
key=getkey();
}while(key!=CR);
textbackground(BLACK);
clrscr();
textbackground(YELLOW);
clreol();
}
----------------解决方案--------------------------------------------------------