首页 > 其他分享 >实验二

实验二

时间:2022-10-12 21:14:38浏览次数:30  
标签:const string passwd void Complex 实验 include

task 4

Complex.h

#pragma once
#include<iostream>
using namespace std;
class Complex
{
    friend Complex add(const Complex& obj1, const Complex& obj2);
    friend bool is_equal(const Complex& obj1, const Complex& obj2);
    friend double abs(const Complex& obj);
public:
    Complex(double _real = 0, double _image = 0);
    Complex(const Complex& obj);
    double get_real()const;
    double get_imag()const;
    void show()const;
    void add(const Complex& obj);
private:
    double real;
    double image;
};

Complex.cpp

#include"Complex.h"
double Complex::get_real() const
{
    return real;
}
double Complex::get_imag()const
{
    return image;
}
void Complex::show()const
{
    if (image)
        cout << real << image << "i";
    else
        cout << real << image;
}
void Complex::add(const Complex& obj)
{
    real += obj.real;
    image += obj.image;

}
Complex::Complex(double _real, double _image)
{
    real = _real;
    image = _image;
}
Complex::Complex(const Complex& obj)
{
    real = obj.real;
    image = obj.image;
}
Complex add(const Complex& obj1, const Complex& obj2)
{
    Complex ret;
    ret.real = obj1.real + obj2.real;
    ret.image = obj1.image + obj2.image;
    return ret;
}
bool is_equal(const Complex& obj1, const Complex& obj2)
{
    return obj1.real == obj2.real && obj2.image == obj2.image;
}
double abs(const Complex& obj)
{
    double _real = obj.real;
    double _image = obj.image;
    return sqrt(_real * _real + _image * _image);
}

test4.cpp

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

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

 

 

task 5

user.h

#pragma once
#include<iostream>
#include<string>
using namespace std;

class User
{
public:
    User(const string& _name, const string& _passwd="111111", const string& _email="");
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
private:
    string name;
    string passwd;
    string email;
    static int count;
};

user.cpp

#include"user.h"
User::User(const string& _name, const string& _passwd, const string& _email)
{
    name = _name;
    passwd = _passwd;
    email = _email;
    count++;
}
void User::set_email()
{
    cout << "Enter email address: ";
    cin >> email;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd()
{
    cout << "Enter old passwd: ";
    string _passwd;
    int input_count = 0;
    while (true)
    {
        cin >> _passwd;
        input_count++;
        if (_passwd != passwd)
            cout << "passwd input error. ";
        else
        {
            break;
        }
        if (input_count == 3)
        {
            cout << "Please try after a while"<<endl;
            break;
        }
        else
        {
            cout << "Please re-enter again: ";
        }
        input_count++;
    }

}
void User::print_info()
{
    cout << "name: " << name << endl;

    cout << "passwd: ";
    int passwd_len = passwd.size();
    for (int i = 1; i <= passwd_len; i++)
        cout << "*";
    cout << endl;

    cout << "email: " << email << endl;

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

test5.cpp

#include "User.h"
#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 User::count = 0;
int main() {
    test();
}

 

标签:const,string,passwd,void,Complex,实验,include
From: https://www.cnblogs.com/xelfin/p/16785528.html

相关文章

  • 实验4:开源控制器实践——OpenDaylight
    一、实验要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器命令行连接控制器sudomn--topo=single,3--controller=remote,ip=127.0.0.1,port=6633......
  • 实验2 类和对象(2)
    1.实验四  Complex类的实现1#include<iostream>2#include<cmath>3usingnamespacestd;4classComplex5{6private:7doubl......
  • 实验4:开源控制器实践——OpenDaylight
    一)基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。(二)进......
  • 实验4:开源控制器实践——OpenDaylight
    (一)基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;Mininet拓扑生成并连接控制器的结果Mininet中ping测试截图通过Postman工具调用OpenDaylight......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验要......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验要......
  • 改进ls实验
    改进要求排序:文件名读入数组,利用qsort排序分栏:文件名读入数组,计算列宽和行高.和..加入-a选项,没有-a,不显示隐藏文件-l:功能不同,单独实现首先我们来看一下ls-l的具体......
  • 实验1c语言开发环境使用和数据类型,运算符和表达式
    1.试验任务1(1)在垂直方向上打印两个字符小人的源代码,以及运行结果截图 \\在垂直方向上打印两个字符小人#include<stdio.h>intmain(){printf("o\n");pr......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ub......
  • 实验2 类和对象(2)
    实验目的能够使用C++语法规则正确定义、实现、测试类。能够使用C++语法规则正确创建对象,并基于对象编程。知道类的static成员用于解决什么问题场景,会正确使用c++语法规......