首页 > 其他分享 >实验二

实验二

时间:2022-10-17 21:58:11浏览次数:39  
标签:const cout double void Complex 实验 include

实验任务4

Complex.hpp

#pragma once

#include <iostream>
#include <cmath>

using namespace std;

class Complex
{
public:
    Complex(double r=0,double i=0);
    Complex(const Complex &c);
    ~Complex()=default;
    
    double get_real() const; 
    double get_imag() const;
    void show() const;
    void add(Complex  const &c);
    
    friend Complex add(Complex c1, Complex c2);
    friend bool is_equal(Complex c1, Complex c2);
    friend double abs(Complex c); 
    
private:
    double real,imag;
    
};

Complex::Complex(double r,double i):real{r},imag{i}{
}
    
Complex::Complex(const Complex &c):real{c.real},imag{c.imag}{
} 


double Complex::get_real() const{
    return real;
}

double Complex::get_imag() const{
    return imag;
}

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

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

Complex add(Complex const &c1,Complex const &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);
}

task 4

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

// 类测试
void test() {
    using namespace std;

    Complex c1(5, -12);
    const Complex c2(3.9);
    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();
}

 

实验任务5

User.hpp

#pragma once

#include<iostream>
#include<string>

using namespace std;

class User{
public:
    User(string nm, string pw = "111111", string em = "");
    ~User() = default;

    void set_email();
    void change_passwd();
    void print_info() const;
    void static print_n(){
        cout << "there are " << count << " users."<<endl;
    };

private:
    string name, passwd, email;
    static int n;
};

int User::n = 0;

User::User(string nm,string pw,string em)
{
    name = nm;
    passwd = pw;
    email = em;
    n++;
}

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

void User::change_passwd()
{
    string old_passwd;
    int count = 0;
    cout << "Enter old password: ";
    while (count != 3)
    {
        cin >> old_passwd;
        
        if (old_passwd==passwd)
        {
            cout << "Enter new passwd: ";
            cin >> passwd;
            cout << "new passwd is set successfully..." << endl;
            break;
        }
        else
        {
            count++;
            if (count != 3)
                cout << "password input error.Please re-enter again: ";
            else
                cout << "password input error.Please try after a while." << endl;
        }
    }
    
}

void User::print_info() const
{
    cout << "name: " << name << endl
    cout << "passwd: ";
    itn len = passwd.length();
    cout << string s1{len, '*'}; << endl;
    cout << "email: " << email << endl;
}

 

task 5

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

// 测试User类
void test()
{
    using std::cout;
    using std::endl;

    cout << "testing 1......\n";
    User user1("Cedric", "030317", "[email protected]");
    user1.print_info();

    cout << endl
         << "testing 2......\n\n";

    User user2("Aron");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

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

int main()
{
    test();
}

 

标签:const,cout,double,void,Complex,实验,include
From: https://www.cnblogs.com/mzxfcyt/p/16800840.html

相关文章

  • 实验二 类与对象(2)
    实验任务三:Employee.hpp:#pragmaonce#include<iostream>#include<string>#include<iomanip>usingstd::string;usingstd::cout;usingstd::endl;usingstd::s......
  • 【数据库】期末必知必会-----第六章 实验部分
    第六章实验部分(这一部分是考试重点)1、SQL语言的组成、特点?组成:1)DDL(数据库定义语言:CREAT、DROP、ALTER)2)DML(数据库操纵语言:INSERT、UPDATE、DELETE、SELECT)3)DCL(数据库控制语......
  • 实验二
    任务四类的定义usingnamespacestd;classComplex{public:Complex(doubler=0,doublei=0){real=r;imag=......
  • 实验2 类和对象(2)
    实验任务4#include"Complex.hpp"#include<iostream>voidtest(){usingnamespacestd;Complexc1(4,-3);constComplexc2(6.6);Complexc3(c1......
  • 实验1C语言开发环境使用和数据类型,运算符,表达式
    #include<stdio.h>#include<stdlib.h>intmain(){printf("0\n");printf("<H>\n");printf("II\n");printf("0\n");printf("<H>\n");pr......
  • 实验二 类与对象
    实验任务一#include<iostream>#include<complex>intmain(){usingnamespacestd;complex<double>c1{3,4},c2{4.5};//支持两个参数//使用时,在语法层面......
  • 实验5:开源控制器实践——POX
    一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运用POX控制器编写自定义网......
  • 实验1 C语言开发环境使用和数据类型、运算符、表达式
    实验任务一#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf......
  • 机械虚拟仿真教学软件延伸实验教学实践和空间-深圳华锐视点
    虚拟仿真VR教学软件作为现代化信息技术与实验教学项目深度融合、拓展实验教学内容广度和深度,延伸实验教学实践和空间,提升实验教学质量和水平的重要举措,在网络授课越来......
  • 实验2
    #pragmaonce#include<iostream>#include<string>#include<iomanip>#include<cmath>classComplex{public:Complex(){real=0;imag=0;}Complex......