|
发表于 2020-8-26 23:00:01
|
显示全部楼层
[code=C/C++]
INT8U key,lastkey;
INT8U err;
INT32U root,a,b,n;
char str[33];
INT32U sum=0;
int times=0;
for (;;) {
if (PC_GetKey(&key)==TRUE){
if(key==0x1B)PC_DOSReturn();
if(key>47&&key<58&×<11||key==8){
if(times==0){
PC_DispStr(27,5," ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr(26,6," ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
}
if(key!=8){
++times;
PC_DispChar(27+times,5,key,DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
sum=sum*10+key-48;
lastkey=key;
}
if(key==8&×>0){
PC_DispChar(27+times,5,' ',DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
sum=(sum-lastkey+48)/10;
times-=1;
}
}
}
if(key==13){
PC_DispStr(5,6,"Result Of The Number:",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
if(times==0){
PC_DispStr(26,6," ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
PC_DispStr(27,5," ",DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
}
a=3;n=2;
for(;a<=sum;a+=2){
OSSemPend(FuncSem,0,&err);
root=(INT32U)sqrt((double)a);
OSSemPost(FuncSem);
for(b=3;;){
if(a%b==0)break;
b+=2;
if(b>root){++n;break;}
}
}
if(sum==1||sum==2)n-=1;
if(sum==0)n=0;
OSSemPend(FuncSem,0,&err);
ultoa(n, str, 10);
OSSemPost(FuncSem);
PC_DispStr(29,6,str,DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
sum=0;times=0;
}
}
[/code]
取自我大三时写的例子:uC/OS-II环境下的质数计算。不必具体看懂,大意是你输入非数字不做响应,PC_GetKey()该函数直接将按键存入实参。 |
|