The Ada Program: main.adb
1 -- main.adb: read and write complex numbers in the form (x±yi)
2
3 with Ada.Text_IO, Ada.Float_Text_IO;
4 use Ada;
5
6 procedure Main is
7
8 type Complex_Number is record Real, Imag: Float; end record;
9
10 procedure Get (Item: out Complex_Number) is
11 C: Character;
12 begin
13 Text_IO.Get (C);
14 if (C /= '(') then
15 raise Text_IO.Data_Error;
16 end if;
17
18 Float_Text_IO.Get (Item.Real);
19 Float_Text_IO.Get (Item.Imag); -- this gets '+' or '-' also!
20
21 Text_IO.Get (C);
22 if (C /= 'i') then
23 raise Text_IO.Data_Error;
24 end if;
25 Text_IO.Get (C);
26 if (C /= ')') then
27 raise Text_IO.Data_Error;
28 end if;
29 end Get;
30
31 procedure Put (Item: in Complex_Number; Aft: in Natural := 2) is
32 begin
33 Text_IO.Put ('(');
34 Float_Text_IO.Put (Item.Real, Aft=>Aft, Exp=>0);
35 if (Item.Imag>=0.0) then Text_IO.Put ('+'); end if;
36 Float_Text_IO.Put (Item.Imag, Aft=>Aft, Exp=>0);
37 Text_IO.Put ("i)");
38 end Put;
39
40 X : Complex_Number;
41
42 begin
43 Get (X); Put (X); Text_IO.New_Line;
44 end Main;