📄 input.c
字号:
/****一些特殊的输入函数****/
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<bios.h>
/****函数get_key(void)用于读取键盘按按钮****/
int get_key(void);
/**本函数专用于输入书号,返回所输入的号码**/
int InputBookNum();
/**本程序专用于输入书价,返回所输入的价格**/
float InputBookPrice();
/**input Yes or No,返回Y或者N**/
char *InputYesorNo();
/**本程序专门用于输入书名,返回书名字符串**/
char* InputBookName();
/*--------------注意:这些输入函数都支持ESC键,
当函数的返回类型为字符串时,按ESC返回NULL,
当函数的返回工为数字时,按ESC返回CANCEL (注:CANCEL宏定义为-1)
---------------*/
/****函数get_key(void)用于读取键盘按按钮(支持鼠标)****/
/*--------bioskey(0)的返回值为16位,如果低8位为非0,则是ASCII码,
如果低8 位为0,则高8位为扩充键盘码---------*/
/*------------------
int get_key(void)
{
union keycode_type key;
key.word=bioskey(0);
return key.byte?key.byte:key.word;
}
---------------------*/
int get_key(void)
{
union keycode_type key;
int button;
/***如果既没有按鼠标也没有按键盘则一直循环**/
do{
/**--查看是否按下鼠标按钮,如果是,等待其释放--**/
button=get_mouse_status(&mx,&my);
if(button)
while(get_mouse_status(&mx,&my));
/**--查看键盘是否有键按下,如果是读其码--**/
key.word=bioskey(1);
if(key.word)
key.word=bioskey(0);
}while(button==0&&key.word==0);
/**----返回鼠标按钮或者键盘码(ASCII码或扩展码)---**/
if(button!=0)
return button;
else
return key.byte?key.byte:key.word;
}
/*******************本函数专用于输入书号********************/
int InputBookNum()
{
int key,count=0,num=0;
char *string;
string=(char*)malloc(7);
get: while((key=get_key())!=ENTER){
switch(key){
case'-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':if(count>=6) continue;
putch(key);
string[count]=key;
count++;
break;
case BACK:cprintf("\b");
putch('\0');
cprintf("\b");
count--;
if(count<=0) count=0;
break;
case ESC: return(CANCEL);
case '\0':
case RIGHT:
case LEFT:
case DOWN:
case PGUP:
case PGDOWN:
case HOME:
case END:
case UP:break;
default:continue;
}
}
string[count]='\0';
if(string[0]=='\0') goto get;
sscanf(string,"%d",&num);
free(string);
return (num);
}
/*********************本程序专用于输入书价*************************/
float InputBookPrice()
{
int key,count=0;
float bookprice=0;
char *bookpricestr;
bookpricestr=(char*)malloc(6);
gotoxy(3,7);
get: while((key=get_key())!=ENTER)
{
switch(key)
{
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':if(count>=5) break;
putch(key);
bookpricestr[count]=key;
count++;
break;
case BACK:if(count==0) break;
cprintf("\b");
putch('\0');
cprintf("\b");
count--;
if(count<=0) count=0;
break;
case ESC:return(CANCEL);
case '\0':
case RIGHT:
case LEFT:
case DOWN:
case PGUP:
case PGDOWN:
case HOME:
case END:
case UP:break;
default:break;
}
}
bookpricestr[count]='\0';
if(bookpricestr[0]=='\0') goto get;
bookprice=atof(bookpricestr);
free(bookpricestr);
return(bookprice);
}
/*******************input Yes or No******************/
char *InputYesorNo()
{
char* yesorno;
int key,count=0;
yesorno=(char*)malloc(2);
get: while((key=get_key())!=ENTER)
{
if(key==SPACE) break;
switch(key)
{
case'y':
case'Y':if(count>=1) break;
putch('Y');
yesorno[count]='Y';
count++;
break;
case'n':
case'N':if(count>=1) break;
putch('N');
yesorno[count]='N';
count++;
break;
case BACK:if(count==0) break;
cprintf("\b");
putch('\0');
cprintf("\b");
count--;
if(count<=0) count=0;
break;
case ESC:return NULL;
case '\0':
case RIGHT:
case LEFT:
case DOWN:
case PGUP:
case PGDOWN:
case HOME:
case END:
case UP:break;
default: break;
}
}
yesorno[count]='\0';
if(yesorno[0]=='\0') goto get;
return(yesorno);
}
/***************************本程序专门用于输入书名************************/
char* InputBookName()
{
int key,count=0;
char *str;
str=(char*)malloc(MAXBOOKNAMELEN);
get: while((key=get_key())!=ENTER)
{
switch(key){
case BACK:
if(count==0) continue; /*当删除完时*/
cprintf("\b");
putch('\0');
cprintf("\b");
count--;
if(count<=0) count=0;
break;
case ESC: return NULL;
/*case SPACE:continue;*/
case '\0':
case RIGHT:
case LEFT:
case DOWN:
case PGUP:
case PGDOWN:
case HOME:
case END:
case UP:break;
default:if(count>MAXBOOKNAMELEN-2) continue; /*当输入太多时*/
putch(key);
str[count]=key;
count++;
}
}
str[count]='\0';
if(str[0]=='\0') goto get;
return(str);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -