Advertisement
RIck687

print.c

Apr 27th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. /*******************************************************************************
  2. print.c by Micah Wood, Michael Markowitz, Patrick Klein, and Christina Lew
  3. Purpose:
  4. Provide print function
  5. Note:
  6. Print Traversal is used for PRTSUCC subcommand and must be executed
  7. via recursion.
  8. *******************************************************************************/
  9. // If compiling using visual studio, tell the compiler not to give its warnings
  10. // about the safety of scanf and printf
  11. #define _CRT_SECURE_NO_WARNINGS 1
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdarg.h>
  16. #include <stdlib.h>
  17. #include "cs2123p6.h"
  18.  
  19. /******************** printAllInList *************************************
  20. void printAllInList(Graph graph)
  21. Purpose:
  22. Print the vertex subscript, max dist from source, course ID,
  23. course name, prereqs (max of 4), and successors for all courses
  24. If the course doesn't exist, show a warning.
  25. Parameters:
  26. I Graph graph
  27. Notes:
  28. No special assumptions were used in this function.
  29. **************************************************************************/
  30. void printAllInList(Graph graph)
  31. {
  32. printf("All Vertices In a List\n");
  33. int i;
  34.  
  35. // Print Headers
  36. printf("Vx TE %-8s %-22s %-31s Successors\n"
  37. , "Course"
  38. , "Name"
  39. , "Prereqs");
  40.  
  41. // For each vertex, prints via printOne function
  42. for(i = 1; i < graph->iNumVertices+1; i++)
  43. {
  44. printOne(graph, i);
  45. }
  46. }
  47.  
  48. /******************** printOne ********************************************
  49. void printOne(Graph graph, int iVertex)
  50. Purpose:
  51. Print the vertex subscript, max dist from source (this isn't set
  52. until the DOPLAN command is executed in Pgm #6, from now use 0),
  53. course ID, course name, prereqs (max of 4), and successors.
  54. If the course doesn't exist, show a warning.
  55. Parameters:
  56. I Graph graph
  57. I int iVertex
  58. Notes:
  59. PRTONE CS2123
  60. **************************************************************************/
  61. void printOne(Graph graph, int iVertex)
  62. {
  63. // Prints the vertex subscript, Course ID, and Course Name
  64. printf("%2d %-2d %-8s %-23s"
  65. , iVertex
  66. , graph->vertexM[iVertex].iDistSource
  67. , graph->vertexM[iVertex].szCourseId
  68. , graph->vertexM[iVertex].szCourseName);
  69.  
  70. int i;
  71. // Initialize an edgeNode that points to the prereqList
  72. EdgeNode *pTemp;
  73. pTemp = graph->vertexM[iVertex].prereqList;
  74.  
  75. // Loops through the prereqList a max of 4 times
  76. for(i = 0; i<4; i++)
  77. {
  78. if (pTemp != NULL)
  79. {
  80. printf("%-8s", graph->vertexM[pTemp->iPrereqVertex].szCourseId);
  81. pTemp = pTemp->pNextEdge;
  82. }
  83. else if(i!=0)
  84. // Prints "..." if there is nothing in the node
  85. {
  86. printf("%-8s", "...");
  87. }
  88. else
  89. {
  90. printf("%-8s", "-");
  91. }
  92. }
  93.  
  94. //print successor list
  95. pTemp = graph->vertexM[iVertex].successorList;
  96. int bEmpty = TRUE;
  97. while(pTemp != NULL)
  98. {
  99. printf("%-8s", graph->vertexM[pTemp->iSuccVertex].szCourseId);
  100. pTemp = pTemp->pNextEdge;
  101. bEmpty = FALSE;
  102. }
  103. //if the successor list was empty, print a '-'
  104. if (bEmpty == TRUE)
  105. printf("-");
  106.  
  107. printf("\n");
  108. }
  109.  
  110. /******************** printSources **********************************
  111. void printSources(Graph graph);
  112. Purpose:
  113. Print each of the courses (ID and name) of sources (i.e., have no prereq).
  114. Parameters:
  115. I Graph graph Graphs to go through to find sources.
  116. Notes:
  117. No special assumptions were made about this function.
  118. **************************************************************************/
  119. void printSources(Graph graph)
  120. {
  121. int i;
  122.  
  123. for (i = 1; i < graph->iNumVertices; i++)
  124. {
  125. if (graph->vertexM[i].prereqList == NULL)
  126. printf("%s %s\n"
  127. , graph->vertexM[i].szCourseId
  128. , graph->vertexM[i].szCourseName);
  129. }
  130. }
  131.  
  132. /******************** printSinks **********************************
  133. void printSinks(Graph graph)'
  134. Purpose:4
  135. Print each of the courses (ID and name) of sinks (i.e., no other courses have them as prereqs).
  136. Parameters:
  137. I Graph graph Graphs to go through to find sinks.
  138. Notes:
  139. No special assumptions were made about this function.
  140. **************************************************************************/
  141. void printSinks(Graph graph)
  142. {
  143. int i;
  144. for (i = 1; i < graph->iNumVertices; i++)
  145. {
  146. if (graph->vertexM[i].successorList == NULL)
  147. printf("%s %s\n"
  148. , graph->vertexM[i].szCourseId
  149. , graph->vertexM[i].szCourseName);
  150. }
  151. }
  152.  
  153. /******************** printTraversal **********************************
  154. void printTraversal(Graph graph, int iCourseVertex, int indent)
  155. Purpose:
  156. Print all the successors of the specified course in a depth first manner
  157. with indentation. For each course, print the course ID and course name.
  158. If the course doesn't exist, show a warning.
  159. Parameters:
  160. I Graph graph
  161. I int iCourseVertex
  162. I int indent
  163. Notes:
  164. No special assumptions were made about this function.
  165. **************************************************************************/
  166. void printTraversal(Graph graph, int iCourseVertex, int indent)
  167. {
  168. //base case
  169. if(graph->vertexM[iCourseVertex].successorList == NULL)
  170. return;
  171.  
  172.  
  173. //Temporary edgenode pointer to traverse
  174. EdgeNode *e;
  175. int i;
  176. for(e = graph->vertexM[iCourseVertex].successorList; e!= NULL; e = e->pNextEdge)
  177. {
  178. printf(" ");
  179. //insert indentation and print
  180. for(i=0; i<indent; i++)
  181. printf(" ");
  182. printf("%s %s\n"
  183. , graph->vertexM[e->iSuccVertex].szCourseId
  184. , graph->vertexM[e->iSuccVertex].szCourseName);
  185. printTraversal(graph, e->iSuccVertex, indent+1);
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement