Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- struct student_rec { long id;
- string last, first;
- };
- struct enroll_rec { long id, crn;
- int year, grade;
- };
- struct section_rec { long crn;
- int year;
- string dept;
- int cnum, snum;
- string building, room;
- };
- struct course_rec { string dept;
- int num;
- string name;
- };
- struct studentWithGrade {
- long id;
- string last, first;
- int grade;
- };
- bool read_student(ifstream &f, student_rec &s);
- bool read_enroll (ifstream &f, enroll_rec &e);
- bool read_section(ifstream &f, section_rec &s);
- bool read_course (ifstream &f, course_rec &c);
- bool find_student(student_rec &student, long id);
- bool find_section(section_rec §ion, int year, string dept, int cnum, int snum);
- int find_enroll(enroll_rec &enroll, long CRN, studentWithGrade *students);
- int main()
- {
- // Start in section, use Year, DepCode, CourseNo- take the CRN number
- // Use CRN number to find student ID, and Grade in enroll
- // Use Student ID to find first and Last name
- student_rec student;
- section_rec section;
- enroll_rec enroll;
- int n = 100;
- string input;
- //cout<< "jfilue" << endl;
- //cin>> input;
- string dept= "CSCI";
- int year= 2014, cnum= 275, snum= 20;
- if (!find_section(section, year, dept, cnum, snum)){
- cout << "Student not found!" << endl;
- }
- studentWithGrade *students;
- students = new studentWithGrade[n];
- int numStudents = find_enroll(enroll, section.crn, students);
- if (numStudents == 0){
- cout << "Student not found!" << endl;
- }
- for (int i=0; i<numStudents; i++)
- {
- if (!find_student(student, students[i].id)) {
- cout << "Student not found!" << endl;
- }
- students[i].first= student.first;
- students[i].last= student.last;
- }
- for (int i=0; i<numStudents; i++)
- {
- cout<< students[i].first << setw(15) << students[i].last << setw(15) << students[i].grade << setw(15) <<endl;
- }
- return 0;
- }
- bool find_section(section_rec §ion, int year, string dept, int cnum, int snum)
- {
- ifstream file("section.txt");
- if (!file) {
- cout << "Secton file not found!" << endl;
- return false;
- }
- while (read_section(file, section))
- {
- if (section.year == year && section.dept == dept && section.cnum == cnum && section.snum == snum)
- {
- return true;
- }
- }
- file.close();
- return false;
- }
- int find_enroll(enroll_rec &enroll, long CRN, studentWithGrade *students)
- {
- ifstream file("enroll.txt");
- if (!file) {
- cout << "Secton file not found!" << endl;
- return false;
- }
- int count=0;
- while (read_enroll(file, enroll)) {
- if (enroll.crn == CRN)
- {
- students[count++].id= enroll.id;
- students[count++].grade= enroll.grade;
- }
- }
- file.close();
- return count;
- }
- bool find_student(student_rec &student, long id)
- {
- ifstream file("student.txt");
- if (!file) {
- cout << "Student file not found!" << endl;
- return false;
- }
- while (read_student(file, student)) {
- if (student.id == id) return true;
- }
- file.close();
- return false;
- }
- bool read_student(ifstream &f, student_rec &s)
- {
- char comma;
- if (f.eof()) return false;
- f >> s.id >> comma;
- getline(f, s.last, ',');
- getline(f, s.first);
- return true;
- }
- bool read_enroll(ifstream &f, enroll_rec &e)
- {
- char comma, str[10];
- if (f.eof()) return false;
- f >> e.id >> comma >> e.crn >> comma >> e.year;
- f.getline(str, 10);
- if (strlen(str) > 1) e.grade = atoi(&str[1]);
- else e.grade = -1;
- return true;
- }
- bool read_section(ifstream &f, section_rec &s)
- {
- char comma;
- if (f.eof()) return false;
- f >> s.crn >> comma >> s.year >> comma;
- getline(f, s.dept, ',');
- f >> s.cnum >> comma >> s.snum >> comma;
- if (comma == ',') {
- getline(f, s.building, ',');
- getline(f, s.room);
- } else {
- s.building[0] = s.room[0] = '\0';
- }
- return true;
- }
- bool read_course(ifstream &f, course_rec &c)
- {
- char comma;
- getline(f, c.dept, ',');
- if (f.eof()) return false;
- f >> c.num >> comma;
- getline(f, c.name);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement