Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中switch/case语句的简化_C - Fatal编程技术网

C语言中switch/case语句的简化

C语言中switch/case语句的简化,c,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, 1

我编写了一个代码,从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;
        }
    }
}
}

请不要标记
C#
C++
,因为您的代码不是,它是普通的
C
使用相应的数组作为地址。然后它折叠成一行:
Read\u函数(&Voltage\u Selection,address\u array[Read])
My Allower Cory,我第一次使用stackoverflow。凯伦,你能告诉我怎么做吗。我不确定要拆下哪些部件。。谢谢..为什么你的
bool
array
bool Stat[20]
是用非bool-ish整数值初始化的?目的是什么?我告诉过你要移除什么。拆下整个
开关
块,并替换为这一行代码。将地址数组声明为:
type address\u array[20]={address\u 0,address\u 1