program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
function LenExpression(S: string): integer;
var
len: integer;
begin
if length(S) <= 0 then
Result := 0
else
case S[length(S)] of
')':
begin
delete(S, length(S), 1);
len := LenExpression(S);
delete(S, length(S) - len + 1, len);
Result := len + LenExpression(S) + 1;
end;
'(':
Result := 1 else
begin
delete(S, length(S), 1);
Result := 1 + LenExpression(S);
end;
end;
end;
function Expression(S: string): integer;
var
i: Integer;
str_value: string;
value: Integer;
value2: Integer;
exp: string;
begin
value := -1;
value2 := -1;
result := -1;
if length(S) > 0 then
begin
for i := 1 to length(S) do
begin
if S[i] in ['0'..'9'] then
begin
str_value := str_value + S[i];
end;
if (S[i] in ['-', '+', '*']) then
begin
exp := S[i];
value := str_value.ToInteger();
str_value := '';
end;
if i = Length(S) then
begin
value2 := str_value.ToInteger();
str_value := '';
end;
end;
if exp = '-' then
result := value - value2;
if exp = '+' then
result := value + value2;
if exp = '*' then
result := value * value2;
if exp = '' then
begin
Writeln('!!error!!');
Result := -1;
end;
end
else
Writeln('!!error!!');
end;
var
S: string;
begin
while true do
begin
Write('Value s: ');
Readln(S);
Writeln('Exp: ' + s + ' = ' + Expression(S).ToString);
end;
end.