首页 > 其他分享 >实验二

实验二

时间:2022-10-16 15:11:33浏览次数:33  
标签:const cout double void Complex 实验 include

任务四

Complex.hpp

#pragma once
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
class Complex
{
public:
    Complex(double x0, double y);
    Complex(const Complex& obj) : x{ obj.x }, y{ obj.y } {}
    double get_real()const { return x; }
    double get_imag()const { return y; }
    void show()const;
    void add( Complex obj);
    friend Complex add( Complex c1, Complex c2);
    friend bool is_equal( Complex c1, Complex c2);
    friend double abs( Complex obj);
private:
    double x, y;
};
Complex::Complex(double x0=0.0, double y0=0.0) {
    x = x0;
    y = y0;
}
void Complex::show() const {
    if (y > 0)
    {
        cout << x << " + " << y << "i";
    }
    else if (y < 0)
    {
        cout << x << " - " << abs(y) << "i";
    }
    else
    {
        cout << x;
    }
}
void Complex::add( Complex obj) {
    x += obj.x;
    y += obj.y;
}
Complex add( Complex c1, Complex c2) {
    Complex c3;
    c3.x = c1.x + c2.x;
    c3.y = c1.y + c2.y;
    return c3;
}
bool is_equal( Complex c1,  Complex c2) {
    if (c1.x == c2.x && c1.y == c2.y)
        return true;
    else
        return false;
}
double abs( Complex obj) {
    double ab;
    ab = sqrt(obj.x * obj.x + obj.y * obj.y);
    return ab;
}

test.cpp

#include "Complex.hpp"
#include <iostream>
void test()
{
    using namespace std;

    Complex c1(1, 4);
    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();
}

 

 

任务五

User.hpp

#pragma once
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class User
{
public:
    User(string name0, string password = "111111" , string email0 = "") :name{name0}, passwd{password}, email{email0}{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;
};
int User::n = 0;
void User::set_email() {
    cout << "Enter email address :"  ;
    cin >> email;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd() {
    cout << "Enter old passwd :";
    int i;
    for (i = 0;i < 3;i++)
    {
        string b;
        cin >> b;
        if (b.compare(passwd) != 0)
        {
            if (i == 2)
                cout << "passwd input error.please try after a while."<<endl;
            else
                cout << "passwd input error.please re-enter again: ";
        }
        else
        {
            cout << "Enter new passwd: ";
            cin >> passwd;
            cout << "new passwd is set successfully...";
            break;
        }
    }
}
void User::print_info()
{
    cout << "name: " << name << endl << "passwd: ";
    string s1(passwd.length(), '*');
    cout << s1 << endl;
    cout << "email: " << email << endl;
}

test.cpp

#include "User.hpp"
#include <iostream>
void test()
{
    using std::cout;
    using std::endl;
    
    cout << "testing1......\n";
    User user1("Jonney", "92197", "[email protected]");
    user1.print_info();
    
    cout << endl << "testing2......\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/shmily-cwh/p/16796246.html

相关文章

  • 实验2
    实验2.4hpp:1#pragmaonce2#include<iostream>3#include<cmath>4usingnamespacestd;5classComplex{6public:7Complex(doublea=0,......
  • 实验2 类与对象(2)
    Task4hpp文件如下#pragmaonce#include<iostream>#include<complex>classComplex{public: Complex(); Complex(doubler,doublei=0.0); Complex(cons......
  • 实验四
    一)基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。......
  • 实验一 C语言开发环境使用和编程初体验
    //实验一#include<stdio.h>intmain(){printf("OO\n");printf("<H><H>\n");printf("IIII\n");return0;}//task1_1.c#include<......
  • 实验一
    #include<stdio.h>#include<stdlib.h>intmain(){printf("o\n");printf("<H>\n");printf("II\n");return0;}#include<stdio.h>#include......
  • 实验4:开源控制器实践---OpenDaylight
    利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。......
  • 实验2 类和对象(2)
    实验任务4:程序源码:complex.hpp1#pragmaonce2#include<iostream>3#include<cmath>4usingnamespacestd;5classComplex{6public:7Com......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的1、能够独立完成OpenDaylight控制器的安装配置;2、能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、......
  • 实验5:开源控制器实践——POX
    实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络应用......