Yossep
VIP   
Messages: 10
Inscrit(e) le: 31/05/2020
Déconnecté(e)
|
Publié le 01/06/2020 à 11:57 |
RE : Exo 10: Création de paquetages (Operations Complexes)
Code NombresComplexes.ads
quote:
package NombresComplexes is
type Complexe is private;
type Tampon_Complexe is private;
type ptr_Complexe is access Complexe;
type Operator is private;
Erreur_C : exception;
-- Methode qui s'applique a un complexe
function InitComplexe return ptr_Complexe;
procedure afficheComplexe(c : In Complexe);
-- Methodes qui s'appliquent au tampon
function Est_il_vide(tp : In Tampon_Complexe) return Boolean;
procedure Charger(c1 : In Complexe; c2 : In Complexe; c3 : In Integer);
procedure Dispatch(tp : In Tampon_Complexe);
--Methodes qui s'appliquent au complexe
function "+"(c1, c2 : Complexe) return Complexe;
function "-"(c1, c2 : Complexe) return Complexe;
function "*"(c1, c2 : Complexe) return Complexe;
function "/"(c1, c2 : Complexe) return Complexe;
--Partie invisible au client
private
type Complexe is
record
re : float := 0.0;
img : float := 0.0;
end record;
type Operator is new integer range 1..4;
type Tampon_Complexe is
record
Element1 : Complexe;
Element2 : Complexe;
Element3 : Operator;
end record;
end NombresComplexes;
|
Code NombresComplexes.adb
quote:
With Ada.Float_Text_IO, Ada.Text_IO;
package body NombresComplexes is
use Ada.Float_Text_IO, Ada.Text_IO;
package Operator_IO is new Enumeration_IO(Operator);
use Operator_IO;
--- Fonction Initialisation d'un complexe ----------
function InitComplexe return ptr_Complexe is
c : Complexe;
ptr : ptr_Complexe;
begin
ptr := new Complexe;
Put("Re: ");get(c.re);Skip_Line;
Put("Img: ");get(c.img);Skip_Line;
ptr.all := c;
return ptr;
end InitComplexe;
--------------------------
procedure afficheComplexe(c: In Complexe) is
begin
Put("z = ");put(c.re, Exp => 0, Aft => 0,Fore => 1);
put(" + ");put(c.img, Exp => 0, Aft => 0,Fore => 1);Put("i");
new_line;
end afficheComplexe;
------------------------
--- Fonction pour gerer le tampon --------------
function Est_il_vide(tp : In Tampon_Complexe) return Boolean is
begin
if (tp.Element1.re = 0.0 Or tp.Element2.re = 0.0) then
return true ;
else
return false;
end if;
end Est_il_vide;
----------------------
procedure Charger(c1 : In Complexe; c2 : In Complexe; c3 : In Integer) is
tp : Tampon_Complexe;
begin
tp.Element1 := c1;
tp.Element2 := c2;
tp.Element3 := Operator(c3);
Dispatch(tp);
end Charger;
-----------------------------
procedure Dispatch(tp : In Tampon_Complexe) is
cr : Complexe;
begin
if Est_il_vide(tp) then
raise
Erreur_C;
else
case tp.Element3 is
when 1 =>
cr := tp.Element1 + tp.Element2;
afficheComplexe(cr);
when 2 =>
cr := tp.Element1 - tp.Element2;
afficheComplexe(cr);
when 3 =>
cr := tp.Element1 * tp.Element2;
afficheComplexe(cr);
when 4 =>
cr := tp.Element1 / tp.Element2;
afficheComplexe(cr);
when others =>
Put_Line(" Mauvais choix !");
raise
Erreur_C;
end case;
end if;
end Dispatch;
---------------------------------------
function "+"(c1, c2 : Complexe) return Complexe is
cr : Complexe;
begin
cr.re := c1.re + c2.re;
cr.img := c1.img + c2.img;
return cr;
end "+";
------------------------------
function "-"(c1, c2 : Complexe) return Complexe is
cr : Complexe;
begin
cr.re := c1.re - c2.re;
cr.img := c1.img - c2.img;
return cr;
end "-";
-----------------------------
function "*"(c1, c2 : Complexe) return Complexe is
cr : Complexe;
begin
cr.re := (c1.re * c2.re) - (c1.img * c2.img);
cr.img := (c1.re * c2.img) + (c2.re * c1.img);
return cr;
end "*";
---------------------------
function "/"(c1, c2 : Complexe) return Complexe is
cr : Complexe;
begin
cr.re := ((c1.re * c2.re) + (c1.img * c2.img)) / ((c2.re * c2.re)+(c2.img * c2.img));
cr.img := ((c1.re * c1.img) - (c1.re * c2.img)) / ((c2.re * c2.re)+(c2.img * c2.img));
return cr;
end "/";
end NombresComplexes;
|
Fichier main.adb
quote:
With Ada.Text_IO, Ada.Integer_Text_IO, NombresComplexes;
procedure Main is
use Ada.Text_IO, Ada.Integer_Text_IO, NombresComplexes;
ptrc1 : Ptr_Complexe; -- Allocation d'un pointeur sur complexe pour accéder au contenu privé
-- des variables c1, c2
c1, c2 : Complexe;
c3 : Integer;
begin
Put_Line("Operations sur les nombres complexes ");
New_Line;
Put_Line("Veillez saisir deux nombres complexes svp !");
ptrc1 := InitComplexe;
New_Line;
c1 := ptrc1.all;
ptrc1 := InitComplexe;
New_Line;
c2 := ptrc1.all;
Put_Line("Choix de l'opération :");
Put_Line(" '+' [1]");
Put_Line(" '-' [2]");
Put_Line(" '*' [3]");
Put_Line(" '/' [4]");
New_Line;
put(">");get(c3);
Charger(c1, c2, c3);
end Main;
|
Merci pour vos commentaires et conseils. |
ftbass
|
Publié le 12/03/2004 à 21:40 |
Exo 10: Création de paquetages (Operations Complexes)
Création du paquetage OperationsComplexes, afin de pouvoir additionner, soustraire, multiplier, et diviser des complexes...
Fichier OperationsComplexes.ads
(Spécifications)
code: package OperationsComplexes is
Type Complexe is record
re : float;
im : float;
end record;
function "+"(c1, c2 : in Complexe) return Complexe;
function "-"(c1, c2 : in Complexe) return Complexe;
function "*"(c1, c2 : in Complexe) return Complexe;
function "/"(c1, c2 : in Complexe) return Complexe;
procedure Put(c1 : in Complexe);
end OperationsComplexes; |
Fichier OperationsComplexes.adb
(Corp)
code: with text_io; use text_io;
with Ada.Text_IO, Ada.Float_Text_IO;use Ada.Text_IO, Ada.Float_Text_IO;
package body OperationsComplexes is
function "+"(c1, c2 : in Complexe) return Complexe is
cr : Complexe;
begin
cr.re := c1.re + c2 .re;
cr.im := c1.im + c2.im;
return cr;
end "+";
function "-"(c1, c2 : in Complexe) return Complexe is
cr : Complexe;
begin
cr.re := c1.re - c2 .re;
cr.im := c1.im - c2.im;
return cr;
end "-";
function "*"(c1, c2 : in Complexe) return Complexe is
cr : Complexe;
begin
cr.re := (c1.re * c2.re) - (c1.im * c2.im);
cr.im := (c1.re * c2.im) + (c2.re * c1.im);
return cr;
end "*";
function "/"(c1, c2 : in Complexe) return Complexe is
cr : Complexe;
begin
cr.re := ((c1.re * c2.re) + (c1.im * c2.im)) / ((c2.re * c2.re) + (c2.im * c2.im));
cr.im := ((c2.re * c1.im) - (c1.re * c2.im)) / ((c2.re * c2.re) + (c2.im * c2.im));
return cr;
end "/";
procedure Put(c1 : in Complexe) is
begin
put(c1.re); put(" ");
if c1.im < 0.0 then
put("-"); put(-c1.im); put(" i");
else
put("+"); put(c1.im); put(" i");
end if;
end Put; |
Et le petit programme qui va tester tout ca:
code: with text_io; use text_io;
with Ada.Text_IO, Ada.Float_Text_IO;use Ada.Text_IO, Ada.Float_Text_IO;
with OperationsComplexes; use OperationsComplexes;
procedure test is
c1, c2, cr : complexe;
begin
c1.re := 1.0;
c1.im := 2.0;
c2.re := 3.0;
c2.im := 4.0;
cr := c1 + c2;
put(cr);
new_line;
cr := c1 - c2;
put(cr);
new_line;
cr := c1 * c2;
put(cr);
new_line;
cr := c1 / c2;
put(cr);
new_line;
end test; |
[Edité le 14/03/2004 à 17:15 par ftbass] |
|