#define is unsafe
来源:互联网 发布:实体店跟淘宝的一样吗 编辑:程序博客网 时间:2024/06/05 04:26
Problem Description
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\n" , 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\n" , 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.
#include <stdio.h>
#define MAX(a , b) ((a) > (b) ? (a) : (b))
int main()
{
printf("%d\n" , 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\n" , 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.
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
6MAX(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))
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
char str[1002];
int max(int a,int b)
{
return a>b?a:b;
}
int add(int a,int b)
{
return a+b;
}
bool isnumber(char c)
{
if(c>='0'&&c<='9')
{
return true;
}
return false;
}
//+:0 (:1 ):2 ,3
int index(char ch)
{
if(ch=='+')
{
return 0;
}
else if(ch=='(')
{
return 1;
}
else if(ch==')')
{
return 2;
}
else if(ch==',')
{
return 3;
}
else
{
return 4;
}
}
char p[5][5]={{'>','<','>','>','>'},{'<','<','=','<',' '},{'>',' ','>','>','>'},{'<','<','>',' ',' '},{'<','<',' ',' ','='}};
typedef struct node{
int i;
int k;
}Node;
char prioty(char s,char d)
{
int a=index(s);
int b=index(d);
return p[a][b];
}
void dfs()
{
stack<char> mysign;
mysign.push('#');
stack<Node> mynumber;
int i=0;
char c=str[i++];
while(c!='#'||mysign.top()!='#')
{
if(isnumber(c)){
int k=c-'0';
c=str[i++];
while(isnumber(c))
{
int temp=c-'0';
k=k*10+temp;
c=str[i++];
}
Node n1={k,0};
mynumber.push(n1);
}
else
{
if(c=='M')
{
i++;
i++;
c=str[i++];
}
else{
switch(prioty(mysign.top(),c))
{
case '<':
mysign.push(str[i-1]);
c=str[i++];
break;
case '=':
mysign.pop();
c=str[i++];
break;
case '>':
char ch=mysign.top();
mysign.pop();
Node a=mynumber.top();
mynumber.pop();
Node b=mynumber.top();
mynumber.pop();
if(ch==',')
{
int r=max(a.i,b.i);
int count=0;
if(a.i>=b.i)
{
count=a.k*2+b.k;
}
else
{
count=a.k+b.k*2;
}
Node t1={r,count};
mynumber.push(t1);
}
if(ch=='+')
{
int r1=add(a.i,b.i);
Node t2={r1,a.k+b.k+1};
mynumber.push(t2);
}
break;
}//swith
}//else
}//else
}//while
Node re=mynumber.top();
cout<<re.i<<" "<<re.k<<endl;
}
int main()
{
// freopen("in.txt","r",stdin);
int n;
cin>>n;
while(n--)
{
cin>>str;
str[strlen(str)]='#';
dfs();
}
return 0;
}
- #define is unsafe
- #define is unsafe hdu3350
- #define is unsafe
- hdu 3350 #define is unsafe
- HDU3350 #define is unsafe 栈的应用
- 【题解】HDU.3350.#define is unsafe
- 杭电 oj 3350 #define is unsafe
- HDU 3350 #define is unsafe 栈的模拟题
- May_Day训练赛模拟题 HDOJ 3350 #define is unsafe
- HDU-3350 #define is unsafe(dfs 解析字符串)
- HDU 3350 #define is unsafe 栈的模拟题 宏问题
- fopen is unsafe 错误
- this program uses gets(), which is unsafe.
- this program uses gets(), which is unsafe.
- Unsafe
- Unsafe
- Unsafe
- RefernceError : jQuery is not define
- Darwin视频服务配置
- swf文件转换其他视频格式工具(例:swf to mp4) ,转换后的视频无水印
- NHibernate说探——Criteria条件查询
- 第九次上机实验报告
- Unsupported major.minor version 51.0
- #define is unsafe
- netstat命令
- Twitter Bootstrap 框架介绍
- Android中如何合并两个工程的功能
- ruby-debug 命令详解
- Android4.0 Launcher源码研究
- 资源文件的读取
- 优化 PHP 代码技巧
- 如何在 Visual Basic 控件中实现 IObjectSafety