A warrior never surrenders, A warrior never quits..


Saturday, June 18, 2011

code for fun

Program to Add/Subtract/Multiply 2 numbers..
Sounds pretty easy, so is the code for it.


#define s(c) if(scanf("%c",&c)){}
#define p(b) if(b+=(c-'0')){} if(b*=10){}
#define w(a,b) while(a>b){
#define i(a,b) if(a=b){}
main(int a,int b,int n,char c,char o)
{
if(scanf("%d",&n)){}
s(c)
w(n--,0)
i(a,0)
i(b,0)
s(c)
w(c,47)
p(a)
s(c)
}
if(a/=10){}
i(o,c)
s(c)
w(c,47)
p(b)
s(c)
}
if(b/=10){}
if(o=='+')
if(a=a+b){}
if(o=='-')
if(a=a-b){}
if(o=='*')
if(a=a*b){}
if(printf("%d\n",a)){}
}
}

However if you have noticed it correctly you would have found it unusual in the sense that it does not have any single semicolon. The unnecessary directives are to make the code shorter but it made the code ugly nevertheless.
I am still trying to make it smaller. If any one has better idea regarding it then comments are welcome.
You can compile the code successfully on any GCC compiler and run it as described below:
3 (3 test cases)
123+456
output: 579
456-123
output:333
123*45
output: 5535

happy coding!

No comments: