数据量大没关系,大不了用 string 也可以模拟,(4G) 够不够?
回去写了个简单的,你可以照着改一下: 不难。
program Project1;
var
Input: string;
procedure Translate(s: string);
var
Stack: array of char;
i, Top: Integer;
temp: string;
begin
SetLength(Stack, Length(s));
i := 1;
Top := 0;
temp := '';
while i <= Length(s)do
begin
case s of
'a'..'z':
temp := temp + s;
'+', '-':
begin
while (Top > 0) and (Stack[Top - 1] <> '(')do
begin
temp := temp + Stack[Top - 1];
Dec(Top);
end;
Stack[Top] := s;
Inc(Top);
end;
'*', '/':
begin
if (Top > 0) and ((Stack[Top - 1] = '*') or (Stack[Top - 1] = '/'))
then
begin
temp := temp + Stack[Top - 1];
Dec(Top);
end;
Stack[Top] := s;
Inc(Top);
end;
'(':
begin
Stack[Top] := s;
Inc(Top);
end;
')':
begin
while (Stack[Top - 1] <> '(') and (Top > 0)do
begin
temp := temp + Stack[Top - 1];
Dec(Top);
end;
if Top = 0 then
begin
temp := '''('' expected!';
Top := 0;
Break;
end;
Dec(Top);
end;
end;
Inc(i);
end;
while (Top > 0)do
begin
if Stack[Top - 1] = '(' then
begin
temp := ''')'' expected1';
Break;
end;
Temp := Temp + Stack[Top - 1];
Dec(Top);
end;
writeln(temp);
end;
begin
writeln('Please input the expression:');
readln(Input);
Translate(Input);
readln;
end.