简体   繁体   中英

Change the macro part, let two outputs have the same value

#define polyMacro(a, b) (a*a + 8*a + 3*a*b - b*b)

//change this part so it will get same value with polyFunc
//change this part so it will get same value with polyFunc
//change this part so it will get same value with polyFunc

int polyFunc(int a, int b) 
{
    return (a * a + 8 * a + 3 * a * b - b * b);
}

void part2(int x, int y) {
    int x_copy = x, y_copy = y;



    printf(" polyFunc(x, y) = %d \n polyMacro(x, y) = %d \n\n", polyFunc(--x, --y), polyMacro(--x_copy, --y_copy));//please change the macro so polyMacro will have the same value with polyFunc 
int main()
{
    int x = -7, y = 3;//give value

    printf("Part 2:\n\n");//print
    part2(x, y);

    while (1);  // needed to keep console open in VS
    return 0;
}

You can modify your macro part like this :

 #define polyMacro(a, b) ({ typeof(a) a1= a;\
                    typeof(b) b1= b;\
                    a1*a1 + 8*a1 + 3*a1*b1 - b1*b1;\
                    })

By doing so, your macro increments or decrements the integer and saves it in a1 and b1 respectively. Operations are now done on a1 and b1.

Instead of:

--x_copy*--x_copy + 8*--x_copy + 3*--x_copy*--y_copy - --y_copy*--y_copy

you get

int a1= --x_copy;
int b1= --y_copy;
a1*a1 + 8*a1 + 3*a1*b1 - b1*b1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM