首页 > 其他分享 >实验2

实验2

时间:2022-10-17 18:33:28浏览次数:31  
标签:real const cout imag Complex 实验 include

#pragma once
#include <iostream> 
#include<string>
#include<iomanip>
#include<cmath>
class Complex {
public:
    Complex() { real = 0; imag = 0; }
    Complex(double x) {real = x; imag = 0; }
    Complex(double x, double y){real = x; imag = y; }
    Complex(Complex& obj) { real = obj.real; imag = obj.imag; }
       
    double get_real()const { return real; }
    double get_imag()const { return imag; }
    Complex add(const Complex& obj) { Complex c; c.real = real + obj.real; c.imag = imag + obj.imag; return c; };
    void show() const { if (imag) { std::cout << real << imag << "i" ; }
    else { std::cout << real; }
    }

    friend Complex add(const Complex &a,const Complex &b);
    friend bool is_equal(const Complex &a,const Complex &b);
    friend double abs(const Complex& a);

private:
    double imag, real;

 

};

Complex add(const Complex &a, const Complex &b) {
    Complex c;
    c.real = a.real + b.real;
    c.imag = a.real + a.imag;
    return c;
 }
bool is_equal(const Complex& a, const Complex& b) {
    return (a.real == b.real) && (a.imag == b.imag);
}
double abs(const Complex& a)
{
    return sqrt(a.real * a.real + a.imag * a.imag);
}
#include"Complex.hpp"
#include<iostream>
void test() {
    using namespace std;
    Complex c1(33, -4);
    const Complex c2(5.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; 
}

int main() { test(); }

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

class User
{
public:
    User(string a) { name = a; password = "111111"; email = " "; }
    User(string a, string b, string c) { name = a; password = b; email = c; }
    void set_email();
    void change_password();
    void print_info();
    static int print_n();

private:
    string name, password, email;

};



 void User::change_password()
{
        cout << "Enter old passord:";
        int cnt = 3;
        while (cnt--)
        {
            string a;
            cin >> a;
            if (a != password)
            {
                cout << (cnt == 0 ? "password input error.Please try after a while\n" : "password input error.Please re-enter again:") ;
            }
            else
            {
                cout << "Enter new password:";
                cin >> password;
                cout << "new password is set successfully..." << endl;
                break;
            }
        }
}

 void User::set_email()
 {
     cout << "Enter email adress:";
     cin >> email;
     cout << "email is set succesfully..." << endl;

 }



 void User::print_info()
{
     int n = password.length();


    cout << "name:  " << name << endl;
    cout << "password:  ";
    for (int i = 0; i < n; i++)
    {
        cout << "*";
    }
    cout << endl;
    cout << "email:  " << email << endl;
}
int User::print_n() {
    static int x = 1;
    x++;
    cout << "there are " << x << " users." << endl;
    return x;
}
#include"User.hpp"
#include<iostream>
void test() {
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "[email protected]");
    user1.print_info();
    cout << endl << "testing 2......\n\n";

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

    cout << endl;
    User::print_n();
}

int main() { test(); }

 

标签:real,const,cout,imag,Complex,实验,include
From: https://www.cnblogs.com/jh025/p/16800182.html

相关文章

  • 同时显示多个数字程序~实验板上的数码管从左到右同时显示“1”、“2”、“3”
    1DAT_74164BITP0.62CLK_74164BITP0.734org0000H5jmpMAIN67;***********************************......
  • 实验-Linux添加磁盘分区挂载以及CentOS下的文件系统
    概览实验项目名称CentOS7下的文件系统实验时间2022年10月17日实验类型□验证性□设计性□综合性一、实验目的1.掌握在虚拟机中......
  • 实验二
    实验四  #pragmaonce#include<iostream>#include<string>#include<iomanip>#include<cmath>usingstd::cout;usingstd::endl;classComplex{public:Complex()......
  • 完成基础实验【硬件课程设计】
    完成基础实验【硬件课程设计】​​前言​​​​推荐​​​​完成基础实验​​​​最后​​前言第一周周二:学生小组完成基础实验2022/8/30仅供学习交流使用​请您阅读文章......
  • 学生小组设计自选实验【硬件课设】
    学生分组和选题【硬件课程设计】​​前言​​​​推荐​​​​学生小组设计自选实验​​​​1方案设计​​​​2系统原理图设计​​​​3系统仿真与仿真结果​​​​4PCB......
  • 实验2 类和对象(2)
    实验4complex.hpp#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(doublex0,doubley);Complex(c......
  • 实验二 类和对象
    #pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classcomplex{public:complex(doubler=0.0,doublei=0.0):real(r),imag(i){}......
  • 实验5:开源控制器实践——POX
    一.基础要求只需要提交h1pingh2、h2和h3的tcpdump抓包结果截图,外加L2_learning模块代码流程图,其余文字请勿赘述;1.使用命令创建拓扑:sudomn--topo=single,3--mac--co......
  • 软件设计实验12
    实验12:外观模式[实验任务一]:计算机开启在计算机主机(Mainframe)中,只需要按下主机的开机按钮(on()),即可调用其他硬件设备和软件的启动方法,如内存(Memory)的自检(check())......
  • 实验室深度学习服务器崩溃——Oops: 0000 [#1] SMP NOPTI
    这两天实验室的服务器总是崩溃,重启已经不能解决问题了,由于是跑深度学习的服务器,而且还是承接国家级项目的运行服务器,可以说是实验室的主要生产力了,给出报错的日志:  Oc......