实验结论
实验任务3
Complex.hpp源码
#ifndef Complex_hpp
#define Complex_hpp
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
public:
Complex(double X = 0.0, double Y = 0.0) : real{X}, imag{Y} {}
Complex(const Complex &now) : real{now.real}, imag{now.imag} {}
~Complex() = default;
double get_real() const { return real; }
double get_imag() const { return imag; }
void show() const
{
cout << real;
if (imag > 0)
cout << " + ";
else if (imag < 0)
{
cout << " - ";
}
else
{
return;
}
cout << abs(imag) << "i";
}
void add(Complex x) { real = real + x.real, imag = imag + x.imag; }
friend Complex add(Complex a, Complex b);
friend bool is_equal(Complex a, Complex b);
friend double abs(Complex a);
private:
double real, imag;
};
Complex add(Complex a, Complex b)
{
Complex ans;
ans.real = a.real + b.real;
ans.imag = a.imag + b.imag;
return ans;
}
bool is_equal(Complex a, Complex b)
{
if (a.real == b.real && a.imag == b.imag)
return true;
return false;
}
double abs(Complex a)
{
return sqrt(a.imag * a.imag + a.real * a.real);
}
#endif
task3.cpp源码
#include <iostream>
#include "Complex.hpp"
using namespace std;
int main()
{
Complex c1(6, -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;
}
运行截图
实验任务4
User.hpp
#ifndef User_hpp
#define User_hpp
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
class User
{
public:
User(string a,string b="111111",string c=""):name{a},password{b},email{c}{n++;}
~User()=default;
void print_info()const{
cout<<"name: "<<name<<"\n";
cout<<"passwd: "<<"******"<<"\n";
cout<<"email: "<<email<<"\n";
return;
}
static void print_n(){
cout<<"there are "<<n<<" users.\n";
return;
}
void set_email(){
cout<<"Enter email address: ";
string now;
cin>>now;
email=now;
cout<<"email is set successfully...\n";
return;
}
void change_passwd(){
string now;
int error_times=0;
bool flag=false;
cout<<"Enter old password: ";
while(1){
cin>>now;
if(now==password){
flag=true;
break;
}
else{
error_times++;
if(error_times==3) break;
cout<<"password input error. Please re-enter again: ";
}
}
if(!flag){
cout<<"password input error. Please try after a while.\n";
return;
}
else{
cout<<"Enter new passwd: ";
cin>>now;
password=now;
cout<<"new passwd is set successfully..."<<"\n";
return;
}
}
private:
string name,password,email;
static int n;
};
int User::n=0;
#endif
task4.cpp
#include "User.hpp"
#include <iostream>
using namespace std;
int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("wzy", "112233", "123@qq.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
User user2("Wzy");
user2.change_passwd();
user2.set_email();
user2.print_info();
User::print_n();
return 0;
}
运行截图
实验总结
这次的实验让我较为深刻的感受到了类的使用,以前写代码时我更多的选择用struct
保存需要操作类型并写普通函数对其进行操作,虽然简单明确但也有缺陷————某种模板我很难直接去复制,因为可能会出现各种形如:函数名命名冲突,变量命名冲突的问题,使用类并调用友元函数虽然代码总量上相对更为繁琐,但确实保证了代码的可读性和更好的拓展性,是很好的写模板方式。
对于课程的User类的一些完善丰富,我感觉比较琐碎就在这里聊聊我感觉可以增加的地方与实现方法。
1.用户名冲突及弱密码问题,为避免用户设置弱密码或多用户设置相同的用户名,可以选择对弱密码,用户名进行哈希,出现冲突则要求用户重新设置。
2.邮箱格式线性处理即可,如果需要进行敏感词屏蔽可以搭建敏感词库并以用户名为模式串进行AC自动机操作,之所以不使用kmp是因为kmp复杂度取决于匹配串个数,词库较大时复杂度较劣。
以上便是我对本次实验的一些想法与观点,如果您认为我所写哪里有错误或歧义请下方评论或联系我,谢谢。