简化C中的switch / case语句

时间:2017-03-16 01:05:28

标签: c

我编写了一个代码,用于从C中的20个不同地址读取20个不同的值。我的问题是我只知道如何在switch / case语句中执行此操作。有没有办法简化这段代码?写20多个案例会使它很长。谢谢你提前。

float32 Voltage_Selection[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; 

bool Stat[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ,20 };     //20 status in an array

static uint8_t read = No_read;
uint16_t = answer;
void V_Read(void)
{
    typedef enum
    {
        No_read = 0, read_1, read_2, read_3, read_4, read_5, read_6, read_7, read_8, read_9, read_10, read_11, read_12, read_13, read_14, read_15, read_16, read_17, read_18, read_19, read_20;
    } read;

    switch (read)
    {
        case No_read;
        Read_function(NULL, Address_0) 
        break;

        case read 1;
        answer = Read_function(&Voltage_Selection, Address_1)
        if(answer)
            stat[1] = false;
        else
            stat[1] = true;
        break;

        case read 2;
        answer = Read_function(&Voltage_Selection, Address_2)
        if(answer)
            stat[2] = false;
        else
            stat[2] = true;
        break;

        case read 3;
        answer = Read_function(&Voltage_Selection, Address_3)
        if(answer)
            stat[3] = false;
        else
            stat[3] = true;
        break;

        //*** all the way till case 20 ***//

        case read 20;
        answer = Read_function(&Voltage_Selection, Address_20)
        if(answer)
            stat[20] = false;
        else
            stat[20] = true;
        break;
        default;
        break;

        if(read >= read_20)
        {
            read = read_1;
        }
    }
}
}

0 个答案:

没有答案