首页 > 其他分享 >实验2

实验2

时间:2022-10-15 09:11:15浏览次数:44  
标签:real cout imag Complex 实验 c1 void

头文件:
#pragma once
#include<iostream>
#include<math.h>
using namespace std;

class Complex
{
public:
    Complex() :real{ 0 }, imag{ 0 }{}
    ~Complex() {}
    Complex(double real) :real{ real }, imag{ 0 }{}
    Complex(double real, double imag) :real{ real }, imag{ imag }{}
    Complex(const Complex& other) :real{ other.real }, imag{ other.imag }{}

    double get_real() const { return real; };
    double get_imag() const { return imag; };
    void show() const;
    void add(const Complex& other);

    friend Complex add(Complex c1, Complex c2);
    friend bool is_equal(Complex c1, Complex c2);
    friend double abs(Complex& other);

private:
    double real, imag;
};

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

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

Complex add (Complex c1, 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& other)
{
    double t;
    t = (double)sqrt((double)pow(other.real, 2) + (double)pow(other.imag, 2));
    return t;
}
源文件
#include <iostream>
#include "Complex.h"

void test() {
    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;
}
int main() {
    test();
}

 

头文件
#pragma once
#include<iostream>
#include<string>
using namespace std;
class User
{
public:
    User(string n1, string p1 = "111111", string e1 = "");
    void set_email();
    void change_passwd();
    void print_info();
    static void print_n() { cout << "there are " << User::n << " users."; }
private:
    string name, passwd, email;
    static int n;
};
User::User(string n1, string p1, string e1) :name{ n1 }, passwd{ p1 }, email{ e1 }
{++n; }
int User::n = 0;
void User::set_email()
{
    cout << "Enter email address: ";
    string s;
    cin >> s;
    email = s;
    cout << "email is set successfully..." << endl;
}
void User::change_passwd()
{
    string c1;
    cout << "Enter old password: ";
    cin >> c1;
    int count = 1;
    while (c1 != passwd && count < 3)
    {
        cout << "password input error. Please re-enter again: ";
        cin >> c1;
        count++;
        if (count == 3)
            cout << "password input error. Please try after a while." << endl;
    }
    if (count != 3)
    {
        cout << "Enter new passwd: ";
        cin >> c1;
        passwd = c1;
        cout << "new passwd is set successfully..." << endl;
    }
}
void User::print_info() {
    cout << "name: " << name << endl;
    cout << "passwd: ";
    for (int i = 0; i < passwd.length(); i++)
        cout << "*";
    cout << endl;
    cout << "email: " << email << endl;
}
源文件
#include "User.h"
#include <iostream>
// 测试User类
void test() {
    using std::cout;
    using std::endl;
    cout << "testing 1......\n";
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();
    cout << endl
        << "testing 2......\n\n";
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();
    cout << endl;
    User::print_n();
}
int main() {
    test();
}

 

标签:real,cout,imag,Complex,实验,c1,void
From: https://www.cnblogs.com/jat2003725w/p/16791565.html

相关文章

  • 实验任务
    实验任务1.1#include<stdio.h>#include<stdlib.h>intmain(){   printf("o\n");   printf("<H>\n");   printf("II\n");   printf("o\n");......
  • Python实验报告——第6章 函数
    实验报告【实验目的】 1.掌握如何创建并调用一个函数,以及如何进行参数传递和指定函数的返回值等。2.掌握变量的作用域和匿名函数。【实验条件】1.PC机或者远程编程......
  • 实验一 C 语言开发环境使用和数据类型,运算符,表达式
      #include<stdio.h>#include<stdlib.h>intmain(){printf("OO\n");printf("<H><H>\n");printf("II II\n");sys......
  • 实验1:C语言开发环境使用和编程初体验
     任务一:#include<stdio.h>#include<stdlib.h>intmain(){printf("00\n");printf("<H><H>\n");printf("III......
  • 实验二 类和对象(2)
    任务四代码:Complex.hpp文件源码:1#pragmaonce23//Complex类的定义4#include<iostream>5#include<cmath>67usingnamespacestd;89class......
  • 实验1
    task1_1#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>\n");printf("......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验2 类和对象(2)
    实验四Complex.hpp#pragmaonce#include<iostream>#include<cmath>usingnamespacestd;classComplex{public:Complex(){};Complex(doublea){......
  • 实验二 类与对象2
    4.实验任务4不使用C++标准库,自行设计并实现一个简化版复数类ComplexComplex.hpp1#include<iostream>2#include<math.h>3usingnamespacestd;45clas......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;能够运......