Question
smallgradelist.txt CS121 267893043 A CS121 454454651 B CS121 279750343 C CS121 546208080 C CS253 454454651 B CS253 279750343 B CS253 546208080 A CS131 656529993 D
smallgradelist.txt
CS121 267893043 A
CS121 454454651 B
CS121 279750343 C
CS121 546208080 C
CS253 454454651 B
CS253 279750343 B
CS253 546208080 A
CS131 656529993 D
CS131 546208080 B
CS131 279750343 B
#pragma once
#include
using namespace std;
class Student {
//Student
// info about a student
//CWID (string)
//courseName (array of string; at most 50 elements)
//Grade (array of char; at most 50 elements)
public:
Student(); // default constructor
Student(const string &cwid); // constructor with parameter
void addCourseGrade(const string &courseName, char grade); // add course name and grade to student's record
double getGPA(); // calculate and return GPA
void printTranscript(); // print transcript - see Student.cpp for the format
string getCWID(); // return the CWID of this student
private:
// any private member variables and methods go here
// TO BE COMPLETED
};
#include "Student.h"
#include
Student::Student() {
// TO BE COMPLETED
string cwid;
string courseName[size];
char grade[size];
}
Student::Student(const string &cwid) {
// TO BE COMPLETED
}
string Student::getCWID() {
// TO BE COMPLETED
}
void Student::addCourseGrade (const string &courseName, char grade) {
// TO BE COMPLETED
}
double Student::getGPA() {
// TO BE COMPLETED
}
// print transcript in this (sample) format:
// TRANSCRIPT FOR CWID=279750343
// CS 121 C
// CS 253 B
// CS 131 B
// GPA = 2.6667
void Student::printTranscript() {
// TO BE COMPLETED
}
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include
#include
#include "Student.h"
using namespace std;
class Registrar {
//Registrar
// info about all students
//Up to 50K entries
//Dynamic array of Student
//private:
//Student* allStudents;
//int size;
//int index;
public:
void readTextfile(string filename); // Load information from a text file with the given filename: THIS IS COMPLETE
void addLine(string courseName, string cwid, char grade); // process a line from the text file
Student& getStudent(string cwid) const; // return the Student object corresponding to a given CWID
// getStudent must throw an exception if cwid is invalid
// add constructors, destructors, assignment operators if needed
private:
// Add private member variables for your class along with any
// other variables required to implement the public member functions
Student* allStudents; // pointer
int size; // the population
int index; // the CWID
};
#include
#include
#include
#include
#include "Registrar.h"
using namespace std;
// Load information from a text file with the given filename
// THIS FUNCTION IS COMPLETE
void Registrar::readTextfile(string filename) {
ifstream myfile(filename);
if (myfile.is_open()) {
cout << "Successfully opened file " << filename << endl;
string courseName;
string cwid;
char grade;
while (myfile >> courseName >> cwid >> grade) {
// cout << cwid << " " << grade << endl;
addLine(courseName, cwid, grade);
}
myfile.close();
}
else
throw invalid_argument("Could not open file " + filename);
}
// return Student object corresponding to a given CWID
// getStudent must throw an exception if cwid is invalid
Student& Registrar::getStudent(string cwid) const {
// TO BE COMPLETED
string x;
cin >> x;
return x;
}
// process a line from the text file
void Registrar::addLine(string courseName, string cwid, char grade) {
// TO BE COMPLETED
}
//////////////////////////////////////////////////////////////////////////////////
//Do not change main
#include
#include
#include
#include
#include "Registrar.h"
#include "Student.h"
using namespace std;
template
bool testAnswer(const string &nameOfTest, const T &received, const T &expected);
template
bool testAnswerEpsilon(const string &nameOfTest, const T &received, const T &expected);
int main() {
{
// test only the Student class
Student student("123456789");
testAnswer("Student::getCWID test", student.getCWID(), string("123456789"));
student.addCourseGrade("cs101", 'A');
testAnswerEpsilon("Student::getGPA test1", student.getGPA(), 4.0);
student.addCourseGrade("cs201", 'C');
testAnswerEpsilon("Student::getGPA test2", student.getGPA(), 3.0);
}
{
// test only the Registrar class
Registrar spring2018;
spring2018.addLine("cs101", "123456789", 'A');
Student s = spring2018.getStudent("123456789");
testAnswerEpsilon("Registrar::getStudent test1", s.getGPA(), 4.0);
spring2018.addLine("cs101", "456789321", 'B');
spring2018.addLine("cs201", "456789321", 'C');
Student t = spring2018.getStudent("456789321");
testAnswerEpsilon("Registrar::getStudent test1", t.getGPA(), 2.5);
// test if error checking is done in Registrar::getStudent
try {
spring2018.getStudent("9999");
cout << "FAILED Registrar::getStudent did not throw exception for invalid CWID" << endl;
}
catch (exception &e) {
cout << "Caught exception: " << e.what() << endl;
cout << "PASSED Registrar::getStudent threw exception for invalid CWID" << endl;
}
}
{
// test Registrar class with text file
Registrar spring2018;
spring2018.readTextfile("gradeslist.txt");
testAnswerEpsilon("Registrar::readTextfile test1", spring2018.getStudent("279750343").getGPA(), 2.66666);
testAnswerEpsilon("Registrar::readTextfile test2", spring2018.getStudent("546208080").getGPA(), 3.0);
}
{
// test Registrar class with large text file
Registrar spring2018;
spring2018.readTextfile("gradeslistbig.txt");
testAnswerEpsilon("Registrar::readTextfile test3", spring2018.getStudent("225202362").getGPA(), 2.5);
testAnswerEpsilon("Registrar::readTextfile test4", spring2018.getStudent("568703135").getGPA(), 26.0/9.0);
spring2018.getStudent("225202362").printTranscript();
spring2018.getStudent("568703135").printTranscript();
}
{
//// test copy constructor
Registrar spring2018;
spring2018.addLine("cs101", "123456789", 'A');
spring2018.addLine("cs101", "456789321", 'B');
spring2018.addLine("cs201", "456789321", 'C');
Registrar fall2018 = spring2018;
testAnswerEpsilon("Registrar copy constructor test1", fall2018.getStudent("456789321").getGPA(), 2.5);
fall2018.addLine("cs101", "456789321", 'B');
testAnswerEpsilon("Registrar copy constructor test2", spring2018.getStudent("456789321").getGPA(), 2.5);
// test assignment operator
Registrar summer2018;
summer2018 = spring2018;
testAnswerEpsilon("Registrar assignment operator test1", summer2018.getStudent("456789321").getGPA(), 2.5);
summer2018.addLine("cs101", "456789321", 'B');
testAnswerEpsilon("Registrar assignment operator test2", spring2018.getStudent("456789321").getGPA(), 2.5);
}
//system("pause"); // DELETE
}
template
bool testAnswer(const string &nameOfTest, const T &received, const T &expected) {
if (received == expected) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
template
bool testAnswerEpsilon(const string &nameOfTest, const T &received, const T &expected) {
const double epsilon = 0.0001;
if ((received - expected < epsilon) && (expected - received < epsilon)) {
cout << "PASSED " << nameOfTest << ": expected and received " << received << endl;
return true;
}
cout << "FAILED " << nameOfTest << ": expected " << expected << " but received " << received << endl;
return false;
}
| ||
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started