首页 > 其他分享 >实验二

实验二

时间:2022-10-19 01:22:46浏览次数:35  
标签:std const cout void Complex 实验 c1

Task 4

hpp文件如下

#pragma once
#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
public:
    Complex(double a = 0, double b = 0) :real{ a }, imag{ b } {};
    Complex(const Complex& c1);
    ~Complex() = default;
    double get_real() const { return real; }
    double get_imag() const { return imag; }
    void show() const;
    void add(Complex const& c1);
    friend Complex add(Complex const& c1, Complex const& c2);
    friend bool is_equal(Complex const& c1, Complex const& c2);
    friend double abs(Complex const& c1);
private:
    double real;
    double imag;
};

Complex::Complex(const Complex& c1)
{
    real = c1.real;
    imag = c1.imag;
}
void Complex::show() const
{
    if (imag == 0)
        cout << real;
    else if (imag < 0)
        cout << real << " - " << abs(imag) << "i";
    else
        cout << real << " + " << imag << "i";
}
void Complex::add(Complex const& c1)
{
    real += c1.real;
    imag += c1.imag;
}
Complex add(Complex const& c1, Complex const& c2)
{
    Complex c3;
    c3.real = c1.real + c2.real;
    c3.imag = c1.imag + c2.imag;
    return c3;
}
bool is_equal(Complex const& c1, Complex const& c2)
{
    if (c1.real == c2.real && c1.imag == c2.imag)
        return true;
    return false;
}
double abs(Complex const& c1)
{
    return sqrt(c1.real * c1.real + c1.imag * c1.imag);
}

cpp文件如下:

#include <Complex>
#include <iostream>
// 类测试
void test() {
	using namespace std;
	Complex c1(3, -4);
	const Complex c2(7.8);
	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

hpp文件如下:

#pragma once
#include<iostream>;
#include<string>
class User {
public:
	User(std::string name0, std::string password0 = "111111",std::string email0 = "");
	void set_email();
	void change_passwd();
	void print_info();
	void static print_n() {
		std::cout << "共有 " << n << " 名用户" << std::endl;
	}
private:
	std::string name;
	std::string email;
	std::string passwd;
	static int n;


};
int	User::n = 0;
User::User(std::string name0,  std::string password0,std::string email0) :name{ name0 }, email{ email0 }, passwd{ password0 } { n++; }
void User::set_email() {
	std::string email1;
	std::cout << "请用户用键盘输入邮箱地址!" << std::endl;
	std::cin >> email1;
	if (email1 != "")
	{
		email = email1;
		std::cout << "邮箱设置成功!" << std::endl;
	}
	else
	std::cout << "未设置邮箱,将使用默认值!" << std::endl;
		
}
void User::change_passwd() {
	std::cout << "请输入旧密码!" << std::endl;
	std::string password;
	for (int i = 0; i < 3; i++)
	{
		flag:
		std::cin >> password;
		if (password != passwd)
		{
			std::cout << "原密码不正确,请重新输入!";
			std::cout << "已连续输入错误" << i + 1 << "次!"<<std::endl;
			if (i == 2)
			{
				std::cout << "已连续输入三次错误密码,请稍后再试。"<<std::endl;
				goto flag;
			}

		}
		else if (password == passwd)
		{
			std::cout << "原密码验证成功,请输入新密码!"<<std::endl;
			std::cin >> passwd;
			std::cout << "密码修改成功!" << std::endl;
			break;
		}
	}

}
void User::print_info() {
	int length = passwd.length();
	std::cout << "用户名为:" << name << std::endl;
	std::cout << "邮箱为:" << email << std::endl;
	std::cout << "密码为:";
	while (length--)
	{
		std::cout << "*";
		if (length == 0)
			std::cout << std::endl;
	}
}

cpp文件如下:

#include "User.hpp"
#include <iostream>
// 测试User类
void test() {
	using std::cout;
	using std::endl;
	cout << "testing 1......\n";
	User user1("Li", "123456", "[email protected]");
	user1.print_info();
	cout << endl
		<< "testing 2......\n\n";
	User user2("lixuan");
	user2.change_passwd();
	user2.set_email();
	user2.print_info();
	cout << endl;
	User::print_n();
}
int main() {
	test();
	}

运行结果如下:

标签:std,const,cout,void,Complex,实验,c1
From: https://www.cnblogs.com/2003dingjiajie/p/16804795.html

相关文章

  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。实验要求(一)基本......
  • 实验二 类和对象
    实验任务4task4.cpp#include"Complex.hpp"#include<iostream>//类测试voidtest(){usingnamespacestd;Complexc1(1,-8);constComplexc2(6......
  • 实验6:开源控制器实践——RYU
    一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、实验环境(一)基本要求1.搭建......
  • 实验6:开源控制器实践——RYU
    实验6:开源控制器实践——RYU一、实验目的1.能够独立部署RYU控制器;2.能够理解RYU控制器实现软件定义的集线器原理;3.能够理解RYU控制器实现软件定义的交换机原理。二、......
  • 实验6:开源控制器实践——RYU
    1.建立拓扑并连接RYU控制器2.通过Ryu的图形界面查看网络拓扑3.(1)创建L2Switch.py文件点击查看代码fromryu.baseimportapp_managerfromryu.controllerimporto......
  • 实验6:开源控制器实践——RYU
    实验要求(一)基本要求1.搭建下图所示SDN拓扑,协议使用OpenFlow1.0,并连接Ryu控制器。建立拓扑并连接Ryu控制器,通过Ryu的图形界面查看网络拓扑2.阅读Ryu文档的TheFirst......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网络......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopamd6......
  • 实验二 类和对象
    #pragmaonce#include<iostream>#include<iomanip>#include<cmath>usingstd::cout;usingstd::endl;usingstd::setfill;usingstd::setw;usingstd::left;......
  • 实验5:开源控制器实践——POX
    生成拓扑:点击查看代码sudomn--topo=single,3--mac--controller=remote,ip=127.0.0.1,port=6633--switchovsk,protocols=OpenFlow10阅读Hub模块代码,使用tcpdu......