0
我有這樣的代碼,解碼從命令防止值輸入零作爲第一個數字
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 |
-------------------------------------------------
感謝
OK,通過增加功能解決:
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;
}
}