cpp 实验4
1 .hpp 2 #pragma once 3 #include<iostream> 4 #include<vector> 5 #include<string> 6 #include<algorithm> 7 #include<numeric> 8 #include<iomanip> 9 using namespace std; 10 class GradeCalc :public vector<int> { 11 public: 12 GradeCalc(const string& cname, int size); 13 void input(); 14 void output()const; 15 void sort(bool ascending = false); 16 int min()const; 17 int max()const; 18 float average()const; 19 void info(); 20 private: 21 void compute(); 22 private: 23 string course_name; 24 int n; 25 vector<int> counts = vector<int>(5, 0); 26 vector<double> rates = vector<double>(5, 0); 27 }; 28 GradeCalc::GradeCalc(const string& cname, int size) :course_name{ cname }, n{ size } {} 29 void GradeCalc::input() { 30 int grade; 31 for (int i = 0; i < n; i++) { 32 cin >> grade; 33 this->push_back(grade); 34 } 35 } 36 void GradeCalc::output()const { 37 for (auto ptr = this->begin(); ptr != this->end(); ptr++) { 38 cout << *ptr << " "; 39 } 40 cout << endl; 41 } 42 void GradeCalc::sort(bool ascending) { 43 if (ascending) std::sort(this->begin(), this->end()); 44 else std::sort(this->begin(), this->end(), greater<int>()); 45 } 46 int GradeCalc::min()const { 47 return *min_element(this->begin(), this->end()); 48 } 49 int GradeCalc::max()const { 50 return *max_element(this->begin(), this->end()); 51 } 52 float GradeCalc::average()const { 53 return accumulate(this->begin(), this->end(), 0) * 1.0 / n; 54 } 55 void GradeCalc::compute() { 56 for (int grade : *this) { 57 if (grade < 60) counts.at(0)++; 58 else if (grade >= 60 && grade < 70) counts.at(1)++; 59 else if (grade >= 70 && grade < 80) counts.at(2)++; 60 else if (grade >= 80 && grade < 90) counts.at(3)++; 61 else if (grade >= 90)counts.at(4)++; 62 63 } 64 for (int i = 0; i < rates.size(); i++) { 65 rates.at(i) = counts.at(i) * 1.0 / n; 66 } 67 } 68 void GradeCalc::info() { 69 cout << "课程名称:\t" << course_name << endl; 70 cout << "排序后课程成绩: \t"; 71 sort(); output(); 72 cout << "最高分:\t" << max() << endl; 73 cout << "最低分:\t" << min() << endl; 74 cout << "平均分:\t" << fixed << setprecision(2) << average() << endl; 75 compute(); 76 vector<string> tmp{ "[0,60) ","[60,70)","[70,80)","[80,90)","[90,100]" }; 77 for (int i = tmp.size() - 1; i >= 0; i--) 78 cout << tmp[i] << "\t: " << counts[i] << "人\t" << fixed << setprecision(2) << rates[i] * 100 << "%" << endl; 79 } 80 81 82 .cpp 83 #include "GradeCalc.hpp" 84 #include<iomanip> 85 void test() { 86 int n; 87 cout << "请输入班级人数: "; 88 cin >> n; 89 GradeCalc c1("OPP", n); 90 cout << "录入成绩: " << endl; 91 c1.input(); 92 cout << "输出成绩: " << endl; 93 c1.output(); 94 cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; 95 c1.info(); 96 } 97 int main() { 98 test(); 99 }
task2
问题1:储存在它继承的基类vector中通过public继承方式继承的基类中的protected接口访问每一个成绩,和实现数据存入.
问题2:计算this所指向容器所有元素的和并取平均。会。整形数据整除会导致数据小数部分的丢失,乘以1.0将分母转化成浮点型数据。
问题3:缺乏对输入数据合法性的判断,如果输入的数据中存在不合法的数据,会导致程序运行结果出错。
1 .hpp 2 #pragma once 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 #include <algorithm> 7 #include <numeric> 8 #include <iomanip> 9 10 using std::vector; 11 using std::string; 12 using std::cin; 13 using std::cout; 14 using std::endl; 15 16 class GradeCalc { 17 public: 18 GradeCalc(const string& cname, int size); 19 void input(); // 录入成绩 20 void output() const; // 输出成绩 21 void sort(bool ascending = false); // 排序 (默认降序) 22 int min() const; // 返回最低分 23 int max() const; // 返回最高分 24 float average() const; // 返回平均分 25 void info(); // 输出课程成绩信息 26 27 private: 28 void compute(); // 成绩统计 29 30 private: 31 string course_name; // 课程名 32 int n; // 课程人数 33 vector<int> grades; // 课程成绩 34 vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] 35 vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 36 }; 37 38 GradeCalc::GradeCalc(const string& cname, int size) : course_name{ cname }, n{ size } {} 39 40 void GradeCalc::input() { 41 int grade; 42 43 for (int i = 0; i < n; ++i) { 44 cin >> grade; 45 grades.push_back(grade); 46 } 47 } 48 49 void GradeCalc::output() const { 50 for (int grade : grades) 51 cout << grade << " "; 52 cout << endl; 53 } 54 55 void GradeCalc::sort(bool ascending) { 56 if (ascending) 57 std::sort(grades.begin(), grades.end()); 58 else 59 std::sort(grades.begin(), grades.end(), std::greater<int>()); 60 61 } 62 63 int GradeCalc::min() const { 64 return *std::min_element(grades.begin(), grades.end()); 65 } 66 67 int GradeCalc::max() const { 68 return *std::max_element(grades.begin(), grades.end()); 69 } 70 71 float GradeCalc::average() const { 72 return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n; 73 } 74 75 void GradeCalc::compute() { 76 for (int grade : grades) { 77 if (grade < 60) 78 counts.at(0)++; 79 else if (grade >= 60 && grade < 70) 80 counts.at(1)++; 81 else if (grade >= 70 && grade < 80) 82 counts.at(2)++; 83 else if (grade >= 80 && grade < 90) 84 counts.at(3)++; 85 else if (grade >= 90) 86 counts.at(4)++; 87 } 88 89 for (int i = 0; i < rates.size(); ++i) 90 rates.at(i) = counts.at(i) * 1.0 / n; 91 } 92 93 void GradeCalc::info() { 94 cout << "课程名称:\t" << course_name << endl; 95 cout << "排序后成绩: \t"; 96 sort(); output(); 97 cout << "最高分:\t" << max() << endl; 98 cout << "最低分:\t" << min() << endl; 99 cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; 100 101 compute(); // 统计各分数段人数、比例 102 103 vector<string> tmp{ "[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]" }; 104 for (int i = tmp.size() - 1; i >= 0; --i) 105 cout << tmp[i] << "\t: " << counts[i] << "人\t" 106 << std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl; 107 } 108 109 110 .cpp 111 #include "GradeCalc.hpp" 112 #include <iomanip> 113 114 void test() { 115 int n; 116 cout << "输入班级人数: "; 117 cin >> n; 118 119 GradeCalc c1("OOP", n); 120 121 cout << "录入成绩: " << endl;; 122 c1.input(); 123 cout << "输出成绩: " << endl; 124 c1.output(); 125 126 cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; 127 c1.info(); 128 } 129 130 int main() { 131 test(); 132 }
task3
问题1:储存在类中的私有数据成员vector<int> grades中。直接访问类中的私有成员grades。
问题2:在某些情况下可以不用继承基类,而直接将基类作为自定义类的数据元素,但是使用类的公有继承则可以直接调用基类的接口,但是组合类时不可以直接从外部调用它的接口,需要进行封装。
1 1. 2 #include<iostream> 3 #include<string> 4 #include<limits> 5 using namespace std; 6 void test1() { 7 string s1, s2; 8 cin >> s1 >> s2; 9 cout << "s1: " << s1 << endl; 10 cout << "s2: " << s2 << endl; 11 } 12 void test2() { 13 string s1, s2; 14 getline(cin, s1); 15 getline(cin, s2); 16 cout << "s1: " << s1 << endl; 17 cout << "s2: " << s2 << endl; 18 } 19 void test3() { 20 string s1, s2; 21 getline(cin, s1, ' '); 22 getline(cin, s2); 23 cout << "s1: " << s1 << endl; 24 cout << "s2: " << s2 << endl; 25 } 26 int main() { 27 cout << "测试1: 使用标准输入流对象cin输入字符串" << endl; 28 test1(); 29 cout << endl; 30 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 31 cout << "测试2: 使用函数getline()输入字符串" << endl; 32 test2(); 33 cout << endl; 34 cout << "测试3: 使用函数getline()输入字符串, 指定字符串分隔符" << endl; 35 test3(); 36 37 } 38 39 2. 40 #include<iostream> 41 #include<string> 42 #include<vector> 43 #include<limits> 44 using namespace std; 45 void output(const vector<string>& v) { 46 for (auto& s : v) 47 cout << s << endl; 48 } 49 void test() { 50 int n; 51 while (cout << "Enter n: ", cin >> n) { 52 vector<string> v1; 53 for (int i = 0; i < n; i++) { 54 string s; 55 cin >> s; 56 v1.push_back(s); 57 } 58 cout << "output v1: " << endl; 59 output(v1); 60 cout << endl; 61 } 62 63 } 64 int main() { 65 cout << "测试: 使用cin多组输入字符串" << endl; 66 test(); 67 } 68 69 70 3. 71 #include<iostream> 72 #include<string> 73 #include<vector> 74 #include<climits> 75 using namespace std; 76 void output(const vector<string>& v) { 77 for (auto& s : v) 78 cout << s << endl; 79 } 80 void test() { 81 int n; 82 while (cout << "Enter n: ", cin >> n) { 83 vector<string> v2; 84 for (int i = 0; i < n; i++) { 85 string s; 86 getline(cin, s); 87 v2.push_back(s); 88 } 89 cout << "output v2: " << endl; 90 output(v2); 91 cout << endl; 92 } 93 } 94 int main() { 95 cout << "测试: 使用函数getline()多组输入字符串" << endl; 96 test(); 97 return 0; 98 }
task4 在读取整数(或其他数据类型)时,cin 会读取输入,但它不会清除缓冲区中的换行符(即用户按下回车键时生成的 '\n')。如果你随后使用 getline() 来读取字符串,getline() 会立即遇到这个换行符并认为输入已经结束。因此,我们使用 cin.ignore() 来跳过这个换行符,确保后续的 getline() 能正确读取用户的字符串输入。
1 hpp 2 #pragma once 3 #include<iostream> 4 using namespace std; 5 template<typename T> 6 class GameResourceManager { 7 private: 8 T resouce; 9 public: 10 GameResourceManager(T Resource):resouce(Resource){} 11 T get()const { 12 return resouce; 13 } 14 void update(T num) { 15 resouce += num; 16 if (resouce < 0) resouce = 0; 17 } 18 19 }; 20 21 22 23 cpp 24 #include "grm.hpp" 25 #include <iostream> 26 27 using std::cout; 28 using std::endl; 29 30 void test1() { 31 GameResourceManager<float> HP_manager(99.99); 32 cout << "当前生命值: " << HP_manager.get() << endl; 33 HP_manager.update(9.99); 34 cout << "增加9.99生命值后, 当前生命值: " << HP_manager.get() << endl; 35 HP_manager.update(-999.99); 36 cout << "减少999.99生命值后, 当前生命值: " << HP_manager.get() << endl; 37 } 38 39 void test2() { 40 GameResourceManager<int> Gold_manager(100); 41 cout << "当前金币数量: " << Gold_manager.get() << endl; 42 Gold_manager.update(50); 43 cout << "增加50个金币后, 当前金币数量: " << Gold_manager.get() << endl; 44 Gold_manager.update(-99); 45 cout << "减少99个金币后, 当前金币数量: " << Gold_manager.get() << endl; 46 } 47 48 49 int main() { 50 cout << "测试1: 用float类型对类模板GameResourceManager实例化" << endl; 51 test1(); 52 cout << endl; 53 54 cout << "测试2: 用int类型对类模板GameResourceManager实例化" << endl; 55 test2(); 56 }
1 .hpp 2 #pragma once 3 #include<iostream> 4 #include<iomanip> 5 using namespace std; 6 class Info { 7 public: 8 string nickname; 9 string contact; 10 string city; 11 int n; 12 public: 13 Info(string nickname_,string contact_,string city_,int n_):nickname(nickname_),contact(contact_),city(city_), n(n_) {} 14 void display()const { 15 cout << string(40, '*') << endl; 16 cout << "昵称:" << "\t" << nickname << endl; 17 cout << "联系方式:" << "\t" << contact << endl; 18 cout << "所在城市:" << "\t" << city << endl; 19 cout << "预定人数:" << "\t" << n << endl; 20 } 21 }; 22 23 24 .cpp 25 #include"Info.h" 26 #include<iomanip> 27 #include<vector> 28 const int capacity = 100; 29 int main() { 30 cout << "录入用户预约信息:" << endl; 31 cout << "录入用户数:"; 32 int n; 33 cin >> n; 34 cout << "昵称" << "\t" << "联系方式(邮箱/手机号)" << "\t" << "所在城市" << "\t" << "预定参加人数" << endl; 35 vector<Info> v; 36 string nickname; 37 string contact; 38 string city; 39 int num; 40 int sum = 0; 41 for (int i = 0; i < n; i++) { 42 cin >> nickname >> contact >> city >> num; 43 sum += num; 44 if (sum > capacity) { 45 char choice; 46 cout << "对不起,只剩" << 100 + num - sum << "个位置" << endl; 47 cout << "1. 输入u,更新(update)预定信息" << endl; 48 cout << "2. 输入q,退出预定" << endl; 49 cout << "你的选择: "; 50 cin >> choice; 51 if (choice == 'u') { 52 sum -= num; 53 cout << "请重新输入预定信息" << endl; 54 cin >> nickname >> contact >> city >> num; 55 sum += num; 56 } 57 else if(choice=='q') { 58 continue; 59 } 60 } 61 v.push_back(Info(nickname, contact, city, num)); 62 } 63 cout << "截至目前,一共有" << sum << "位听众预约。" << "预约听众信息如下:" << endl; 64 for (auto i : v) { 65 i.display(); 66 } 67 cout << string(40, '*'); 68 return 0; 69 }
1 date.h 2 #pragma once 3 class Date { 4 private: 5 int year; 6 int month; 7 int day; 8 int totalDays; 9 public: 10 Date(int year, int month, int day); 11 int getYear()const { return year; } 12 int getMonth()const { return month; } 13 int getDay()const { return day; } 14 int getMaxDay()const; 15 bool isLeapYear()const { 16 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 17 } 18 void show() const; 19 int distance(const Date& date)const { 20 return totalDays - date.totalDays; 21 } 22 }; 23 accumulator.h 24 #pragma once 25 #include "date.h" 26 class Accumulator { 27 private: 28 Date lastDate; 29 double value; 30 double sum; 31 public: 32 Accumulator(const Date&date,double value):lastDate(date),value(value),sum{0}{} 33 double getSum(const Date& date) const { 34 return sum + value * date.distance(lastDate); 35 } 36 void change(const Date& date, double value) { 37 sum = getSum(date); 38 lastDate = date; 39 this->value = value; 40 } 41 void reset(const Date& date, double value) { 42 lastDate = date; 43 this->value; 44 sum = 0; 45 } 46 }; 47 account.h 48 #pragma once 49 #include"date.h" 50 #include"accumulator.h" 51 #include<string> 52 using namespace std; 53 class Account { 54 private: 55 string id; 56 double balance; 57 static double total; 58 protected: 59 Account(const Date& date, const string &id); 60 void record(const Date& date, double amount, const string& desc); 61 void error(const string& msg) const; 62 public:const string& getId() { return id; } 63 double getBalance()const { return balance; } 64 static double getTotal() { return total; } 65 void show()const; 66 }; 67 class SavingsAccount:public Account { 68 private: 69 Accumulator acc; 70 double rate; 71 public: 72 SavingsAccount(const Date& date, const string& id, double rate); 73 double getRate() const { return rate; } 74 void deposit(const Date& date, double amount, const string& desc); 75 void withdraw(const Date& date, double amount, const string& desc); 76 void settle(const Date& date); 77 }; 78 class CreditAccount :public Account { 79 private: 80 Accumulator acc; 81 double credit; 82 double rate; 83 double fee; 84 double getDebt()const { 85 double balance = getBalance(); 86 return(balance < 0 ? balance : 0); 87 } 88 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 89 double getCredit()const { return credit; } 90 double getRate()const { return rate; } 91 double getFee() const { return fee; } 92 double getAvailableCredit()const { 93 if (getBalance() < 0)return credit + getBalance(); 94 else return credit; 95 } 96 void deposit(const Date& date, double amount, const string& desc); 97 void withdraw(const Date& date, double amount, const string& desc); 98 void settle(const Date& date); 99 void show()const; 100 }; 101 102 date.cpp 103 #include"date.h" 104 #include<iostream> 105 #include<cstdlib> 106 using namespace std; 107 namespace { 108 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 109 } 110 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 111 if (day <= 0 || day > getMaxDay()) { 112 cout << "Invalid date: "; 113 show(); 114 cout << endl; 115 exit(1); 116 } 117 int years = year - 1; 118 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 119 if (isLeapYear() && month > 2) totalDays++; 120 } 121 int Date::getMaxDay()const { 122 if (isLeapYear() && month == 2) 123 return 29; 124 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 125 } 126 void Date::show()const { 127 cout << getYear() << "-" << getMonth() << "-" << getDay(); 128 } 129 130 accumulator.cpp 131 #include "account.h" 132 #include <cmath> 133 #include<iostream> 134 using namespace std; 135 double Account::total = 0; 136 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 137 date.show(); 138 cout << "\t#" << id << "created" << endl; 139 } 140 void Account::record(const Date& date, double amount, const string& desc) { 141 amount = floor(amount * 100 + 0.5) / 100; 142 balance += amount; 143 total += amount; 144 date.show(); 145 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 146 } 147 void Account::show()const { cout << id << "\tBalance:" << balance; } 148 void Account::error(const string& msg)const { 149 cout << "Error(#" << id << "):" << msg << endl; 150 } 151 SavingsAccount::SavingsAccount(const Date&date,const string &id,double rate):Account(date,id),rate(rate),acc(date,0){} 152 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 153 record(date, amount, desc); 154 acc.change(date, getBalance()); 155 } 156 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 157 if (amount > getBalance()) { 158 error("not enough money"); 159 } 160 else { 161 record(date, -amount, desc); 162 acc.change(date, getBalance()); 163 } 164 } 165 void SavingsAccount::settle(const Date& date) { 166 double interest = acc.getSum(date) * rate/date.distance(Date(date.getYear()-1,1,1)); 167 if (interest != 0)record(date, interest, "interest"); 168 acc.reset(date, getBalance()); 169 } 170 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){} 171 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 172 record(date, amount, desc); 173 acc.change(date, getDebt()); 174 } 175 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 176 if (amount - getBalance() > credit) { 177 error("not enouogh credit"); 178 } 179 else { 180 record(date, -amount, desc); 181 acc.change(date, getDebt()); 182 } 183 } 184 void CreditAccount::settle(const Date& date) { 185 double interest = acc.getSum(date) * rate; 186 if (interest != 0) record(date, interest, "interest"); 187 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 188 acc.reset(date, getDebt()); 189 } 190 void CreditAccount::show()const { 191 Account::show(); 192 cout << "\tAvailable credit:" << getAvailableCredit(); 193 } 194 task7.cpp 195 #include "account.h" 196 #include<iostream> 197 using namespace std; 198 int main() { 199 Date date(2008, 11, 1); 200 SavingsAccount sa1(date, "S3755217", 0.015); 201 SavingsAccount sa2(date, "02342342", 0.015); 202 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 203 sa1.deposit(Date(2008, 11, 5), 5000, "Salary"); 204 ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); 205 sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 206 ca.settle(Date(2008, 12, 1)); 207 ca.deposit(Date(2008, 12,1), 2016, "repay the credit"); 208 sa1.deposit(Date(2008, 12, 5), 5500, "salary"); 209 sa1.settle(Date(2009, 1, 1)); 210 sa2.settle(Date(2009, 1, 1)); 211 ca.settle(Date(2009, 1, 1)); 212 cout << endl; 213 sa1.show(); cout << endl; 214 sa2.show(); cout << endl; 215 ca.show(); cout << endl; 216 cout << "Total: " << Account::getTotal() << endl; 217 return 0; 218 219 }
task7
1.定义一个基类,account,并从基类继承得到了两个派生类SavingAccounts和CreditAccounts.
2.抽象出了一个新的类accumulator专门用于计算银行账户存款的积累.
不足:虽然派生类中有相同的成员函数,deposit,withdraw,settle,但是由于实现不同只能在各自派生类中定义。