Mike Austin's Blog

Tuesday, October 03, 2006

Poor man's operator precedence

#include <stdio.h>
#include <string.h>

int main( int argc, char* argv[] ) {
printf( "((((" );
for( int i = 1; i != argc; i++ ){
if( strcmp(argv[i], "^") == 0 ) printf( ")^(" );
else if( strcmp(argv[i], "*") == 0 ) printf( "))*((" );
else if( strcmp(argv[i], "/") == 0 ) printf( "))/((" );
else if( strcmp(argv[i], "+") == 0 ) printf( ")))+(((" );
else if( strcmp(argv[i], "-") == 0 ) printf( ")))-(((" );
else printf( "%s", argv[i] );
}
printf( "))))\n" );
return 0;
}

$ ./parenthesise a \* b + c ^ d / e
((((a))*((b)))+(((c)^(d))/((e))))

from wikipedia.org

0 Comments:

Post a Comment

<< Home