source: doc/theses/fangren_yu_MMath/test.adb@ 96a11655

Last change on this file since 96a11655 was f5bf3c2, checked in by Peter A. Buhr <pabuhr@…>, 8 months ago

more proofreading on introduction chapter

  • Property mode set to 100644
File size: 2.4 KB
Line 
1with Ada.Text_IO; use Ada.Text_IO;
2with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
3
4procedure test is
5 Function Random return Integer is begin return 3; end;
6 Function Random return Float is begin return 3.5; end;
7 Function Random return Unbounded_String is begin return To_Unbounded_String( "abc" ); end;
8
9 Procedure Print( V : Integer ) is begin Put_Line( Integer'Image(V) ); end;
10 Procedure Print( V : Float ) is begin Put_Line( Float'Image(V) ); end;
11 Procedure Print( V : Unbounded_String ) is begin Put_Line( Ada.Strings.Unbounded.To_String(V) ); end;
12
13 Function Func( V : Integer ) return Integer is begin return V; end;
14 Function Func( V : Float ) return Float is begin return V; end;
15 Function Func( V : Unbounded_String ) return Unbounded_String is begin return V; end;
16 Function Func( V1 : Integer; V2 : Float ) return Float is begin return Float(V1) + V2; end;
17
18 Function "-"( L, R : Integer ) return Integer is begin return L + (-R); end; -- prevent infinite recusion
19
20 i : Integer;
21 f : Float;
22 s : Unbounded_String;
23
24 Type Complex is
25 record
26 Re, Im : Float;
27 end record;
28 c : Complex := (Re => 1.0, Im => 1.0);
29 Procedure Grind (X : Complex) is begin Put_Line( "Grind1" ); end;
30 Procedure Grind (X : Unbounded_String) is begin Put_Line( "Grind2" ); end;
31
32 generic
33 type T is private;
34 with function "+"( X, Y: T ) return T;
35 function twice(X : T) return T;
36
37 function twice( X: T ) return T is
38 begin
39 Put_Line( "XXX" ); return X + X; -- The formal operator "*".
40 end twice;
41
42 function Int_Twice is new Twice( Integer, "+" => "+" );
43 function float_Twice is new Twice( float, "+" => "+" );
44
45 -- generic units cannot be overloaded
46 -- generic
47 -- type T is private;
48 -- with function "+"( X, Y: T ) return T;
49 -- function twice( X : T; Y : T ) return T;
50 --
51 -- function twice( X: T; Y : T ) return T is
52 -- begin
53 -- Put_Line( "XXX" ); return X + X; -- The formal operator "*".
54 -- end twice;
55begin
56 i := Random;
57 Print( i );
58 f := Random;
59 Print( f );
60 s := Random;
61 Print( s );
62
63 Print( Func( 7 ) );
64 Print( Func( 7.5 ) );
65 Print( Func( To_Unbounded_String( "abc" ) ) );
66 Print( Func( 3, 3.5 ) );
67
68 Grind( X => (Re => 1.0, Im => 1.0) );
69 Grind( c );
70 Grind( To_Unbounded_String( "abc" ) );
71
72 i := Int_Twice( 2 );
73 Put_Line( Integer'Image(i) );
74 f := float_Twice( 2.5 );
75 Print( f );
76end test;
77
78-- Local Variables: --
79-- tab-width: 4 --
80-- compile-command: "gnatmake test.adb" --
81-- End: --
Note: See TracBrowser for help on using the repository browser.