首页 > > 详细

CS100 Homework 7 讲解

CS100 Homework 7 (Spring, 2022)
Deadline: 2022-05-30 23:59:59
Late submission will open for 24 hours after the deadline, with -50% point deduction.
Problem 1. Gradesheet
In this problem, you are going to implement a simple Gradesheet class. The students’ grades are recorded
in a grade sheet. Each student has their name, student number and grade, which are covered in the Grade
class (See the code for details). You need to implement the all the getters and setters of Grade.
All grades are stored in a std::vector inside a Gradesheet class. You need to implement all the
provided member functions as follows.
• Default constructor and destructor. The default constructor initializes an empty grade sheet.
• Return the size of gradesheet, that is the number of records in this gradesheet.
std::size_t size() const;
• Compute the average of all students’ grades.
double average() const;
• Add a new student’s grade into the gradesheet. If the student’s name or student number already
exists, do nothing and return false. Otherwise, add the grade and return true.
bool addGrade(const Grade &grade);
• Return the grade of the student with the given name or student number. Return -1 if such student
is not found.
double findByName(const std::string &name);
double findByNumber(int number);
• Overloading subscript operator. Returns a reference to the i-th Grade recorded in the grade sheet.
The numbering starts with 0.
Grade& operator[](size_t i);
const Grade& operator[](size_t i) const;
1
CS100 Homework 7 (Spring, 2022) Deadline: 2022-05-30 23:59:59
• Overloading operator<< which applied to an output stream. The desired output should be like:
There are 3 columns in total. Each column is 10 characters wide. There are 30 ’-’ for those dashed
lines. Each row contains the student name, number and grade. Notice that all the numbers and
strings should be left-aligned. For the grades, you should print the number with a precision of 3
(use std::precision(3)). DO NOT forget to add the boundary as shown in the example above.
friend std::ostream& operator<<(std::ostream& os, const Gradesheet& sheet);
• Sort the gradesheet by student name in alphabetical order or by student number in ascending
order or by grade in descending order. You can use std::sort from for simplicity.
We also provide you 3 classes (NameComparator, NumberComparator, GradeComparator). You
should overload the operator() of each class for comparison.
void sortByName();
void sortByNumber();
void sortByGrade();
Sample code
Gradesheet sheet;
sheet.addGrade(Grade("Bob", 1, 95));
sheet.addGrade(Grade("Carl", 2, 100));
sheet.addGrade(Grade("Alex", 3, 90));
sheet.sortByGrade();
std::cout << "size == " << sheet.size() << "\n" << sheet;
The output is
size == 3
/------------------------------\
|Name Number Grade |
|------------------------------|
|Carl 2 100 |
|Bob 1 95 |
|Alex 3 90 |
\------------------------------/
Notes
• The Grade class is very simple with only one constructor, some getters and some setters. The
constructor initializes every member with the corresponding parameter. Complete this class first.
Page 2
CS100 Homework 7 (Spring, 2022) Deadline: 2022-05-30 23:59:59
• You may find std::setw, std::setprecision and std::left defined in standard library file
helpful to implement the operator<<.
• It is guaranteed that the length of the student name and number will not exceed 10.
Submission Guideline
When you submit your code, your main function will be replaced by one on OJ. You MUST NOT modify
the definition of the class. Otherwise, you will NOT receive any scores.
Page 3
CS100 Homework 7 (Spring, 2022) Deadline: 2022-05-30 23:59:59
Problem 2. Singly Linked-List
In this task, you will need to write an STL-style templated singly-linked-list and its iterator. Considering
that many of you may have only a vague idea of template programming, we have provided a framework
for you so that you don’t have to learn too many things. Moreover, since the compiler won’t generate
any code for templated functions that are not used, we also provide a simple test which involves some
compile-time checks and some runtime tests. You can paste or #include your code at the beginning of
7-2 simple tests.cpp to run the simple test.
In the framework, first we have the Slist node class. This is a simple structure that defines the
node of the linked-list. You probably need to add some constructors for this class.
The Iterator
Then it comes the Slist iterator

联系我们 - QQ: 99515681 微信:codinghelp
© 2021 www.7daixie.com
程序辅导网!