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

实验二_OOP_张文瑞_202213260018

时间:2023-10-23 09:02:32浏览次数:29  
标签:std cout 张文瑞 double void int OOP include 202213260018

1、实验一

 程序源码:

#include <iostream>
#include<cmath>

class Complex {
    private:
        double real, imag;
    public:
        Complex(double r = 0, double i = 0) : real{r}, imag{i} {};
        Complex(const Complex &c) : real{c.real}, imag{c.imag} {};
        double get_real() const {return real;}
        double get_imag() const {return imag;}
        void add(const Complex &c) {
            real += c.get_real();
            imag += c.get_imag();
        }
        void show() {
            std::cout << real;
            if (imag > 0) std::cout << " + " << abs(imag) << 'i';
            if (imag < 0) std::cout << " - " << fabs(imag) << 'i';
        }
        void show() const {
            std::cout << real;
            if (imag > 0) std::cout << " + " << abs(imag) << 'i';
            if (imag < 0) std::cout << " - " << fabs(imag) << 'i';
        }
    public:
        friend Complex add(const Complex&, const Complex&);
        friend bool is_equal(const Complex&, const Complex&);
        friend double abs(const Complex&);
};
Complex add(const Complex &c1, const Complex &c2) {
    double r, i;
    r = c1.get_real() + c2.get_real();
    i = c1.get_imag() + c2.get_imag();
    Complex c(r, i);
    return c;
}
bool is_equal(const Complex &c1, const Complex &c2) {
    bool r, i;
    r = c1.get_real() == c2.get_real();
    i = c1.get_imag() == c2.get_imag();
    return r && i;
}
double abs(const Complex &c) {
    return sqrt(pow(c.get_real(), 2) + pow(c.get_imag(), 2));
}
View Code
#include <iostream>
#include <cmath>
#include "Complex.hpp"

// 复数类Complex: 测试
void test() {
    using namespace std;
    Complex c1(3, -4);
    const Complex c2(4.5);
    Complex c3(c1);
    cout << "c1 = ";
    c1.show();
    cout << endl;
    cout << "c2 = ";
    c2.show();
    cout << endl;
    cout << "c2.imag = " << c2.get_imag() << endl;
    cout << "c3 = ";
    c3.show();
    cout << endl;
    cout << "abs(c1) = ";
    cout << abs(c1) << endl;
    cout << boolalpha;
    cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
    Complex c4;
    c4 = add(c1, c2);
    cout << "c4 = c1 + c2 = ";
    c4.show();
    cout << endl;
    c1.add(c2);
    cout << "c1 += c2, " << "c1 = ";
    c1.show();
    cout << endl;
}

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

运行截图:

 

 

1、实验二

 程序源码:

#include <iostream>
#include <string>
#include <limits>
class User {
    private:
        static int n;
    public:
        static void print_n();
    private:
        std::string name, password, email;
    public:
        User(
            std::string nm,
            std::string p = "111111",
            std::string e = "")
            : name{nm}, password{p}, email{e}
            {n += 1;};
        void set_email();
        void change_passwd();
        void print_info();
};
int User::n = 0;

void User::print_n() {
    std::cout << "there are " << n << " users.\n";
};

void User::set_email() {
    using namespace std;
    cout << "Enter emaill address: ";
    string e;
    cin >> e;
    cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
    cout << "emaill is set successfully...\n";
    email = e;
};

void User::change_passwd() {
    using namespace std;
    string o, n;
    for (int i = 0; i <= 3; i++) {
        if (i == 3) {
            cout << "Please try after a while. \n";
            break;
        }
        if (i == 0)
            cout << "Enter old password: ";
        else
            cout << "Please re-enter again: ";
        cin >> o;
        cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
        if (o != password) {
            cout << "password input error. ";
            continue;
        } else {
            cout << "Enter new passwd: ";
            cin >> n;
            cin.ignore((std::numeric_limits<streamsize>::max)(),'\n');
            password = n;
            cout << "new passwd is set successfully...\n";
            break;
        };
    };
};

void User::print_info() {
    using namespace std;
    cout << "name: \t" << name << endl;
    string s(password.size(),'*');
    cout << "password: \t" << s << endl;
    cout << "email: \t" << email << endl;
};
View Code
#include "User.hpp"
#include <iostream>
// 测试User类
void test() {
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Alice", "pw123456", "[email protected]");
    user1.print_info();
    cout << endl
         << "testing 2......\n\n";
    User user2("Bob");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    cout << endl;
    User::print_n();
}
int main() {
    test();
}
View Code

运行截图:

1、实验三

 程序源码:

 

//account.h
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
class SavingsAccount {            //储蓄账户类
    private:
        int id;                    //帐号
        double balance;            //余额
        double rate;            //存款的年利率
        int lastDate;            //上次变更余额的时期
        double accumulation;    //余额按日累加之和
        static double total;    //所有帐户的总金额
        //记录一笔账,date为日期,amount为金额,desc为说明
        void record(int date, double amount);
        //获得到指定日期为止的存款金额按日累积值
        double accumulate(int date) const {
            return accumulation + balance * (date - lastDate);
        }
    public:
        //构造函数
        SavingsAccount(int date, int id, double rate);
        int getId() const {return id;}
        double getBalance() const {return balance;}
        double getRate() const {return rate;}
        static double getTotal() {return total;}
        void deposit(int date, double amount);        //存入现金
        void withdraw(int date, double amount);        //取出现金
        //结算利息,每年1月1日调用一次该函数
        void settle(int date);
        //显示账户信息
        void show() const;
};
#endif //__ACCOUNT _H__
View Code
//account.cpp
#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;

double SavingsAccount::total = 0;
//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(int date, int id, double rate)
    : id(id), balance(0), rate(rate), lastDate (date), accumulation(0) {
    cout << date << "\t#" << id << " is created" << endl;
}
void SavingsAccount::record(int date, double amount) {
    accumulation = accumulate(date);
    lastDate = date;
    amount = floor(amount * 100 + 0.5) / 100;        //保留小数点后两位
    balance += amount;
    total += amount;
    cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}
void SavingsAccount::deposit(int date, double amount) {
    record(date, amount);
}
void SavingsAccount::withdraw(int date, double amount) {
    if ( amount > getBalance())
        cout << "Error: not enough money" << endl;
    else
        record(date, - amount);
}
void SavingsAccount::settle(int date) {
    double interest = accumulate(date) * rate / 365;
//计算年息
    if (interest != 0)
        record(date, interest);
    accumulation = 0;
}
void SavingsAccount::show() const {
    cout << "#" << id << "\tBalance: " << balance;
}
View Code
//5_11.cpp
#include "account.h"
#include <iostream>
using namespace std;
int main() {
    //建立几个帐户
    SavingsAccount sa0(1, 21325302, 0.015);
    SavingsAccount sa1(1, 58320212, 0.015);
    //几笔账目
    sa0.deposit(5, 5000) ;
    sa1.deposit(25, 10000);
    sa0.deposit(45, 5500);
    sa1.withdraw(60, 4000);
    //开户后第90天到了银行的计息日,结算所有账户的年息
    sa0.settle(90);
    sa1.settle(90);
    //输出各个帐户信息
    sa0.show();cout << endl;
    sa1.show();cout << endl;
    cout << "Total:" << SavingsAccount::getTotal() << endl;
    return 0;
}
View Code

 

运行截图:

 

标签:std,cout,张文瑞,double,void,int,OOP,include,202213260018
From: https://www.cnblogs.com/zhangwenrui/p/17781553.html

相关文章

  • 两台实体机器4个虚拟机节点的Hadoop集群搭建(Ubuntu版)
    安装UbuntuLinux元信息两台机器,每台机器两台UbuntuUbuntu版本:ubuntu-22.04.3-desktop-amd64.iso处理器数量2,每个处理器的核心数量2,总处理器核心数量4单个虚拟机内存8192MB(8G),最大磁盘大小30G参考链接清华大学开源软件镜像站https://mirrors.tuna.tsinghua.edu.cn/ubunt......
  • Hadoop的安装
    安装JDK设置环境变量配置Hadoop下载hadoop安装包#wgethttp://mirror.bit.edu.cn/apache/hadoop/common/hadoop-3.2.0/hadoop-3.2.0.tar.gz配置hadoop-env.sh(/usr/local/src/hadoop-3.2.0/etc/hadoopex)配置core-site.xml配置hdfs-sit......
  • 报错Failed to execute spark task, with exception 'org.apache.hadoop.hive.ql.meta
    在执行hive on spark的时候 上面的错误可能有以下几种问题:1.版本问题 不匹配2.时间参数问题  设置的参数太小了3.在hive-site.xml文件中没有配置spark的home我的问题属于第一个问题导致没有跑成功当时也在想是不是内存出现了问题 ......
  • hadoop集群 大数据项目实战_电信用户行为分析_day04
    进行HIVE环境配置1.上传相关的包 2.对上传的包进行下载和创建软连接 3.配置相关的文件4.分别发送给其他机子 假设你需要在所有机器执行同一个指令,则你就需要相关设置  5.在hive的onf文件中创建hive-site.xml进行相关设置```xml<configuration><--元数据存......
  • hadoop官方文档解读
    Hadoop是一个分布式计算框架,用于存储和处理大规模数据集。首先搞清楚为什么需要使用HadoopHadoop进行数据处理可以充分利用分布式计算和存储的优势,适用于大规模数据的批处理和分布式计算场景。裸机上进行数据处理则更适合小规模数据或需要实时处理的场景。在裸机上进行数据处......
  • 实验1_OOP_22物联网1班_张文瑞
    1.实验任务1:  实验源代码:1//标准库string,vector,array基础用法2#include<iostream>3#include<string>4#include<vector>5#include<array>6//函数模板7//对满足特定条件的序列类型T对象,使用范围for输出8template<typenameT>9voidoutpu......
  • oop 实验1类和对象基础编程
    #include<iostream>#include<string>#include<vector>#include<array>//通用函数(此处是模板函数)用于输出容器中的元素,支持范围for(范围for循环,是一种用于遍历容器、数组和其他序列容器的现代C++迭代循环结构。它提供了一种更简洁和易读的方法来遍历容器的元素,而无需手动......
  • hadoop集群 大数据项目实战_电信用户行为分析_day03
    配置系统环境  Reis1.先把之前的dump.rdb删除掉rm-rfdump.rdb 2.把原始项目给的dump.rdb放进来,它里面包含了需要的数据,比如端口;在这部之前必须要进行关闭端口,随后传送文件,最后重启端口相关指令:   bin/redis-server conf/redis.conf   bin/redis-cli  bin......
  • 浏览器事件循环 event loop(消息循环)
     打开浏览器 即 开启一个浏览器进程(主要负责浏览器UI,用户交互,子进程拉起关闭等)并由浏览器进程拉起网络进程(多Tab共享)采用多线程模式,GPU 进程(多Tab共享)等当每开启一个tab 页,浏览器进程会负责为该Tab 拉起一个渲染进程,每一个渲染进程都会拉起一个渲染主线程(单线程......
  • 统计Hadoop空间增量
    #!/bin/bash#设置Hadoop路径hadoop_path="/path/to/hadoop"#获取当前日期current_date=$(date+%Y-%m-%d)#获取Hadoop集群中各个目录的总空间大小,并保存到文件$hadoop_path/bin/hdfsdfs-du-s-h/user>current_space.txt#用awk命令提取目录名称和空间大小,并保......