#include<stdio.h>
#include<windows.h>
#include<conio.h>
int key_to=1;
void set_key_pos(void *handle,int x,int y){
COORD pos={y,x};
SetConsoleCursorPosition(handle,pos);
}
int num1,num2;
int main(){
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO curinfo;
curinfo.dwSize=1;curinfo.bVisible=FALSE;
SetConsoleCursorInfo(handle,&curinfo);
SetConsoleTextAttribute(handle,0x07);
set_key_pos(handle,0,0);
printf("KEY_TO = 1");
set_key_pos(handle,1,0);
printf("KEY_PRESSED = ");
SetConsoleTextAttribute(handle,0x07);
set_key_pos(handle,5,10);printf("A");
set_key_pos(handle,7,10);printf("B");
set_key_pos(handle,9,10);printf("A+B");
SetConsoleTextAttribute(handle,0xf0);
set_key_pos(handle,5,20);printf(" 0");
set_key_pos(handle,7,20);printf(" 0");
set_key_pos(handle,9,20);printf(" 0");
SetConsoleTextAttribute(handle,0x07);
set_key_pos(handle,5,30);printf("<-");
for(;;){
char c=_getch();
set_key_pos(handle,1,14);
SetConsoleTextAttribute(handle,0x07);
printf("%d ",c);
if(c==72){ //向上键
key_to=1;
}else if(c==80){ //向下键
key_to=2;
}else if(c>='0' && c<='9'){ //数字键
if(key_to==1){
num1=num1*10+c-'0';
}else if(key_to==2){
num2=num2*10+c-'0';
}
}else if(c==8){ //Backspace
if(key_to==1){
num1/=10;
}else{
num2/=10;
}
}else{
//什么也不处理
}
SetConsoleTextAttribute(handle,0x07);
set_key_pos(handle,0,9);
printf("%d ",key_to);
if(key_to==1){
SetConsoleTextAttribute(handle,0x07);
set_key_pos(handle,5,30);printf("<-");
set_key_pos(handle,7,30);printf(" ");
}
if(key_to==2){
SetConsoleTextAttribute(handle,0x07);
set_key_pos(handle,7,30);printf("<-");
set_key_pos(handle,5,30);printf(" ");
}
SetConsoleTextAttribute(handle,0xf0);
set_key_pos(handle,5,20);printf("%10d",num1);
set_key_pos(handle,7,20);printf("%10d",num2);
set_key_pos(handle,9,20);printf("%10d",num1+num2);
}
return 0;
}