实验一

实验任务三

 

实验一实验一

 

 

 

#include "Complex.hpp"
#include <iostream>

int main()
{
    using namespace std;

    Complex c1(3, 5);
    const Complex c2(2.4);
    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;
}

 

#include<iostream>
#include<math.h>

class Complex 
{
public:
    Complex() = default;
    Complex(double real1,double imag1=0):real(real1),imag(imag1){}
    Complex(const Complex& complex) { real = complex.real;imag = complex.imag; }
    double get_real()const;
    double get_imag()const;
    void show()const;
    void add(const Complex &c);
    friend Complex add(Complex c1,Complex c2);
    friend bool is_equal(Complex c1, Complex c2);
    friend double abs(Complex c);
private:
    double real;
    double imag;
};

double Complex::get_real()const
{
    return real;
}

double Complex::get_imag()const
{
    return imag;
}

void Complex::show()const
{
    if (imag > 0)
        std::cout << real << '+' << imag << 'i' ;
    else if(imag==0)
        std::cout << real << imag;
    else
        std::cout << real << imag<<'i';
}

void Complex::add(const Complex& c)
{
    real += c.real;
    imag += c.imag;
}

Complex add(Complex c1, Complex c2)
{
    Complex c;
    c.real = c1.real + c2.real;
    c.imag = c1.imag + c2.imag;
    return c;
}

bool is_equal(Complex c1, Complex c2)
{
    if (c1.real == c2.real)
    {
        if (c1.imag == c2.imag)
            return true;
        else
            return false;
    }
    else
        return false;
}

double abs(Complex c)
{
    double ans = sqrt(c.real * c.real + c.imag * c.imag);
    return ans;
}

 

 

 

 

 

 

 

 

实验任务四

#include<string>
#include<iostream>

using namespace std;

class User
{
public:
    User(string name1, string passwd1 = "111111", string email1 = "") :
        name(name1), passwd(passwd1), email(email1)
    {
        n++;
    };
    ~User() = default;
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n();
private:
    string name;
    string passwd;
    string email;
    static int n;
};
int User::n = 0;

void User::set_email()
{
    string new_email;
    cout << "Enter email address:";
    cin >> new_email;
    email = new_email;
    cout << "email is set successfully..." << endl;
}

void User::change_passwd()
{
    int n = 1;
    string passwd1;
    cout << "Enter old password:";
    while (n)
    {
        cin >> passwd1;
        if (passwd == passwd1)
        {
            cout << "Enter new password:";
            cin >> passwd1;
            passwd = passwd1;
            cout << "new password is set successfully..." << endl;
            return;
        }
        else if (n == 3)
        {
            cout << "password input error. Please try after a while." << endl;
            return;
        }
        else
            cout << "password input error. Please re-enter again:";


        n++;
    }
}

void User::print_info()
{
    cout << "name:     " << name << endl
        << "password: " << "******" << endl
        << "email:    " << email << endl;
}

void User::print_n()
{
    cout << "there are " << n << " users." << endl;
}
#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();
}

 

实验一实验一

 

 

实验一

 

 

上一篇:boost::units::conversion_factor相关的测试程序


下一篇:自考新教材p132