




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Our goal is to write a program that calculates the grades for students in a class and outputs the students and their averages in alphabetical order. The program source code is broken into three parts:
// File: main_student.cpp // Purpose: Compute student averages and output them alphabetically. #include
unsigned int max(unsigned int x, unsigned int y) { return x>y? x : y; }
int main(int argc, char* argv[]) { if (argc != 3) { std::cerr << "Usage:\n " << argv[0] << " infile-students outfile-grades\n"; return 1; } std::ifstream in_str(argv[1]); if (!in_str) { std::cerr << "Could not open " << argv[1] << " to read\n"; return 1; } std::ofstream out_str(argv[2]); if (!out_str) { std::cerr << "Could not open " << argv[2] << " to write\n"; return 1; }
int num_homeworks, num_tests; double hw_weight; in_str >> num_homeworks >> num_tests >> hw_weight; std::vector
// Read the students, one at a time. while(one_student.read(in_str, num_homeworks, num_tests)) { students.push_back(one_student); }
// Compute the averages. At the same time, determine the maximum name length. unsigned int i; unsigned int max_length = 0; for (i=0; i<students.size(); ++i) { students[i].compute_averages(hw_weight); max_length = max(max_length, students[i].first_name().size() + students[i].last_name().size()); } max_length += 2; // account for the output padding with ", "
// Sort the students alphabetically by name. std::sort(students.begin(), students.end(), less_names);
// Output a header... out_str << "\nHere are the student semester averages\n"; const std::string header = "Name" + std::string(max_length-4, ’ ’) + " HW Test Final"; const std::string underline(header.size(), ’-’); out_str << header << ’\n’ << underline << std::endl;
// Output the students... for (i=0; i<students.size(); ++i) { unsigned int length = students[i].last_name().size() + students[i].first_name().size() + 2; students[i].output_name(out_str); out_str << std::string(max_length - length, ’ ’) << " "; students[i].output_averages(out_str); } return 0; // everything fine }
// File: student.cpp // Purpose: Implementation of the class Student #include
// Read information about a student, returning true if the information was read correctly. bool Student::read(std::istream& in_str, unsigned int num_homeworks, unsigned int num_tests) {
// If we don’t find an id, we’ve reached the end of the file & silently return false. if (!(in_str >> id_number_)) return false;
// Once we have an id number, any other failure in reading is treated as an error.
// read the name if (! (in_str >> first_name_ >> last_name_)) { std::cout << "Failed reading name for student " << id_number_ << std::endl; return false; }
unsigned int i; int score;
// Read the homework scores hw_scores_.clear(); for (i=0; i<num_homeworks && (in_str >> score); ++i) hw_scores_.push_back(score); if (hw_scores_.size() != num_homeworks) { std::cout << "Pre-mature end of file or invalid input reading " << "hw scores for " << id_number_ << std::endl; return false; }
// Read the test scores test_scores_.clear(); for (i=0; i<num_tests && (in_str >> score); ++i) test_scores_.push_back(score); if (test_scores_.size() != num_tests) { std::cout << "Pre-mature end of file or invalid input reading " << "test scores for" << id_number_ << std::endl; return false; } return true; // everything was fine
}
// Compute and store the hw, test and final average for the student. void Student::compute_averages(double hw_weight) { double dummy_stddev; avg_and_stddev(hw_scores_, hw_avg_, dummy_stddev); avg_and_stddev(test_scores_, test_avg_, dummy_stddev); final_avg_ = hw_weight * hw_avg_ + (1 - hw_weight) * test_avg_; }
std::ostream& Student::output_name(std::ostream& out_str) const { out_str << last_name_ << ", " << first_name_; return out_str; }
std::ostream& Student::output_averages(std::ostream& out_str) const { out_str << std::fixed << std::setprecision(1); out_str << hw_avg_ << " " << test_avg_ << " " << final_avg_ << std::endl; return out_str; }
// Boolean function to define alphabetical ordering of names. The vector sort // function requires that the objects be passed by CONSTANT REFERENCE. bool less_names(const Student& stu1, const Student& stu2) { return stu1.last_name() < stu2.last_name() || (stu1.last_name() == stu2.last_name() && stu1.first_name() < stu2.first_name()); }
const string header = "Name" + string(max_length-4, ’ ’) + " HW Test Final";
creates a constant string by “adding” (concatenating) existing strings.
Add code to the end of the main() function to compute and output the average of the semester grades and to output a list of the semester grades sorted into increasing order.
Write an operator< for comparing two Date objects.
// name_main.cpp // Demonstrates another example with the use of classes, including an output stream operator #include
int main() {
std::vector
std::cout <<"\nEnter a sequence of names (first and last) and this program will alphabetize them\n";
while (std::cin >> first >> last) { names.push_back(Name(first, last)); }
std::sort(names.begin(), names.end()); std::cout << "\nHere are the names, in alphabetical order.\n";
for (int i = 0; i < names.size(); ++i) { std::cout << names[i] << "\n"; }
return 0; }
// name.h #include
class Name { public:
// CONSTRUCTOR Name(const std::string& fst, const std::string& lst);
// ACCESSORS // Providing a const reference to the string allows the string to be // examined and treated as an r-value without the cost of copying it. const std::string& first() const { return first_; } const std::string& last() const { return last_; }
// MODIFIERS void set_first(const std::string & fst) { first_ = fst; } void set_last(const std::string& lst) { last_ = lst; }
private: // REPRESENTATION std::string first_, last_; };
// operator< to allow sorting bool operator< (const Name& left, const Name& right);
// operator<< to allow output std::ostream& operator<< (std::ostream& ostr, const Name& n);
// name.cpp #include "name.h"
// Here we use special syntax to call the string class copy // constructors instead of calling the default constructor and then // performing an assignment in the body of the constructor function. Name::Name(const std::string& fst, const std::string& lst) : first_(fst), last_(lst) {}
// operator< bool operator< (const Name& left, const Name& right) { return left.last()<right.last() || (left.last()==right.last() && left.first()<right.first()); }
// operator<< is the output stream operator. It takes two arguments: // the stream (such as cout) and the object to be output. The << // operator should always a reference to the output stream to allow a // sequence of outputs in a single statement. std::ostream& operator<< (std::ostream& ostr, const Name& n) { ostr << n.first() << " " << n.last(); return ostr; }