Advertisement
RIck687

cs2123p6.h

Apr 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. /**********************************************************************
  2. cs2123p6.h
  3. Purpose:
  4. Defines constants:
  5. max constants
  6. error constants
  7. warning constants
  8. boolean constants
  9. Defines typedef for
  10. EdgeNode - graph edge containing preq and successor vertex
  11. Vertex - contains course information (course number, name), existence boolean,
  12. successor list first node pointer, predecessor list first node pointer
  13. GraphImp - array of vertices and a count of them
  14. Graph - pointer to an allocated GraphImp
  15. PlanImp - tbd
  16. Defines function prototypes for functions used in pgm5 (recursive and non-recursive)
  17. Defines function prototypes for functions used in pgm6
  18. Defines WARNING macro
  19. Notes:
  20.  
  21. **********************************************************************/
  22. /*** constants ***/
  23. #define MAX_TOKEN 50 // Maximum number of actual characters for a token
  24. #define MAX_LINE_SIZE 100 // Maximum number of character per input line
  25. #define MAX_VERTICES 60
  26. #define OVERFLOW_BEGIN 29 // begin of overflow area
  27. // Error constants (program exit values)
  28. #define ERR_COMMAND_LINE 900 // invalid command line argument
  29. #define ERR_ALGORITHM 903 // Unexpected error in algorithm
  30.  
  31. #define ERR_TOO_MANY_COURSE 1 // Too many courses
  32. #define ERR_BAD_COURSE 3 // Bad Course Data
  33. #define ERR_BAD_PREREQ 4 // Bad Prereq Data
  34. #define ERR_MISSING_SWITCH "missing switch"
  35. #define ERR_EXPECTED_SWITCH "expected switch, found"
  36. #define ERR_MISSING_ARGUMENT "missing argument for"
  37.  
  38. // exitUsage control
  39. #define USAGE_ONLY 0 // user only requested usage information
  40. #define USAGE_ERR -1 // usage error, show message and usage information
  41.  
  42. // boolean constants
  43. #define FALSE 0
  44. #define TRUE 1
  45.  
  46. // EdgeNode represents one edge in a graph
  47. typedef struct EdgeNode
  48. {
  49. int iPrereqVertex; // prereq
  50. int iSuccVertex; // successor
  51. struct EdgeNode *pNextEdge; // points to next edge
  52. } EdgeNode;
  53.  
  54.  
  55. typedef struct Vertex
  56. {
  57. char szCourseId[8]; // Course Identifier
  58. char szCourseName[21]; // Course Full Name
  59. char szDept[4]; // Department (e.g., CS, MAT)
  60. int bExists; // pgm6 DELETE command causes this to be set to TRUE
  61. // TRUE - this vertex exists, FALSE - deleted
  62. EdgeNode * prereqList;
  63. EdgeNode * successorList;
  64. int iSemesterLevel; // Which semester should this be taken
  65. int iHashChainNext; // pgm 6 extra credit
  66. int iDistSource; // Distance from a source
  67. } Vertex;
  68.  
  69. // GraphImp of a double adjacency list graph
  70. typedef struct
  71. {
  72. int iNumVertices; // Number of vertices in the vertexM array
  73. Vertex vertexM[MAX_VERTICES]; // Array of vertices
  74. int iOverflowBegin; // The subscript of the first overflow entry
  75. // in the array of vertices.
  76. // Any subscript less than this value is in
  77. // the primary area.
  78. int iFreeHead; // Subscript of a free list of entries
  79. // in the overflow portion of the
  80. // graph's vertexM array
  81. } GraphImp;
  82.  
  83. typedef GraphImp *Graph;
  84.  
  85. // Degree Plan
  86.  
  87. typedef struct
  88. {
  89. int semesterM[5][MAX_VERTICES]; // Array which has five rows for each semester.
  90. int bIncludeM[MAX_VERTICES]; // TRUE if this course is to be included in the plan
  91. } PlanImp;
  92. typedef PlanImp * Plan;
  93.  
  94.  
  95. // Prototypes
  96.  
  97. // Extra functions made by students (if anything)
  98. void processInput(Graph graph, Plan plan);
  99. void processCommands(Graph graph, char szCommand[], char pszRemainingTxt[], int *iVert, Plan plan);
  100. void insertVertexCourse(Graph graph, char szCourseId[], char szName[], int *iVert);
  101. void insertToHeadOfList(EdgeNode **list, EdgeNode *pEdge);
  102. EdgeNode * newEdgeNode(int iPrereqVertex, int iSuccVertex);
  103. void freeLists(Graph graph);
  104. void deleteList(EdgeNode*list);
  105. int distFromSource(Graph graph, int iVertex);
  106. void populateArray(Graph graph, Plan plan);
  107. int findMaxSemester(Graph graph);
  108.  
  109. // Recursive functions for program 5
  110. int maxChainLength(Graph graph, int iVertex);
  111. void printTraversal(Graph graph, int iCourseVertex, int indent);
  112. void printLongChains(Graph graph, int iVertex, int pathM[], int iLevel, int iLongLength);
  113. int causesCycle(Graph graph, int iPrereqVertex, int iVertex);
  114.  
  115. // Non-recursive for program 5
  116. int findCourse(Graph graph, char szCourseId[]);
  117. void insertPrereq(Graph graph, int iPrereqVertex, int iCourseVertex);
  118. void printAllInList(Graph graph);
  119. void printOne(Graph graph, int iVertex);
  120. void printSources(Graph graph);
  121. void printSinks(Graph graph);
  122.  
  123. Graph newGraph();
  124.  
  125. int insertCourse(Graph graph, char szCourseId[]);
  126. // Program 6 function for delete
  127. void deleteCourse (Graph graph, int iVertex);
  128.  
  129. // Program 6 functions for Plan
  130. void doPlan(Graph graph, Plan plan);
  131. int setLevel(Graph g, Plan plan, int iVertex, int iLev);
  132. Plan newPlan();
  133.  
  134. // hash function for extra credit
  135. int hash(Graph g, char SzCourseId[]);
  136. void printHash(Graph g);
  137. void printChain(Graph g, int iVertex);
  138.  
  139. // functions in most programs, but require modifications
  140. void processCommandSwitches(int argc, char *argv[], char **ppszCommandFileName);
  141. void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo);
  142.  
  143. // Utility routines provided by Larry
  144. void ErrExit(int iexitRC, char szFmt[], ...);
  145. char * getToken(char *pszInputTxt, char szToken[], int iTokenSize);
  146.  
  147. /*
  148. WARNING macro
  149. Parameters:
  150. I szFmt - a printf format
  151. I ... - a variable number of parameters corresponding to szFmt's
  152. format codes.
  153. Results:
  154. Prints "WARNING" and the value(s) specified by the szFmt.
  155. Notes:
  156. Since this generates multiple C statements, we surround them
  157. with a dummy do while(0) which only executes once. Notice that the
  158. dummy do while isn't ended with a ";" since the user of
  159. the macro naturally specifies a ";". Example:
  160. if (x != 0)
  161. WARNING("X must be blah blah");
  162. else
  163. { // normal processing
  164. ....
  165. }
  166. If we used {} in the macro definition instead of the dummy do while(0),
  167. the generated code would have a bad ";":
  168. if (x != 0)
  169. {
  170. printf("\tWARNING: ");
  171. printf("X must be blah blah");
  172. printf("\n");
  173. } ; // yuck, bad ";" causing the compiler to not understand the else
  174. else
  175. { // normal processing
  176. ....
  177. }
  178. */
  179. #define WARNING(szFmt, ...) do { \
  180. printf("\tWARNING: "); \
  181. printf(szFmt, __VA_ARGS__); \
  182. printf("\n"); \
  183. } while (0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement