简体   繁体   English

使用宏时出错

[英]Error using macros

I have this code where I am processing the data. 我有这个代码,我正在处理数据。 It is a code Chef question. 这是一个代码厨师问题。 I take a series of inputs from the user and process them to find the maximum difference between 2 numbers. 我从用户那里获取一系列输入并处理它们以找到2个数字之间的最大差异。 But when I use macros to get maximum difference it gives me a wrong answer, where as when I use the function (which I have currently commented) gives the right answer. 但是当我使用宏来获得最大的差异时,它给了我一个错误的答案,就像当我使用函数(我目前已经评论过)给出了正确的答案。 Here is the code. 这是代码。

#include <stdio.h>
#define mod(a,b)a>b?a-b:b-a
/*int mod(int a, int b)
{
    if(a>b)
        return a-b;
    else
        return b-a;
}*/
int main()
{
    int p1,o1=0;//player1
    int p2,o2=0;//player2
    int margin=0;//win margin
    int rounds;//number of rounds
    scanf("%d",&rounds);
    while(rounds--)
   {
        scanf("%d %d",&p1,&p2);
        o2+=p2;o1+=p1;
        if(mod(o1,o2)>mod(margin,0))
            margin=o1-o2;
    }
    if(margin<0)
        printf("%d %d\n",2,margin*-1);
    else
        printf("%d %d\n",1,margin);
    return 0;
}

The sample input that I enter is: 我输入的示例输入是:

5
140 82
89 134
90 110
112 106
88 90

1 The expected answer of this input is:: 1 58 1此输入的预期答案是:: 1 58

whereas when I use macro to do the computations it gives an output that looks like this:: 而当我使用宏来进行计算时,它会给出一个看起来像这样的输出::

2 3

Why is the macro giving wrong answer. 为什么宏给出了错误的答案。 Please do rep guys.. Thanks in advance.. 请做代表..提前谢谢..

正确的宏看起来像

#define mod(a,b) ( ( a ) > ( b ) ? ( a ) - ( b ) : ( b ) - ( a ) )

Macros perform textual substitutions. 宏执行文本替换。 As a result, when they get "pasted" into code, unexpected side effects can occur. 因此,当它们被“粘贴”到代码中时,可能会发生意外的副作用。 You want to wrap your arguments in parentheses to get the correct result. 您希望将参数括在括号中以获得正确的结果。

ie: 即:

#define mod(a,b) ( ( a ) > ( b ) ? ( a ) - ( b ) : ( b ) - ( a ) )

This ensures that whatever is substituted for a and b are correctly "grouped". 这确保了替代ab任何东西都被正确地“分组”。

mod(o1,o2)>mod(margin,0)

becomes

o1>o2?o1-o2:o2-o1>margin>0?margin-0:0-margin

which gets parsed as 被解析为

o1>o2 ? o1-o2 : ( ((o2-o1)>margin) > 0 ) ? margin-0 : 0-margin

The ternary operator has very low priority, just above assignment. 三元运算符的优先级非常低,仅在分配之上。 Everything else on the right side gets evaluated first, and only then the ternary. 右侧的其他所有内容首先进行评估,然后才进行三元评估。 This includes the greater than between the two mods. 这包括两个mod之间的大于。 This is only possible because C treats boolean expressions as arithmetic values and happily computes on with them -- any decent language would balk here. 这是唯一可能的,因为C将布尔表达式视为算术值,并愉快地计算它们 - 任何体面的语言都会在这里犹豫不决。 The result of the first comparison is 0 or 1 and used in the next one, comparing it against 0. How to fix this by bracketing properly or, better, using functions (modern compilers inline them later anyway!) has been described by the others. 第一次比较的结果是0或1,并在下一个比较中使用,将其与0进行比较。如何通过正确包围或者更好地使用函数来解决这个问题(现代编译器后来无论如何内联它们!)已被其他人描述。

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

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