From: Mehdi Saada <00120260a@gmail.com>
Subject: algorithm, two implementations that act the same, same type etc
Date: Sun, 14 Feb 2021 11:35:54 -0800 (PST) [thread overview]
Message-ID: <b77499f6-18e9-4a53-b115-4b6f772ce5d1n@googlegroups.com> (raw)
Hi,
I have an issue with an algorithm - self-imposed "homework" I guess, the first one is mine, the wrong, the second is the "right" one, that works.
I can't figure out why they would logically be any different.
basically instead of using a stack on access values, I stack the values themselves. the rest is the same.
except this function ALL the rest of the project is the same. they are in related package so from the first I call the second to ensure the result is right, and it shows its not.
could someone throw an eye and tell if it seems identical or that something gross escaped my sight ?
45+78*56-(5-(8+5)-7);
gives (really) : 4428 and mine gives 4413
11-0/5+88/5;
it should be: 28 and mine gives 11
my function:
function Calcul ( V : in T_Vect_Token) return Integer is
package ppile is new P_Pile_4(T_Elem => Integer,
Max_Pile => 80);
use ppile;
Pile_operandes : ppile.T_Pile(40);
op1_calcul, op2_calcul: Integer;
begin
for I in V'Range loop
if V(I).all'Tag = T_Token_Operande'Tag then
ppile.Empiler(Pile_operandes,T_Token_Operande(v(I).all).Get_Elem);
elsif V(I).all'Tag = T_Token_Operateur'Tag then
Depiler(Pile_operandes,op2_calcul);
Depiler(Pile_operandes,op1_calcul);
case T_Token_Operateur(v(I).all).L_Operateur is
when '*' => Empiler(Pile_operandes,op1_calcul * op2_calcul);
when '-' => Empiler(Pile_operandes,op1_calcul - op2_calcul);
when '+' => Empiler(Pile_operandes,op1_calcul + op2_calcul);
when '/' => Empiler(Pile_operandes,op1_calcul / op2_calcul);
end case;
else raise P_Expression.Exc_Erreur;
end if;
end loop;
Depiler(Une_Pile => Pile_operandes,
Elem => op2_calcul);
if ppile.Pile_Vide(Pile_operandes) then Put_Line("BIEN !"); end if;
Put_Line("vrai calcul : " & P_Expression.Calcul(V)'Image);
Put_Line("faux calcul: "); return op1_calcul;
exception
when E: others => Put_Line(Exception_Information(E)); return 0;
end Calcul;
the good function:
function Calcul ( V : in T_Vect_Token) return Integer is
Pile : P_Pile_Token.T_Pile(80);
Res : Integer;
Token1,
Token2 : T_Ptr_Token ;
Ptr_Token : T_Ptr_Token ;
begin
for I in V'range loop
Ptr_Token := V(I);
if Ptr_Token.all'Tag = T_Token_Operande'Tag
then
Empiler(Pile,Ptr_Token);
elsif
Ptr_Token.all'Tag = T_Token_Operateur'Tag
then
Depiler(Pile,Token2);
Depiler(Pile,Token1);
case Get_Elem(T_Token_Operateur(Ptr_Token.all)) is
when'+' => Res := Get_Elem (T_Token_Operande(Token1.all)) +
Get_Elem (T_Token_Operande(Token2.all));
when'-' => Res := Get_Elem (T_Token_Operande(Token1.all))-
Get_Elem (T_Token_Operande(Token2.all));
when'*' => Res := Get_Elem (T_Token_Operande(Token1.all)) *
Get_Elem (T_Token_Operande(Token2.all));
when'/' => Res := Get_Elem (T_Token_Operande(Token1.all)) /
Get_Elem (T_Token_Operande(Token2.all));
end case;
Empiler(Pile,Set_Ptr(Res));
else
Ada.Text_Io.Put_Line("TOKEN intru ");
raise Exc_Erreur;
end if;
end loop;
Depiler(Pile,Ptr_Token);
if Pile_Vide(Pile)
then
return Get_Elem(T_Token_Operande(Ptr_Token.all));
else
Ada.Text_Io.Put_Line("Pile non vide ");
raise Exc_Erreur;
end if;
exception
when others => Ada.Text_Io.Put_Line ("Erreur CALCUL");
raise Exc_Erreur;
end Calcul;
next reply other threads:[~2021-02-14 19:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-14 19:35 Mehdi Saada [this message]
2021-02-15 11:07 ` algorithm, two implementations that act the same, same type etc AdaMagica
2021-02-15 13:02 ` Niklas Holsti
2021-02-15 16:34 ` Mehdi Saada
2021-02-15 19:15 ` Niklas Holsti
2021-02-17 10:50 ` Mehdi Saada
2021-02-17 12:14 ` Mehdi Saada
2021-02-17 17:10 ` Niklas Holsti
2021-02-17 21:13 ` Mehdi Saada
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox