實驗結論
重載函數add()
源碼
這是按照給出的結構體 Complex
並且沒有使用函數模板寫出的代碼。
#include <iostream>
using namespace std;
typedef struct
{
double real;
double imaginary;
} Complex;
int add(int, int);
double add(double, double);
Complex add(Complex, Complex);
int main()
{
int a = 1, b = 5;
double x = 1.12, y = 5.36;
Complex m, n;
m.real = -12,m.imaginary=5;
n.real = -5,n.imaginary=2;
cout << add(a, b) << endl;
cout << add(x, y) << endl;
cout << add(m, n).real << "+" << add(m, n).imaginary <<"i"<< endl;
cin.get();
return 0;
}
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
Complex add(Complex a, Complex b)
{
Complex c;
c.real = a.real + b.real;
c.imaginary = a.imaginary + b.imaginary;
return c;
}
這是使用了C++自帶的<complex>
並且使用函數模板簡化后的代碼。
#include <complex>
#include <iostream>
using namespace std;
template <typename T>
T add(T a, T b)
{
return a + b;
}
int main()
{
int a = 1, b = 5;
double x = 1.12, y = 5.36;
complex<double> m{2.2, 5.4};
complex<double> n{3.5, 2.1};
cout << add(a, b) << endl;
cout << add(x, y) << endl;
cout << add(m, n).real() << "+" << add(m, n).imag() << "i" << endl;
cin.get();
return 0;
}
運行截圖
因為除了測試數據有所不同,其他部分這兩份代碼的運行截圖並無兩樣,故只給出一份
快速排序函數模板
實現了隨機生成任意數量的整數和浮點數后,並排序。最后輸出排序前后的整數和浮點數序列
源碼
頭文件 QuickSort.h
#ifndef _QUICKSORT_
#define _QUICKSORT_
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void quick_sort(vector<T> &nums, int start, int end)
{
if (start >= end)
return;
T mid = nums[start];
int left = start, right = end;
while (left < right)
{
while (nums[right] >= mid && left < right)
right--;
while (nums[left] <= mid && left < right)
left++;
swap(nums[left], nums[right]);
}
swap(nums[left], nums[start]);
quick_sort(nums, start, left - 1);
quick_sort(nums, left + 1, end);
}
template <typename T>
void quick(vector<T> &nums)
{
quick_sort(nums, 0, nums.size() - 1);
}
template <typename T>
void output(vector<T> a)
{
int count = 0;
for (auto i : a)
{
count++;
cout << left << setw(5) << i << " ";
if (count == 10)
{
cout << endl;
count = 0;
}
}
cout << endl;
}
#endif
主函數
#include "QuickSort.h"
#include <ctime>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
mt19937 gen(time(NULL));
uniform_int_distribution<> dis(1, 100);
uniform_real_distribution<> disf(1, 100);
void pause();
int main()
{
vector<int> nums;
vector<double> numsf;
int count;
cout << "How many numbers you want to generate randomly?" << endl;
cin >> count;
for (int i = 0; i < count; i++)
{
nums.push_back(dis(gen));
numsf.push_back(disf(gen));
}
cout << "int_unsorted:" << endl;
output(nums);
quick(nums);
cout << "int_sorted:" << endl;
output(nums);
cout << endl;
cout << setprecision(2) << setiosflags(ios::fixed);
cout << "double_unsorted:" << endl;
output(numsf);
cout << endl;
cout << "double_sorted:" << endl;
quick(numsf);
output(numsf);
pause();
return 0;
}
void pause()
{
cin.get();
cin.get();
}
運行截圖
上圖分別是生成40個數和20個數的運行截圖。
用戶類User
源碼
頭文件 User.h
#ifndef _USER_H
#define _USER_H
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
string input_star()
{
char password[30];
string passwd;
int index = 0;
while (1)
{
char ch;
ch = getch();
if (ch == 8)
{
if (index != 0)
{
cout << char(8) << " " << char(8);
index--;
}
}
else if (ch == '\r')
{
password[index] = '\0';
cout << endl;
break;
}
else
{
cout << "*";
password[index++] = ch;
}
}
passwd.assign(password);
return passwd;
}
class User
{
private:
string name = "";
string passwd = "111111";
string mail = "";
public:
User(string a, string b, string c) : name(a), passwd(b), mail(c){};
void SetPasswd()
{
string temp;
temp = input_star();
if (temp == "")
return;
passwd = temp;
}
void SetName()
{
string temp;
cin >> temp;
name = temp;
}
void SetMail()
{
string temp;
cin >> temp;
mail = temp;
}
string ReadName()
{
return name;
}
string ReadMail()
{
return mail;
}
string ReadPasswd()
{
return passwd;
}
void SetInfo()
{
cout << "Please enter the name:" << endl;
SetName();
cout << "Please enter the email:" << endl;
SetMail();
cout << "Please enter the password:" << endl;
SetPasswd();
}
void PrintInfo()
{
cout << "Name: " << name << endl;
cout << "Password: ******" << endl;
cout << "Mail: " << mail << endl;
}
};
#endif
主函數
//TODO:文件讀寫,實現軟件關閉后打開依然可以登錄
#include "User.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const User Default("", "111111", "");
vector<User> users;
char input;
string iemail, ipasswd, iname;
void Register();
void Login();
void Forget();
void Menu();
void LoginMenu();
int main()
{
while (1)
{
Menu();
cin >> input;
if (input == 'E')
break;
if (input == 'R')
{
Register();
}
if (input == 'L')
{
Login();
}
if (input == 'F')
{
Forget();
}
}
return 0;
}
void Menu()
{
cout << "Menu: R(egister),L(ogin),F(orget password),E(xit)" << endl;
}
void LoginMenu()
{
cout << "Menu: Change your P(assword) N(ame) E(mail). S(how info). Q(uit)" << endl;
}
void Register()
{
users.push_back(Default);
users.back().SetInfo();
while (1)
{
if (users.back().ReadMail().find('@') == string::npos)
{
cout << "The email you enter is wrong.Please enter again:" << endl;
users.back().SetMail();
}
else
break;
}
int temp = users.size();
for (int i = 0; i < temp - 1; i++)
{
if (users[i].ReadMail() == users.back().ReadMail() && users[i].ReadName() == users.back().ReadName())
{
users.pop_back();
cout << "Mail and name has been registered." << endl;
}
else if (users[i].ReadName() == users.back().ReadName())
{
users.pop_back();
cout << "The name has been registered." << endl;
}
else if (users[i].ReadMail() == users.back().ReadMail())
{
users.pop_back();
cout << "The mail has been registered." << endl;
}
}
}
void Login()
{
cout << "Please enter your email:" << endl;
cin >> iemail;
cout << "Please enter your password:" << endl;
ipasswd = input_star();
for (auto &i : users)
{
if (i.ReadMail() == iemail && ipasswd == i.ReadPasswd())
{
cout << "Login Sussfully" << endl;
while (1)
{
LoginMenu();
cin >> input;
if (input == 'Q')
break;
if (input == 'P')
{
cout << "Please enter your original password:" << endl;
ipasswd = input_star();
if (i.ReadPasswd() == ipasswd)
{
cout << "Please enter your new password:" << endl;
i.SetPasswd();
cout << "You have changed your password successfully.Please login again" << endl;
break;
}
else
{
cout << "Your original password is wrong!" << endl;
}
}
if (input == 'N')
{
cout << "Please enter your new name:" << endl;
i.SetName();
cout << "Change successfully!" << endl;
}
if (input == 'E')
{
cout << "Please enter your new mail:" << endl;
i.SetMail();
while (1)
{
if (i.ReadMail().find('@') == string::npos)
{
cout << "The email you enter is wrong.Please enter again:" << endl;
i.SetMail();
}
else
break;
}
cout << "Change successfully!" << endl;
}
if (input == 'S')
{
i.PrintInfo();
}
}
}
else
{
cout << "Login Fail!" << endl;
}
}
}
void Forget()
{
cout << "Please enter your email:" << endl;
cin >> iemail;
cout << "Please enter your name:" << endl;
cin >> iname;
if (users.size() == 0)
cout << "No match account" << endl;
else
{
for (auto i : users)
{
if (i.ReadMail() == iemail && i.ReadName() == iname)
{
cout << "Your password is\"" << i.ReadPasswd() << "\"" << endl;
}
else
{
cout << "No match account" << endl;
}
}
}
}
運行截圖
因為實現的功能比較多,運行截圖不能全部展示,建議自行編譯運行查看。
實驗總結與體會
-
體會到了重載函數使用的便利性,特別是可以使用函數模板進行整合的時候,代碼會顯得非常簡潔凝練。
-
在設計
快速排序函數模板
時,首次用到了vector
,查閱了相當多的資料,比起C語言時的數組方便了好多用起來就一個字"爽"而且迭代器
在我查閱的資料里被反復提及,沒有去查是什么意思,當作一個注意點,之后可以去查查。 -
User類
的實現花了我不少時間,雖然寫出來的東西有那么一點點偏離題意?雖然輸入文本時顯示*
是借鑒了網上的代碼,但我還是進行了修改以實現復用。這個主函數基本實現了多用戶的 注冊,登錄,忘記密碼,修改信息 的系統。我給大家簡單介紹一下實現的功能。-
用戶注冊時不能使用已經存在的用戶名和郵箱。
-
需要輸入密碼的地方,輸入內容顯示星號。
-
同時輸入正確的郵箱和用戶名可以找回密碼。這樣不太安全,應該是給用戶發郵件的,不過本來就是模擬登錄流程,所以就這樣啦。
-
郵箱正確性檢測,含有@才可以通過。
-
登錄后可以修改個人信息,修改密碼需要正確輸入原密碼,且修改成功后需要重新登錄。
-
可能有遺漏,畢竟寫的時候也是想一出寫一出。大家可以自行編譯嘗試。
-
TODO:文件讀寫,實現軟件關閉后打開依然可以登錄
互評博客:
GeorgeWan
Nibelungenlied
W天秤