Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- print.c by Micah Wood, Michael Markowitz, Patrick Klein, and Christina Lew
- Purpose:
- Provide print function
- Note:
- Print Traversal is used for PRTSUCC subcommand and must be executed
- via recursion.
- *******************************************************************************/
- // If compiling using visual studio, tell the compiler not to give its warnings
- // about the safety of scanf and printf
- #define _CRT_SECURE_NO_WARNINGS 1
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include "cs2123p6.h"
- /******************** printAllInList *************************************
- void printAllInList(Graph graph)
- Purpose:
- Print the vertex subscript, max dist from source, course ID,
- course name, prereqs (max of 4), and successors for all courses
- If the course doesn't exist, show a warning.
- Parameters:
- I Graph graph
- Notes:
- No special assumptions were used in this function.
- **************************************************************************/
- void printAllInList(Graph graph)
- {
- printf("All Vertices In a List\n");
- int i;
- // Print Headers
- printf("Vx TE %-8s %-22s %-31s Successors\n"
- , "Course"
- , "Name"
- , "Prereqs");
- // For each vertex, prints via printOne function
- for(i = 1; i < graph->iNumVertices+1; i++)
- {
- printOne(graph, i);
- }
- }
- /******************** printOne ********************************************
- void printOne(Graph graph, int iVertex)
- Purpose:
- Print the vertex subscript, max dist from source (this isn't set
- until the DOPLAN command is executed in Pgm #6, from now use 0),
- course ID, course name, prereqs (max of 4), and successors.
- If the course doesn't exist, show a warning.
- Parameters:
- I Graph graph
- I int iVertex
- Notes:
- PRTONE CS2123
- **************************************************************************/
- void printOne(Graph graph, int iVertex)
- {
- // Prints the vertex subscript, Course ID, and Course Name
- printf("%2d %-2d %-8s %-23s"
- , iVertex
- , graph->vertexM[iVertex].iDistSource
- , graph->vertexM[iVertex].szCourseId
- , graph->vertexM[iVertex].szCourseName);
- int i;
- // Initialize an edgeNode that points to the prereqList
- EdgeNode *pTemp;
- pTemp = graph->vertexM[iVertex].prereqList;
- // Loops through the prereqList a max of 4 times
- for(i = 0; i<4; i++)
- {
- if (pTemp != NULL)
- {
- printf("%-8s", graph->vertexM[pTemp->iPrereqVertex].szCourseId);
- pTemp = pTemp->pNextEdge;
- }
- else if(i!=0)
- // Prints "..." if there is nothing in the node
- {
- printf("%-8s", "...");
- }
- else
- {
- printf("%-8s", "-");
- }
- }
- //print successor list
- pTemp = graph->vertexM[iVertex].successorList;
- int bEmpty = TRUE;
- while(pTemp != NULL)
- {
- printf("%-8s", graph->vertexM[pTemp->iSuccVertex].szCourseId);
- pTemp = pTemp->pNextEdge;
- bEmpty = FALSE;
- }
- //if the successor list was empty, print a '-'
- if (bEmpty == TRUE)
- printf("-");
- printf("\n");
- }
- /******************** printSources **********************************
- void printSources(Graph graph);
- Purpose:
- Print each of the courses (ID and name) of sources (i.e., have no prereq).
- Parameters:
- I Graph graph Graphs to go through to find sources.
- Notes:
- No special assumptions were made about this function.
- **************************************************************************/
- void printSources(Graph graph)
- {
- int i;
- for (i = 1; i < graph->iNumVertices; i++)
- {
- if (graph->vertexM[i].prereqList == NULL)
- printf("%s %s\n"
- , graph->vertexM[i].szCourseId
- , graph->vertexM[i].szCourseName);
- }
- }
- /******************** printSinks **********************************
- void printSinks(Graph graph)'
- Purpose:4
- Print each of the courses (ID and name) of sinks (i.e., no other courses have them as prereqs).
- Parameters:
- I Graph graph Graphs to go through to find sinks.
- Notes:
- No special assumptions were made about this function.
- **************************************************************************/
- void printSinks(Graph graph)
- {
- int i;
- for (i = 1; i < graph->iNumVertices; i++)
- {
- if (graph->vertexM[i].successorList == NULL)
- printf("%s %s\n"
- , graph->vertexM[i].szCourseId
- , graph->vertexM[i].szCourseName);
- }
- }
- /******************** printTraversal **********************************
- void printTraversal(Graph graph, int iCourseVertex, int indent)
- Purpose:
- Print all the successors of the specified course in a depth first manner
- with indentation. For each course, print the course ID and course name.
- If the course doesn't exist, show a warning.
- Parameters:
- I Graph graph
- I int iCourseVertex
- I int indent
- Notes:
- No special assumptions were made about this function.
- **************************************************************************/
- void printTraversal(Graph graph, int iCourseVertex, int indent)
- {
- //base case
- if(graph->vertexM[iCourseVertex].successorList == NULL)
- return;
- //Temporary edgenode pointer to traverse
- EdgeNode *e;
- int i;
- for(e = graph->vertexM[iCourseVertex].successorList; e!= NULL; e = e->pNextEdge)
- {
- printf(" ");
- //insert indentation and print
- for(i=0; i<indent; i++)
- printf(" ");
- printf("%s %s\n"
- , graph->vertexM[e->iSuccVertex].szCourseId
- , graph->vertexM[e->iSuccVertex].szCourseName);
- printTraversal(graph, e->iSuccVertex, indent+1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement