实验任务1
实验任务2
1派生类GreadeCalc定义中,继承了vector<int>部分,因此储存在了用来储存整数的容器;派生类方法通过this->指针访问成绩;input方法通过this->push->back(grade)来实现数据存入对象.
2.line68分母的功能是作为除数计算出平均分;去掉*1.0对结果有影响,乘1.0可以将结果精确到小数点后两位,没有*1.0将会把平均分取整,影响实际结果。
3.数据验证,如果输入一个错误数据,系统应自动返回并给予提醒,并重新输入一个新数据
性能优化,如果学生基数大,造成数据的庞大,会导致程序的复杂度加大,不利于实际运用。
实验任务3
1.组合类GradCalc定义中,成绩储存在vector<int>中;组合类方法sort等访问成绩通过直接访问类的私有成员grade
2.类接口内部实现可以用this->指针代替直接访问类中私有成员,增强程序的安全性
实验任务4
.
1.cin.ignore用于忽略输入流中的字符,cin.ignore(n,delim)中n是忽略的字符数,delim是要停止忽略的字符,如果 n
被设置为 numeric_limits<streamsize>::max(),则意味着忽略直到达到最大可能的字符数量,这通常用于确保忽略掉直到下一个特定分隔符(如换行符)为止的所有字符。
2.
3.
这行代码的作用依然是为了清除输入缓冲区中可能存在的任何剩余字符,尤其是上一个输入操作后留下的换行符
实验任务5
1 #pragma once 2 #include<iostream> 3 #include <limits> 4 5 6 using namespace std; 7 template<typename T> 8 9 class GameResourceManager { 10 private: 11 T resource; 12 13 public: 14 GameResourceManager(T initial_resource) : resource(initial_resource) {} 15 16 // 获取当前的资源数量 17 T get() const { 18 return resource; 19 } 20 21 void update(T delta) { 22 resource += delta; 23 if (resource < 0) { 24 resource = 0; 25 } 26 } 27 };
实验任务6
1 #pragma once 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 6 class Info { 7 private: 8 string nickname; 9 string contact; 10 string city; 11 int n; // 预定参加人数 12 13 public: 14 // 带参数的构造函数,用于初始化预约信息 15 Info(const string& nick, const string& cont, const string& cit, int num) 16 : nickname(nick), contact(cont), city(cit), n(num) {} 17 18 // 显示信息 19 void display() const { 20 cout << "昵称: " << nickname << ", 联系方式: " << contact << ", 所在城市: " << city << ", 预定参加人数: " << n << endl; 21 } 22 23 // 获取预定人数 24 int getNumber() const { 25 return n; 26 } 27 };
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include "info.hpp" 5 6 using namespace std; 7 8 const int capacity = 100; // livehouse最多能容纳的听众人数 9 vector<Info> audience_lst; // 用于存放线上预约登记的所有听众信息 10 11 void addAudience() { 12 string nickname, contact, city; 13 int n; 14 15 while (true) { 16 cout << "请输入昵称: "; 17 getline(cin, nickname); 18 if (nickname == "q" ) { 19 cout << "结束信息输入" << endl; 20 break; 21 } 22 23 cout << "请输入联系方式(email或手机号): "; 24 getline(cin, contact); 25 26 cout << "请输入所在城市: "; 27 getline(cin, city); 28 29 cout << "请输入预定参加人数: "; 30 cin >> n; 31 cin.ignore(); // 忽略换行符 32 33 // 检查预定人数是否超过剩余容量 34 int total_booked = 0; 35 for (const auto& info : audience_lst) { 36 total_booked += info.getNumber(); 37 } 38 if (total_booked + n > capacity) { 39 cout << "预定人数超过场地剩余容量,请输入q退出预定,或输入u更新预定信息。" << endl; 40 char choice; 41 cout << "请输入q退出或u更新: "; 42 cin >> choice; 43 cin.ignore(); // 忽略换行符 44 if (choice == 'q' || choice == 'Q') { 45 cout << "已取消预定。" << endl; 46 break; 47 } 48 else if (choice == 'u' || choice == 'U') { 49 continue; // 重新输入预定信息 50 } 51 else { 52 cout << "无效选择,请重新输入。" << endl; 53 continue; // 或者可以选择退出循环,具体取决于需求 54 } 55 } 56 else { 57 audience_lst.emplace_back(nickname, contact, city, n); 58 cout << "预定成功!" << endl; 59 break; // 或者可以选择继续添加更多预定,具体取决于需求 60 } 61 } 62 } 63 64 void printAudienceInfo() { 65 cout << "预约参加livehouse的听众信息如下:" << endl; 66 for (const auto& info : audience_lst) { 67 info.display(); 68 } 69 } 70 71 int main() { 72 char choice; 73 do { 74 cout << "请输入新的预定信息(输入q退出):" << endl; 75 addAudience(); 76 77 cout << "是否继续输入新的预定信息?(y/n): "; 78 cin >> choice; 79 cin.ignore(); // 忽略换行符 80 } while (choice == 'y' ); 81 82 printAudienceInfo(); 83 84 return 0; 85 }
实验任务7
//account.h #pragma once #ifndef ACCOUNT H #define ACCOUNT H #include"date.h" #include"accumulator.h" #include<string> class Account { private: std::string id; double balance; static double total; protected: Account(const Date& date, const std::string& id); void record(const Date& date, double amount, const std::string& desc); void error(const std::string& msg)const; 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 getAvailableCredit()const { if (getBalance() < 0) return credit + getBalance(); else 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 H
//accumulator.h #pragma once #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//ACCUMULATOR H#pragma once
//date.h #pragma once #ifndef DATE H #define DATE H class Date { private: int year; int month; int day; int totalDays; 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 H
//account.cpp #include"account.h" #include<cmath> #include<iostream> using namespace std; double Account::total = 0; 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(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(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()); } void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { if (amount - getBalance() > credit) { error("not enough credit"); } else { record(date, -amount, desc); acc.change(date, getDebt()); } } void CreditAccount::settle(const Date& date) { double interest = acc.getSum(date) * rate; if (interest != 0)record(date, interest, "interest"); if (date.getMonth() == 1) record(date, -fee, "annual fee"); acc.reset(date, getDebt()); } void CreditAccount::show()const { Account::show(); cout << "\tAvailable credit:" << getAvailableCredit(); }
//date.cpp #include"date.h" #include<iostream> #include<cstdlib> using namespace std; namespace { 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(); }
//tsak7.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; }
标签:const,string,int,double,Date,实验,date,程序设计 From: https://www.cnblogs.com/CK1NG/p/18553209