简体   繁体   English

精简多个elseif语句

[英]streamlining multiple elseif statements

I am currently working on a project that uses keypad numbers as save location for a sensor (f_temp) and saves it into float array sensorData[], u8_key is the variable that recognizes the key-press. 我目前正在一个项目中,该项目使用小键盘数字作为传感器的保存位置(f_temp),并将其保存到浮点数组sensorData []中,u8_key是可识别按键的变量。 How can i improve this without using multiple else if statements? 如何在不使用其他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 *");
      } 

the simplest fix is to use a switch...case statement such as 最简单的解决方法是使用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;
    ...

and so on. 等等。 Notice you need that break statement to ensure that only one case is hit at a time. 请注意,您需要该break语句来确保一次仅命中一个案例。

EDIT: Note that Christian Gibbons' solution is more efficient and elegant, and you should use that. 编辑:请注意,Christian Gibbons的解决方案更有效,更优雅,您应该使用它。 The switch statement should only be used if you need to do different things with each case. 只有在每种情况下需要执行不同的操作时,才应使用switch语句。

The only special case that requires a conditional is '*' . 需要条件的唯一特殊情况是'*' As Lee Daniel Crocker pointed out, the decimal characters are represented sequentially, so you can reliably get the decimal represented by a character with simple math. 正如Lee Daniel Crocker指出的那样,十进制字符是顺序表示的,因此您可以通过简单的数学可靠地获得由字符表示的十进制。 '1' - '0' = 1 , and so on. '1' - '0' = 1 ,依此类推。 The only special cases will be '0' , which is not filling the same index in your array sequentially as its value would suggest, and '*' , which is obviously not a digit. 唯一的特殊情况是'0''*' ,它不会像其值所建议的那样顺序填充数组中的同一索引,而这显然不是数字。 For '0' , that can be solved with modulo arithmetic. 对于'0' ,可以用模算法解决。 So with all that, here is a sample piece of code should perform the same as your function: 因此,下面是示例代码,其功能应与您的函数相同:

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);
}

My solution, created by observing all the cases and finding similarities between them: 我的解决方案是通过观察所有案例并找到它们之间的相似性而创建的:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM