實驗結論
實驗任務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復雜度取決於匹配串個數,詞庫較大時復雜度較劣。
以上便是我對本次實驗的一些想法與觀點,如果您認為我所寫哪里有錯誤或歧義請下方評論或聯系我,謝謝。