0

我目前正在开发一个项目,该项目使用键盘数字作为传感器 (f_temp) 的保存位置并将其保存到浮点数组 sensorData[] 中,u8_key 是识别按键的变量。如何在不使用多个 else 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 *");
      } 
4

3 回答 3

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 语句。

于 2018-08-03T19:40:05.807 回答
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);
}
于 2018-08-03T19:53:00.253 回答
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);
}
于 2018-08-03T20:01:48.520 回答