作业4
实验2
实验任务一
GradeCalc.cpp
#include <algorithm>
#include <array>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include "GradeCalc.hpp"
GradeCalc::GradeCalc(const std::string &cname):course_name{cname},is_dirty{true} {
counts.fill(0);
rates.fill(0);
}
void GradeCalc::input(int n) {
if(n < 0) {
std::cerr << "无效输入! 人数不能为负数\n";
std::exit(1);
}
grades.reserve(n);
int grade;
for(int i = 0; i < n;) {
std::cin >> grade;
if(grade < 0 || grade > 100) {
std::cerr << "无效输入! 分数须在[0,100]\n";
continue;
}
grades.push_back(grade);
++i;
}
is_dirty = true; // 设置脏标记:成绩信息有变更
}
void GradeCalc::output() const {
for(auto grade: grades)
std::cout << grade << ' ';
std::cout << std::endl;
}
void GradeCalc::sort(bool ascending) {
if(ascending)
std::sort(grades.begin(), grades.end());
else
std::sort(grades.begin(), grades.end(), std::greater<int>());
}
int GradeCalc::min() const {
if(grades.empty())
return -1;
auto it = std::min_element(grades.begin(), grades.end());
return *it;
}
int GradeCalc::max() const {
if(grades.empty())
return -1;
auto it = std::max_element(grades.begin(), grades.end());
return *it;
}
double GradeCalc::average() const {
if(grades.empty())
return 0.0;
double avg = std::accumulate(grades.begin(), grades.end(), 0.0)/grades.size();
return avg;
}
void GradeCalc::info() {
if(is_dirty)
compute();
std::cout << "课程名称:\t" << course_name << std::endl;
std::cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << std::endl;
std::cout << "最高分:\t" << max() << std::endl;
std::cout << "最低分:\t" << min() << std::endl;
const std::array<std::string, 5> grade_range{"[0, 60) ",
"[60, 70)",
"[70, 80)",
"[80, 90)",
"[90, 100]"};
for(int i = static_cast<int>(grade_range.size())-1; i >= 0; --i)
std::cout << grade_range[i] << "\t: " << counts[i] << "人\t"
<< std::fixed << std::setprecision(2) << rates[i]*100 << "%\n";
}
void GradeCalc::compute() {
if(grades.empty())
return;
counts.fill(0);
rates.fill(0.0);
// 统计各分数段人数
for(auto grade:grades) {
if(grade < 60)
++counts[0]; // [0, 60)
else if (grade < 70)
++counts[1]; // [60, 70)
else if (grade < 80)
++counts[2]; // [70, 80)
else if (grade < 90)
++counts[3]; // [80, 90)
else
++counts[4]; // [90, 100]
}
// 统计各分数段比例
for(size_t i = 0; i < rates.size(); ++i)
rates[i] = counts[i] * 1.0 / grades.size();
is_dirty = false; // 更新脏标记
}
GradeCalc.hpp
#pragma once
#include <vector>
#include <array>
#include <string>
class GradeCalc {
public:
GradeCalc(const std::string &cname);
void input(int n); // ¼��n���ɼ�
void output() const; // ����ɼ�
void sort(bool ascending = false); // ���� (Ĭ�Ͻ���)
int min() const; // ������ͷ֣���ɼ�δ¼�룬����-1)
int max() const; // ������߷� ����ɼ�δ¼�룬����-1)
double average() const; // ����ƽ���� ����ɼ�δ¼�룬����0.0)
void info(); // ����γ̳ɼ���Ϣ
private:
void compute(); // �ɼ�ͳ��
private:
std::string course_name; // ���
std::vector<int> grades; // �γ̳ɼ�
std::array<int, 5> counts; // �����������������[0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
std::array<double, 5> rates; // ���������������ռ��
bool is_dirty; // ���ǣ���¼�Ƿ�ɼ���Ϣ�б��
};
task1.cpp
#include <iostream>
#include <string>
#include "GradeCalc.hpp"
void test() {
GradeCalc c1("OOP");
std::cout << "录入成绩:\n";
c1.input(5);
std::cout << "输出成绩:\n";
c1.output();
std::cout << "排序后成绩:\n";
c1.sort(); c1.output();
std::cout << "*************成绩统计信息*************\n";
c1.info();
}
int main() {
test();
}

问题
1. 问题1
std::string course_name; 存储课程名
std::vector<int> grades; 存储成绩
std::array<int, 5> counts; 存储各段成绩个数
std::array<double, 5> rates; 存储各段成绩占比
2. 问题2
不合法,因为GradeCalc没有提供push_back方法
3. 问题3
(1)会被调用1次,起到标记是否更改的作用
(2)不需要,可以通过重置is_dirty来重新触发
4. 问题4
```c++
double GradeCalc::median() const {
if (grades.empty())
return 0;
std::vector<int> temp = grades;
std::sort(temp.begin(), temp.end());
int n = temp.size();
if (n % 2 == 1) {
return temp[n / 2];
} else {
return (temp[n/2 - 1] + temp[n/2]) / 2;
}
}
```
5. 实验5
不能删去:不清除数据,会导致后面更改数据时是在原有数据上处理,此时会出问题
6. 问题6
(1) 无影响
(2) 有影响,使用reserve能减少内存分配次数,提高性能
实验任务2
task2.cpp
#include <algorithm>
#include <array>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
#include "GradeCalc.hpp"
GradeCalc::GradeCalc(const std::string &cname): course_name{cname}, is_dirty{true}{
counts.fill(0);
rates.fill(0);
}
void GradeCalc::input(int n) {
if(n < 0) {
std::cerr << "无效输入! 人数不能为负数\n";
return;
}
this->reserve(n);
int grade;
for(int i = 0; i < n;) {
std::cin >> grade;
if(grade < 0 || grade > 100) {
std::cerr << "无效输入! 分数须在[0,100]\n";
continue;
}
this->push_back(grade);
++i;
}
is_dirty = true;
}
void GradeCalc::output() const {
for(auto grade: *this)
std::cout << grade << ' ';
std::cout << std::endl;
}
void GradeCalc::sort(bool ascending) {
if(ascending)
std::sort(this->begin(), this->end());
else
std::sort(this->begin(), this->end(), std::greater<int>());
}
int GradeCalc::min() const {
if(this->empty())
return -1;
return *std::min_element(this->begin(), this->end());
}
int GradeCalc::max() const {
if(this->empty())
return -1;
return *std::max_element(this->begin(), this->end());
}
double GradeCalc::average() const {
if(this->empty())
return 0.0;
double avg = std::accumulate(this->begin(), this->end(), 0.0) / this->size();
return avg;
}
void GradeCalc::info() {
if(is_dirty)
compute();
std::cout << "课程名称:\t" << course_name << std::endl;
std::cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << std::endl;
std::cout << "最高分:\t" << max() << std::endl;
std::cout << "最低分:\t" << min() << std::endl;
const std::array<std::string, 5> grade_range{"[0, 60) ",
"[60, 70)",
"[70, 80)",
"[80, 90)",
"[90, 100]"};
for(int i = static_cast<int>(grade_range.size())-1; i >= 0; --i)
std::cout << grade_range[i] << "\t: " << counts[i] << "人\t"
<< std::fixed << std::setprecision(2) << rates[i]*100 << "%\n";
}
void GradeCalc::compute() {
if(this->empty())
return;
counts.fill(0);
rates.fill(0);
// 统计各分数段人数
for(int grade: *this) {
if(grade < 60)
++counts[0]; // [0, 60)
else if (grade < 70)
++counts[1]; // [60, 70)
else if (grade < 80)
++counts[2]; // [70, 80)
else if (grade < 90)
++counts[3]; // [80, 90)
else
++counts[4]; // [90, 100]
}
// 统计各分数段比例
for(size_t i = 0; i < rates.size(); ++i)
rates[i] = counts[i] * 1.0 / this->size();
is_dirty = false;
}
#pragma once
#include <array>
#include <string>
#include <vector>
class GradeCalc: private std::vector<int> {
public:
GradeCalc(const std::string &cname);
void input(int n); // ¼��n���ɼ�
void output() const; // ����ɼ�
void sort(bool ascending = false); // ���� (Ĭ�Ͻ���)
int min() const; // ������ͷ�
int max() const; // ������߷�
double average() const; // ����ƽ����
void info(); // ����ɼ�ͳ����Ϣ
private:
void compute(); // ����ɼ�ͳ����Ϣ
private:
std::string course_name; // ���
std::array<int, 5> counts; // �����������������[0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
std::array<double, 5> rates; // �����������ռ��
bool is_dirty; // ���ǣ���¼�Ƿ�ɼ���Ϣ�б��
};
#include <iostream>
#include <string>
#include "GradeCalc.hpp"
void test() {
GradeCalc c1("OOP");
std::cout << "录入成绩:\n";
c1.input(5);
std::cout << "输出成绩:\n";
c1.output();
std::cout << "排序后成绩:\n";
c1.sort(); c1.output();
std::cout << "*************成绩统计信息*************\n";
c1.info();
}
int main() {
test();
}

问题
-
class GradeCalc: private std::vector
-
不会:当前继承为私有继承,私有继承隐藏父类公有接口,因此对象c无法使用父类的push_back。
-
组合更合适,因为成绩计算不涉及功能的拓展,他是一些功能和数据的集合。
实验任务三
task3.cpp
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include "Graph.hpp"
// Circle��ʵ��
void Circle::draw() { std::cout << "draw a circle...\n"; }
// Triangle��ʵ��
void Triangle::draw() { std::cout << "draw a triangle...\n"; }
// Rectangle��ʵ��
void Rectangle::draw() { std::cout << "draw a rectangle...\n"; }
// Canvas��ʵ��
void Canvas::add(const std::string& type) {
Graph* g = make_graph(type);
if (g)
graphs.push_back(g);
}
void Canvas::paint() const {
for (Graph* g : graphs)
g->draw();
}
Canvas::~Canvas() {
for (Graph* g : graphs)
delete g;
}
// ���ߺ���ʵ��
// �ַ��� �� ö��ת��
GraphType str_to_GraphType(const std::string& s) {
std::string t = s;
std::transform(s.begin(), s.end(), t.begin(),
[](unsigned char c) { return std::tolower(c);});
if (t == "circle")
return GraphType::circle;
if (t == "triangle")
return GraphType::triangle;
if (t == "rectangle")
return GraphType::rectangle;
return GraphType::circle; // ȱʡ����
}
// ����ͼ�Σ����ضѶ���ָ��
Graph* make_graph(const std::string& type) {
switch (str_to_GraphType(type)) {
case GraphType::circle: return new Circle;
case GraphType::triangle: return new Triangle;
case GraphType::rectangle: return new Rectangle;
default: return nullptr;
}
}
#pragma once
#include <string>
#include <vector>
enum class GraphType {circle, triangle, rectangle};
// Graph�ඨ��
class Graph {
public:
virtual void draw() {}
virtual ~Graph() = default;
};
// Circle������
class Circle : public Graph {
public:
void draw();
};
// Triangle������
class Triangle : public Graph {
public:
void draw();
};
// Rectangle������
class Rectangle : public Graph {
public:
void draw();
};
// Canvas������
class Canvas {
public:
void add(const std::string& type); // �����ַ�������ͼ��
void paint() const; // ʹ��ͳһ�ӿڻ�������ͼ��
~Canvas(); // �ֶ��ͷ���Դ
private:
std::vector<Graph*> graphs;
};
// 4. ���ߺ���
GraphType str_to_GraphType(const std::string& s); // �ַ���תö������
Graph* make_graph(const std::string& type); // ����ͼ�Σ����ضѶ���ָ��
#include <string>
#include "Graph.hpp"
void test() {
Canvas canvas;
canvas.add("circle");
canvas.add("triangle");
canvas.add("rectangle");
canvas.paint();
}
int main() {
test();
}

问题
-
(1)std::vector<Graph*> graphs;
(2) class Circle : public Graph;
class Triangle : public Graph;
class Rectangle : public Graph; -
(1)如果没设成虚函数,则调用的是父类版本的方法
(2) 无法绘制图形,因为选择保存父类而不是父类指针会导致子类信息丢失。
(3) 会导致内存泄漏,子类析构函数无法被正常调用 -
(1) Canvas类的析构函数中.
(2)原始指针方便使用,但是容易引发内存泄漏和指针悬空
实验任务四
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Toy {
public:
void show_info();
virtual void function() = 0;
protected:
string name;
string type;
string function_name;
};
class Toy_1 :public Toy{
public:
Toy_1();
void function() override;
};
class Toy_2 :public Toy{
public:
Toy_2();
void function() override;
};
class ToyFactory {
public:
void create(Toy* toy);
void show_all_toy_info() ;
private:
vector<Toy*> toy_vec;
};
void Toy::show_info() {
cout << "名字: " << name << "类型: " << type << "特异功能: " << function_name << endl;
}
Toy_1::Toy_1() {
name = "初音未来";
type = "歌手";
function_name = "唱歌";
}
void Toy_1::function() {
cout << "歌唱: Happy birthday to you" << endl;
}
Toy_2::Toy_2() {
name = "千早爱音";
type = "诗人";
function_name = "作诗";
}
void Toy_2::function() {
cout << "诗朗诵: 唐诗三百首" << endl;
}
void ToyFactory::create(Toy* toy) {
toy_vec.push_back(toy);
}
void ToyFactory::show_all_toy_info() {
for(auto& t : toy_vec) {
t->show_info();
t->function();
}
}
int main() {
ToyFactory tf;
tf.create(new Toy_1);
tf.create(new Toy_2);
tf.show_all_toy_info();
}

浙公网安备 33010602011771号