首页 > 其他分享 >实验二

实验二

时间:2022-10-13 18:45:03浏览次数:49  
标签:const cout void Complex 实验 c1 include

任务四

Complex.hpp

#include<iostream>
#include<cmath>
using namespace std;
class Complex{
    public:
        Complex(double a=0.0,double b=0.0):real{a},imag{b}{};
        Complex(const Complex &c1);
        ~Complex()=default;
        double get_real() const {return real;}
        double get_imag() const {return imag;}
        void show() const;
        void add(Complex const &c1);
        friend Complex add(Complex const&c1,Complex const&c2);
        friend bool is_equal(Complex const&c1,Complex const&c2);
        friend double abs(Complex const&c1);
    private:
        double real;
        double imag;
};

Complex::Complex(const Complex &c1){
    real=c1.real;
    imag=c1.imag;
}

void Complex::show()const{
    if(imag==0)
    cout<<real;
    else if(imag<0)
    cout<<real<<"-"<<abs(imag)<<"i";
    else
    cout<<real<<"+"<<imag<<"i";
}

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

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

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

double abs(Complex const&c1){
    return sqrt(c1.real*c1.real+c1.imag*c1.imag);
}

  task4.cpp

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

void test() {
    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;
}

int main() {
    test();
}

  

 

 更换数据

 

 任务五

User.cpp

#include<iostream>
#include<string>
using namespace std;

class User{
    public:
        User(string name,string passwd="111111",string email=""):n{name},p{passwd},e{email}{count++;}
        ~User()=default;
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n();
    private:
        string n,p,e;
        static int count;
};
int User::count=0;
void User::set_email(){
    cout<<"Enter email adress:";
    cin>>e;
    cout<<"email is set successfully..."<<endl;
}
void User::change_passwd(){
    cout<<"Enter old password:";
    string temp;
    int i=3;
    while(i){
        cin>>temp;
        if(temp==p){
            cout<<"new passwd is set successfully..."<<endl;
            break;
        }
        else{
            i--;
            if(i!=0)
            cout<<"password input error.Please re-enter again:";
        }
        if(i==0)
        {
            cout<<"password input error.Please try after a while."<<endl;
        }
    }
}
void User::print_info()
{
    cout<<"name: "<<n<<endl;
    cout<<"passwd:"<<"******"<<endl;
    cout<<"email: "<<e<<endl;
}
void User::print_n()
{
    cout<<"there are "<<count<<"users."<<endl;
}

  task5.cpp

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

void test() {
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();

    cout << endl
         << "testing 2......\n\n";
         
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout << endl;
    User::print_n();
}

int main() {
    test();
}

 

 

 

标签:const,cout,void,Complex,实验,c1,include
From: https://www.cnblogs.com/yzhag/p/16789273.html

相关文章