实验1

1.自定义complex类的实现

  complex.hpp代码展示:

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
#include <cmath>


class complex 
{
public:
    
    complex(double i = 0.0, double j = 0.0);
    complex(const complex& p);
    double  get_real() const;
    double  get_imag() const;
    void show() const;
    void add(complex p8);
    friend  complex add(complex p2, complex p3);
    friend bool is_equal(complex p6, complex p7);
    friend double ads(complex p5);
    
private:
    double x;
    double y;
};

complex::complex(double i, double j) :x(i), y(j)
 {}

complex::complex(const complex &p) {
    x = p.x;
    y = p.y;
    
}

double  complex::get_real() const {
    return x;
}

double complex::get_imag() const
{
    return y;
}
void complex::show() const 
{
    using namespace std;
    const double real = x;
    const double imag=y;
    cout << real;
    if (y >= 0) cout << "+";
    cout << imag << "i";
    
}

void complex::add ( complex p8) 
{
    x += p8.x;
    y += p8.y;
}

   complex add( complex p2,complex p3)
 {
    complex p4;
    p4.get_real()==p2.get_real() + p3.get_real();
    p4.get_imag()== p2.get_imag() + p3.get_imag();
    return p4;
    
 }

 double abs (complex p5)
{
    double sum;
    sum = p5.get_real()*p5.get_real () + p5.get_imag()*p5.get_imag();
    sum = sqrt(sum);
    return sum;

}

bool is_equal(complex p6,complex p7)
{
    return p6.get_real() == p7.get_real() && p6.get_imag() == p7.get_imag();
}

#endif

   调用实现主函数展示:

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


int main()
{
    using namespace std;
    complex c1(3, -4);
    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;
}

运行效果展示:

 

2.User类的实现

  user.hpp代码展示:

#ifndef USER_HPP
#define USER_HPP

#include <iostream>
#include<iomanip>
#include <string>
#include<cmath>

using namespace std;

class User
{
public:
    User(string name, string passwd = "111111", string email = "\0");
    User();
    User(const User& u);
    ~User()
    {
        n--;
    };
    void set_email();
    void change_passwd();
    void  print_info();
    static void shown();
private:
    string _name;
    string _passwd;
    string _email;
    static  int n;
};

int User::n = 0;
User::User(string name, string passwd, string email) : _name{ name }, _passwd{ passwd }, _email{ email } {
    n++;
}

User::User(const User& u) : _name{ u._name }, _passwd{ u._passwd }, _email{u._email} {
    n++;
}
void User::set_email()
{
    cout << "请输入邮箱:";
    cin >> _email;
}

void User::change_passwd()
{
    string a;
    bool x=false;
    cout << "请输入原密码:" << endl;
    
    for (int i=0; i < 3; i++)
    {
        cin >> a;
        if (a.compare(_passwd) !=0)
        {
                cout << "输入错误,请重新输入:";
        }    
        else
        {
            x= true;
            cout << "请输入新密码:" << endl;
            break;
        }
    }

    if (n)
    {
        cin >>_passwd;
        cout << "修改成功" << endl;
    }
    else
    {
        cout << "密码输入错误,请稍后再试" << endl;
    }



}

void User::shown()
{
    cout << "there are " << n << " users" << endl;
}

void User::print_info()
{
    
    cout << "名字:" << _name << endl;
    cout << "密码:" << "******"<< endl;
    cout << "邮件:" << _email << endl;
    shown();
}

#endif

调用实现函数:

#include"User.hpp"


 int main()
 {  
     using namespace std;
     cout << "testing 1......" << endl;
     User user1("Frank", "39984", "sob@gmail.com");
     user1.print_info();
     cout << endl << "testing 2......" << endl;
     User user2("Sobriety");
     user2.change_passwd();
     user2.set_email();
     user2.print_info();
    
}

运行效果展示:

 

 个人总结:

学习编程是一个重在实践的过程,书上永远不可能包含所以的用法和实例,要自己在实践中发现自己所忽略的和自己根本不了解的编程问题。

posted @ 2021-10-25 14:12  社交废物  阅读(53)  评论(3)    收藏  举报