实验2

实验2.4

程序源码

Complex.hpp 源代码

#pragma once
#include <iostream>
#include <cmath>
using namespace std;
class Complex{
    public:
        Complex(double a=0,double b=0):real{a},imag{b}{};
        Complex(const Complex& p):real{p.real},imag{p.imag}{};
        void show()const;
        double get_imag()const;
        double get_real()const;
        void add(const Complex& p);
        friend bool is_equal(const Complex& a,const Complex& b);
        friend double abs(const Complex& p);
        friend Complex add(const Complex& a,const Complex& b);
    private:
        double real;double imag;
};
void Complex::show()const{
if(real==0&&imag==0){
    cout<<0;
}
else  if(real==0&&imag!=0){
    cout<<imag<<"i";
}
else if(real!=0&&imag==0){
    cout<<real;
}
else if(real!=0&&imag!=0){
    if(imag<0){
        cout<<real<<imag<<"i";
    }
    else{
        cout<<real<<"+"<<imag<<"i";
    }
}
};


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

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

double abs(const Complex& p){
    return sqrt(p.get_real()*p.get_real()+p.get_imag()*p.get_imag());
}

Complex add(const Complex& a,const Complex& b) {
    Complex c(a.get_real()+b.get_real(),a.get_imag()+b.get_imag());
    return c;
}

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

bool is_equal(const Complex& a,const Complex& b){
    bool i;
    if(a.get_real()==b.get_real()&&a.get_imag()==b.get_imag()){
        i=1;
    }
    else{
        i=0;
    }
    return i;
}

 

task4.cpp源码

#include "Complex.hpp"
#include <iostream>
void test(){
    using namespace std;

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

实验2.5

程序源码

User.hpp文件源码

#pragma once
#include <iostream>
#include <string>
using namespace std;
class User{
    public:
    User(string a="Mary",string b="111111",string c=""):name{a},passwd{b},email{c}{count++;};
    void print_info();
    void change_passwd();
    void set_email();
    static void print_n();
    private:
        string name,passwd,email;static int count;
};

void User::print_info(){
    string s1(passwd.size(),'*');
    cout<<"name: "<<name<<endl;
    cout<<"passwd: "<<s1<<endl;
    cout<<"email: "<<email<<endl;
}

int User::count=0;

void User::print_n(){
    cout<<"There are "<<count<<" users."<<endl;
}

void User::change_passwd(){
    cout<<"Enter old password: ";
    string s1;
    int i=1;
    cin>>s1;
    for(i=1;i<=2;i++){
        if(s1==passwd){
            cout<<"Enter new passwd: ";
            cin>>passwd;
            break;
        }
        else{
        cout<<"password input error.Please re-enter again:";
            cin>>s1;
        }
    }
    if(i==3){
        cout<<"password input error.Please try after a while."<<endl;
    }
}

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

task5.cpp源码

#include "User.hpp"
#include <iostream>
void test(){
    using std::cout;
    using std::endl;

    cout<<"testing 1......\n";
    User user1("jonny","92197","xyz@hotmail.com");
    user1.print_info();

    cout<<endl;
    cout<<"testing 2......\n\n";

    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    cout<<endl;
    User::print_n();
}
int main(){
    test();
}

修改后

posted @ 2022-10-17 10:27  丁鑫铖  阅读(20)  评论(0编辑  收藏  举报