我有这个代码解码来自命令的输入字符
if( arg && isdigit( arg[ 0 ] ) {
/* Decode the input char from commands. */
if( cmd->digit_counter == 0 ) memset( cmd->next_chan_buffer, 0, 5 );
cmd->next_chan_buffer[ cmd->digit_counter ] = arg[ 0 ];
cmd->digit_counter++;
cmd->frame_counter = cmd->delay;
/**
* Send an enter command if we type more digits than there are stations.
*/
if( cmd->digit_counter > 0 && (station_get_max_position( cmd->station ) < 10) ) {
commands_handle( cmd, ENTER, 0 );
} else if( cmd->digit_counter > 1 && (station_get_max_position( cmd->station ) < 100) ) {
commands_handle( cmd, ENTER, 0 );
} else if( cmd->digit_counter > 2 ) {
commands_handle( cmd, ENTER, 0 );
}
}
此键盘数字中输入的计数位数和方框中的写入数字,例如
|5|4|2|0|
我想防止第一个计数数字为0(或接受数字仅为1到9)和第二个,第三个......数字接受0到9之间的数字
digits count 1 2 3 4
-------------------------------------------------
| only digits 1 to 9 | 0 to 9 | 0 to 9 | 0 to 9 |
-------------------------------------------------
由于
好的,通过添加功能解决:
int first_digit( int num )
{
while(num >= 10) {
num = ( num - ( num % 10 ) ) / 10;
}
return num;
}
if( arg && isdigit( arg[ 0 ] ) {
/* Decode the input char from commands. */
if( cmd->digit_counter == 0 ) memset( cmd->next_buffer, 0, 5 );
cmd->next_buffer[ cmd->digit_counter ] = arg[ 0 ];
if( first_digit( atoi( cmd->next_chan_buffer ) ) != 0 ) {
cmd->digit_counter++;
cmd->frame_counter = cmd->delay;
}
}
答案 0 :(得分:0)
您可以执行此类检查,而不是使用isdigit()。
if( arg && arg[0] >= '1' && arg[0] <= '9' ) { // only accept '1' through '9'
从'0'到'9'的字符在C中始终是相邻的顺序,因此这项检查可以满足您的需要。