/*Problem description
Input a four operation expression with brackets from the keyboard, and remove the possible extra brackets. Keep the relative position of variables and operators in the original expression unchanged and equivalent to the original expression, without simplification. In addition, the case where '+' - 'is used as a sign is not considered, that is, the input expression will not appear (+ a) or (- a).
Input format
Expression string, no longer than 255, and no space characters. All variables in the expression are single lowercase English letters, and the operator only has the addition + subtraction - multiplication * Division / and other operation symbols.
Output format
Expressions without extra brackets
sample input
Example 1:
a+(b+c)-d
Example two:
a+b/(c+d)
Example three:
(a*b)+c/d
Example four:
((a+b)*f)-(i/j)
sample output
Example 1:
a+b+c-d
Example two:
a+b/(c+d)
Example three:
a*b+c/d
Example four:
(a+b)*f-i/j
*/
#if 1 #include<string.h> void Output( char [] ) ; int Solve( char [] , int ) ; int pd( char [] , int , int ); int main(void) { char zf[256]; gets( zf ); Solve( zf , 0 ); Output( zf ) ; return 0 ; } int pd( char zf[] , int left, int right ) { int i , leftAcount ; if( zf[ left - 1 ] == '-') { i = left; leftAcount = 1; while( ++ i < right) { if(zf[i] == '(') { leftAcount ++; } if(zf[i] == '+' && leftAcount == 1) { return 0; } } } if( zf[ left - 1 ] == '/') { return 0; } if(zf[ left - 1 ] != '*' && zf[ left - 1 ] != '/' && zf[ right + 1 ] != '*' && zf[ right + 1 ] != '/') { return 1; } i = left ; leftAcount = 1; while(++i < right) { if(zf[i] == '(') { leftAcount ++; } if(zf[i] == '*' && leftAcount == 1) { return 1; } } return 0; } int Solve( char zf[] , int i ) { int left , right ; while( zf[i] != '\0') { if( zf[i] == ')') { return i ; } if( zf[i] == '(') { left = i ; right = Solve( zf , i + 1 ) ; i = right ; if( pd( zf , left , right )) { zf[left] = ' ' ; zf[right] = ' ' ; } } i ++ ; } } void Output( char zf[] ) { int i = 0 ; while( zf[ i ] != '\0') { if(zf[i] != ' ') { putchar(zf[i]); } i ++ ; } } #endif