实验任务1
task1_1.cpp
#include <iostream> using std::cout; using std::endl; // 类A的定义 class A { public: A(int x0, int y0); void display() const; private: int x, y; }; A::A(int x0, int y0): x{x0}, y{y0} { } void A::display() const { cout << x << ", " << y << endl; } // 类B的定义 class B { public: B(double x0, double y0); void display() const; private: double x, y; }; B::B(double x0, double y0): x{x0}, y{y0} { } void B::display() const { cout << x << ", " << y << endl; } void test() { cout << "测试类A: " << endl; A a(3, 4); a.display(); cout << "\n测试类B: " << endl; B b(3.2, 5.6); b.display(); } int main() { test(); }
运行结果截图:
task1_2.cpp
#include <iostream> #include <string> using std::cout; using std::endl; using std::string; // 定义类模板 template<typename T> class X{ public: X(T x0, T y0); void display(); private: T x, y; }; template<typename T> X<T>::X(T x0, T y0): x{x0}, y{y0} { } template<typename T> void X<T>::display() { cout << x << ", " << y << endl; } void test() { cout << "测试1: 类模板X中的抽象类型T用int实例化" << endl; X<int> x1(3, 4); x1.display(); cout << endl; cout << "测试2: 类模板X中的抽象类型T用double实例化" << endl; X<double> x2(3.2, 5.6); x2.display(); cout << endl; cout << "测试3: 类模板X中的抽象类型T用string实例化" << endl; X<string> x3("hello", "oop"); x3.display(); } int main() { test(); }
运行结果截图:
实验任务2
GradeCalc.hpp
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <iomanip> using std::vector; using std::string; using std::cin; using std::cout; using std::endl; 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:成绩存储在基类vector<int>的对象里。派生类方法是通过this指针指向vector<int>对象来访问成绩的。input是通过this指针指向基类vector<int>对象来存入数据的。
问题2:分母的功能是作为除数除以总分获得平均分。有影响,乘以1.0代表这个返回值是float类型,有小数点,能正常获得小数点后的值,如果没有就默认不保留小数点后的值。
问题3:在compute成员函数里没有对无效的数据进行操作处理,有时候输入成绩时会产生错误,导致无效成绩被录入。
实验任务3
代码:
GradeCalc.hpp:
1 #pragma once 2 #include <iostream> 3 #include <vector> 4 #include <string> 5 #include <algorithm> 6 #include <numeric> 7 #include <iomanip> 8 9 using std::vector; 10 using std::string; 11 using std::cin; 12 using std::cout; 13 using std::endl; 14 15 class GradeCalc { 16 public: 17 GradeCalc(const string& cname, int size); 18 void input(); // 录入成绩 19 void output() const; // 输出成绩 20 void sort(bool ascending = false); // 排序 (默认降序) 21 int min() const; // 返回最低分 22 int max() const; // 返回最高分 23 float average() const; // 返回平均分 24 void info(); // 输出课程成绩信息 25 26 private: 27 void compute(); // 成绩统计 28 29 private: 30 string course_name; // 课程名 31 int n; // 课程人数 32 vector<int> grades; // 课程成绩 33 vector<int> counts = vector<int>(5, 0); // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] 34 vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 35 }; 36 37 GradeCalc::GradeCalc(const string& cname, int size) : course_name{ cname }, n{ size } {} 38 39 void GradeCalc::input() { 40 int grade; 41 42 for (int i = 0; i < n; ++i) { 43 cin >> grade; 44 grades.push_back(grade); 45 } 46 } 47 48 void GradeCalc::output() const { 49 for (int grade : grades) 50 cout << grade << " "; 51 cout << endl; 52 } 53 54 void GradeCalc::sort(bool ascending) { 55 if (ascending) 56 std::sort(grades.begin(), grades.end()); 57 else 58 std::sort(grades.begin(), grades.end(), std::greater<int>()); 59 60 } 61 62 int GradeCalc::min() const { 63 return *std::min_element(grades.begin(), grades.end()); 64 } 65 66 int GradeCalc::max() const { 67 return *std::max_element(grades.begin(), grades.end()); 68 } 69 70 float GradeCalc::average() const { 71 return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n; 72 } 73 74 void GradeCalc::compute() { 75 for (int grade : grades) { 76 if (grade < 60) 77 counts.at(0)++; 78 else if (grade >= 60 && grade < 70) 79 counts.at(1)++; 80 else if (grade >= 70 && grade < 80) 81 counts.at(2)++; 82 else if (grade >= 80 && grade < 90) 83 counts.at(3)++; 84 else if (grade >= 90) 85 counts.at(4)++; 86 } 87 88 for (int i = 0; i < rates.size(); ++i) 89 rates.at(i) = counts.at(i) * 1.0 / n; 90 } 91 92 void GradeCalc::info() { 93 cout << "课程名称:\t" << course_name << endl; 94 cout << "排序后成绩: \t"; 95 sort(); output(); 96 cout << "最高分:\t" << max() << endl; 97 cout << "最低分:\t" << min() << endl; 98 cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; 99 100 compute(); // 统计各分数段人数、比例 101 102 vector<string> tmp{ "[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]" }; 103 for (int i = tmp.size() - 1; i >= 0; --i) 104 cout << tmp[i] << "\t: " << counts[i] << "人\t" 105 << std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl; 106 }
task3.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:成绩存储在对象的元素vector<int>里。通过vector<int>对象的接口访问到的。实验二是指针指向的基类元素vector<int>里的值,实验三是对象的成员vector<int>里的值。
问题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; 10 cout << "s1:" << s1 << endl; 11 cout << "s2:" << s2 << endl; 12 } 13 14 void test2() { 15 string s1, s2; 16 getline(cin, s1); 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 }
运行结果截图:
问题1:这段代码的作用是清除输入缓冲区中所有字符,直到遇到换行符 '\n'
为止。
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 }
运行结果截图:
问题2:用途是清除输入缓冲区中所有字符,直到遇到换行符 '\n'
为止。
实验任务5
grm.hpp
#ifndef GRM_HPP #define GRM_HPP // 类模板定义 template <typename T> class GameResourceManager { private: T resource; // 私有数据成员,用于存储资源数量,类型为模板参数指定的类型 public: // 带参数的构造函数,用于初始化当前资源数量 GameResourceManager(T initialResource) : resource(initialResource) {} // 获取当前的资源数量 T get() const { return resource; } // 更新当前的资源数量(增加、减少),当资源数量减少到<0时,则归0 void update(T change) { resource += change; if (resource < 0) { resource = 0; } } }; #endif
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
#ifndef INFO_HPP #define INFO_HPP #include <iostream> #include <string> class Info { private: std::string nickname; std::string contact; std::string city; int n; // 预定参加人数 public: // 带参数的构造函数,用于初始化预约信息 Info(const std::string& _nickname, const std::string& _contact, const std::string& _city, int _n) : nickname(_nickname), contact(_contact), city(_city), n(_n) {} // 显示信息的函数 void display() const { std::cout << "昵称: " << nickname << std::endl; std::cout << "联系方式: " << contact << std::endl; std::cout << "所在城市: " << city << std::endl; std::cout << "预定参加人数: " << n << std::endl; } }; #endif
task6.cpp
#include <iostream> #include <vector> #include <string> #include "info.hpp" using namespace std; int main() { const int capacity = 100; // livehouse最多能容纳的听众人数 vector<Info> audience_lst; // 存放线上预约登记的所有听众信息 int total_num = 0; // 当前已预约的总人数 while (true) { if (total_num >= capacity) { cout << "已达到场地最大容纳人数,预约结束。" << endl; break; } string nickname, contact, city; int n; cout << "请输入昵称: "; getline(cin, nickname); cout << "请输入联系方式(可以是邮箱或手机号): "; getline(cin, contact); cout << "请输入所在城市: "; getline(cin, city); cout << "请输入预定参加人数: "; cin >> n; // 处理超出剩余容量的情况 if (total_num + n > capacity) { cout << "预定人数超出场地剩余容量,输入q退出预定,输入u更新预定信息: "; string choice; cin >> choice; cin.ignore(); // 清除输入缓冲区的换行符,避免影响后续的getline读取 if (choice == "q") { continue; } else if (choice == "u") { cout << "请重新输入预定参加人数: "; cin >> n; cin.ignore(); } } Info new_info(nickname, contact, city, n); audience_lst.push_back(new_info); total_num += n; cin.ignore(); // 清除输入缓冲区的换行符,避免影响下一轮循环的输入读取 } // 打印输出预约参加livehouse的听众信息 cout << "预约参加livehouse的听众信息如下:" << endl; for (const auto& info : audience_lst) { info.display(); cout << "---------------------------" << endl; } return 0; }
运行结果截图:
实验任务7
date.h:
1 #pragma once 2 #ifndef DATE H 3 #define DATE H 4 class Date { 5 private: 6 int year; 7 int month; 8 int day; 9 int totalDays; 10 public: 11 Date(int year, int month, int day); 12 int getYear()const { return year; } 13 int getMonth()const { return month; } 14 int getDay()const { return day; } 15 int getMaxDay()const; 16 bool isLeapYear()const { 17 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 18 } 19 void show()const; 20 int distance(const Date& date)const { 21 return totalDays - date.totalDays; 22 } 23 }; 24 #endif// DATE H
date.cpp:
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 using namespace std; 5 namespace { 6 const int DAYS_BEFORE_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 + DAYS_BEFORE_MONTH[month - 1] + day; 17 if (isLeapYear() && month > 2)totalDays++; 18 } 19 int Date::getMaxDay()const { 20 if (isLeapYear() && month == 2) 21 return 29; 22 else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 23 } 24 25 void Date::show()const { 26 cout << getYear() << "-" << getMonth() << "-" << getDay(); 27 }
accumulator.h:
1 #pragma once 2 #ifndef ACCUMULATOR H 3 #define ACCUMULATOR H 4 #include"date.h" 5 class Accumulator { 6 private: 7 Date lastDate; 8 double value; 9 double sum; 10 public: 11 Accumulator(const Date& date, double value) :lastDate(date), value(value), sum{ 0 } { 12 } 13 14 double getSum(const Date& date)const { 15 return sum + value * date.distance(lastDate); 16 } 17 18 void change(const Date& date, double value) { 19 sum = getSum(date); 20 lastDate = date; this->value = value; 21 } 22 23 void reset(const Date& date, double value) { 24 lastDate = date; this->value = value; sum = 0; 25 } 26 }; 27 #endif//ACCUMULATOR H
account.h:
1 #pragma once 2 #ifndef ACCOUNT H 3 #define ACCOUNT H 4 #include"date.h" 5 #include"accumulator.h" 6 #include<string> 7 class Account { 8 private: 9 std::string id; 10 double balance; 11 static double total; 12 protected: 13 Account(const Date& date, const std::string& id); 14 void record(const Date& date, double amount, const std::string& desc); 15 void error(const std::string& msg)const; 16 public: 17 const std::string& getId()const { return id; } 18 double getBalance()const { return balance; } 19 static double getTotal() { return total; } 20 21 void show()const; 22 }; 23 class SavingsAccount :public Account { 24 private: 25 Accumulator acc; 26 double rate; 27 public: 28 SavingsAccount(const Date& date, const std::string& id, double rate); 29 double getRate()const { return rate; } 30 31 void deposit(const Date& date, double amount, const std::string& desc); 32 void withdraw(const Date& date, double amount, const std::string& desc); 33 void settle(const Date& date); 34 }; 35 class CreditAccount :public Account { 36 private: 37 Accumulator acc; 38 double credit; 39 double rate; 40 double fee; 41 double getDebt()const { 42 double balance = getBalance(); 43 return (balance < 0 ? balance : 0); 44 } 45 public: 46 CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); 47 double getCredit()const { return credit; } 48 double getRate()const { return rate;} 49 double getAvailableCredit()const { 50 if (getBalance() < 0) 51 return credit + getBalance(); 52 else 53 return credit; 54 } 55 void deposit(const Date& date, double amount, const std::string& desc); 56 void withdraw(const Date& date, double amount, const std::string& desc); 57 void settle(const Date& date); 58 void show()const; 59 }; 60 #endif//ACCOUNT H
account.cpp:
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 double Account::total = 0; 6 7 Account::Account(const Date& date, const string& id) :id{ id }, balance{ 0 } { 8 date.show(); cout << "\t#" << id << "created" << endl; 9 } 10 11 12 void Account::record(const Date& date, double amount, const string& desc) { 13 amount = floor(amount * 100 + 0.5) / 100; 14 balance += amount; 15 total += amount; 16 date.show(); 17 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 18 } 19 20 void Account::show()const { cout << id << "\tBalance:" << balance; } 21 void Account::error(const string& msg)const { 22 cout << "Error(#" << id << "):" << msg << endl; 23 } 24 25 SavingsAccount::SavingsAccount(const Date&date,const string&id,double rate):Account(date,id),rate(rate), acc(date,0){} 26 27 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 28 record(date, amount, desc); 29 acc.change(date, getBalance()); 30 } 31 32 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 33 if (amount > getBalance()) { 34 error("not enough money"); 35 } 36 else { 37 record(date, -amount, desc); 38 acc.change(date, getBalance()); 39 } 40 } 41 42 void SavingsAccount::settle(const Date& date) { 43 double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); 44 if (interest != 0)record(date, interest, "interest"); 45 acc.reset(date, getBalance()); 46 } 47 48 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){} 49 50 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 51 record(date, amount, desc); 52 acc.change(date, getDebt()); 53 } 54 55 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 56 if (amount - getBalance() > credit) { 57 error("not enough credit"); 58 } 59 else { 60 record(date, -amount, desc); 61 acc.change(date, getDebt()); 62 } 63 } 64 65 void CreditAccount::settle(const Date& date) { 66 double interest = acc.getSum(date) * rate; 67 if (interest != 0)record(date, interest, "interest"); 68 if (date.getMonth() == 1) 69 record(date, -fee, "annual fee"); 70 acc.reset(date, getDebt()); 71 } 72 73 void CreditAccount::show()const { 74 Account::show(); 75 cout << "\tAvailable credit:" << getAvailableCredit(); 76 }
task7.cpp:
1 #include"account.h" 2 #include<iostream> 3 4 using namespace std; 5 6 int main() { 7 Date date(2008, 11, 1); 8 SavingsAccount sa1(date, "S3755217", 0.015); 9 SavingsAccount sa2(date, "02342342", 0.015); 10 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 11 12 sa1.deposit(Date(2008, 11, 5), 5000, "salary"); 13 ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); 14 sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 15 16 ca.settle(Date(2008, 12, 1)); 17 18 ca.deposit(Date(2008, 12, 1), 2016, "repay the credit"); 19 sa1.deposit(Date(2008, 12, 5), 5500, "salary"); 20 21 sa1.settle(Date(2009, 1, 1)); 22 sa2.settle(Date(2009, 1, 1)); 23 ca.settle(Date(2009, 1, 1)); 24 25 cout << endl; 26 sa1.show(); cout << endl; 27 sa2.show(); cout << endl; 28 ca.show(); cout << endl; 29 cout << "Total:" << Account::getTotal() << endl; 30 return 0; 31 }
运行结果:
总结:
运用了类的继承,定义一个基类account,并从基类继承得到了两个派生类SavingAccounts和CreditAccounts。
运用多个函数,增加了代码的功能
不足:有些代码过于繁琐,应做适当改进
标签:std,const,组合,继承,void,int,date,include,模板 From: https://www.cnblogs.com/xuruize/p/18560243