最近遇到了一个问题,不知道该如何解决,求助各位了。
     假设有10个char类型的数组,用户有可能选择其中的一个也可能选择几个进行运算(如果只选择了其中一个,那么直接使用该数组的数据;如果选择两个或两个以上,那么选中的各个数组之间按位进行或运算之后取最后的结果),如果用if else或者switch来列举用户的选择的话分支太多了,有没有可能用数组循环或者其他好的办法来解决?C++  

解决方案 »

  1.   

    int res1[10], res2[10], res3[10], ...
    int* res[] = {res1, res2, res3, ...};
    int sel[] = {0, 1, 10};
    int re[10];
    for (int i = 0; i < sizeof(sel) / sizeof(int); ++i)
        if (0 == i)
            copy res[sel[i]] to re
        else
            or every item in re with res[sel[i]] respectly
      

  2.   

    char user_sel(char a[],int sel[],int n)
    {
       char c=0;
       for(i=0;i<n;i++)
       c|=a[sel[i]];
       return c; 
    }
    你看这么处理如何。