• 欢迎光临~

实验二

开发技术 开发技术 2022-10-18 次浏览

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", "lkx@hotmail.com");
	user1.print_info();
	cout << endl
		<< "testing 2......nn";
	User user2("lixuan");
	user2.change_passwd();
	user2.set_email();
	user2.print_info();
	cout << endl;
	User::print_n();
}
int main() {
	test();
	}

运行结果如下:

实验二

程序员灯塔
转载请注明原文链接:实验二
喜欢 (0)