Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. // Derek Churchill
  2. // Assignment 5
  3. // 2017-03-26
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #include <ctype.h>
  10.  
  11. struct courseInfo {
  12. int courseID;
  13. char courseName[30];
  14. };
  15. typedef struct courseInfo CI;
  16.  
  17. struct studentInfo {
  18. char studentID[20];
  19. char fName[20];
  20. char lName[25];
  21. int n;
  22. CI data[10];
  23. struct studentInfo *next;
  24. };
  25. typedef struct studentInfo SI;
  26.  
  27. SI *head=NULL,*tail=NULL;
  28.  
  29. void addStudent(char id[],char fName[],char lName[],int n,char courses[][100],char code[][100]);
  30. void deleteStudent(int studentID);
  31. SI *searchStudentID(int id);
  32. SI *searchStudentlName(char *lName);
  33. void displayStudentInfo();
  34. void saveStudentInfo();
  35. void loadStudentInfo();
  36. void exit();
  37.  
  38. int main(void) {
  39. loadStudentInfo();
  40. displayStudentInfo();
  41. }
  42.  
  43. void loadStudentInfo() {
  44. FILE *input = fopen("studentList.txt","r");
  45. bool scanning = true;
  46. char tempFields[4][100];
  47. char tempCourses[10][100];
  48. char tempCodes[10][100];
  49. char *line;
  50. int i = 0,ppl = 0;
  51.  
  52. while(scanning == true) {
  53.  
  54. line = fgets(line,25,input);
  55. strtok(line,"\n");
  56. if(strcmp("***",line) != 0) {
  57.  
  58. if(i <= 3) {
  59. strcpy(tempFields[i],line);
  60. i++;
  61. }
  62.  
  63. if(i > 3) {
  64. for(int j = 0; j < atoi(tempFields[3]); j++) {
  65. int w = 0;
  66. line = fgets(line,25,input);
  67. strtok(line,"\n");
  68. char *token = strtok(line, " ");
  69. while(token != NULL) {
  70. if(w == 0) { strcpy(tempCourses[j],token);}
  71. else if(w == 1) { strcpy(tempCodes[j],token);}
  72. w++;
  73. token = strtok(NULL, " ");
  74. }
  75. }
  76.  
  77. addStudent(tempFields[0],tempFields[1],tempFields[2],atoi(tempFields[3]),tempCourses, tempCodes);
  78.  
  79. i = 0;
  80. }
  81.  
  82. }
  83. else {scanning = false;}
  84. }
  85.  
  86. }
  87.  
  88. void addStudent(char id[],char fName[],char lName[],int n,char course[][100],char code[][100]) {
  89. SI *newNode = (SI*)malloc(sizeof(SI));
  90. newNode->next = NULL;
  91. if(!head) {
  92. head = newNode;
  93. head->next = NULL;
  94. }
  95. else {
  96. SI *node = head;
  97. if(atoi(id) < atoi(node->studentID)) {
  98. SI *temp = node;
  99. head = newNode;
  100. head->next = node;
  101. }
  102. else {
  103. while(node->next != NULL && atoi(id) > atoi(node->next->studentID)) {
  104. node = node->next;
  105. }
  106. if(node->next == NULL) {
  107. node->next = newNode;
  108. }
  109. else {
  110. SI *tempBigger;
  111. tempBigger = node->next;
  112. node->next = newNode;
  113. newNode->next = tempBigger;
  114. }
  115. }
  116. }
  117. strcpy(newNode->studentID,id);
  118. strcpy(newNode->fName,fName);
  119. strcpy(newNode->lName,lName);
  120. newNode->n = n;
  121.  
  122. for(int i = 0; i < n; i++) {
  123. strcpy(newNode->data[i].courseName,course[i]);
  124. newNode->data[i].courseID = atoi(code[0]);
  125. }
  126.  
  127. }
  128.  
  129. void displayStudentInfo() {
  130. SI *node = head;
  131. int n = 1;
  132. while(node != NULL) {
  133. printf("Student: %d\n",n);
  134. puts(node->studentID);
  135. puts(node->fName);
  136. puts(node->lName);
  137. printf("%d\n",node->n);
  138. for(int i = 0; i < node->n; i++) {
  139. printf("%s %d\n",node->data[i].courseName,node->data[i].courseID);
  140. }
  141. node = node->next;
  142. n++;
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement