Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH ada.text_IO; USE ada.text_IO;
- WITH ada.Integer_Text_IO; USE ada.integer_text_IO;
- --------------------------------------------------------------------------------
- --| Assignment 3
- --| Zachary
- --| NOTES:
- --| This program takes a text file and creates an adjacency array.
- --| Then loops through and applys Appel and Hakens 4 color theorum.
- --| Then each element and its color is output to a text file/screen.
- --------------------------------------------------------------------------------
- PROCEDURE Assignment_3 IS
- --TEXT FILE VARIABLES
- inputFile : File_Type;
- inputName : string(1..20);
- outputFile : File_Type;
- outputName : string(1..20);
- Last : integer;
- inNum : integer;
- array_Length : integer := 0;
- --DEFINE POSSIBLE COLORS
- TYPE ColorTypes IS (green, blue, red, orange, empty);
- PACKAGE Color_IO IS NEW Enumeration_IO (enum => ColorTypes);
- USE Color_IO;
- --DEFINITION OF GRAPH NODES
- TYPE graphNode;
- TYPE graphPtr IS ACCESS graphNode;
- TYPE graphNode IS RECORD
- Element : integer;
- color : colorTypes;
- next : graphPtr;
- END RECORD;
- --DEFINE RECORD TO USE IN ADJACENCY ARRAY
- TYPE adj_Record IS RECORD
- next : graphPtr;
- END RECORD;
- --DEFINE ADJACENCY LIST ARRAY
- SUBTYPE index IS positive RANGE 1..100;
- TYPE adjacency_List IS ARRAY (index) OF adj_Record;
- List : adjacency_List;
- --DEFINE PROCEDURE TO DETERMINE IF A COLOR IS ADJACENT
- --PRE: Takes a list and boolean color values
- --POST: Passes back all the colors used in the list as "true"
- PROCEDURE isAdjacent
- (List : IN graphPtr; Blu, Grn, Rd, Org : IN OUT Boolean) IS
- Curr : graphPtr := List;
- BEGIN
- WHILE Curr /= NULL LOOP
- IF Curr.Color = blue THEN
- Blu := True;
- ELSIF Curr.Color = green THEN
- Grn := True;
- ELSIF Curr.Color = red THEN
- Rd := True;
- ELSIF Curr.Color = orange THEN
- Org := True;
- END IF;
- Curr := Curr.Next;
- END LOOP;
- END isAdjacent;
- --DEFINE COLORING NODES HELPER FUNCTION
- --PRE: takes in a list, element, color
- --POST: returns the list with all nodes of "element" colored
- PROCEDURE colorAll
- (L : IN OUT adjacency_List; E : IN integer; LengthL : IN integer; Color : IN ColorTypes) IS
- tempPtr : graphPtr;
- BEGIN
- FOR i IN 1..lengthL LOOP
- tempPtr := L(i).next;
- WHILE tempPtr /= NULL LOOP
- IF tempPtr.Element = E THEN
- tempPtr.Color := Color;
- tempPtr := tempPtr.Next;
- ELSE
- tempPtr := TempPtr.Next;
- END IF;
- END LOOP;
- END LOOP;
- END colorAll;
- --DEFINE PROCEDURE TO ASSIGN COLORS TO EACH NODE IN A GRAPH
- --PRE: Takes in a graph
- --POST: Returns graph such that no two nodes have the same color
- PROCEDURE assignColors
- (Graph : IN OUT adjacency_List; Length : IN integer) IS
- Blu : Boolean := False;
- Grn : Boolean := False;
- Rd : Boolean := False;
- Org : Boolean := False;
- CurrentL : graphPtr;
- TempPtr : graphPtr;
- Tempcolor : ColorTypes := empty;
- BEGIN
- FOR i IN 1..array_length LOOP
- TempPtr := Graph(i).Next;
- currentL := Graph(i).Next;
- Blu := False;
- Grn := False;
- Rd := False;
- Org := False;
- IF tempPtr.Color = empty THEN
- isAdjacent(List => currentL,
- Blu => Blu,
- Grn => Grn,
- Rd => Rd,
- Org => Org);
- IF Blu = False THEN
- tempPtr.Color := Blue;
- tempColor := Blue;
- ELSIF Grn = False THEN
- tempptr.Color := Green;
- tempColor := Green;
- ELSIF Rd = False THEN
- tempptr.Color := Red;
- tempColor := red;
- ELSE
- tempptr.Color := Orange;
- tempColor := Orange;
- END IF;
- ColorALL(L => Graph,
- E => tempptr.element,
- LengthL => length,
- Color => tempColor);
- ELSE
- TempPtr := TempPtr.Next;
- END IF;
- tempColor := empty;
- END LOOP;
- END assignColors;
- --DEFINE PROCEDURE TO ADD A NODE
- --PRE: Takes in a graph, a node to be added
- --POST: Passes out a graph(adj list) with the added node.
- PROCEDURE insertNode (G : IN OUT graphPtr; E : IN integer) IS
- P : graphPtr := G;
- BEGIN
- IF P = NULL THEN
- P := NEW graphNode'(Element => E, Color => empty, Next => NULL);
- ELSE
- insertNode(G => P.next, E => E);
- END IF;
- G := P;
- END insertNode;
- temp : integer := 0;
- tempPtr : graphPtr := NULL;
- outPtr : graphPtr;
- BEGIN --------*** Assignment 3 ***----------
- --PROMPT FOR INPUT FILE
- Put("Enter Adjacency List File Name: ");
- Get_Line(Item => inputName, Last => Last);
- Open (File => inputFile, mode => ada.Text_IO.In_File, Name => inputName(1..Last));
- New_Line;
- --READS EACH LINE AND BUILD THE GRAPH
- WHILE End_Of_File(File => inputFile) = False LOOP
- --RETRIEVE FIRST ELEMENT IN THE LINE
- TempPtr := Null;
- Get (Item => inNum, file => inputFile);
- --ADD NODE
- insertNode(G => TempPtr, E => inNum);
- List(inNum).Next := TempPtr;
- WHILE End_Of_Line(File => inputFile) = False LOOP
- --RETRIEVE THE REST OF THE ELEMENTS IN THE LINE
- Get (Item => temp, file => inputFile);
- TempPtr := List(inNum).Next;
- --ADD NODE
- insertNode(G => TempPtr, E => temp);
- List(inNum).Next := TempPtr;
- END LOOP;
- --INCRIMENT "LENGTH OF ARRAY"
- array_Length := array_Length + 1;
- Skip_Line(File => inputFile);
- END LOOP;
- --CLOSE INPUT FILE
- close(File => inputFile);
- --OUTPUT NUBER OF NODES IN THE GRAPH
- put("# Nodes: ");
- ada.Integer_Text_IO.put(array_length,Width => 3);
- New_Line(2);
- --PROMPT FOR OUTPUT FILE
- Put("Enter Output File: ");
- Get_Line(Item => outputName, Last => Last);
- Open (File => outputFile, mode => ada.Text_IO.Out_File, Name => outputName(1..Last));
- New_Line;
- --ASSIGN COLORS TO THE GRAPH
- assignColors(Graph => List,
- Length => array_Length);
- --WRITE FULL ADJ LIST TO SCREEN
- put("Adjacency List:");
- new_line;
- FOR i IN 1..array_length LOOP
- outPtr := List(i).next;
- LOOP
- put(integer'image(outptr.element));
- put(item => outptr.color);
- IF i <= 9 THEN
- put(" ");
- ELSE
- put(" ");
- END IF;
- IF outPtr.Next /= NULL THEN
- outPtr := outPtr.Next;
- ELSE
- EXIT;
- END IF;
- END LOOP;
- New_Line;
- END LOOP;
- New_Line;
- --WRITE NODES AND ASSIGNED COLORS TO THE TEXT FILE/SCREEN
- put("Color Assignments");
- New_Line;
- FOR i IN 1..Array_Length LOOP
- --OUTPUT TO SCREEN
- put(integer'image(List(i).Next.element));
- IF i <= 9 THEN
- put(" ");
- ELSE
- put(" ");
- END IF;
- put(List(i).Next.Color);
- New_Line;
- --OUTPUT TO TEXT FILE
- put(Item => integer'image(List(i).Next.element), File => outputFile);
- IF i < 9 THEN
- put(Item => " ", File => outputFile);
- ELSE
- put(Item => " ", File => outputFile);
- END IF;
- put(item => List(i).Next.color, File => outputFile);
- New_Line(File => outputFile);
- END LOOP;
- --CLOSE OUTPUT FILE
- close(File => outputFile);
- new_line;
- --NOTIFY COLORS ASSIGNMENT COMPLETE AND OUTPUT FILE COMPLETED
- put("Colors assigned and written to output file.");
- New_Line(2);
- --NOTIFY USER OF SUCCESSFUL COMPLETION.
- Put("END OF PROGRAM.");
- New_Line(2);
- END Assignment_3;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement