自己实现的简单的Complex类

最近学习C++时,为了充分认识运算符重载自己在闲暇时间谢了一个complex类,现在放上来和大家共享共享:

 

首先看到的是头文件

#ifndef COMPLEX_H
#define COMPLEX_H
#include<iostream>
using namespace std;

class Complex
{
    friend Complex operator+(const Complex&,const Complex&);
    friend Complex operator-(const Complex&,const Complex&);
    friend Complex operator*(const Complex&,const Complex&);
    friend Complex operator/(const Complex&,const Complex&);
    friend istream& operator>>(istream&,Complex&);
    friend ostream& operator<<(ostream&,const Complex&);
    public:
        Complex(const Complex&);
        Complex(int real = 0,int imag = 0);
        virtual ~Complex();
        Complex& operator++();
        Complex operator++(int index);
        Complex& operator--();
        Complex operator--(int index);
        operator bool();
        operator string();
    private:
        int real;
        int imag;
};
#endif // COMPLEX_H
 
代码文件中定义了Complex的简单的一些使用操作。
下面是对这些操作的主要实现:
#include "Complex.h"
#include<iostream>
#include<cstdlib>
using namespace std;
Complex::Complex(const Complex& souceComplex)
{
    real = souceComplex.real;
    imag = souceComplex.imag;
}
Complex::Complex(int real,int imag):real(real),imag(imag){}
Complex::~Complex(){}
Complex& Complex::operator++()
{
    ++real;
    ++imag;
    return *this;
}
Complex Complex::operator++(int index)
{
    Complex temp;
    ++real;
    ++imag;
    return temp;
}

Complex& Complex::operator--()
{
    --real;
    --imag;
    return *this;
}
Complex Complex::operator--(int index)
{
    Complex temp;
    --real;
    --imag;
    return temp;
}

Complex operator+(const Complex& first,const Complex& second)
{
    Complex temp;
    temp.real = first.real+second.real;
    temp.imag = first.imag+second.imag;
    return temp;
}
Complex operator-(const Complex& first,const Complex& second)
{
    Complex temp;
    temp.real = first.real-second.real;
    temp.imag = first.imag-second.imag;
    return temp;
}
Complex operator*(const Complex& first,const Complex& second)
{
    Complex temp;
    temp.real = first.real*second.real-first.imag*second.imag;
    temp.imag = first.imag*second.real+second.imag*first.real;
    return temp;
}
Complex operator/(const Complex& first,const Complex& second)
{
    Complex temp;

    return temp;
}
istream& operator>>(istream& in,Complex& souce)
{
    cout<<"Please input real and image:"<<endl;
    in>>souce.imag>>souce.real;
    return in;
}
ostream& operator<<(ostream& out,const Complex& result)
{
    cout<<"The Comples is:"<<endl;
    out<<'<'<<result.real<<'+'<<result.imag<<"i>"<<endl;
    return out;
}

Complex::operator bool()
{
    bool flag = false;
    if(real>0)
    {
        flag = true;
    }
    return flag;
}
Complex::operator string()
{
    char* str;
    str = (char*)malloc(sizeof(char)*20);
    int count = 0;
    while(real/10!=0)
    {
        int temp = real/10;
        str[count] = temp+'0';
        ++count;
    }

    str[count] = real%10+'0';
    ++count;
    str[count]='+';

    while(imag/10!=0)
    {
        int temp = imag/10;
        str[count] = temp+'0';
        ++count;
    }
    ++count;
    str[count] = imag%10+'0';
    ++count;
    str[count]='i';
    ++count;
    str[count]='\0';
    return str;
}


以上就是个人在学习运算符重载时做的一个简单的Complex,不是很好需要完善。

在Java中,我们可以定义一个名为Complex来表示复数,并提供加法和减法的操作。下面是一个简单的例子: ```java public class Complex { private double realPart; // 实部 private double imaginaryPart; // 虚部 // 构造函数 public Complex(double real, double imag) { this.realPart = real; this.imaginaryPart = imag; } // 获取实部和虚部的方法 public double getReal() { return realPart; } public double getImaginary() { return imaginaryPart; } // 加法方法 public Complex add(Complex other) { double real = this.realPart + other.realPart; double imag = this.imaginaryPart + other.imaginaryPart; return new Complex(real, imag); } // 减法方法 public Complex subtract(Complex other) { double real = this.realPart - other.realPart; double imag = this.imaginaryPart - other.imaginaryPart; return new Complex(real, imag); } // 打印复数的方法 @Override public String toString() { if (imaginaryPart == 0) { return Double.toString(realPart) + " (实数)"; } else { return Double.toString(realPart) + " + " + imaginaryPart + "i"; } } public static void main(String[] args) { Complex c1 = new Complex(3, 4); Complex c2 = new Complex(1, -2); Complex sum = c1.add(c2); Complex diff = c1.subtract(c2); System.out.println("c1: " + c1); System.out.println("c2: " + c2); System.out.println("c1 + c2: " + sum); System.out.println("c1 - c2: " + diff); } } ``` 在这个例子中,我们定义了复数的属性、构造函数以及基本的算术操作。`add`和`subtract`方法分别计算两个复数的和与差,然后返回新的`Complex`对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值