zoukankan      html  css  js  c++  java
  • 实验2:函数重载、函数模板、简单类的定义和实现(实现多用户登录,注册,修改信息)

    头图

    实验结论

    重载函数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 == '
    ')
            {
                password[index] = '';
                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类的实现花了我不少时间,虽然写出来的东西有那么一点点偏离题意?虽然输入文本时显示*是借鉴了网上的代码,但我还是进行了修改以实现复用。这个主函数基本实现了多用户注册,登录,忘记密码,修改信息 的系统。我给大家简单介绍一下实现的功能。

      1. 用户注册时不能使用已经存在的用户名和邮箱。

      2. 需要输入密码的地方,输入内容显示星号。

      3. 同时输入正确的邮箱和用户名可以找回密码。这样不太安全,应该是给用户发邮件的,不过本来就是模拟登录流程,所以就这样啦。

      4. 邮箱正确性检测,含有@才可以通过。

      5. 登录后可以修改个人信息,修改密码需要正确输入原密码,且修改成功后需要重新登录。

      6. 可能有遗漏,毕竟写的时候也是想一出写一出。大家可以自行编译尝试。

    TODO:文件读写,实现软件关闭后打开依然可以登录

    互评博客:
    GeorgeWan
    Nibelungenlied
    W天秤

  • 相关阅读:
    C++ list<list<int> >类型的对象遍历
    Apache与Nginx服务器对比
    服务器重写技术:rewrite
    冒泡排序(python版)
    有k个list列表, 各个list列表的元素是有序的,将这k个列表元素进行排序( 基于堆排序的K路归并排序)
    堆排序(C++版)
    [转载] 单链表的相关操作
    TCP三次握手连接与四次握手断开
    [转载] TCP与UDP对比
    进程与线程的联系与区别
  • 原文地址:https://www.cnblogs.com/KOKODA/p/10566358.html
Copyright © 2011-2022 走看看