实验四
实验2
GradeCalc.hpp:
#include <algorithm> #include <iomanip> #include <iostream> #include <numeric> #include <string> #include <vector> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; class GradeCalc : public vector<int> { public: GradeCalc(const string &cname, int size); void input(); // 录入成绩 void output() const; // 输出成绩 void sort(bool ascending = false); // 排序 (默认降序) int min() const; // 返回最低分 int max() const; // 返回最高分 float average() const; // 返回平均分 void info(); // 输出课程成绩信息 private: void compute(); // 成绩统计 private: string course_name; // 课程名 int n; // 课程人数 vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 }; GradeCalc::GradeCalc(const string &cname, int size) : course_name{cname}, n{size} {} void GradeCalc::input() { int grade; for (int i = 0; i < n; ++i) { cin >> grade; this->push_back(grade); } } void GradeCalc::output() const { for (auto ptr = this->begin(); ptr != this->end(); ++ptr) cout << *ptr << " "; cout << 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 { return *std::min_element(this->begin(), this->end()); } int GradeCalc::max() const { return *std::max_element(this->begin(), this->end()); } float GradeCalc::average() const { return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n; } void GradeCalc::compute() { for (int grade : *this) { if (grade < 60) counts.at(0)++; else if (grade >= 60 && grade < 70) counts.at(1)++; else if (grade >= 70 && grade < 80) counts.at(2)++; else if (grade >= 80 && grade < 90) counts.at(3)++; else if (grade >= 90) counts.at(4)++; } for (int i = 0; i < rates.size(); ++i) rates.at(i) = counts.at(i) * 1.0 / n; } void GradeCalc::info() { cout << "课程名称:\t" << course_name << endl; cout << "排序后成绩: \t"; sort(); output(); cout << "最高分:\t" << max() << endl; cout << "最低分:\t" << min() << endl; cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; compute(); // 统计各分数段人数、比例 vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)", "[80, 90)", "[90, 100]"}; for (int i = tmp.size() - 1; i >= 0; --i) cout << tmp[i] << "\t: " << counts[i] << "人\t" << std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl; }
task2.cpp:
#include "GradeCalc.hpp" #include <iomanip> void test() { int n; cout << "输入班级人数: "; cin >> n; GradeCalc c1("OOP", n); cout << "录入成绩: " << endl; ; c1.input(); cout << "输出成绩: " << endl; c1.output(); cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; c1.info(); } int main() { test(); }
问题1:派生类GradeCalc定义中,成绩存储在哪里?派生类方法sort, min, max, average,
output都要访问成绩,是通过什么接口访问到每个成绩的?input方法是通过什么接口实现数
据存入对象的?
1.在GradeCalc派生类中,成绩储存在继承的vector<int>中。
2.sort通过sort函数;min通过min_element函数;max通过max_element函数;average通过accumulate函数;output通过迭代器。
3.input是通过push_back实现的。
问题2:代码line68分母的功能是?去掉乘以1.0代码,重新编译、运行,结果有影响吗?为什
么要乘以1.0?
1.分母是n除以n是计算平均值的。
2.有影响,计算平均值时候要转化为double类型。
3.否则int类型会让数据不准,小数部分没法计算,被舍弃了。
问题3:从真实应用场景角度考虑,GradeCalc类在设计及代码实现细节上,有哪些地方尚未
考虑周全,仍需继续迭代、完善?
1.cin输入时候没有对成绩的范围进行认定。
2.处理成绩时,没有保证成绩的私密性。
实验3
GradeCalc.hpp
#include <algorithm> #include <iomanip> #include <iostream> #include <numeric> #include <string> #include <vector> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; class GradeCalc { public: GradeCalc(const string &cname, int size); void input(); // 录入成绩 void output() const; // 输出成绩 void sort(bool ascending = false); // 排序 (默认降序) int min() const; // 返回最低分 int max() const; // 返回最高分 float average() const; // 返回平均分 void info(); // 输出课程成绩信息 private: void compute(); // 成绩统计 private: string course_name; // 课程名 int n; // 课程人数 vector<int> grades; // 课程成绩 vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 }; GradeCalc::GradeCalc(const string &cname, int size) : course_name{cname}, n{size} {} void GradeCalc::input() { int grade; for (int i = 0; i < n; ++i) { cin >> grade; grades.push_back(grade); } } void GradeCalc::output() const { for (int grade : grades) cout << grade << " "; cout << 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 { return *std::min_element(grades.begin(), grades.end()); } int GradeCalc::max() const { return *std::max_element(grades.begin(), grades.end()); } float GradeCalc::average() const { return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n; } void GradeCalc::compute() { for (int grade : grades) { if (grade < 60) counts.at(0)++; else if (grade >= 60 && grade < 70) counts.at(1)++; else if (grade >= 70 && grade < 80) counts.at(2)++; else if (grade >= 80 && grade < 90) counts.at(3)++; else if (grade >= 90) counts.at(4)++; } for (int i = 0; i < rates.size(); ++i) rates.at(i) = counts.at(i) * 1.0 / n; } void GradeCalc::info() { cout << "课程名称:\t" << course_name << endl; cout << "排序后成绩: \t"; sort(); output(); cout << "最高分:\t" << max() << endl; cout << "最低分:\t" << min() << endl; cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; compute(); // 统计各分数段人数、比例 vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)", "[80, 90)", "[90, 100]"}; for (int i = tmp.size() - 1; i >= 0; --i) cout << tmp[i] << "\t: " << counts[i] << "人\t" << std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl; }
task3.cpp
#include "GradeCalc.hpp" #include <iomanip> void test() { int n; cout << "输入班级人数: "; cin >> n; GradeCalc c1("OOP", n); cout << "录入成绩: " << endl; ; c1.input(); cout << "输出成绩: " << endl; c1.output(); cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; c1.info(); } int main() { test(); }
问题1:组合类GradeCalc定义中,成绩存储在哪里?组合类方法sort, min, max, average,
output都要访问成绩,是通过什么接口访问到每个成绩的?观察与实验任务2在代码写法细节
上的差别。
1.存储在类型为vector<int>的私有成员grades里。
2.min通过min_element函数;max通过max_element函数;average通过accumulate函数;output通过遍历每个元素打印出来。
问题2:对比实验任务2和实验任务3,主体代码逻辑(测试代码)没有变更,类GradeCalc的
接口也没变,变化的是类GradeCalc的设计及接口内部实现细节。你对面向对象编程有什么新
的理解和领悟吗?
1.通过多文件类编写程序,可以随时的更改函数的实现的设计。而接口什么的都没有改变。
实验4
task4_1.cpp
#include <iostream> #include <limits> #include <string> using namespace std; void test1() { string s1, s2; cin >> s1 >> s2; // cin: 从输入流读取字符串, 碰到空白符(空格/回车/Tab)即结束 cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; } void test2() { string s1, s2; getline(cin, s1); // getline(): 从输入流中提取字符串,直到遇到换行符 getline(cin, s2); cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; } void test3() { string s1, s2; getline(cin, s1, ' '); // 从输入流中提取字符串,直到遇到指定分隔符 getline(cin, s2); cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; } int main() { cout << "测试1: 使用标准输入流对象cin输入字符串" << endl; test1(); cout << endl; cout << "测试2: 使用函数getline()输入字符串" << endl; test2(); cout << endl; cout << "测试3: 使用函数getline()输入字符串, 指定字符串分隔符" << endl; test3(); }
问题1:去掉task4_1.cpp的line35,重新编译、运行,给出此时运行结果截图。查阅资料,回答
line35在这里的用途是什么?
这句话是在清楚我cin留下的换行符\n,避免影响到getline函数。
task4_2.cpp
#include <iostream> #include <limits> #include <string> #include <vector> using namespace std; void output(const vector<string> &v) { for (auto &s : v) cout << s << endl; } void test() { int n; while (cout << "Enter n: ", cin >> n) { vector<string> v1; for (int i = 0; i < n; ++i) { string s; cin >> s; v1.push_back(s); } cout << "output v1: " << endl; output(v1); cout << endl; } } int main() { cout << "测试: 使用cin多组输入字符串" << endl; test(); }
task4_3.cpp
#include <iostream> #include <limits> #include <string> #include <vector> using namespace std; void output(const vector<string> &v) { for (auto &s : v) cout << s << endl; } void test() { int n; while (cout << "Enter n: ", cin >> n) { cin.ignore(numeric_limits<streamsize>::max(), '\n'); vector<string> v2; for (int i = 0; i < n; ++i) { string s; getline(cin, s); v2.push_back(s); } cout << "output v2: " << endl; output(v2); cout << endl; } } int main() { cout << "测试: 使用函数getline()多组输入字符串" << endl; test(); }
问题2:去掉task4_3.cpp的line16,重新编译、运行,给出此时运行结果。查阅资料,回答line16
在这里的用途是什么?
这行代码的清除了我输入n留下的换行符,如果不清除就会getline一个空的字符串。
实验五
grm.hpp
#include <iostream> using namespace std; template <typename T> class GameResourceManager { public: GameResourceManager(T resource); T get(); void update(T value); private: T resource; }; template <typename T> GameResourceManager<T>::GameResourceManager(T resource) : resource{resource} {} template <typename T> T GameResourceManager<T>::get() { return resource; } template <typename T> void GameResourceManager<T>::update(T value) { resource += value; if (resource < 0) resource = 0; }
task5.cpp
#include "grm.hpp" #include <iostream> using std::cout; using std::endl; void test1() { GameResourceManager<float> HP_manager(99.99); cout << "当前生命值: " << HP_manager.get() << endl; HP_manager.update(9.99); cout << "增加9.99生命值后, 当前生命值: " << HP_manager.get() << endl; HP_manager.update(-999.99); cout << "减少999.99生命值后, 当前生命值: " << HP_manager.get() << endl; } void test2() { GameResourceManager<int> Gold_manager(100); cout << "当前金币数量: " << Gold_manager.get() << endl; Gold_manager.update(50); cout << "增加50个金币后, 当前金币数量: " << Gold_manager.get() << endl; Gold_manager.update(-99); cout << "减少99个金币后, 当前金币数量: " << Gold_manager.get() << endl; } int main() { cout << "测试1: 用float类型对类模板GameResourceManager实例化" << endl; test1(); cout << endl; cout << "测试2: 用int类型对类模板GameResourceManager实例化" << endl; test2(); }
实验6
info.hpp
#pragma once #include <iostream> #include <string> #include <vector> using namespace std; class Info { public: string nickname; string contact; string city; int n; Info(string nickname, string contact, string city, int n) : nickname{nickname}, contact{contact}, city{city}, n{n} {} void dispaly() { cout << "------------------------------------------------------------\n"; cout << "昵称: " << nickname << endl; cout << "联系方式 " << contact << endl; cout << "所在城市 " << city << endl; cout << "预定人数 " << n << endl; cout << "-------------------------------------------------------------\n"; } };
task6.cpp
#include "Info.hpp" #include <iostream> #include <limits> #include <string> #include <vector> using namespace std; int main() { const int capacity = 100; vector<Info> audience_lst; int total = 0, n; string nickname, city, contact; cout << "昵称 " << "联系方式(邮箱/手机号) " << "城市 " << "人数" << endl; while (cin >> nickname >> contact >> city >> n) { if (total + n <= capacity) { total += n; audience_lst.emplace_back(nickname, contact, city, n); } else { cout << "对不起,只剩下" << capacity - total << "个位置" << endl; cout << "输入u,更新预定信息" << endl; cout << "输入q,放弃预定" << endl; char choice; cin >> choice; if (choice == 'u') { cout << "请重新输入信息:" << endl; continue; } else break; } } cout << "截至目前一共有" << total << "位听众预约" << endl; for (Info &info : audience_lst) info.dispaly(); return 0; }
实验7
date.h
#ifndef __DATE_H__ #define __DATE_H__ class Date { private: int year; // 年 int month; // 月 int day; // 日 int totalDays; // 该日期是从公元元年1月1日开始的第几天 public: Date(int year, int month, int day); // 用年、月、日构造日期 int getYear() const { return year; } int getMonth() const { return month; } int getDay() const { return day; } int getMaxDay() const; bool isLeapYear() const { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } void show() const; // 计算两个日期之间相差多少天 int distance(const Date &date) const { return totalDays - date.totalDays; } }; #endif
date.cpp
#include "date.h" #include <cstdlib> #include <iostream> using namespace std; // 命名空间使下面的定义只在当前文件中有效 namespace { // 存储平年中的某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项 const int DAYS_BEFORE_MONTH[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; } Date::Date(int year, int month, int day) : year(year), month(month), day(day) { if (day <= 0 || day > getMaxDay()) { cout << "Invalid date."; show(); cout << endl; exit(1); } int years = year - 1; totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; if (isLeapYear() && month > 2) totalDays++; } int Date::getMaxDay() const { if (isLeapYear() && month == 2) { return 29; } else { return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; } } void Date::show() const { cout << getYear() << '-' << getMonth() << '-' << getDay(); }
accumulator.h
#ifndef _ACCUMULATOR_H_ #define _ACCUMULATOR_H_ #include "date.h" class Accumulator { private: Date lastDate; double value; double sum; public: Accumulator(const Date& date, double value):lastDate(date),value(value),sum(0){ } double getSum(const Date& date) const { return sum + value * date.distance(lastDate); } void change(const Date& date, double value) { sum = getSum(date); lastDate = date; this->value = value; } void reset(const Date& date, double value) { lastDate = date; this->value = value; sum = 0; } }; #endif
account.h
#ifndef _ACCOUNT_H_ #define _ACCOUNT_H_ #include "accumulator.h" #include "date.h" #include <string> class Account { private: std::string id; double balance; static double total; protected: Account(const Date &date, const std::string &id); void error(const std::string &msg) const; void record(const Date &data, double amount, const std::string &desc); public: const std::string &getId() const { return id; } double getBalance() const { return balance; } static double getTotal() { return total; } void show() const; }; class SavingsAccount : public Account { private: Accumulator acc; double rate; public: SavingsAccount(const Date &date, const std::string &id, double rate); double getRate() const { return rate; } void deposit(const Date &date, double amount, const std::string &desc); void withdraw(const Date &date, double amount, const std::string &desc); void settle(const Date &date); }; class CreditAccount : public Account { private: Accumulator acc; double credit; double rate; double fee; double getDebt() const { double balance = getBalance(); return (balance < 0 ? balance : 0); } public: CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee); double getCredit() const { return credit; } double getRate() const { return rate; } double getFee() const { return fee; } double getAvailableCredit() const { return credit; } void deposit(const Date &date, double amount, const std::string &desc); void withdraw(const Date &date, double amount, const std::string &desc); void settle(const Date &date); void show() const; }; #endif
account.cpp
#include "account.h" #include <cmath> #include <iostream> using namespace std; double Account::total = 0; // Account类的实现 Account::Account(const Date &date, const string &id) : id(id), balance(0) { date.show(); cout << "\t#" << id << " created" << endl; } void Account::record(const Date &date, double amount, const string &desc) { amount = floor(amount * 100 + 0.5) / 100; balance += amount; total += amount; date.show(); cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; } void Account::show() const { cout << id << "\tBalance: " << balance; } void Account::error(const string &msg) const { cout << "Error(#" << id << "): " << msg << endl; } // SavingsAccount类相关成员函数的实现 SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate) : Account(date, id), rate(rate), acc(date, 0) {} void SavingsAccount::deposit(const Date &date, double amount, const string &desc) { record(date, amount, desc); acc.change(date, getBalance()); } void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) { if (amount > getBalance()) { error("not enough money"); } else { record(date, -amount, desc); acc.change(date, getBalance()); } } void SavingsAccount::settle(const Date &date) { double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); if (interest != 0) record(date, interest, "interest"); acc.reset(date, getBalance()); } // CreditAccount类相关成员函数的实现 CreditAccount::CreditAccount(const Date &date, const string &id, double credit, double rate, double fee) : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {} void CreditAccount::deposit(const Date &date, double amount, const string &desc) { record(date, amount, desc); acc.change(date, getDebt()); }
7_10.cpp
#include "account.h" #include <iostream> using namespace std; int main() { Date date(2008, 11, 1); SavingsAccount sa1(date, "S3755217", 0.015); SavingsAccount sa2(date, "02342342", 0.015); CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); sa1.deposit(Date(2008, 11, 5), 5000, "salary"); ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); ca.settle(Date(2008, 12, 1)); ca.deposit(Date(2008, 12, 1), 2016, "repay the credit"); sa1.deposit(Date(2008, 12, 5), 5500, "salary"); sa1.settle(Date(2009, 1, 1)); sa2.settle(Date(2009, 1, 1)); ca.settle(Date(2009, 1, 1)); cout << endl; sa1.show(); cout << endl; sa2.show(); cout << endl; ca.show(); cout << endl; cout << "Total: " << Account::getTotal() << endl; return 0; }