首页 > 其他分享 >实验五_OOP_张文瑞_202213260018

实验五_OOP_张文瑞_202213260018

时间:2023-12-04 09:44:25浏览次数:35  
标签:std const string 张文瑞 double Date OOP date 202213260018

实验任务3
pets.hpp源码

 1 #include <iostream>
 2 using std::string;
 3 class MachinePets {
 4     private:
 5         string nickname;
 6     public:
 7         MachinePets(const string s);
 8         string get_nickname() const;
 9         virtual string talk() = 0;
10 };
11 MachinePets::MachinePets(const string s) : nickname{s} {}
12 string MachinePets::get_nickname() const {return nickname;};
13 class PetCats : public MachinePets {
14     public:
15         PetCats(const string s);
16         string talk() override;
17 };
18 PetCats::PetCats(const string s) : MachinePets{s} {}
19 string PetCats::talk() {
20     return "miao wu~";
21 }
22 class PetDogs : public MachinePets {
23     public:
24         PetDogs(const string s);
25         string talk() override;
26 };
27 PetDogs::PetDogs(const string s) : MachinePets{s} {}
28 string PetDogs::talk() {
29     return "wang wang~";
30 }
View Code

task3.cpp源码

 1 #include <iostream>
 2 #include "pets.hpp"
 3 
 4 void play(MachinePets &obj) {
 5     std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
 6 }
 7 
 8 void test() {
 9     PetCats cat("miku");
10     PetDogs dog("da huang");
11 
12     play( cat );
13     play( dog );
14 }
15 
16 int main() {
17     test();
18 }
View Code

运行截图:

 实验任务4
Person.hpp源码

 1 #include<iostream>
 2 using std::string;
 3 class Person {
 4     private:
 5         string name;
 6         string telephone;
 7         string email;
 8     public:
 9         Person() = default;
10         Person(string n, string t, string e = "");
11         Person(const Person &p);
12         ~Person() = default;
13         void update_telephone();
14         void update_email();
15         friend std::istream& operator>>(std::istream &i, Person &p);
16         friend std::ostream& operator<<(std::ostream &o, const Person &p);
17         friend bool operator==(const Person &p1, const Person &p2);
18 };
19 Person::Person(string n, string t, string e)
20     : name{n}, telephone{t}, email{e} {
21 }
22 Person::Person(const Person &p)
23     : name{p.name}, telephone{p.telephone}, email{p.email} {
24 }
25 void Person::update_telephone() {
26     std::cin.clear();
27     string t;
28     std::cout << "输入电话号码:";
29     std::cin >> t;
30     telephone = t;
31     std::cout << "电话号码已更新..." << std::endl;
32 }
33 void Person::update_email() {
34     std::cin.clear();
35     string e;
36     std::cout << "输入email地址:";
37     std::cin >> e;
38     email = e;
39     std::cout << "email地址已更新..." << std::endl;
40 }
41 std::istream& operator>>(std::istream &i, Person &p) {
42     i >> p.name;
43     i >> p.telephone;
44     i >> p.email;
45     return i;
46 }
47 std::ostream& operator<<(std::ostream &o, const Person &p) {
48     o << "name:\t\t" << p.name << std::endl;
49     o << "telephone:\t" << p.telephone << std::endl;
50     o << "email:\t\t" << p.email << std::endl;
51     o << std::endl;
52     return o;
53 }
54 bool operator==(const Person &p1, const Person &p2) {
55     bool fn = (p1.name == p2.name);
56     bool ft = (p1.telephone == p2.telephone);
57     bool fe = (p1.email == p2.email);
58     return fn && ft && fe;
59 }
View Code

task4.cpp源码

#include <iostream>
#include <vector>
#include "Person.hpp"

void test() {
    using namespace std;

    vector<Person> phone_book;
    Person p;

    cout << "输入一组联系人的联系方式,E直至按下Ctrl+Z终止\n";
    while(cin >> p)
        phone_book.push_back(p);

    cout << "\n更新phone_book中索引为0的联系人的手机号、邮箱:\n";
    phone_book.at(0).update_telephone();
    phone_book.at(0).update_email();

    cout << "\n测试两个联系人是否是同一个:\n";
    cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
}

int main() {
    test();
}
View Code

实验任务5
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 operator - (const Date& date) const {
        return totalDays - date.totalDays;
    }
};

#endif //__DATE_H__
View Code

date.cpp源码

#include "date.h"
#include <iostream>
#include <cstdlib>
using namespace std;

namespace {    //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();
}
View Code

 

accumulator.h源码

#ifndef __ACCUMULATOR_H__
#define __ACCUMULATOR_H__
#include "date.h"

class Accumulator {    //将某个数值按日累加
private:
    Date lastDate;    //上次变更数值的时期
    double value;    //数值的当前值
    double sum;        //数值按日累加之和
public:
    //构造函数,date为开始累加的日期,value为初始值
    Accumulator(const Date &date, double value)
        : lastDate(date), value(value), sum(0) { }

    //获得到日期date的累加结果
    double getSum(const Date &date) const {
        return sum + value * (date - lastDate);
    }

    //在date将数值变更为value
    void change(const Date &date, double value) {
        sum = getSum(date);
        lastDate = date;
        this->value = value;
    }

    //初始化,将日期变为date,数值变为value,累加器清零
    void reset(const Date &date, double value) {
        lastDate = date;
        this->value = value;
        sum = 0;
    }
};

#endif //__ACCUMULATOR_H__
View Code

 

account.h源码

 

#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:
    //供派生类调用的构造函数,id为账户
    Account(const Date &date, const std::string &id);
    //记录一笔帐,date为日期,amount为金额,desc为说明
    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; }
    //存入现金,date为日期,amount为金额,desc为款项说明
    virtual void deposit(const Date &date, double amount, const std::string &desc) = 0;
    //取出现金,date为日期,amount为金额,desc为款项说明
    virtual void withdraw(const Date &date, double amount, const std::string &desc) = 0;
    //结算(计算利息、年费等),每月结算一次,date为结算日期
    virtual void settle(const Date &date) = 0;
    //显示账户信息
    virtual 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; }
    virtual void deposit(const Date &date, double amount, const std::string &desc);
    virtual void withdraw(const Date &date, double amount, const std::string &desc);
    virtual 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 {    //获得可用信用
        if (getBalance() < 0)
            return credit + getBalance();
        else
            return credit;
    }
    virtual void deposit(const Date &date, double amount, const std::string &desc);
    virtual void withdraw(const Date &date, double amount, const std::string &desc);
    virtual void settle(const Date &date);
    virtual void show() const;
};

#endif //__ACCOUNT_H__

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) {
    if (date.getMonth() == 1) {    //每年的一月计算一次利息
        double interest = acc.getSum(date) * rate
            / (date - 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());
}

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();
}

 

8_8.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);
    Account *accounts[] = { &sa1, &sa2, &ca };
    const int n = sizeof(accounts) / sizeof(Account*);    //账户总数

    cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
    char cmd;
    do {
        //显示日期和总金额
        date.show();
        cout << "\tTotal: " << Account::getTotal() << "\tcommand> ";

        int index, day;
        double amount;
        string desc;

        cin >> cmd;
        switch (cmd) {
        case 'd':    //存入现金
            cin >> index >> amount;
            getline(cin, desc);
            accounts[index]->deposit(date, amount, desc);
            break;
        case 'w':    //取出现金
            cin >> index >> amount;
            getline(cin, desc);
            accounts[index]->withdraw(date, amount, desc);
            break;
        case 's':    //查询各账户信息
            for (int i = 0; i < n; i++) {
                cout << "[" << i << "] ";
                accounts[i]->show();
                cout << endl;
            }
            break;
        case 'c':    //改变日期
            cin >> day;
            if (day < date.getDay())
                cout << "You cannot specify a previous day";
            else if (day > date.getMaxDay())
                cout << "Invalid day";
            else
                date = Date(date.getYear(), date.getMonth(), day);
            break;
        case 'n':    //进入下个月
            if (date.getMonth() == 12)
                date = Date(date.getYear() + 1, 1, 1);
            else
                date = Date(date.getYear(), date.getMonth() + 1, 1);
            for (int i = 0; i < n; i++)
                accounts[i]->settle(date);
            break;
        }
    } while (cmd != 'e');
    return 0;
}
View Code

运行截图2:

 

标签:std,const,string,张文瑞,double,Date,OOP,date,202213260018
From: https://www.cnblogs.com/zhangwenrui/p/17874261.html

相关文章

  • centos7.5 hadoop NAT 静态IP网络环境搭建
    1设置VMware网络环境1.选择VMNet8并将子网IP修改为192.168.10.0,保证集群ip都在这个网段下2.选择NAT设置,配置NAT的网关为192.168.10.22设置windows11网络环境1.打开控制面板\网络和Internet\网络连接,选择VMnet8,右键选择属性2.双击Internet协议版......
  • Hadoop集群部署后相关WEB界面打不开大概原因
    集群部署完毕后,查看相关WEB界面,打不开的原因可能如下:1、可以先去检查LINUX(CentOS7)机器的防火墙是否关闭,命令如下:systemctlstatusfirewalld.service(查看防火墙状态)(如果显示为关闭状态,则进行下一步;如果显示尚未关闭,则进行关闭并设定开机如下所示)systemctlstopfirewalld......
  • Loop循环
    循环参考视频链接:【循环Loop】聪明办法学Python第二版_哔哩哔哩_bilibilifor语句names=["Baidu","Google","Runoob","Taobao"]#打印列表fornameinnames:print(name)BaiduGoogleRunoobTaobaoword='runoob'#打印字符串forlett......
  • JAVAOOP日历之Calendar日历类型的常见方法
    1、将字符串转化为date类型 2、为日历对象添加年、月、日在当前时间上加年、月、日3、获取日历对象的年、月、日 4、将日历对象转化为时间戳(两种方法) 5、两个时间类型的比较 感谢大佬的观看!!! ......
  • Hadoop 一些脚本总结
    1.各个模板分开启动/停止1.整体启动/停止HDFSstart-dfs.sh/stop-dfs.sh2.整体启动/停止YARNstart-yarn.sh/stop-yarn.sh2.各个服务组件逐一启动/停止1.分别启动/停止HDFS组件hdfs--daemonstart/stopnamenode/datanode/secondarynamenode2.分别启动/停止YARN......
  • Hadoop 配置日志聚集
    本文章的前置Hadoop-3.3.6分布式集群搭建步骤概念:集群中的每台Hadoop节点的运行都会产生日志,开启日志聚集后,将所有运行日志信息上传到HadoopHDFS系统上,可以更好的查看运行详情,方便开发及调试。注意:需要重启NodeManage、ResourceManager和HistoryServer来开启日志聚集功能。1.......
  • hadoop高可用集群集成hive
    hive和hadoop的关系从上图可以看出实际上外界是通过thriftserver这个节点和hadoop的hdfs以及rm进行交互的,中间为driver因此当我修改完hadoop集群为高可用的时候,hive也需要重新配置环境以下是我hive的全新配置<configuration><!--Hive元数据存储配置--><propert......
  • Hadoop API 通过租户代理访问 Kerberos 安全 Hadoop 集群
    概述访问HDFS其实很简单这里记录一些方法,具备下面的特征:通过租户代理访问Kerberos认证的HADOOP资源获取FileSystem对象的技巧:FileSystem.get(URI,configuration)通用doAs模板通用hdfsCommand模板,使用try-with-resources。截取hdfs://namenode/user/...,使得nam......
  • 【补】Python中关于OOP的常用术语
    【一】抽象与实现【1】抽象抽象是一种概念或思维工具,用于简化复杂的问题并将其分解为易于管理的部分。抽象可以帮助我们理解事物的本质和行为,同时也可以帮助我们在设计软件时更好地组织代码和数据结构。【2】实现实现则是对抽象的一种具体表达。它是对抽象的概念或模型进......
  • OOP部分面试题的前世今生
    一、从变量声明开始    在.NET程序中定义一个变量时,会在RAM(随机存取存储器)中为其分配一些内存块。该内存块有3样东西:名称,数据类型、值。  变量究竟会被分配到那种类型的内存,取决于变量的数据类型。在.NET中有两种可分配的内存:堆和栈。  为了便于理解,用以下代码来......