Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need this in c++ code 1. Recode the roster example we studied in class such that it lists the classes each student is enrolled in.

Need this in c++ code

1. Recode the roster example we studied in class such that it lists the classes each student is enrolled in. Example roster files are here. Here is an example printout:

All Students last name, first name: courses enrolled Kathleen Andreson: CS1 CS3 CS4 Gerald Edwards: CS1 CS2 CS3 Mary Price: CS1 ... 

The container of the master student roster should be modified as follows: the vector (or other sequential container) should contain student entries. Each entry is a linked list (or other sequential container) where the first entry is the first (or last) name, followed by entries of the courses the student is enrolled in. You are not allowed to use associative containers. The advisable structures are as follows: vector> studentEntries or list> studentEntries

The printed out list may be tab-separated. You may assume that the text file structure is: one name per line; the line contains a first name followed by last name separated by white space. Each student has exactly two names: first and last (no middle names). The course name is the file name without the extension.

example file

Mary Smith Emma Watson Rachel Maddow Colin Powell Catherine Ward Doris Howard Cynthia Jenkins Betty Torres Juan Cox Kathleen Anderson Mark Russell Gerald Edwards Mikhail Nesterenko Charles Martin Linda Johnson David Collins Elizabeth Jones Jonathan Baker Maria Kelly Martin Taylor Dennis White Brian Sanchez

roster example

#include #include #include #include #include #include using std::ifstream; using std::string; using std::getline; using std::list; using std::vector; using std::cout; using std::endl; using std::move; // reading a list from a fileName void readRoster(list& roster, string fileName); // printing a list out void printRoster(const list& roster); int main(int argc, char* argv[]){ if (argc <= 1){ cout << "usage: " << argv[0] << " list of courses, dropouts last" << endl; exit(1); } // vector of courses of students vector > courseStudents; for(int i=1; i < argc-1; ++i){ list roster; readRoster(roster, argv[i]); cout << " " << argv[i] << " "; printRoster(roster); courseStudents.push_back(move(roster)); } // reading in dropouts list dropouts; readRoster(dropouts, argv[argc-1]); cout << " dropouts "; printRoster(dropouts); // master list of students list allStudents; for(auto& lst : courseStudents) allStudents.splice(allStudents.end(), lst); cout << " all students unsorted "; printRoster(allStudents); // sorting master list allStudents.sort(); cout << " all students sorted "; printRoster(allStudents); // eliminating duplicates allStudents.unique(); cout << " all students, duplicates removed "; printRoster(allStudents); // removing individual dropouts for (const auto& str : dropouts) allStudents.remove(str); cout << " all students, dropouts removed "; printRoster(allStudents); } // reading in a file of names into a list of strings void readRoster(list& roster, string fileName){ ifstream course(fileName); string first, last; while(course >> first >> last) roster.push_back(move(first + ' ' + last)); course.close(); } // printing a list out void printRoster(const list& roster){ for(const auto& str : roster) cout << str << endl; }

2. Repeat the assignment for the object-oriented version of the code shown here. You need to modify the class Student to include a list of classes the student is enrolled in.

Student class

#include #include #include #include #include #include using std::ifstream; using std::string; using std::getline; using std::list; using std::vector; using std::cout; using std::endl; using std::move; class Student{ public: Student(string firstName, string lastName): firstName_(firstName), lastName_(lastName) {} // move constructor, not really needed, generated automatically Student(Student && org): firstName_(move(org.firstName_)), lastName_(move(org.lastName_)) {} // force generation of default copy constructor Student(const Student & org) = default; string print() const {return firstName_ + ' ' + lastName_;} // needed for unique() and for remove() friend bool operator== (Student left, Student right){ return left.lastName_ == right.lastName_ && left.firstName_ == right.firstName_; } // needed for sort() friend bool operator< (Student left, Student right){ return left.lastName_ < right.lastName_ || (left.lastName_ == right.lastName_ && left.firstName_ < right.firstName_); } private: string firstName_; string lastName_; }; // reading a list from a fileName void readRoster(list& roster, string fileName); // printing a list out void printRoster(const list& roster); int main(int argc, char* argv[]){ if (argc <= 1){ cout << "usage: " << argv[0] << " list of courses, dropouts last" << endl; exit(1);} // vector of courses of students vector > courseStudents; for(int i=1; i < argc-1; ++i){ list roster; readRoster(roster, argv[i]); cout << " " << argv[i] << " "; printRoster(roster); courseStudents.push_back(move(roster)); } // reading in dropouts list dropouts; readRoster(dropouts, argv[argc-1]); cout << " dropouts "; printRoster(dropouts); list allStudents; // master list of students for(auto& lst : courseStudents) allStudents.splice(allStudents.end(),lst); cout << " all students unsorted "; printRoster(allStudents); allStudents.sort(); // sorting master list cout << " all students sorted "; printRoster(allStudents); allStudents.unique(); // eliminating duplicates cout << " all students, duplicates removed "; printRoster(allStudents); for (const auto& str : dropouts) // removing individual dropouts allStudents.remove(str); cout << " all students, dropouts removed "; printRoster(allStudents); } void readRoster(list& roster, string fileName){ ifstream course(fileName); string first, last; while(course >> first >> last) roster.push_back(Student(first, last)); course.close(); } // printing a list out void printRoster(const list& roster){ for(const auto& student : roster) cout << student.print() << endl; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions