实验任务2:
实验代码:
GradeCalc.hpp:
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 #include <numeric> 6 #include <iomanip> 7 8 using std::vector; 9 using std::string; 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 14 class GradeCalc: public vector<int> { 15 public: 16 GradeCalc(const string &cname, int size); 17 void input(); // 录入成绩 18 void output() const; // 输出成绩 19 void sort(bool ascending = false); // 排序 (默认降序) 20 int min() const; // 返回最低分 21 int max() const; // 返回最高分 22 float average() const; // 返回平均分 23 void info(); // 输出课程成绩信息 24 25 private: 26 void compute(); // 成绩统计 27 28 private: 29 string course_name; // 课程名 30 int n; // 课程人数 31 vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] 32 vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 33 }; 34 35 GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {} 36 37 void GradeCalc::input() { 38 int grade; 39 40 for(int i = 0; i < n; ++i) { 41 cin >> grade; 42 this->push_back(grade); 43 } 44 } 45 46 void GradeCalc::output() const { 47 for(auto ptr = this->begin(); ptr != this->end(); ++ptr) 48 cout << *ptr << " "; 49 cout << endl; 50 } 51 52 void GradeCalc::sort(bool ascending) { 53 if(ascending) 54 std::sort(this->begin(), this->end()); 55 else 56 std::sort(this->begin(), this->end(), std::greater<int>()); 57 } 58 59 int GradeCalc::min() const { 60 return *std::min_element(this->begin(), this->end()); 61 } 62 63 int GradeCalc::max() const { 64 return *std::max_element(this->begin(), this->end()); 65 } 66 67 float GradeCalc::average() const { 68 return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n; 69 } 70 71 void GradeCalc::compute() { 72 for(int grade: *this) { 73 if(grade < 60) 74 counts.at(0)++; 75 else if(grade >= 60 && grade < 70) 76 counts.at(1)++; 77 else if(grade >= 70 && grade < 80) 78 counts.at(2)++; 79 else if(grade >= 80 && grade < 90) 80 counts.at(3)++; 81 else if(grade >= 90) 82 counts.at(4)++; 83 } 84 85 for(int i = 0; i < rates.size(); ++i) 86 rates.at(i) = counts.at(i) * 1.0 / n; 87 } 88 89 void GradeCalc::info() { 90 cout << "课程名称:\t" << course_name << endl; 91 cout << "排序后成绩: \t"; 92 sort(); output(); 93 cout << "最高分:\t" << max() << endl; 94 cout << "最低分:\t" << min() << endl; 95 cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; 96 97 compute(); // 统计各分数段人数、比例 98 99 vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"}; 100 for(int i = tmp.size()-1; i >= 0; --i) 101 cout << tmp[i] << "\t: " << counts[i] << "人\t" 102 << std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl; 103 }
demo2.cpp:
1 #include "GradeCalc.hpp" 2 #include <iomanip> 3 4 void test() { 5 int n; 6 cout << "输入班级人数: "; 7 cin >> n; 8 9 GradeCalc c1("OOP", n); 10 11 cout << "录入成绩: " << endl;; 12 c1.input(); 13 cout << "输出成绩: " << endl; 14 c1.output(); 15 16 cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; 17 c1.info(); 18 } 19 20 int main() { 21 test(); 22 }
实验截图:
问题1:储存在GradeCalc继承的基类vector<int>grade中;通过public继承方式继承的vector自带的接口访问成绩,输入成绩。
问题2:分母的作用:计算平均值;去掉1.0,编译结果有影响,accumulate作为继承vector自带的接口,数据类型为整型,整除后结果仍为整型数据,乘1.0将数据类型转换为浮点型。
问题3:未能做到将成绩与具体的学生对应;对输入错误的数据不能正确进行反馈。
实验任务3:
实验代码:
GradeCalc.hpp:
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 #include <numeric> 6 #include <iomanip> 7 8 using std::vector; 9 using std::string; 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 14 class GradeCalc { 15 public: 16 GradeCalc(const string &cname, int size); 17 void input(); // 录入成绩 18 void output() const; // 输出成绩 19 void sort(bool ascending = false); // 排序 (默认降序) 20 int min() const; // 返回最低分 21 int max() const; // 返回最高分 22 float average() const; // 返回平均分 23 void info(); // 输出课程成绩信息 24 25 private: 26 void compute(); // 成绩统计 27 28 private: 29 string course_name; // 课程名 30 int n; // 课程人数 31 vector<int> grades; // 课程成绩 32 vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] 33 vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 34 }; 35 36 GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {} 37 38 void GradeCalc::input() { 39 int grade; 40 41 for(int i = 0; i < n; ++i) { 42 cin >> grade; 43 grades.push_back(grade); 44 } 45 } 46 47 void GradeCalc::output() const { 48 for(int grade: grades) 49 cout << grade << " "; 50 cout << endl; 51 } 52 53 void GradeCalc::sort(bool ascending) { 54 if(ascending) 55 std::sort(grades.begin(), grades.end()); 56 else 57 std::sort(grades.begin(), grades.end(), std::greater<int>()); 58 59 } 60 61 int GradeCalc::min() const { 62 return *std::min_element(grades.begin(), grades.end()); 63 } 64 65 int GradeCalc::max() const { 66 return *std::max_element(grades.begin(), grades.end()); 67 } 68 69 float GradeCalc::average() const { 70 return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n; 71 } 72 73 void GradeCalc::compute() { 74 for(int grade: grades) { 75 if(grade < 60) 76 counts.at(0)++; 77 else if(grade >= 60 && grade < 70) 78 counts.at(1)++; 79 else if(grade >= 70 && grade < 80) 80 counts.at(2)++; 81 else if(grade >= 80 && grade < 90) 82 counts.at(3)++; 83 else if(grade >= 90) 84 counts.at(4)++; 85 } 86 87 for(int i = 0; i < rates.size(); ++i) 88 rates.at(i) = counts.at(i) *1.0 / n; 89 } 90 91 void GradeCalc::info() { 92 cout << "课程名称:\t" << course_name << endl; 93 cout << "排序后成绩: \t"; 94 sort(); output(); 95 cout << "最高分:\t" << max() << endl; 96 cout << "最低分:\t" << min() << endl; 97 cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; 98 99 compute(); // 统计各分数段人数、比例 100 101 vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"}; 102 for(int i = tmp.size()-1; i >= 0; --i) 103 cout << tmp[i] << "\t: " << counts[i] << "人\t" 104 << std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl; 105 }
demo3.cpp:
1 #include "GradeCalc.hpp" 2 #include <iomanip> 3 4 void test() { 5 int n; 6 cout << "输入班级人数: "; 7 cin >> n; 8 9 GradeCalc c1("OOP", n); 10 11 cout << "录入成绩: " << endl;; 12 c1.input(); 13 cout << "输出成绩: " << endl; 14 c1.output(); 15 16 cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; 17 c1.info(); 18 } 19 20 int main() { 21 test(); 22 }
实验截图:
问题1:储存在GradeCalc类的私有成员vector<int> grades中;访问类中的私有成员grades。
问题2:在自定义类时,既可以和基类继承,也可以和基类进行组合。
实验任务4:
实验代码:
task4_1.cpp:
1 #include <iostream> 2 #include <string> 3 #include <limits> 4 5 using namespace std; 6 7 void test1() { 8 string s1, s2; 9 cin >> s1 >> s2; // cin: 从输入流读取字符串, 碰到空白符(空格/回车/Tab)即结束 10 cout << "s1: " << s1 << endl; 11 cout << "s2: " << s2 << endl; 12 } 13 14 void test2() { 15 string s1, s2; 16 getline(cin, s1); // getline(): 从输入流中提取字符串,直到遇到换行符 17 getline(cin, s2); 18 cout << "s1: " << s1 << endl; 19 cout << "s2: " << s2 << endl; 20 } 21 22 void test3() { 23 string s1, s2; 24 getline(cin, s1, ' '); //从输入流中提取字符串,直到遇到指定分隔符 25 getline(cin, s2); 26 cout << "s1: " << s1 << endl; 27 cout << "s2: " << s2 << endl; 28 } 29 30 int main() { 31 cout << "测试1: 使用标准输入流对象cin输入字符串" << endl; 32 test1(); 33 cout << endl; 34 35 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 36 37 cout << "测试2: 使用函数getline()输入字符串" << endl; 38 test2(); 39 cout << endl; 40 41 cout << "测试3: 使用函数getline()输入字符串, 指定字符串分隔符" << endl; 42 test3(); 43 }
task4_2.cpp:
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <limits> 5 6 using namespace std; 7 8 void output(const vector<string> &v) { 9 for(auto &s: v) 10 cout << s << endl; 11 } 12 13 void test() { 14 int n; 15 while(cout << "Enter n: ", cin >> n) { 16 vector<string> v1; 17 18 for(int i = 0; i < n; ++i) { 19 string s; 20 cin >> s; 21 v1.push_back(s); 22 } 23 24 cout << "output v1: " << endl; 25 output(v1); 26 cout << endl; 27 } 28 } 29 30 int main() { 31 cout << "测试: 使用cin多组输入字符串" << endl; 32 test(); 33 }
task4_3.cpp:
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <limits> 5 6 using namespace std; 7 8 void output(const vector<string> &v) { 9 for(auto &s: v) 10 cout << s << endl; 11 } 12 13 void test() { 14 int n; 15 while(cout << "Enter n: ", cin >> n) { 16 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 17 18 vector<string> v2; 19 20 for(int i = 0; i < n; ++i) { 21 string s; 22 getline(cin, s); 23 v2.push_back(s); 24 } 25 cout << "output v2: " << endl; 26 output(v2); 27 cout << endl; 28 } 29 } 30 31 int main() { 32 cout << "测试: 使用函数getline()多组输入字符串" << endl; 33 test(); 34 }
实验截图:
问题1 / 2:在输入操作中忽略从标准输入流 cin 中读取的多余字符,直到遇到换行符 \n 为止。这个函数常用于清理输入缓冲区,以避免在接下来的输入操作中出现意外的输入。
实验任务5:
实验代码:
grm.hpp:
1 #pragma once 2 3 #include <iostream> 4 5 using namespac std; 6 7 template<typename T> 8 class GameResourceManager { 9 public: 10 GameResourceManager(T Resource):resource(Resource) {} 11 T get()const { 12 return resource; 13 } 14 void update(T num) { 15 resource += num; 16 if(resource < 0) resource = 0; 17 } 18 private: 19 T resource; 20 };
grm.cpp:
1 #include "grm.hpp" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 void test1() { 8 GameResourceManager<float> HP_manager(99.99); 9 cout << "当前生命值: " << HP_manager.get() << endl; 10 HP_manager.update(9.99); 11 cout << "增加9.99生命值后, 当前生命值: " << HP_manager.get() << endl; 12 HP_manager.update(-999.99); 13 cout <<"减少999.99生命值后, 当前生命值: " << HP_manager.get() << endl; 14 } 15 16 void test2() { 17 GameResourceManager<int> Gold_manager(100); 18 cout << "当前金币数量: " << Gold_manager.get() << endl; 19 Gold_manager.update(50); 20 cout << "增加50个金币后, 当前金币数量: " << Gold_manager.get() << endl; 21 Gold_manager.update(-99); 22 cout <<"减少99个金币后, 当前金币数量: " << Gold_manager.get() << endl; 23 } 24 25 26 int main() { 27 cout << "测试1: 用float类型对类模板GameResourceManager实例化" << endl; 28 test1(); 29 cout << endl; 30 31 cout << "测试2: 用int类型对类模板GameResourceManager实例化" << endl; 32 test2(); 33 }
实验截图:
实验任务6:
实验代码:
info.hpp:
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 #include <iomanip> 6 7 using namespace std; 8 9 class Info { 10 public: 11 Info(string nickname0, string contact0, string city0, int n0) : 12 nickname{nickname0}, contact{contact0}, city{city0}, n{n0} {} 13 void display() const { 14 cout << string(40, '-') << endl; 15 cout << "昵称:" << "\t" << nickname << endl; 16 cout << "联系方式:" << "\t" << contact << endl; 17 cout << "所在城市:" << "\t" << city << endl; 18 cout << "预定人数:" << "\t" << n << endl; 19 } 20 private: 21 string nickname; 22 string contact; 23 string city; 24 int n; 25 };
task6.cpp:
1 #include "Info.hpp" 2 #include <vector> 3 4 const int capacity = 100; 5 6 void task6() { 7 cout << "录入用户预约信息:" << endl; 8 cout << endl; 9 cout << "昵称" << "\t" << "联系方式(邮箱/手机号)" << "\t" << "所在城市" << "\t" << "预定参加人数" << endl; 10 vector<Info> audience_lst; 11 string nickname, contact, city; 12 int num; 13 int sum = 0; 14 while(cin >> nickname >> contact >> city >> num) { 15 audience_lst.push_back(Info(nickname, contact, city, num)); 16 sum += num; 17 if(sum == capacity) 18 break; 19 if(sum > capacity) { 20 sum -= num; 21 audience_lst.pop_back(); 22 char choice; 23 cout << "对不起,只剩" << capacity - sum << "个位置." << endl; 24 cout << "1. 输入u,更新(update)预定信息" << endl; 25 cout << "2. 输入q, 退出预定" << endl; 26 cout << "你的选择:"; 27 cin >> choice; 28 if (choice == 'u') { 29 cout << "请重新输入预定信息:" << endl; 30 continue; 31 } 32 else if(choice == 'q') 33 break; 34 } 35 } 36 cout << "截至目前,一共有" << sum << "位听众预约。" << "预约听众信息如下:" << endl; 37 for(auto i : audience_lst) { 38 i.display(); 39 } 40 cout << string(40, '-'); 41 } 42 43 int main() { 44 task6(); 45 return 0; 46 }
实验截图:
实验任务7:
实验代码:
date.h:
1 #pragma once 2 class Date { 3 private: 4 int year; 5 int month; 6 int day; 7 int totalDays; 8 public: 9 Date(int year, int month, int day); 10 int getYear() const {return year;} 11 int getMonth() const {return month;} 12 int getDay() const {return day;} 13 int getMaxDay() const; 14 bool isLeapYear() const { 15 return year%4==0 && year%100 != 0 || year%400==0; 16 } 17 void show() const; 18 19 int distance(const Date& date) const { 20 return totalDays-date.totalDays; 21 } 22 };
date.cpp:
1 #include "date.h" 2 #include<iostream> 3 #include<cstdlib> 4 using namespace std; 5 namespace { 6 const int DAYS_BEFIRE_MONTH[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; 7 } 8 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 9 if (day <= 0 || day > getMaxDay()) { 10 cout << "Invalid date: "; 11 show(); 12 cout << endl; 13 exit(1); 14 } 15 int years = year - 1; 16 totalDays = years * 365 + years / 4 - years / 100 + years / 400 17 + DAYS_BEFIRE_MONTH[month - 1] + day; 18 if (isLeapYear() && month > 2) totalDays++; 19 } 20 int Date::getMaxDay()const { 21 if (isLeapYear() &&month == 2) 22 return 29; 23 else 24 return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 25 } 26 void Date::show()const { 27 cout << getYear() << "-" << getMonth() << "-" << getDay(); 28 }
accumulator.h:
1 #include "date.h" 2 class Accumulator { 3 private: 4 Date lastDate; 5 double value; 6 double sum; 7 public: 8 Accumulator(const Date&date,double value):lastDate(date),value(value),sum{0}{} 9 double getSum(const Date& date) const { 10 return sum + value * date.distance(lastDate); 11 } 12 void change(const Date& date, double value) { 13 sum = getSum(date); 14 lastDate = date; 15 this->value = value; 16 } 17 void reset(const Date& date, double value) { 18 lastDate = date; 19 this->value; 20 sum = 0; 21 } 22 };
account.h:
1 #include"date.h" 2 #include"accumulator.h" 3 #include<string> 4 using namespace std; 5 class Account { 6 private: 7 string id; 8 double balance; 9 static double total; 10 protected: 11 Account(const Date& date, const string &id); 12 void record(const Date& date, double amount, const string& desc); 13 void error(const string& msg) const; 14 public:const string& getId() { return id; } 15 double getBalance()const { return balance; } 16 static double getTotal() { return total; } 17 void show()const; 18 }; 19 class SavingsAccount:public Account { 20 private: 21 Accumulator acc; 22 double rate; 23 public: 24 SavingsAccount(const Date& date, const string& id, double rate); 25 double getRate() const { return rate; } 26 void deposit(const Date& date, double amount, const string& desc); 27 void withdraw(const Date& date, double amount, const string& desc); 28 void settle(const Date& date); 29 }; 30 class CreditAccount :public Account { 31 private: 32 Accumulator acc; 33 double credit; 34 double rate; 35 double fee; 36 double getDebt()const { 37 double balance = getBalance(); 38 return(balance < 0 ? balance : 0); 39 } 40 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 41 double getCredit()const { return credit; } 42 double getRate()const { return rate; } 43 double getFee() const { return fee; } 44 double getAvailableCredit()const { 45 if (getBalance() < 0)return credit + getBalance(); 46 else return credit; 47 } 48 void deposit(const Date& date, double amount, const string& desc); 49 void withdraw(const Date& date, double amount, const string& desc); 50 void settle(const Date& date); 51 void show()const; 52 };
account.cpp:
1 #include "account.h" 2 #include<iostream> 3 using namespace std; 4 int main() { 5 Date date(2008, 11, 1); 6 SavingsAccount sa1(date, "S3755217", 0.015); 7 SavingsAccount sa2(date, "02342342", 0.015); 8 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 9 sa1.deposit(Date(2008, 11, 5), 5000, "Salary"); 10 ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); 11 sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 12 ca.settle(Date(2008, 12, 1)); 13 ca.deposit(Date(2008, 12,1), 2016, "repay the credit"); 14 sa1.deposit(Date(2008, 12, 5), 5500, "salary"); 15 sa1.settle(Date(2009, 1, 1)); 16 sa2.settle(Date(2009, 1, 1)); 17 ca.settle(Date(2009, 1, 1)); 18 cout << endl; 19 sa1.show(); cout << endl; 20 sa2.show(); cout << endl; 21 ca.show(); cout << endl; 22 cout << "Total: " << Account::getTotal() << endl; 23 return 0; 24 }
实验截图:
改进:每个派生类都将基类的成员原样继承过来,因此无需为每个派生类重复定义数据成员和相应的获取函数
问题:两个派生类SavingsAccount和CreditAccount虽然具有相同的成员函数,但由于其实现不同,只能在派生类中给出它们的实现,因而它们是彼此独立的函数,不能将三个账户放在一个数组里进行操作。