complex.cpp文件源码:
#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#include <iostream>
#include <cmath>
class Complex {
public:
Complex() {};//默认构造函数
~Complex() {};//析构函数
Complex(double r, double i = 0) :real(r), imag(i) {};
Complex(const Complex& c) {
real = c.real;
imag = c.imag;
};
double get_real() const { return real; }
double get_imag() const { return imag; }
void show() const {
if (imag > 0 && real != 0)
std::cout << get_real() << " + " << get_imag() << "i";
else if (imag < 0 && real != 0)
std::cout << get_real() << get_imag() << "i";
else if (imag == 0)
std::cout << get_real();
else
std::cout << get_imag()<<"i";
}
void add(Complex x);
friend Complex add(const Complex &x,const Complex &y);
friend bool is_equal(Complex x, Complex y);
friend double abs(Complex& x);
private:
double real, imag;
};
void Complex::add(Complex x) {
imag += x.imag;
real += x.real;
}
Complex add( const Complex &x, const Complex &y) {
Complex temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
bool is_equal(Complex x, Complex y) {
if (x.imag == y.imag && x.real == y.real) return 1;//
else
return 0;//
}
double abs(Complex& x) {
double value;
value = sqrt(x.imag * x.imag + x.real * x.real);
return value;
}
#endif
task3.cpp文件源码:
#include "Complex.hpp"
#include <iostream>
int main()
{
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;
}
测试截图:
User.hpp文件源码
#ifndef USER_HPP
#define USER_HPP
#include <iostream>
#include <regex>
class User {
public:
User(std::string _name, std::string _passwd = "111111", std::string _email = "") :
name(_name), passwd(_passwd), email(_email) {n++;};
void set_email();
void change_passwd();
void print_info() const {
std::cout << "name:\t" << name << std::endl;
std::cout << "passwd:\t" << "******" << std::endl;
std::cout << "email:\t" << email << std::endl;
}
static void print_n() {
std::cout << "there are " << n << " users";
}
private:
std::string name, passwd, email;
static int n;
};
int User::n = 0;
void User::set_email() {
std::string _email;
std::cout << "Enter email address: ";
std::cin >> _email;
if (_email != email)
{
email = _email;
std::cout << "email is set successfully..." << std::endl;
}
else
{
std::cout << "Incorrect email format.";
}
}
void User::change_passwd() {
std::string oldpd;
std::string newpd;
int t = 3, time = 3;//t是还可以输老密码的次数,time是还可以输新密码的次数
std::cout << "Enter old password: ";
while (t--) {
std::cin >> oldpd;//用户输入印象中的老密码
if (oldpd == passwd) //老密码输入正确
{
while (time--) {
std::cout << "Enter new passwd:";
std::cin >> newpd;
bool value = regex_match(newpd, std::regex(".{6}.*"));// 正则匹配,密码长度需大于等于6位
if (value && newpd != passwd) {
passwd = newpd;
std::cout << "new passwd is set successfully..." << std::endl;
break;//新密码输入正确,跳出小循环
}
else
std::cout << "your passwd is too simple!" << std::endl;
}
break;//3次新密码输入机会用完或者新密码输入正确,跳出大循环
}
else {
if (t == 0) {
std::cout << "password input error. Please try after a while." << std::endl;
break;
}
else std::cout << "password input error. Please re-enter again: ";
}
}
}
#endif
task4.cpp
#include "User.hpp"
#include <iostream>
int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("Jonny", "92197", "xyz@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.print_info();
User::print_n();
}
总结:
1、在task3复数类的hpp文件中,既可以用对象直接调用数据成员,例如:x.real,也可以用对象调用成员函数进而获得数据成员的值例如x.get_real()。而在main函数中则不可以使用前一种调用方式。
2、(1)在task4的User类中最好使用不同的变量来区分不同数据可以输入的次数。
(2)必须定义静态成员函数print_n()已经静态数据成员n以确保n不会在主函数运行过程中被析构函数释放。n的作用域为全局。
3、尚存的问题:不知道怎么对邮箱格式的合法性进行判断,导致无论邮箱输入什么都算合法。