#define is unsafe——I

                                            

                                          I. #define is unsafe

Have you used #define in C/C++ code like the code below?

#include <stdio.h>
#define MAX(a , b) ((a) > (b) ? (a) : (b))
int main()
{
  printf("%d " , MAX(2 + 3 , 4));
  return 0;
}

Run the code and get an output: 5, right?
You may think it is equal to this code:

#include <stdio.h>
int max(a , b) {  return ((a) > (b) ? (a) : (b));  }
int main()
{
  printf("%d " , max(2 + 3 , 4));
  return 0;
}

But they aren't.Though they do produce the same anwser , they work in two different ways.
The first code, just replace the MAX(2 + 3 , 4) with ((2 + 3) > (4) ? (2 + 3) : 4), which calculates (2 + 3) twice.
While the second calculates (2 + 3) first, and send the value (5 , 4) to function max(a , b) , which calculates (2 + 3) only once.

What about MAX( MAX(1+2,2) , 3 ) ? 
Remember "replace".
First replace: MAX( (1 + 2) > 2 ? (1 + 2) : 2 , 3)
Second replace: ( ( ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 ) > 3 ? ( ( 1 + 2 ) > 2 ? ( 1 + 2 ) : 2 ) : 3).
The code may calculate the same expression many times like ( 1 + 2 ) above.
So #define isn't good.In this problem,I'll give you some strings, tell me the result and how many additions(加法) are computed.
 

Input

The first line is an integer T(T<=40) indicating case number.
The next T lines each has a string(no longer than 1000), with MAX(a,b), digits, '+' only(Yes, there're no other characters).
In MAX(a,b), a and b may be a string with MAX(c,d), digits, '+'.See the sample and things will be clearer.
 

Output

For each case, output two integers in a line separated by a single space.Integers in output won't exceed 1000000.
 

Sample Input

6
MAX(1,0)
1+MAX(1,0)
MAX(2+1,3)
MAX(4,2+2)
MAX(1+1,2)+MAX(2,3)
MAX(MAX(1+2,3),MAX(4+5+6,MAX(7+8,9)))+MAX(10,MAX(MAX(11,12),13))

Sample Output

1 0
2 1
3 1
4 2
5 2
28 14




题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,
分析:
MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b*2次。
#include <cstdio> 
#include <iostream>  
#include <cstring> 
using namespace std;  
struct state{  
    state(int a,int b){s=a,k=b;}  
    int s,k;    //s为和,k为次数  
};  
state find(string str){  
    int in=0,len=str.length(),num=0;//当前位置,字符串长度,和  
  
    if(str[in]>='0'&&str[in]<='9'){    //第一个字母是整数,读取这个数  
        while(in<len&&str[in]>='0'&&str[in]<='9')num=num*10+str[in++]-'0';  
        if(in>=len)return state(num,0);//如果只剩一个数,直接返回  
        else{   
            state st=find(str.substr(in+1));  
            return state(num+st.s,st.k+1);  
        }  
    }else if(str[in]=='M'){  
        in+=4;  
        int cnt=1,mid=0;  
        while(cnt>0){   //匹配MAX()右括号的位置,并找出对应这个MAX的逗号的位置  
            if(str[in]=='(')cnt++;  
            else if(str[in]==')')cnt--;  
            if(str[in]==','&&cnt==1)mid=in;   
            in++;  
        }  
        state le=find(str.substr(4,mid-4));   //求MAX(A,B)中的A  
        state ri=find(str.substr(mid+1,in-mid-2));  //求MAX(A,B)中的B  
        int p1,p2;  
        if(le.s>ri.s){  
            p1=le.s;  
            p2=le.k*2+ri.k;   
        }else{  
            p1=ri.s;  
            p2=le.k+ri.k*2;  
        }  
        if(in>=len-1){   //已经到达末端  
            return state(p1,p2);      
        }else{      //MAX(A,B) + C的形式,求C  
            state st=find(str.substr(in+1));   //处理加号后面的部分  
            return state(p1+st.s,st.k+p2+1);  
        }  
    }      
}  
int main(){   
    int cas;  
    char str[1005];  
    cin>>cas;  
    while(cas--){  
        cin>>str;  
        state rs=find(str);  
        cout<<rs.s<<" "<<rs.k<<endl;  
    }  
    return 0;     
}


原文地址:https://www.cnblogs.com/fenhong/p/5280034.html