Answer the question
In order to leave comments, you need to log in
How can this code be modified to integers?
How to make it so that you can also enter whole numbers (that is, 55 + 22, for example)
program Project2;
{$APPTYPE CONSOLE}
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
Res,len:integer;
begin
Res:=0;
if length(S)>0 then
begin
case s[length(S)] of
')': begin
delete(S,length(S),1);
len:=LenExpression(S);
Res:=Expression(S);
delete(S,length(S)-len+1,len);
end;
'0'..'9': begin
Res:=(ord(S[length(S)])-48);
writeln(Res);
delete(S,length(S),1);
end;
end;
if length(S)>0 then
Case s[length(S)] of
'-': begin
delete(S,length(S),1);
Result:=Expression(S)-Res;
end;
'+': begin
delete(S,length(S),1);
Result:=Expression(S)+Res;
end;
'*': begin
delete(S,length(S),1);
Result:=Expression(S)*Res;
end;
'(': Result:=Res;
end
else Result:=Res;
end
else Writeln('!!error!!');
end;
var
S:String;
begin
Write('S: ');
Readln(S);
Writeln('Exp: ',Expression(S));
readln;
end.
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question