Java教程

实验报告一

本文主要是介绍实验报告一,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include<iostream>
#include<math.h> 
using namespace std; 

class Complex
{
    private:
        double a,b;
        
    public:
        Complex();
        Complex(double a1);
        Complex(double a1,double b1);
        Complex(const Complex &p);//复制构造函数 

        double get_real() const{ return a;}
        double get_imag() const{ return b;}
        
        void show()const;
        void add(Complex &q);
        
        friend Complex add(const Complex &c1,const Complex &c2);
        friend bool is_equal(const Complex &c1,const Complex &c2);
        friend double abs(const Complex &q);    
};


Complex::Complex():a(0),b(0){
}
 Complex::Complex(double a1)
 {
     a=a1;
     b=0;
 }
 Complex::Complex(double a1,double b1)
{
    a=a1;
    b=b1;
}
Complex::Complex(const Complex &p)
{
    a=p.a;
    b=p.b;    
}


void Complex::add(Complex &q)
{
    a=a+q.a;
    b=b+q.b;
}

void Complex::show() const
{
    double x,y;
    x=get_real();
    y=get_imag();
    
    if(x==0&&y==0)
        cout<<x;
    else if(x==0&&y!=0)
            cout<<y<<"i";
    else if(y==0&&x!=0)
            cout<<x<<endl; 
    else if(x!=0&&y<0)
            cout<<x<<y<<"i";
    else if(x!=0&&y>0)
            cout<<x<<"+"<<y<<"i"; 
}

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

bool is_equal(const Complex &c1, const Complex &c2)
{
    if(c1.a==c2.a&&c1.b==c2.b)
                return true;
    else    return false;
}

double abs(const Complex &p)
{
    double c3;
    c3=sqrt(p.a*p.a+p.b*p.b);    
    return c3;
}

 

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

class User
{    
    private:
    string name;
    string password;
    string email;
    static int n;
    
    public:
        User(string name1); 
        User(string nameq, string password1, string email1); 
           ~User() = default;
           
           void set_email() ;
           void change_passwd() ;
           void print_info() ;
           
       static void print_n()
    {
        cout << "there are " << n << " users." << endl;
    }
};

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

void User::change_passwd()
{
    string passwd1,passwd2;
    int t,i;
    cout<<"Enter old password:";
    t=0;
    for(i=1;i<=3;i++)
    {
        cin>>passwd1;
        
        if(passwd1==password)
        {
            cout << "Enter new password: ";
            cin>>passwd1;
            cout << "new password is set successfully..." << endl;
            break;            
        }
        else
        {
            t++;
            
            if(t==3)
            {
                cout << "password input error.Please try after a while." << endl;
                break;
            }
            else
                cout << "password input error. Please re-enter again: ";
        }
    }        
}
void User::print_info()
{
    cout << "name: " << name << endl;
    cout << "password: ******" << endl;
    cout << "email: " << email << endl;
}

int User::n = 0;

User::User(string name1)
{
    name = name1;
    password = "111111";
    email = " ";
    n++;
}
User::User(string name1, string password1, string email1)
{
    name = name1;
    password = password1;
    email = email1;
    n++;
}

 

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