Red cape flying cat wrote a small calculator. The calculator can process expressions containing non-negative integers, +, -, *, and parentheses.
However, his calculator does not understand the custom operators @ and $. Before giving an expression to the calculator, you need to replace every custom operator with an equivalent expression using only ordinary operators.
The custom operators are defined as:
a @ b = a - (a * b) + b
a $ b = (a @ b)
Therefore:
a $ b = (a - (a * b) + b)
Here, a and b are both factors.
In this problem, a factor is:
FACTOR = NUMBER | (EXPR)
That is, a factor is either a non-negative integer or a parenthesized expression.
For each custom operator, its left operand is the nearest valid factor immediately on its left, and its right operand is the nearest valid factor immediately on its right.
You must repeatedly perform the following operation:
@ or $, in the current expression.a be the nearest factor on its left.b be the nearest factor on its right.@, replace a@b with a-(a*b)+b.$, replace a$b with (a-(a*b)+b).After a replacement, the new expression becomes the current expression. Continue until there is no custom operator left.
The final expression must contain only digits, +, -, *, (, and ). Do not output spaces.
For:
2@3
The result is:
2-(2*3)+3
For:
2$3
The result is:
(2-(2*3)+3)
For:
(2+3)@4
The left factor is (2+3), and the right factor is 4, so the result is:
(2+3)-((2+3)*4)+4
For:
2*3@4
The left factor of @ is only 3, not 2*3. Therefore, the result is:
2*3-(3*4)+4
For multiple custom operators, always replace the leftmost one first:
2@3$4
=> 2-(2*3)+3$4
=> 2-(2*3)+(3-(3*4)+4)
This is a partial judge problem. The judge provides main.c and function.h.
You only need to implement:
void replace_custom_operator(char expr[], char result[]);
expr is the original expression. You should store the final expression in result. The judge will print result.
The provided function.h also contains suggested helper function names used by the reference solution. You do not have to use exactly those helper functions, but they may help you split the implementation into smaller parts.
The input is read by the judge's main.c.
The input contains one line: a valid expression expr.
1 <= |expr| <= 2000
0 <= NUMBER <= 1000000000
After replacing all custom operators, the expression length will not exceed 50000.
+, -, *, @, $, (, and ).The output is produced by the judge's main.c.
Output the expression after replacing all custom operators.
The output should not contain spaces.