zoukankan      html  css  js  c++  java
  • 实验一

    实验3:

    Complex.h

     #ifndef COMPLEX_HPP
     #define COMPLEX_HPP 
     #include<iostream>
     #include<cmath>
     using namespace std;
     class Complex
     {
     private:
         double real,image;
    public:
        Complex( double x,double y):real(x),image(y){};
        Complex(){}
        Complex(double x):real(x),image(0){}
        Complex(Complex &c):real(c.real),image(c.image){}
         
        double get_real()const{return real;}
        double get_image()const{return image;}
        void show()const;
        void add(Complex c){real+=c.real;image+=c.image;}
        
        ~Complex()=default;
        
        friend Complex add(const Complex &c1,const Complex &c2);
        friend bool is_equal(const Complex &c1,const Complex &c2);
        friend double abs(const Complex &c1);
    };
    void Complex:: show()const
    {
        if(image>0)
        cout<<real<<"+"<<image<<"i"<<endl;
        else if(image<0)
        cout<<real<<image<<"i"<<endl;
        else
        cout<<real<<endl;
    }
    Complex add(const Complex &c1,const Complex &c2)
    {
        Complex c3;
        c3.real=c1.real+c2.real;
        c3.image=c1.image+c2.image;
        return c3;
    }
    bool is_equal(const Complex &c1,const Complex &c2)
    {
        if(c1.real==c2.real&&c1.image==c2.image)
        return true;
        else 
        return false;
    }
    double abs(const Complex &c1)
    {
        return sqrt(c1.real*c1.real+c1.image*c1.image);
    }
    #endif

    task3.cpp

    #include "Complex.hpp"
    #include <iostream>
    
    int main()
    {
        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;
    }

     实验4:

    User.hpp

    #ifndef USER_HPP
    #define USER_HPP
    
    // User类的定义
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cmath>
    
    using namespace std;
    
    class User{
        public:
            User(string a, string b = "111111", string c = ""): name{a}, passwd{b}, email{c} {count++;}
            void set_email() {
                string newone = "";
                while(newone.find('@') == -1) {
                    cout << "Enter email address: ";
                    cin >> newone;
                    email = newone;
                }
            }
            void change_passwd() {
                cout << "Enter old password: ";
                string front;
                cin >> front;
                if(front == passwd) {
                    cout << "Enter new password: ";
                    string newone;
                    cin >> newone;
                    while(newone.length()!=6) {
                        cout << "Enter new password: ";
                        cin >> newone;
                        if(newone.length() == 6) {
                            passwd = newone;
                            cout << "new passwd is set successfully..." << endl;
                        }
                    }
    
                }
            }
            void print_info() {
                cout << "name: " << name << endl;
                cout << "password: ******" << endl;
                cout << "email: " << email << endl;
            }
            static void print_n() {
                cout << "there are " << count << " users." << endl;
            }
        private:
            string name, passwd, email;
            static int count;
    };
    
    int User::count = 0;
    
    #endif

    task4.cpp

    #include "User.hpp"
    #include <iostream>
    int main()
    {
        using namespace std;
        cout << "testing 1......" << endl;
        User user1("HHY", "200283", "hhy@hotmail.com");
        user1.print_info();
        cout << endl
             << "testing 2......" << endl
             << endl;
        User user2("WYXbenbi");
        user2.change_passwd();
        user2.set_email();
        user2.print_info();
        User::print_n();
    }

     

  • 相关阅读:
    hbuilder外置服务器设置(局域网移动端调试)
    【转载】解决微信OAuth2.0网页授权回调域名只能设置一个的问题
    【转载】如何使用PHP构建一个高性能的弹幕后端服务
    【转载】PHP学习资源整理
    【php基础】php运算符 php取整函数
    Browser 对象(一、history)
    Opencv 图像平滑基础二维离散卷积C++ API
    Opencv 图像平滑基础二维离散卷积 python API
    道格拉斯轨迹抽稀算法Android 百度地图SDK
    Opencv 几何变换
  • 原文地址:https://www.cnblogs.com/ruanfandd/p/15450952.html
Copyright © 2011-2022 走看看