Java教程

实验一

本文主要是介绍实验一,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#ifndef COMPLEX_HPP
#define COMPLEX_HPP

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

class Complex{
    public:
        Complex();
        Complex(double a);
        Complex(double a,double b);
        Complex(const Complex &c);
        double get_real() const{return real;}
        double get_imag() const{return imag;}
        void show() const;
        void add(const Complex &c);
        friend Complex add(const Complex &c1,const Complex &c2);
        friend bool is_equal(Complex c1,Complex c2);
        friend double abs(Complex c);
    private:
        double real,imag;
};

Complex::Complex():real(0),imag(0){}
Complex::Complex(double a):real(a),imag(0){}
Complex::Complex(double a,double b):real(a),imag(b){}

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

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

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

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

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

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

#endif

 task4

#ifndef USER_HPP
#define USER_HPP

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

class User{
    public:
        User(string n);
        User(string n,string p,string e);
        void set_email();
        void change_passwd();
        void print_info();
        static void print_n();
    private:
        string name,password,email;
        static int n0;
};

int User::n0=0;

User::User(string n):name(n),password("111111"),email(""){
    n0++;
}

User::User(string n,string p,string e):name(n),password(p),email(e){
    n0++;
}

void User::set_email(){
    string e;
    cout<<"please set your email:";
    cin>>e;
    email=e;
}

void User::change_passwd(){
    string p1,p2;
    for(int i=1;i<=3;i++){
        cout<<"please input the old password:";
        cin>>p1;
        if(password==p1){
            cout<<"please input the new password:";
            cin>>p2;
            password=p2;
            break;
        }
        if(i==3) cout<<"please try again later.";
    }
}

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

void User::print_n(){
    if(n0>=1) cout<<"there are "<<n0<<" users."<<endl;
    else cout<<"there are "<<n0<<" user."<<endl;
}

#endif

 

这篇关于实验一的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!