c++實驗報告 實驗


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、尚存的問題:不知道怎么對郵箱格式的合法性進行判斷,導致無論郵箱輸入什么都算合法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM