实验一

实验3:

Complex.h

 #ifndef COMPLEX_HPP
 #define COMPLEX_HPP 
 #include<iostream>
 #include<cmath>
 using namespace std;
 class Complex
 {
 private:
     double real,image;
public:
    Complex( double x,double y):real(x),image(y){};
    Complex(){}
    Complex(double x):real(x),image(0){}
    Complex(Complex &c):real(c.real),image(c.image){}
     
    double get_real()const{return real;}
    double get_image()const{return image;}
    void show()const;
    void add(Complex c){real+=c.real;image+=c.image;}
    
    ~Complex()=default;
    
    friend Complex add(const Complex &c1,const Complex &c2);
    friend bool is_equal(const Complex &c1,const Complex &c2);
    friend double abs(const Complex &c1);
};
void Complex:: show()const
{
    if(image>0)
    cout<<real<<"+"<<image<<"i"<<endl;
    else if(image<0)
    cout<<real<<image<<"i"<<endl;
    else
    cout<<real<<endl;
}
Complex add(const Complex &c1,const Complex &c2)
{
    Complex c3;
    c3.real=c1.real+c2.real;
    c3.image=c1.image+c2.image;
    return c3;
}
bool is_equal(const Complex &c1,const Complex &c2)
{
    if(c1.real==c2.real&&c1.image==c2.image)
    return true;
    else 
    return false;
}
double abs(const Complex &c1)
{
    return sqrt(c1.real*c1.real+c1.image*c1.image);
}
#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;
}

 实验4:

User.hpp

#ifndef USER_HPP
#define USER_HPP

// User类的定义
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

class User{
    public:
        User(string a, string b = "111111", string c = ""): name{a}, passwd{b}, email{c} {count++;}
        void set_email() {
            string newone = "";
            while(newone.find('@') == -1) {
                cout << "Enter email address: ";
                cin >> newone;
                email = newone;
            }
        }
        void change_passwd() {
            cout << "Enter old password: ";
            string front;
            cin >> front;
            if(front == passwd) {
                cout << "Enter new password: ";
                string newone;
                cin >> newone;
                while(newone.length()!=6) {
                    cout << "Enter new password: ";
                    cin >> newone;
                    if(newone.length() == 6) {
                        passwd = newone;
                        cout << "new passwd is set successfully..." << endl;
                    }
                }

            }
        }
        void print_info() {
            cout << "name: " << name << endl;
            cout << "password: ******" << endl;
            cout << "email: " << email << endl;
        }
        static void print_n() {
            cout << "there are " << count << " users." << endl;
        }
    private:
        string name, passwd, email;
        static int count;
};

int User::count = 0;

#endif

task4.cpp

#include "User.hpp"
#include <iostream>
int main()
{
    using namespace std;
    cout << "testing 1......" << endl;
    User user1("HHY", "200283", "hhy@hotmail.com");
    user1.print_info();
    cout << endl
         << "testing 2......" << endl
         << endl;
    User user2("WYXbenbi");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    User::print_n();
}

 

 

posted @ 2021-10-24 14:07  软饭弟弟  阅读(25)  评论(5编辑  收藏  举报