You are on page 1of 8

The Task

A cell phone company is continually building and leasing communication towers. Each tower has one-
way, direct communication links to other towers. The task is find if there is a communication link from
one tower to another, possibly through other towers.

The input to the program is essentially a list of pairs of tower names. Each pair will appear on a single
line and will contain distinct names. Some pairs represent one-way communication links between
towers, and other pairs represent queries. Links and queries may be interspersed. – REMOVE LINKS????

All the communication links of the system are added one link at a time and no links are removed. Here is
an example of a link:
Tower_A Tower_B.

A link is terminated by a period.

A query asks if a communication link is possible from one tower to another by any combination of one-
way links. If another link is added to the system later in the input, then the answer to the same query
may be different. In the output, a plus sign (+) represents an affirmative answer; a minus sign (-) says
there is no such link.

Links are distinguished from queries by ending in a question mark and not a period. Tower_A Tower_B?

Input/Output

The program should read from the standard input and and write to the standard output. (Do not call exit
because it may be interpreted as abnormal program termination.)

For each query in the input there should be exactly one line of output. If the query is true (there is a
communication channel through the network), the output line should begin with a plus sign (+);
otherwise with a minus sign (-). The rest of the output line repeats the towers in the query.

For the following input:


Tower_A Tower_B.
Tower_B Tower_C .
Tower_A Tower_C? # A query Tower_B Tower_D.
Tower_A Tower_D ? # Another query Tower_F Tower_E.
Tower_D Tower_B ? # A third query xxxx yyyy? # Unknown tower names Tower_D
Tower_B .
Tower_D Tower_B ? # Now there is a link the output should be
+ Tower_A => Tower_C
+ Tower_A => Tower_D – Tower_D => Tower_B
- Xxxx => yyyy

+ Tower_D => Tower_B

The names of the towers will contain only the characters [a-zA-Z_0-9], in particular they will have no
spaces in them. Names will have at least 1 character and no more than 50 characters. Furthermore the
capitalization of the names is significant. That is, WestMelbourneTower is not to be considered the same
tower as

wESTMeLbOuRnEtOwEr. Characters, if any, after the period (.) or question mark (?) on a line are to be
ignored. No line will will contain more than 150 characters.

Please carefully observe spacing. There may, or may not, be a space before the period, the question
mark, and the comment in the input. There will be one or more spaces between the tower names in the
input. There must be a space after the plus and minus sign in the output. There must be a space before
and after the arrow (=>) in the output.

You are required to create your own graph package graph.ads graph.adb and name it Graph. The
package can be generic or non-generic. The graph is to be a list of adjacency lists. You are required to
use the generic, doublylinked list package from the standard library. These requirements are generally
reasonable ones for the task anyway. There are more sophisticated approaches and less sophisticated
approaches, but these requirements ensure the educational objectives of the assignment are met.

Example Ada Program

Here is an example of compiling and running an Ada program from the command line:
$ gnatmake -gnaty copy gcc -c -gnaty copy.adb gnatbind -x copy.ali gnatlink
copy.ali
$ ./copy.exe < copy.adb
– copy.adb: copy standard input to output numbering lines
with Text_IO, Ada.Integer_Text_IO;
use Ada;
procedure Copy is
Line : String (1 .. 100);
Length : Integer;
Num : Integer := 0;
begin
while not (Text_IO.End_Of_File) loop
Text_IO.Get_Line (Line, Length);
Num := Num + 1;
Integer_Text_IO.Put (Item => Num, Width => 4);
Text_IO.Put (Item => “ “ & Line (1 .. Length));
Text_IO.New_Line;
end loop;
end Copy;

Answer:
Hear.abd:
with Text_IO, Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
use Ada;
procedure Hear is
--arrays
type towerAa is array(1..100000) of Unbounded_String;
type towerBb is array(1..100000) of Unbounded_String;
towerA: towerAa;
towerB: towerBb;
--Variables
line : String (1 .. 100);
length : Integer;
num : Integer := 0;
numArray : Integer := 1;
firstStart : Integer := 0;
spacePos : Integer := 0;
secondStart: Integer := 0;
lastChar : Integer := 0;
--function to check if there is link
function CheckFunc (tower1, tower2 : String) return
integer is
begin
--Loop number of elements in link tower array A
for TA in 1..NumArray loop
--If the tower1 is exists in links towers array A
if tower1 = towerA(TA) then
if tower A exists in tower A array and tower B exists in
tower B array, then there is a direct link and return 1
if towerB(TA) = tower2 then
return 1;
end if;
--Return TA if there is no direct link
return TA;
end if;
end loop;
--If the loop ends without direct or undirect like then
return 0
return 0;
end CheckFunc;
begin
--Loop number of lines in the file
while not (Text_IO.End_Of_File) loop
--Get the line and the length of it
text_IO.Get_Line (Line, Length);
num := Num + 1;
--Get the start position of the first char
For x in 1 .. Length loop
if Line ((x) .. x) /= “ “ then
firstStart := x;
exit;
end if;
end loop;
--Get the position of the first space between towers
for x in FirstStart .. Length loop
if Line ((x) .. x) = “ “ then
spacePos := x;
exit;
end if;
end loop;

--Get the start position of the second tower name


for x in SpacePos .. Length loop
if Line ((x) .. x) /= “ “ then
secondStart := x;
exit;
end if;
end loop;
--Get the last char position
for x in SecondStart .. Length loop
if Line ((x) .. x) = “.” Or Line
((x) .. x) = “?” then
lsstChar := x;
exit;
end if;
end loop;
--If the line end with ., then add the twers
link to the list
if Line (LastChar .. LastChar) = “.” Then
--Add towers names to the new position in the arrays to be a
new link
towerA(NumArray):=To_Unbounded_String(Line (FirstStart ..
SpacePos));
towerB(NumArray):=To_Unbounded_String(Line
(SecondStart .. LastChar-1));
--Add one to the length of links arrays
numArray := numArray + 1;
end if;
--If the line ends with ?, then check if there is
link
if Line (LastChar .. LastChar) = “?” then
--If the check fun return 1, there is a like
if CheckFunc (Line (FirstStart .. SpacePos), Line
(SecondStart .. LastChar-1)) = 1 then
--Print + if there is a link
text_IO.Put(“+ “);

end if;
--If the check function return 0, then there is
no link
if CheckFunc (Line (FirstStart .. SpacePos), Line
(SecondStart .. LastChar-1)) = 0 then
--Print – if there is no link
text_IO.Put(“- “);
end if;
--Print the rest of line
text_IO.Put(Line (FirstStart .. SpacePos));
text_IO.Put(“ => “);
text_IO.Put(Line (SecondStart .. LastChar-1));
text_IO.New_Line;
end if;
end loop;
end Hear;

Graph. Ads:
package Graph is
function CheckFunc (tower1, tower2 : String) return Integer;
end Graph;
Graph. Adb:
with Text_IO, Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
use Ada;
package body Graph is
--function to check if there is link
function CheckFunc (tower1, tower2 : String) return
Integer is
begin
text_IO.Put(“ => “);
--the loop ends without direct or undirect like then return
0
return 0;
end CheckFunc;
end Graph;

You might also like