Guest User

Untitled

a guest
Jan 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. #define MAX_CLASSES 20
  7.  
  8.  
  9. /* Function Prototypes */
  10. struct course * initcourse( int, char *, char *, float, char *, char *);
  11. void add( struct course * );
  12.  
  13. /* Definition of a data node holding course information */
  14. struct course {
  15. int term;
  16. char name[15];
  17. char abbrev[20];
  18. float hours;
  19. char grade [4];
  20. char type[12];
  21. struct course *next;
  22. };
  23.  
  24.  
  25. /* head points to first node in list, end points to last node in list */
  26. /* initializes both to NULL, no nodes yet */
  27. struct course *head = (struct course *) NULL;
  28. struct course *end = (struct course *) NULL;
  29.  
  30.  
  31. /* Initializes a node, allocates memory for the node, and returns */
  32. /* a pointer to the new node. Must pass correct parameters. */
  33. struct course * initcourse( int term, char *name, char *abbrev, float hours, char *grade, char *type)
  34. {
  35. struct course *ptr;
  36. ptr = (struct course *) calloc( 1, sizeof(struct course ) );
  37. if( ptr == NULL )
  38.  
  39. return (struct course *) NULL;
  40.  
  41. else
  42. {
  43. ptr->term = term;
  44. strcpy( ptr->name, name );
  45. strcpy( ptr->abbrev, abbrev );
  46. ptr->hours = hours;
  47. strcpy( ptr->grade, grade );
  48. strcpy( ptr->type, type );
  49. return ptr;
  50. }
  51. }
  52.  
  53.  
  54. /* This adds a node to the end of the list. You must allocate a node and */
  55. /* then pass its address to this function */
  56. void add(struct course *new)
  57. {
  58. if (head == NULL)
  59. {
  60. head = new;
  61. }
  62. else
  63. {
  64. end->next = new;
  65. end = new;
  66. }
  67. }
  68.  
  69. /* Prints all information in a node */
  70. void printnode( struct course *ptr )
  71. {
  72. printf("Term ->%dn", ptr->term );
  73. printf("Name ->%sn", ptr->name );
  74. printf("Abbreviation ->%sn", ptr->abbrev );
  75. printf("Hours ->%fn", ptr->hours );
  76. printf("Grade ->%sn", ptr->grade );
  77. printf("Type ->%sn", ptr->type );
  78. }
  79.  
  80.  
  81.  
  82.  
  83. /* Prints List of Nodes */
  84. void printlist( struct course *ptr )
  85. {
  86. while( ptr != NULL )
  87. {
  88. printnode( ptr );
  89. ptr = ptr->next;
  90. }
  91. }
  92.  
  93. /* Calculates GPA */
  94. /* float gpa ( struct course *ptr ) */
  95. /* { */
  96. /* float totalhours; */
  97. /* float gpa; */
  98. /* float gradepoints; */
  99.  
  100. /* while (ptr != NULL ) */
  101. /* { */
  102. /* totalhours += (ptr->hours); */
  103. /* gradepoints = (ptr->hours * ptr->grade); */
  104. /* } */
  105. /* gpa = (gradepoints /ptr->hours); */
  106. /* } */
  107.  
  108.  
  109.  
  110. int main()
  111. {
  112.  
  113. int term;
  114. char name[15];
  115. char abbrev[20];
  116. float hours;
  117. char grade[4];
  118. char type[12];
  119. float gpa;
  120. struct course *ptr;
  121.  
  122. struct course course1, course2, course3;
  123.  
  124. course1.term = 1234;
  125. strcpy(course1.name,"cse1234");
  126. strcpy(course1.abbrev,"systems");
  127. course1.hours = 4;
  128. strcpy(course1.grade,"A");
  129. strcpy(course1.type,"GEC");
  130.  
  131.  
  132. ptr = initcourse(course1.term, course1.name, course1.abbrev, course1.hours, course1.grade, course1.type);
  133.  
  134. struct course *head, *ptr2;
  135. head = ptr;
  136. // ptr2 = ptr;
  137.  
  138. add(ptr);
  139.  
  140. course2.term = 4332;
  141. strcpy(course2.name,"cse4332");
  142. strcpy(course2.abbrev,"Database");
  143. course2.hours = 4;
  144. strcpy(course2.grade,"B");
  145. strcpy(course2.type,"Technical");
  146.  
  147. ptr2 = initcourse(course2.term, course2.name, course2.abbrev, course2.hours, course2.grade, course2.type);
  148.  
  149. add(ptr2);
  150.  
  151. printlist(head);
  152.  
  153.  
  154.  
  155. }
  156.  
  157. void add(struct course *new)
  158. {
  159. if (head == NULL)
  160. {
  161. head = new;
  162. }
  163. else
  164. {
  165. end->next = new;
  166. end = new;
  167. }
  168. }
  169.  
  170. struct course *head, *ptr2;
  171. head = ptr;
  172.  
  173. head = ptr;
  174. add(ptr);
Add Comment
Please, Sign In to add comment