Enter a four-character arithmetic expression containing parentheses on the keyboard, which may contain extra parentheses,
Remove all unnecessary parentheses. The relative positions of all variables and operators in the original expression remain unchanged.
Equivalent.
For example, the input expression must be an output expression.
A + (B + C) A + B + C
(A * B) + C/D a * B + C/D
A + B/(c-d)
Note that B + A cannot be output when a + B is input.
The expression is input as a string and the length cannot exceed 255. Do not set an error.
All variables are single lowercase letters. We only need to remove all the extra parentheses. We do not need to simplify the expressions.
II,AlgorithmAnalysis
The four arithmetic expressions contain the following operators: '+', '-', '*', '/', and '(', ')'. Their priority ranges from low to high.
'+', '-' → '*', '/' → '(',')'
Whether a bracket is removed as a redundant bracket, the key is to analyze the operators with the lowest priority in the bracket and left adjacent
Calculation priority between operators in parentheses:
Set the expression to (S1 op S2 );
Op-operators with the lowest priority in parentheses ('+', '-' or '*','/');
1. If the operator of the Left adjacent brackets is '/', brackets must be retained regardless of the OP operator, that is ...... /(S1
op S2 )....... Because the original formula (S1 op S2) is used as the divisor, once parentheses are removed, only S1 is used as the divisor. No
it may be equivalent to the original formula;
2. If the left adjacent brackets are '*' or '-', and OP is '+' or '-', the parentheses are retained, that is ...... *-(S1 +-
S2 ). The reason that parentheses cannot be removed is:
If the left adjacent mark is '*', the priority is greater than the operator in parentheses, therefore, brackets must be used to ensure that '+' or
'-' in parentheses are given priority.
If the left adjacent operator is '-', after parentheses are removed, because '+' and '-' in parentheses need to be changed, the result is not equal to the original format.
price;
3. If the operator of the right adjacent brackets is '*' or '/', and the op is '+' or '-', the op operation in the original formula must take priority,
therefore, brackets cannot be removed, that is, (S1 +-S2 )*/......;
4. In addition to the preceding conditions, parentheses are removed. The result is equivalent to the original formula, that is... S1 op S2 ....
starting from the nested parentheses in the innermost layer, we gradually sort the parentheses in the outer layer according to the above rules until the parentheses in the outermost layer are retained or removed. This sorting process can be described using the following recursive process reduce:
function reduce (expression string S, var op): string;
{sorting expression s, return the op operator with the lowest priority in the S string and the sorting result}
begin
If S is the variable then
begin
op = ''; reduce = variable s; exit
end;
If S is (s) form then
begin
reduce = reduce (s ', OP ); exit
end;
{s is in the 's1 op S2 'format}
A = reduce (S1, C1); B = reduce (S2, C2 );
If (OP in ['*', '-']) and (C2 in ['+', '-']) Then S2 = '(S2 )';
If (OP in ['*', '/']) and (C1 in ['+', '-']) Then S1 = '(S1 )';
If (OP in ['/']) and (C2 <> '') Then S2 = '(S2 )';
reduce = 's1 op s2'
end;
For example, remove unnecessary parentheses in the '(a + B) * F)-(I/J)' expression. The process of sorting based on the above algorithms is as follows:
'(A + B) * F)-(I/J )','-'
┌ ── ─ ┴ ── ─
'(A + B) * f)', '*' (I/J )','/'
│
'(A + B) * F',' * 'I/J ','/'
── ┴ ── ┐ ┌ ── ┴ ┐
'(A + B)', '+' F', ''' I ', '''j ',''
│
'A + B ',' +'
┌ ── ┐
'A', ''' B ',''
(Figure 1.1-1)
Finally, the results are sorted from the bottom up: (A + B) * f-I/J.
III,ProgramAnalysis
Program P11;
VaR
INS: string; {arithmetic expression}
P: Char; {minimum operator}
Function suitable (S: string; I: byte): byte;
{S [I] is ')', and the suitable function outputs the corresponding '(' position in S}
VaR
T: byte;
Begin
T: = 1;
Repeat
Dec (I );
If s [I] = ')'
Then Inc (t)
Else if s [I] = '('then Dec (t)
Until T = 0;
Suitable: = I
End; {suitable}
function find (S: string): byte;
{location of the lowest operator in output expression s}
var
I, K: byte;
begin
I: = length (s); K: = 0;
while I> 0 DO
begin
If (s [I] = '+') or (s [I] = '-')
then begin
Find: = I; exit
end; {then}
If (k = 0) and (S [I] = '*') or (s [I] = '/') then K: = I;
If s [I] = ') 'Then I: = Suitable (S, I);
Dec (I)
end; {While}
Find: = k
end; {find}
Function reduce (S: string; var P: Char): string;
{Sort expression s and return the operator P with the lowest priority}
VaR
A, B: string; {result of sorting the Left and Right items of expression s}
C1, C2: Char; {the lowest-level operator for the left and right items of expression s}
I: byte; {location of the lowest-level operator of expression s}
Begin
If length (S) = 1 {returns a variable if the expression is a variable}
Then begin
Reduce: = s; P: = ''; exit
End; {then}
If (s [length (s)] = ') and (suitable (S, length (s) = 1)
{If the expression is enclosed by a pair of parentheses, the results of the expressions in the brackets are returned}
Then begin
Reduce: = reduce (copy (S, 2, length (S)-2), P );
Exit
End; {then}
I: = find (s); P: = s [I]; {find the lowest-level operator and its position}
A: = reduce (copy (s, 1, I-1), C1); {sort the Left and Right items respectively}
B: = reduce (copy (S, I + 1, length (S)-I), C2 );
{Determine whether to enclose brackets based on P, C1, C2}
If (P in ['*', '-']) and (C2 in ['+', '-']) Then B: = '(' + B + ') ';
If (P in ['*', '-']) and (C1 in ['+', '-']) Then A: = '(' + A + ') ';
If (P = '/') and (C2 <> '') Then B: = '(' + B + ')';
Reduce: = a + P + B {return finishing result}
End; {reduce}
Begin
Write ('str = ');
Readln (INS); {input arithmetic expression}
Writeln (reduce (INS, p) {remove unnecessary parentheses and print the finishing result}
End. {main}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////
Example 23Remove the parentheses of multiple operators.
Enter a four-digit arithmetic expression containing parentheses on the keyboard, which may contain parentheses with multiple operators. Sort the expressions by programming and remove all parentheses with multiple operators, the relative positions of all variables and operators in the original expression remain unchanged and are equivalent to those in the original expression.
For example:
input expression |
output expression |
A + B (+ C) |
A + B + C |
(A * B) + C/(d * C) |
a * B + A/(d * C) |
A + B/(c-d) |
A + B/(c-d) |
Note: enter a ticketA + BCannot outputB +.
The expression is input as a string and the length cannot exceed255,No error is required for input.
All variables are single lowercase letters. We only need to remove all the parentheses and avoid simplifying the expression.
Algorithm Analysis
For the four arithmetic expressions, we will analyze which parentheses can be removed.
Set the expression (S1 op S2);OPOperators with the lowest priority in parentheses ("+", "-", "×","/");
① If the operator of the Left adjacent brackets is "/", the brackets must be retained... /(S1 op S2 )... Format.
② The budget character of the Left adjacent brackets is "*" or "-". When op is "+" or "-", brackets are retained... × (S1 + S2 )..." , "… -(S1 + S2 )..." , "… * (S1-s2 )..." , "… -(S1-s2 )...".
③ The operator of the right adjacent brackets is "×" or "/", and the op is "+" or "-". The op operation in the original formula must be performed first, so the brackets are not removed, that is, "(S1 + S2 )*...".
In addition to the above situations, you can remove the brackets, that is, "…". S1 op S2 ..." It is equivalent to (S1 op S2 )...".
People gradually sort out parentheses from the innermost layer of nested parentheses according to the above rules until the outermost brackets are retained or removed. This sorting process can be implemented using a recursive process.
【Reference Program]
Program AA;
VaR CH: Char;
St: string;
Function LOC (S: string; I: byte): byte;
VaR Q: byte;
Begin
Q: = 1;
Repeat
INC (I );
If s [I] = '('then Inc (q)
Else if s [I] = ')' Then Dec (Q );
Until q = 0;
Loc: = I;
End;
Function find (S: string): byte;
VaR I, K: byte;
Begin
I: = 1;
K: = 0;
While I <= length (s) do
Begin
If (s [I] = '+') or (s [I] = '-') then
Begin
Find: = I; exit;
End;
If (k = 0) and (S [I] = '*') or (s [I] = '/') then K: = I;
If s [I] = '('then I: = LOC (S, I );
INC (I );
End;
Find: = K;
End;
Function del (S: string; var P: Char): string;
VaR I, j: byte;
Substring, CH2: Char;
Left, right: string;
Begin
I: = 0; J: = 0;
I: = pos (', S); j: = pos (') ', S );
If (I = 0) and (j = 0) then
Begin DEL: = s; exit; end;
If (s [1] = '(') and (loc (s, 1) = length (s) then
Begin
DEL: = del (copy (S, 2, length (S)-2), P );
Exit;
End;
I: = find (s );
P: = s [I];
Left: = del (copy (s, 1, I-1), rows );
Right: = del (copy (S, I + 1, length (S)-I), CH2 );
If (P in ['*', '/']) and (values in ['+', '-']) then
Left: = '(' + Left + ')';
If (P in ['*', '/']) and (CH2 in ['+', '-']) or (P = '/') and (CH2 <> '') then
Right: = '(' + right + ')';
DEL: = left + P + right;
End;
Begin
Readln (ST );
Writeln (DEL (St, CH ));
End.