fork download
  1. #include <stdio.h>
  2.  
  3. #define D1_VAL 1
  4. #define D2_VAL 2
  5. #define D3_VAL 19
  6. #define D4_VAL 4
  7. #define D5_VAL 14
  8.  
  9. #define D_CNT 5
  10. #define D_OFS 0
  11.  
  12. #define O1_VAL 6
  13. #define O2_VAL 12
  14. #define O3_VAL 15
  15. #define O4_VAL 17
  16.  
  17. #define O_CNT 4
  18. #define O_OFS D_CNT
  19.  
  20. #define PIN_NUM_CNT (D_CNT+O_CNT)
  21. #define PIN_GRP_CNT 2
  22.  
  23. enum decode_res_e {
  24. DECODE_OK = 0,
  25. DECODE_ERR = !DECODE_OK
  26. };
  27.  
  28. int const pin_num_tab[PIN_NUM_CNT] = {
  29. D1_VAL, D2_VAL, D3_VAL, D4_VAL, D5_VAL,
  30. O1_VAL, O2_VAL, O3_VAL, O4_VAL,
  31. };
  32.  
  33. typedef struct { unsigned char chr, ofs, cnt; } pin_grp_t;
  34.  
  35. pin_grp_t const pin_grp_tab[2] = {
  36. { .chr = 'd', .ofs = D_OFS, .cnt = D_CNT },
  37. { .chr = 'o', .ofs = O_OFS, .cnt = O_CNT },
  38. };
  39.  
  40. int pin_to_num(int * num, unsigned char * pin)
  41. {
  42. unsigned char i = 0, tmp = pin[1] - 0x31;
  43. while (i < PIN_GRP_CNT) {
  44. if (pin_grp_tab[i].chr == pin[0]) {
  45. if (tmp >= pin_grp_tab[i].cnt) break;
  46. *num = pin_num_tab[pin_grp_tab[i].ofs + tmp];
  47. return DECODE_OK;
  48. }
  49. i++;
  50. }
  51. return DECODE_ERR;
  52. }
  53.  
  54. int main(void) {
  55. int num;
  56. unsigned char const inp[] = "d6";
  57. printf("\f%s is ",inp);
  58. if (pin_to_num(&num,inp) == DECODE_OK) {
  59. printf("%d\r\n",num);
  60. } else {
  61. printf("err\r\n");
  62. }
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
d6 is err