繁体   English   中英

更改宏部分,让两个输出具有相同的值

[英]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;
}

您可以像这样修改宏部分:

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

通过这样做,您的宏会增加或减少整数并将其分别保存在 a1 和 b1 中。 现在在 a1 和 b1 上完成操作。

代替:

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

你得到

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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