samedi 25 juin 2016

#define C++ keyword 'case' in switch case statements?


I am preparing for a coding competition where you get bonus points if your code is shorter. I was wondering if one could '#define' the 'case' keyword from switch case statements so that if I have to use switch case statements in the competition, I don't have to type 'case' keyword again and again in the switch statement.

Example Code Snippet :

             switch(input_string[0])
        {
            case '+':p = (a + b);t;
            case '-':p = (a - b);t;
            case '*':p = (a * b);t;
            case '/':p = (a / b);t;
            case '%':p = (a % b);t;
            case '^':p = pow(a,b);t;
        }

a,b and p are integers. When the character input_string[0] is '+', a and b are added and the sum is assigned to p.

IMPORTANT: The 't' in the above code snippet is the break statement. I have used '#define t break;' so that I don't have to write the break keyword multiple times. I was wondering if one could do the same with the 'case' keyword.

Could I define case as '#define c case' and use 'c' wherever I need to use 'case' in the switch case statements so that the resulting code snippet looks like this:

        switch(e[i])
        {
            c '+':p = (a + b);t;
            c '-':p = (a - b);t;
            c '*':p = (a * b);t;
            c '/':p = (a / b);t;
            c '%':p = (a % b);t;
            c '^':p = pow(a,b);t;
        }

I tried doing '#define c case' but I get the following error - "error: expected 'case' keyword before expression"


Aucun commentaire:

Enregistrer un commentaire