精简多个elseif语句

时间:2018-08-03 19:01:50

标签: c

我目前正在一个项目中,该项目使用小键盘数字作为传感器的保存位置(f_temp),并将其保存到浮点数组sensorData []中,u8_key是可识别按键的变量。 我该如何在不使用多个if语句的情况下改善这一点?

void saveValue(void){
      if (u8_key == '1'){
            writeLCD(0x01,0,0,1); // clears LCD
            sensorData[0] = f_temp;
            outStringLCD("Saved to 1"); // writes to LCD for visual confirmation of value being saved and to what location on LCD
          }

         else if (u8_key == '2'){
            writeLCD(0x01,0,0,1);
            sensorData[1] = f_temp;
            outStringLCD("Saved to 2");
         }

        else if (u8_key == '3'){
            writeLCD(0x01,0,0,1);
            sensorData[2] = f_temp;
            outStringLCD("Saved to 3");
      }         
              else if (u8_key == '4'){
            writeLCD(0x01,0,0,1);
            sensorData[3] = f_temp;
            outStringLCD("Saved to 4");
      } 
        else if (u8_key == '5'){
            writeLCD(0x01,0,0,1);
            sensorData[4] = f_temp;
            outStringLCD("Saved to 5");
      } 
        else if (u8_key == '6'){
            writeLCD(0x01,0,0,1);
            sensorData[5] = f_temp;
            outStringLCD("Saved to 6");
      } 
        else if (u8_key == '7'){
            writeLCD(0x01,0,0,1);
            sensorData[6] = f_temp;
            outStringLCD("Saved to 7");

      } 
        else if (u8_key == '8'){
            writeLCD(0x01,0,0,1);
            sensorData[7] = f_temp;
            outStringLCD("Saved to 8");

      } 
        else if (u8_key == '9'){
            writeLCD(0x01,0,0,1);
            sensorData[8] = f_temp;
            outStringLCD("Saved to 9");

      } 
        else if (u8_key == '0'){
            writeLCD(0x01,0,0,1);
            sensorData[9] = f_temp;
            outStringLCD("Saved to 0");

      } 
        else if (u8_key == '*'){
            writeLCD(0x01,0,0,1);
            sensorData[10] = f_temp;
            outStringLCD("Saved to *");
      } 

3 个答案:

答案 0 :(得分:2)

最简单的解决方法是使用switch ... case语句,例如

switch(u8_key)
{
    case '1':
        writeLCD(0x01,0,0,1); // clears LCD
        sensorData[0] = f_temp;
        outStringLCD("Saved to 1");
        break;

    case '2': 
        writeLCD(0x01,0,0,1); // clears LCD
        sensorData[1] = f_temp;
        outStringLCD("Saved to 2");
        break;
    ...

,依此类推。请注意,您需要使用break语句来确保一次只命中一个案例。

编辑: 请注意,Christian Gibbons的解决方案更有效,更优雅,您应该使用它。仅当您需要对每种情况执行不同的操作时,才应使用switch语句。

答案 1 :(得分:2)

唯一需要条件的特殊情况是'*'。正如Lee Daniel Crocker指出的那样,十进制字符是顺序表示的,因此您可以通过简单的数学可靠地获得由字符表示的十进制。 '1' - '0' = 1,依此类推。唯一的特殊情况是'0''*'(显然不是数字),'0'不会按其值的建议顺序填充数组中的同一索引。对于void saveValue(void){ int index = ((u8_key - '0' + 9) %10); if (u8_key == '*') { index = 10; } writeLCD(0x01,0,0,1); sensorData[index] = f_temp; char buff[11] = "Saved to \0\0"; buff[9] = u8_key; outStringLCD(buff); } ,可以使用模运算来解决。因此,下面是一个示例代码,其功能应与您的功能相同:

pprint.pprint(unique_domains)

答案 2 :(得分:2)

我的解决方案是通过观察所有案例并找到它们之间的相似性而创建的:

if (('0' <= u8_key && '9' >= u8_key) || '*' == u8_key) {
       writeLCD(0x01, 0, 0, 1);
       const size_t idx = '*' == u8_key ? 10 : (u8_key - '0' + 9) % 10;
       sensorData[idx] = f_temp;
       const char buf[] = {'S','a','v','e','d',' ','t','o',' ',u8_key,'\0'};
       outStringLCD(buf);
}