From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: * X-Spam-Status: No, score=1.5 required=3.0 tests=BAYES_50,FREEMAIL_FROM, FROM_STARTS_WITH_NUMS autolearn=no autolearn_force=no version=3.4.5-pre1 X-Received: by 2002:ac8:5909:: with SMTP id 9mr11694639qty.39.1613331355189; Sun, 14 Feb 2021 11:35:55 -0800 (PST) X-Received: by 2002:a25:268c:: with SMTP id m134mr19339872ybm.253.1613331354953; Sun, 14 Feb 2021 11:35:54 -0800 (PST) Path: eternal-september.org!reader02.eternal-september.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer04.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sun, 14 Feb 2021 11:35:54 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=85.240.216.69; posting-account=rhqvKAoAAABpikMmPHJSZh4400BboHwT NNTP-Posting-Host: 85.240.216.69 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: algorithm, two implementations that act the same, same type etc From: Mehdi Saada <00120260a@gmail.com> Injection-Date: Sun, 14 Feb 2021 19:35:55 +0000 Content-Type: text/plain; charset="UTF-8" X-Received-Bytes: 5002 Xref: reader02.eternal-september.org comp.lang.ada:61351 List-Id: 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;