Skip to content

Commit 7da6bd6

Browse files
Update overloading lab to not use console input
1 parent 1e154a7 commit 7da6bd6

File tree

4 files changed

+237
-174
lines changed

4 files changed

+237
-174
lines changed

courses/ada_essentials/090_overloading/88-overloading.lab.rst

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,36 @@ Overloading Lab
88

99
* Requirements
1010

11-
- Create multiple functions named "Convert" to convert between digits and text representation
11+
- Create multiple functions named :ada:`Convert` to convert between a character digits and its name
1212

13-
+ One routine should take a digit and return the text version (e.g. **3** would return **three**)
13+
+ One routine should take a digit and return the name (e.g. **'3'** would return **three**)
1414

15-
+ One routine should take text and return the digit (e.g. **two** would return **2**)
15+
+ One routine should take the name and return the digit (e.g. **two** would return **'2'**)
1616

17-
- Query the user to enter text or a digit and print its equivalent
18-
- If the user enters consecutive entries that are equivalent, print a message
17+
+ Hint: enumerals for the name will be easier than dealing with strings
1918

20-
+ e.g. **4** followed by **four** should get the message
19+
- Create overloaded addition functions that will add any combination of the two (digit and name)
2120

22-
* Hints
21+
+ The result can be an integer (i.e. dont worry about converting the result of :ada:`'7' + eight`)
2322

24-
- You can use enumerals for the text representation
23+
+ Hint: It might be easier to convert one to the other before adding!
2524

26-
+ Then use *'Image* / *'Value* where needed
27-
28-
- Use an equivalence function to compare different types
25+
- The prompt has four equations in the comments - use those to prove your code works
2926

3027
-------------------------------------------------
3128
Overloading Lab Solution - Conversion Functions
3229
-------------------------------------------------
3330

34-
.. container:: source_include 090_overloading/lab/overloading/answer/main.adb :start-after:--Conversion_Functions :end-before:--Conversion_Functions :code:Ada :number-lines:4
31+
.. container:: source_include 090_overloading/lab/overloading/answer/main.adb :start-after:conversions_begin :end-before:conversions_end :code:Ada :number-lines:5
32+
33+
--------------------------------------
34+
Overloading Lab Solution - Operators
35+
--------------------------------------
36+
37+
.. container:: source_include 090_overloading/lab/overloading/answer/main.adb :start-after:operators_begin :end-before:operators_end :code:Ada :number-lines:32
3538

3639
-------------------------------------------------
3740
Overloading Lab Solution - Main
3841
-------------------------------------------------
3942

40-
.. container:: source_include 090_overloading/lab/overloading/answer/main.adb :start-after:--Main :end-before:--Main :code:Ada :number-lines:40
43+
.. container:: source_include 090_overloading/lab/overloading/answer/main.adb :start-after:main_begin :end-before:main_end :code:Ada :number-lines:79
Lines changed: 128 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,153 @@
11
with Ada.Text_IO; use Ada.Text_IO;
22
procedure Main is
33

4-
type Digit_T is range 0 .. 9;
4+
--|conversions_begin
5+
subtype Digit_T is Character range '0' .. '9';
56
type Digit_Name_T is
67
(Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine);
78

8-
function Convert (Value : Digit_Name_T) return Digit_T;
9-
function Convert (Value : Character) return Digit_Name_T;
10-
function Convert (Value : String) return Digit_T;
9+
function Convert
10+
(Value : Digit_T)
11+
return Digit_Name_T;
12+
function Convert
13+
(Value : Digit_Name_T)
14+
return Digit_T;
15+
function Convert
16+
(Value : Digit_Name_T)
17+
return Integer;
1118

12-
function "=" (L : Digit_Name_T; R : Digit_T) return Boolean is
13-
begin
14-
return Convert (L) = R;
15-
end "=";
16-
17-
function Convert (Value : Digit_Name_T) return Digit_T is
19+
function Convert
20+
(Value : Digit_T)
21+
return Digit_Name_T is
1822
begin
1923
case Value is
20-
when Zero => return 0;
21-
when One => return 1;
22-
when Two => return 2;
23-
when Three => return 3;
24-
when Four => return 4;
25-
when Five => return 5;
26-
when Six => return 6;
27-
when Seven => return 7;
28-
when Eight => return 8;
29-
when Nine => return 9;
24+
when '0' =>
25+
return Zero;
26+
when '1' =>
27+
return One;
28+
when '2' =>
29+
return Two;
30+
when '3' =>
31+
return Three;
32+
when '4' =>
33+
return Four;
34+
when '5' =>
35+
return Five;
36+
when '6' =>
37+
return Six;
38+
when '7' =>
39+
return Seven;
40+
when '8' =>
41+
return Eight;
42+
when '9' =>
43+
return Nine;
3044
end case;
3145
end Convert;
3246

33-
function Convert (Value : Character) return Digit_Name_T is
47+
function Convert
48+
(Value : Digit_Name_T)
49+
return Digit_T is
3450
begin
3551
case Value is
36-
when '0' => return Zero;
37-
when '1' => return One;
38-
when '2' => return Two;
39-
when '3' => return Three;
40-
when '4' => return Four;
41-
when '5' => return Five;
42-
when '6' => return Six;
43-
when '7' => return Seven;
44-
when '8' => return Eight;
45-
when '9' => return Nine;
46-
when others => return Zero;
52+
when Zero =>
53+
return '0';
54+
when One =>
55+
return '1';
56+
when Two =>
57+
return '2';
58+
when Three =>
59+
return '3';
60+
when Four =>
61+
return '4';
62+
when Five =>
63+
return '5';
64+
when Six =>
65+
return '6';
66+
when Seven =>
67+
return '7';
68+
when Eight =>
69+
return '8';
70+
when Nine =>
71+
return '9';
4772
end case;
4873
end Convert;
4974

50-
function Convert (Value : String) return Digit_T is
75+
function Convert
76+
(Value : Digit_Name_T)
77+
return Integer is
5178
begin
52-
return Convert (Digit_Name_T'Value (Value));
79+
return Digit_Name_T'Pos (Value);
5380
end Convert;
81+
--|conversions_end
82+
83+
--|operators_begin
84+
function "+"
85+
(Left : Digit_T;
86+
Right : Digit_Name_T)
87+
return Integer;
88+
function "+"
89+
(Left : Digit_Name_T;
90+
Right : Digit_T)
91+
return Integer;
92+
function "+"
93+
(Left : Digit_T;
94+
Right : Digit_T)
95+
return Integer;
96+
function "+"
97+
(Left : Digit_Name_T;
98+
Right : Digit_Name_T)
99+
return Integer;
100+
101+
function "+"
102+
(Left : Digit_T;
103+
Right : Digit_Name_T)
104+
return Integer is
105+
L : constant Digit_Name_T := Convert (Left);
106+
begin
107+
return L + Right;
108+
end "+";
109+
110+
function "+"
111+
(Left : Digit_Name_T;
112+
Right : Digit_T)
113+
return Integer is
114+
Sum : constant Integer := Convert (Left) + Convert (Right);
115+
begin
116+
return Sum;
117+
end "+";
54118

55-
function Get_Line return String is
56-
S : String (1 .. 100);
57-
L : Integer;
119+
function "+"
120+
(Left : Digit_T;
121+
Right : Digit_T)
122+
return Integer is
123+
L : constant Digit_Name_T := Convert (Left);
124+
R : constant Digit_Name_T := Convert (Right);
58125
begin
59-
Get_Line (S, L);
60-
return S (1 .. L);
61-
end Get_Line;
126+
return L + R;
127+
end "+";
62128

63-
Last_Entry : Digit_T := 0;
129+
function "+"
130+
(Left : Digit_Name_T;
131+
Right : Digit_Name_T)
132+
return Integer is
133+
begin
134+
return Integer'(Convert (Left)) + Integer'(Convert (Right));
135+
end "+";
136+
--|operators_end
64137

138+
--|main_begin
65139
begin
66-
loop
67-
Put ("Input: ");
68-
declare
69-
Str : constant String := Get_Line;
70-
begin
71-
exit when Str'Length = 0;
72-
if Str (Str'First) in '0' .. '9' then
73-
declare
74-
Converted : constant Digit_Name_T := Convert (Str (Str'First));
75-
begin
76-
Put (Digit_Name_T'Image (Converted));
77-
if Converted = Last_Entry then
78-
Put_Line (" - same as previous");
79-
else
80-
Last_Entry := Convert (Converted);
81-
New_Line;
82-
end if;
83-
end;
84-
else
85-
declare
86-
Converted : constant Digit_T := Convert (Str);
87-
begin
88-
Put (Digit_T'Image (Converted));
89-
if Converted = Last_Entry then
90-
Put_Line (" - same as previous");
91-
else
92-
Last_Entry := Converted;
93-
New_Line;
94-
end if;
95-
end;
96-
end if;
97-
end;
98-
end loop;
140+
141+
-- One + 2
142+
Put_Line (Integer'Image (One + '2'));
143+
144+
-- 3 + Four
145+
Put_Line (Integer'Image ('3' + Four));
146+
147+
-- Five + Six
148+
Put_Line (Integer'Image (Five + Six));
149+
150+
-- 7 + 8
151+
Put_Line (Integer'Image ('7' + '8'));
99152
end Main;
153+
--|main_end

0 commit comments

Comments
 (0)