用于双字符串比较的C case语句

时间:2016-03-13 16:40:40

标签: c if-statement strcmp

我觉得必须有一个更好的方法来做到这一点,而不是我尝试的极长的方式。我有两个令牌(token3和token4),它们可以是字符串R1到R6。我也有相同名称的int变量,R1到R6。我基本上希望case语句是

(variable of name token3) += (variable of name token4);

我实现这个的方式看起来很荒谬,而且过于重复,但我不知道另一种方式。必须有一些方法来简化这个吗?

        // ADD COMMAND
        else if (!strcmp(token2_instr, "ADD")) {
            printf("%s\n", token2_instr);
            token3_arg1 = strtok_r(NULL, ", ", &line_save);
            token4_arg2 = strtok_r(NULL, " \n", &line_save);
            int num = atoi(token4_arg2);

            // ADD Reg, Reg
            if (num == 0) {             
                if (!strcmp(token3_arg1, "R1")) {
                    if (!strcmp(token4_arg2, "R1"))
                        R1 += R1;
                    else if(!strcmp(token4_arg2, "R2"))
                        R1 += R2;
                    else if(!strcmp(token4_arg2, "R3"))
                        R1 += R3;
                    else if(!strcmp(token4_arg2, "R4"))
                        R1 += R4;
                    else if(!strcmp(token4_arg2, "R5"))
                        R1 += R5;
                    else if(!strcmp(token4_arg2, "R6"))
                        R1 += R6;   
                }
                else if(!strcmp(token3_arg1, "R2")) {
                    if (!strcmp(token4_arg2, "R1"))
                        R2 += R1;
                    else if(!strcmp(token4_arg2, "R2"))
                        R2 += R2;
                    else if(!strcmp(token4_arg2, "R3"))
                        R2 += R3;
                    else if(!strcmp(token4_arg2, "R4"))
                        R2 += R4;
                    else if(!strcmp(token4_arg2, "R5"))
                        R2 += R5;
                    else if(!strcmp(token4_arg2, "R6"))
                        R2 += R6;   
                }
                else if(!strcmp(token3_arg1, "R3")) {
                    if (!strcmp(token4_arg2, "R1"))
                        R3 += R1;
                    else if(!strcmp(token4_arg2, "R2"))
                        R3 += R2;
                    else if(!strcmp(token4_arg2, "R3"))
                        R3 += R3;
                    else if(!strcmp(token4_arg2, "R4"))
                        R3 += R4;
                    else if(!strcmp(token4_arg2, "R5"))
                        R3 += R5;
                    else if(!strcmp(token4_arg2, "R6"))
                        R3 += R6;   
                }
                else if(!strcmp(token3_arg1, "R4")) {
                    if (!strcmp(token4_arg2, "R1"))
                        R4 += R1;
                    else if(!strcmp(token4_arg2, "R2"))
                        R4 += R2;
                    else if(!strcmp(token4_arg2, "R3"))
                        R4 += R3;
                    else if(!strcmp(token4_arg2, "R4"))
                        R4 += R4;
                    else if(!strcmp(token4_arg2, "R5"))
                        R4 += R5;
                    else if(!strcmp(token4_arg2, "R6"))
                        R4 += R6;   
                }
                else if(!strcmp(token3_arg1, "R5")) {
                    if (!strcmp(token4_arg2, "R1"))
                        R5 += R1;
                    else if(!strcmp(token4_arg2, "R2"))
                        R5 += R2;
                    else if(!strcmp(token4_arg2, "R3"))
                        R5 += R3;
                    else if(!strcmp(token4_arg2, "R4"))
                        R5 += R4;
                    else if(!strcmp(token4_arg2, "R5"))
                        R5 += R5;
                    else if(!strcmp(token4_arg2, "R6"))
                        R5 += R6;   
                }
                else if(!strcmp(token3_arg1, "R6")) {
                    if (!strcmp(token4_arg2, "R1"))
                        R6 += R1;
                    else if(!strcmp(token4_arg2, "R2"))
                        R6 += R2;
                    else if(!strcmp(token4_arg2, "R3"))
                        R6 += R3;
                    else if(!strcmp(token4_arg2, "R4"))
                        R6 += R4;
                    else if(!strcmp(token4_arg2, "R5"))
                        R6 += R5;
                    else if(!strcmp(token4_arg2, "R6"))
                        R6 += R6;   
                }
            }
            // ADD Reg, Immediate
            else {
                if (!strcmp(token3_arg1, "R1"))
                    R1 += num;
                else if(!strcmp(token3_arg1, "R2"))
                    R2 += num;
                else if(!strcmp(token3_arg1, "R3"))
                    R3 += num;
                else if(!strcmp(token3_arg1, "R4"))
                    R4 += num;
                else if(!strcmp(token3_arg1, "R5"))
                    R5 += num;
                else if(!strcmp(token3_arg1, "R6"))
                    R6 += num;      
            }

        }

2 个答案:

答案 0 :(得分:2)

通过引入正确的数据结构,可以以优雅的方式解决此问题。保罗格里菲斯在评论中提到了这一点。

首先,让我们设置一个数据结构:

const int numberOfVariables = 6;

int R[numberOfVariables];
for (int i=0; i<numberOfVariables; i++) {
    R[i] = 0;
}

char* tokenNames[] = {"R1", "R2", "R3", "R4", "R5", "R6"};

numberOfVariablestokenNames是您可以更改行为的点,如果您有更多或更少的名称或其他变量名称。所以基本上,我们不是有六个变量,而是有一个数组用于名称,一个数组用于它们的值。

然后:

// Assumptions for this snippet.
char token3_arg1[] = "R3";
char token4_arg2[] = "R4";

// Find the index of the name of token 3.
int indexToken3 = 0;
for (; indexToken3<numberOfVariables; indexToken3++) {
    // (debug) printf("check: %s", tokenNames[indexToken3]);
    if (!strcmp(token3_arg1, tokenNames[indexToken3])) {
        break;
    }
}

// Find the index of the name of token 4.
int indexToken4 = 0;
for (; indexToken4<numberOfVariables; indexToken4++) {
    // (debug) printf("check: %s", tokenNames[indexToken4]);
    if (!strcmp(token4_arg2, tokenNames[indexToken4])) {
        break;
    }
}

Paul Griffiths通过将变量名中的数字转换为数字来查找索引。在这里,我更喜欢灵活的方式 - 想象你有R33

最后有两个指数:

R[indexToken3] += R[indexToken4];

背后的诀窍是每个Rn的计数器与其名称的索引相同。

如果项目变大,我强烈建议编写一个函数来查找索引,甚至使用完成的库函数。

答案 1 :(得分:1)

这是一个丑陋的黑客,但至少它避免了重复和strcmp()链:

static inline int *regname2ptr( char *regname, int *p1, int *p2, int *p3, int *p4, int *p5, int *p6)
{
if (*regname, || *regname != 'R') return NULL; // let it go boom
switch(reg[1] - '0' ) {
case 1: return p1;
case 2: return p2;
case 3: return p3;
case 4: return p4;
case 5: return p5;
case 6: return p6;
default: return NULL; // let it go boom encore
}

the_func()
{
int r1,r2,r3,r4,r5,r6;
int *src, *dst;

dst = regname2ptr(token4_arg1,  &r1,&r2,&r3,&r4,&r5,&r6);
src = regname2ptr(token4_arg2,  &r1,&r2,&r3,&r4,&r5,&r6);

*dst += *src;
}