首页 > 其他分享 >实验二

实验二

时间:2022-10-19 14:47:54浏览次数:48  
标签:real const cout double void Complex 实验

task4.cpp

#include "Complex.hpp"
#include <iostream>
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();
}

Complex.hpp

#pragma once
#include<iostream>
#include<cmath>
using namespace std;
class Complex {
public:
    Complex(double r = 0.0, double i = 0.0);
    Complex(const Complex& C);
    double get_real() const;
    double get_imag() const;
    void show()const;
    void add(const Complex& c);
    friend Complex add(const Complex& c1, const Complex& c2);
    friend bool is_equal(const Complex& c1, const Complex& c2);
    friend double abs(const Complex& c);
private:
    double real, imag;
};
Complex::Complex(double r, double i) :real(r), imag(i) {
}
Complex::Complex(const Complex& C) {
    real = C.real;
    imag = C.imag;
}
double Complex::get_real()const {
    return real;
}
double Complex::get_imag()const {
    return imag;
}
void Complex::show()const {
    if (real != 0)cout << real;
    if (imag > 0)cout << " + " << imag << "i" ;
    else if (imag < 0)cout << imag << "i" ;
}
void Complex::add(const Complex& c) {
    real += c.real;
    imag += c.imag;
}
Complex add(const Complex& c1, const Complex& c2) {
    Complex c;
    c.real = c1.real + c2.real;
    c.imag = c1.imag + c2.imag;
    return c;
}
bool is_equal(const Complex& c1, const Complex& c2) {
    if (c1.real == c2.real && c1.imag == c2.imag)
        return true;
    else
        return false;
}
double abs(const Complex& c) {
    return sqrt(c.real * c.real + c.imag * c.imag);
}

 task5.cpp

#include "User.hpp"
#include <iostream>
void test() {
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();
    cout << endl
        << "testing 2......\n\n";
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    cout << endl;
    User::print_n();
}
int main() {
    test();
}

User.hpp

#pragma once
using namespace std;
#include<iostream>
class User {
public:
    User(string n0, string p = "111111", string e = "") :name(n0), passwd(p), email(e) {
        n++;
    }
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() {
        cout << "there are " << n << " users.";
    }
private:
    string name, passwd, email;
    static int n;
};
void User::set_email() {
    cout << "Enter email address: ";
    cin >> email;
    cout << endl << "email is set successfully..." << endl;
}
    void User::change_passwd() {
        string password;
        int i;
        cout << "Enter old password: ";
            cin >> password;
        for (i = 1; i <= 3; i++) {
            if (password == passwd) {
                cout << "Enter new password: ";
                cin >> passwd;
                cout << endl << "new password is set successfully..." << endl;
                break;
            }
            else
                cout << "password input error.";
                if (i < 3) {
                    cout << "Please re-enter again: ";
                        cin >> password;
                    cout << endl;
                }
                else cout << "Please try after a while." << endl;
        }
    }
        void User::print_info() {
            int m = passwd.length(),i;
            cout << "name:  " << name << endl;
            cout << "passwd:  ";
            for (i = 1; i <= m; i++)
                cout << '*';
            cout << endl;
            cout << "email:  " << email;
        }
        int User::n = 0;

 

 

 

标签:real,const,cout,double,void,Complex,实验
From: https://www.cnblogs.com/fyh2021/p/16806077.html

相关文章

  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU(一)基础要求1.建立拓扑并接连RYU控制器,并通过RYU的图形界面查看网络拓扑。2.运行当中的L2Switch,在目标主机使用tcpdump验证L2Switch。......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。建立拓扑并连接Ryu控制器,浏......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验6:开源控制器实践——RYU
    1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。2.阅读Ryu文档的TheFirstApplication一节,运行当中的L2Switch,h1pingh2......
  • 实验二
    task.4#include<iostream>#include<math.h>usingnamespacestd;classComplex{private:floatreal,imag;public:Complex(floatr,floati);//带两个......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。实验要求(一)基......
  • 实验6:开源控制器实践——RYU
    实验要求(一)基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器。2.建立拓扑并连接RYU控制器3.通过Ryu的图形界面查看网络拓扑4.阅读Ryu文档的The......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够......
  • 实验二 类与对象(2)
     Task4:Complex.hpp 1#pragmaonce23//Complex类的定义4#include<iostream>5#include<string>6#include<math.h>78usingstd::cout;9usin......