首页 > 其他分享 >实验二

实验二

时间:2022-10-18 01:55:24浏览次数:41  
标签:const cout double void Complex 实验 include

task4:

Complex.hpp

#include<iostream>
#include<math.h>

using namespace std; 

class Complex{
public:
    Complex(double c31, double c32);
    Complex(const Complex& c4);

    double get_real() const;
    double get_imag() const;
    void show() const;
    void add(const Complex& c4);
   
    friend Complex add(const Complex c1, const Complex c2);
    friend bool is_equal(const Complex c1,const Complex c2);
    friend int abs(const Complex c3); 

private:
    double real;
    double imag;
};

Complex::Complex(double c31=0, double c32=0) :real{c31},imag{c32}{} 
Complex::Complex(const Complex& c4): real{c4.real}, imag{c4.imag}{}

double Complex::get_real() const {
    return real;
}

double Complex::get_imag() const{
    return imag;
}

void Complex::show() const{
     if(imag == 0) 
         cout << real;
     else if(imag < 0) 
         cout << real << " - " << -1 * imag << "i";
     else
         cout << real << " + " << imag << "i";    
}

void Complex::add(const Complex& c4){
    real += c4.real;
    imag += c4.imag;
}

Complex add(Complex c1, Complex c2) {
     Complex c3(c1.real + c2.real, c1.imag + c2.imag);
     return c3;
 }

 bool is_equal(Complex c1, Complex c2) {
     return (c1.real == c2.real && c2.imag == c1.imag);
 }

 int abs(Complex c4) {
     return sqrt(c4.real * c4.real + c4.imag * c4.imag);
 }

test4.cpp

#include "Complex.hpp"
#include <iostream>
#include<math.h>

using namespace std;

// 类测试
void test() {
    using namespace std;

    Complex c1(5, -7);
    const Complex c2(1.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();
}

 

task5:

User.hpp

#include <iostream>
#include <string>
  
using namespace std;
  
class User {
public:
    User(string Name, string Passwd = "111111", string Email = "") 
        : name{Name} , passwd{Passwd}, email{Email} {count++;}
    
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() {
        cout << "there are " << count << " users."; 
    }
 
 private: 
    string name;
    string passwd;
    string email;
    static int count;
 };
 
 int User::count = 0;

 void User::set_email() {
     cout << "Enter email address: " ;
     cin >> email;
     cout << "email is set successfully..." << endl;
 }
 
 void User::change_passwd() {
    string newpasswd;
    cout << "Enter old password: ";
    cin >> newpasswd;
    for(int count = 1; count <= 2 && newpasswd != passwd; count++){
        cout <<"password input error. Please re-enter again: ";
        cin >> newpasswd;
    }
    if(newpasswd == passwd){
        cout << "Enter new passwd: ";
        cin >> passwd;
        cout << "new passwd is set successfully..." << endl;
    }
    else
        cout << "password input error. Please try after a while." << endl;    
 }
 
 void User::print_info() {
     string s1(passwd.length(), '*');
     cout << "name: " << name << endl;
     cout << "passwd: " << s1 << endl;
     cout << "email: " << email << endl;    
}

test5.cpp

#include "User.hpp"
#include <iostream>

// 测试User类
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();
}

 

标签:const,cout,double,void,Complex,实验,include
From: https://www.cnblogs.com/miantiaooooo/p/16801262.html

相关文章

  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;够运......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 实验二 类和对象(2)
    task4:Complex.hpp:1#pragmaonce23#include<iostream>4#include<cmath>56usingstd::cout;7usingstd::endl;89classComplex{10public......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑。建立拓扑并连接Ryu控制器,浏览......
  • 实验6:开源控制器实践——RYU
    实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。实验要求(一)基本要求搭建下图所示SDN拓扑,协......
  • 实验二
    task1:#include<iostream>#include<complex>intmain(){usingnamespacestd;complex<double>c1={3,4},c2={4.5};constcomplex<double>c3{c2};c......
  • 实验6:开源控制器实践——RYU
    一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Deskto......
  • 【计算机网络实验】NAT配置实验
    【实训目的】掌握内部网络设计过程和私有IP地址使用。验证端口地址转换工作机制。掌握路由器地址转换配置过程。验证私有地址与公有地址之间的转换过程。验证IP分组和TCP报......
  • 【计算机网络实验】单区域OSPF配置实验
    【实训目的】掌握路由器OSPF配置过程验证OSPF创建动态路由项过程验证OSPF聚合网络地址过程【实训环境】eNSP模拟软件【实验原理】配置过程分为两部分:完成所有路由器接口IP地......
  • 【计算机网络实验】虚拟局域网组建
    【实训目的】(1)掌握基于端口的vlan划分方法(2)熟悉端口的基本参数应用(3)掌握Vlan成员的添加和删除方法【实训环境】eNSP模拟软件【实验原理】虚拟局域......