Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. struct student_rec { long id;
  10. string last, first;
  11. };
  12.  
  13. struct enroll_rec { long id, crn;
  14. int year, grade;
  15. };
  16.  
  17. struct section_rec { long crn;
  18. int year;
  19. string dept;
  20. int cnum, snum;
  21. string building, room;
  22. };
  23.  
  24. struct course_rec { string dept;
  25. int num;
  26. string name;
  27. };
  28.  
  29. struct studentWithGrade {
  30. long id;
  31. string last, first;
  32. int grade;
  33. };
  34.  
  35. bool read_student(ifstream &f, student_rec &s);
  36. bool read_enroll (ifstream &f, enroll_rec &e);
  37. bool read_section(ifstream &f, section_rec &s);
  38. bool read_course (ifstream &f, course_rec &c);
  39.  
  40. bool find_student(student_rec &student, long id);
  41. bool find_section(section_rec &section, int year, string dept, int cnum, int snum);
  42. int find_enroll(enroll_rec &enroll, long CRN, studentWithGrade *students);
  43.  
  44. int main()
  45. {
  46. // Start in section, use Year, DepCode, CourseNo- take the CRN number
  47. // Use CRN number to find student ID, and Grade in enroll
  48. // Use Student ID to find first and Last name
  49.  
  50. student_rec student;
  51. section_rec section;
  52. enroll_rec enroll;
  53.  
  54. int n = 100;
  55.  
  56.  
  57. string input;
  58.  
  59. //cout<< "jfilue" << endl;
  60. //cin>> input;
  61.  
  62. string dept= "CSCI";
  63. int year= 2014, cnum= 275, snum= 20;
  64.  
  65. if (!find_section(section, year, dept, cnum, snum)){
  66. cout << "Student not found!" << endl;
  67. }
  68.  
  69. studentWithGrade *students;
  70. students = new studentWithGrade[n];
  71.  
  72. int numStudents = find_enroll(enroll, section.crn, students);
  73.  
  74. if (numStudents == 0){
  75. cout << "Student not found!" << endl;
  76. }
  77.  
  78. for (int i=0; i<numStudents; i++)
  79. {
  80.  
  81. if (!find_student(student, students[i].id)) {
  82. cout << "Student not found!" << endl;
  83. }
  84.  
  85. students[i].first= student.first;
  86. students[i].last= student.last;
  87. }
  88.  
  89.  
  90. for (int i=0; i<numStudents; i++)
  91. {
  92. cout<< students[i].first << setw(15) << students[i].last << setw(15) << students[i].grade << setw(15) <<endl;
  93. }
  94.  
  95.  
  96.  
  97. return 0;
  98. }
  99.  
  100. bool find_section(section_rec &section, int year, string dept, int cnum, int snum)
  101. {
  102. ifstream file("section.txt");
  103.  
  104. if (!file) {
  105. cout << "Secton file not found!" << endl;
  106. return false;
  107. }
  108.  
  109. while (read_section(file, section))
  110. {
  111. if (section.year == year && section.dept == dept && section.cnum == cnum && section.snum == snum)
  112. {
  113. return true;
  114. }
  115. }
  116.  
  117. file.close();
  118. return false;
  119. }
  120.  
  121. int find_enroll(enroll_rec &enroll, long CRN, studentWithGrade *students)
  122. {
  123. ifstream file("enroll.txt");
  124.  
  125. if (!file) {
  126. cout << "Secton file not found!" << endl;
  127. return false;
  128. }
  129.  
  130. int count=0;
  131.  
  132. while (read_enroll(file, enroll)) {
  133. if (enroll.crn == CRN)
  134. {
  135. students[count++].id= enroll.id;
  136. students[count++].grade= enroll.grade;
  137. }
  138. }
  139.  
  140. file.close();
  141. return count;
  142.  
  143. }
  144.  
  145.  
  146. bool find_student(student_rec &student, long id)
  147. {
  148. ifstream file("student.txt");
  149.  
  150. if (!file) {
  151. cout << "Student file not found!" << endl;
  152. return false;
  153. }
  154.  
  155. while (read_student(file, student)) {
  156. if (student.id == id) return true;
  157. }
  158.  
  159. file.close();
  160. return false;
  161. }
  162.  
  163. bool read_student(ifstream &f, student_rec &s)
  164. {
  165. char comma;
  166. if (f.eof()) return false;
  167. f >> s.id >> comma;
  168. getline(f, s.last, ',');
  169. getline(f, s.first);
  170. return true;
  171. }
  172.  
  173. bool read_enroll(ifstream &f, enroll_rec &e)
  174. {
  175. char comma, str[10];
  176. if (f.eof()) return false;
  177. f >> e.id >> comma >> e.crn >> comma >> e.year;
  178. f.getline(str, 10);
  179. if (strlen(str) > 1) e.grade = atoi(&str[1]);
  180. else e.grade = -1;
  181. return true;
  182. }
  183.  
  184. bool read_section(ifstream &f, section_rec &s)
  185. {
  186. char comma;
  187. if (f.eof()) return false;
  188. f >> s.crn >> comma >> s.year >> comma;
  189. getline(f, s.dept, ',');
  190. f >> s.cnum >> comma >> s.snum >> comma;
  191. if (comma == ',') {
  192. getline(f, s.building, ',');
  193. getline(f, s.room);
  194. } else {
  195. s.building[0] = s.room[0] = '\0';
  196. }
  197. return true;
  198. }
  199.  
  200. bool read_course(ifstream &f, course_rec &c)
  201. {
  202. char comma;
  203. getline(f, c.dept, ',');
  204. if (f.eof()) return false;
  205. f >> c.num >> comma;
  206. getline(f, c.name);
  207. return true;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement