Hello,
Sorry for my bad english , I'm French and this first and second semester we've learned programming with Freepascal.
We're asked , for exemple from a string like '5+13+8' to return the decimal resultat , which is in this case 26 .
I've managed to make a program and to compile it :
program interface1;
uses
SysUtils;
Const MAX = 255;
Type
Green = array[1..MAX] of String;
procedure classifier ( chaine : string; var g : green );
var k : Integer;
begin
g[1] :='+';
for k:=2 to length(chaine)+1 do
g[k]:= Copy(chaine,k-1,1);
end;
procedure nombreetsigne ( g : Green ; var nombre : Integer; var signe : string; var pos : Integer);
var sub : String;
begin
signe := g[pos];
sub :='';
pos := pos +1;
while ( (g[pos]<>'+') and (g[pos]<>'-')) do
begin
sub := sub+g[pos];
pos := pos+1;
end;
nombre := StrtoInt(sub);
end;
function calculerresultat( chaine : string; g : Green ) : Integer;
var pos , nombre , transit : Integer;
signe : String;
begin
transit := 0;
pos := 1;
while (pos <> length(chaine)) do
begin
nombreetsigne(g,nombre,signe,pos);
if (signe ='+') then
transit := transit + nombre;
if ( signe ='-') then
transit := transit - nombre;
end;
calculerresultat := transit;
end;
var chaine : String;
g : Green;
begin
write('entrez votre calcul :');
readln(chaine);
classifier(chaine,g);
writeln(calculerresultat(chaine,g));
end.
However , when trying to run it the following appears :
An unhandled exception occurred at $0000000000402941:
EAccessViolation: Access violation
$0000000000402941
$00000000004004B3
$00000000004005E5
I've already searched in the forum and i know it is related to an acess to an invalid memory ( not created yet )
i'm pretty sure it has a link with the variable pos , in fact i want it to be considerer has an input and output for the procedure nombreetsigne
Thank you for your help!