Complex

The complex.h is the class header describing a complex number class.

Please write the corresponding implementation file complex.cpp. main.cpp will test it. If we give 2.1+0.2i / 0.2-0.3i, the output should be 2.76923 + 5.15385i .
.h文件
#include<string>
#include<iostream>
class Complex {
  public:
    Complex();
    Complex(double re, double im);
    ~Complex();
    const double getReal(void) const;
    const double getImaginary(void) const;
    void setReal(double re);
    void setImaginary(double im);
    void fromString(const std::string& complexString);
    Complex operator+(const Complex& theComplex);
    Complex operator-(const Complex& theComplex);
    Complex operator*(const Complex& theComplex);
    Complex operator/(const Complex& theComplex);
  private:
    double real;
    double imag;
};
std::ostream& operator<<(std::ostream& out, Complex& theComplex);
main函数:
#include <iostream>
#include <string>
#include "complex.h"
using namespace std;

int main() {
    string complexString1;
    char op;
    string complexString2;
    cin >> complexString1 >> op >> complexString2;

    Complex complex1;
    complex1.fromString(complexString1);
    Complex complex2;
    complex2.fromString(complexString2);
    Complex complex3;

    if (op == '+') {
        complex3 = complex1 + complex2;
    } else if (op == '-') {
        complex3 = complex1 - complex2;
    } else if (op == '*') {
        complex3 = complex1 * complex2;
    } else if (op == '/') {
        complex3 = complex1 / complex2;
    }
    cout << complex1 << " " << op
    << " (" << complex2 << ") = " << complex3 << endl;
    return 0;
}


解答:
using namespace std;
#include"complex.h"
#include<string>
#include<sstream>
Complex::Complex()
{

}
Complex::Complex(double re, double im):real(re),imag(im){}
Complex::~Complex()
{

}
const double Complex::getReal(void) const
{
    return real;
}
const double Complex::getImaginary(void) const
{
    return imag;
}
void Complex::setReal(double re)
{
    real=re;
}
void Complex::setImaginary(double im)
{
    imag=im;
}
void Complex::fromString(const std::string& complexString)
{

    int i=0;
    while(complexString[i]!='+'&&complexString[i]!='-')
        ++i;

    string sub1,sub2;
    stringstream ss1,ss2;
    sub1=complexString.substr(0,i);
    sub2=complexString.substr(i+1,complexString.length()-i-2);
    ss1<<sub1;
    ss1>>real;

    ss2<<sub2;
    ss2>>imag;
       if(complexString[i]=='-')
           imag=-imag;

}
Complex Complex:: operator+(const Complex& theComplex)
{
    Complex temp;
    temp.real=this->real+theComplex.real;
    temp.imag=this->imag+theComplex.imag;
    return temp;
}
Complex Complex:: operator-(const Complex& theComplex)
{
    Complex temp;
    temp.real=this->real-theComplex.real;
    temp.imag=this->imag-theComplex.imag;
    return temp;
}
Complex Complex:: operator*(const Complex& theComplex)
{
    Complex temp;
    temp.real=real*theComplex.real-imag*theComplex.imag;
    temp.imag=real*theComplex.imag+imag*theComplex.real;
    return temp;
}
Complex Complex:: operator/(const Complex& theComplex)
{
    Complex temp1(theComplex.getReal(),-theComplex.getImaginary());
    Complex temp2=(*this)*temp1;
    double k=theComplex.getReal()*theComplex.getReal()+theComplex.getImaginary()*theComplex.getImaginary();
    temp2.real/=k;
    temp2.imag/=k;
    return temp2;

}

std::ostream &operator<<(ostream &stream,Complex& theComplex)
{
    stream << theComplex.getReal();
    if(theComplex.getImaginary()>0)
    {
        stream << " "<<'+'<<" ";
        if(theComplex.getImaginary()==1)
            stream<<'i';
        else
            stream<<theComplex.getImaginary()<<'i';
    }
    else
    {
        stream << " "<<'-'<<" ";
        if(theComplex.getImaginary()==-1)
            stream<<'i';
        else
            stream<<-theComplex.getImaginary()<<'i';
    }



    return stream;
}

心得:
(1)我有三次提交都没能过编译的原因是因为在.cpp文件中忘了加上了.h文件
(2)

void Complex::fromString(const std::string& complexString)
{

    int i=0;
    while(complexString[i]!='+'&&complexString[i]!='-')
        ++i;

    string sub1,sub2;
    stringstream ss1,ss2;
    sub1=complexString.substr(0,i);
    sub2=complexString.substr(i+1,complexString.length()-i-2);
    ss1<<sub1;//输入
    ss1>>real;//输出

    ss2<<sub2;
    ss2>>imag;
       if(complexString[i]=='-')
           imag=-imag;

}

这个函数运用的十分好,直接将string类型的转换成了int的了,用输入流。
(3)

std::ostream &operator<<(ostream &stream,Complex& theComplex)
{
    stream << theComplex.getReal();
    if(theComplex.getImaginary()>0)
    {
        stream << " "<<'+'<<" ";
        if(theComplex.getImaginary()==1)
            stream<<'i';
        else
            stream<<theComplex.getImaginary()<<'i';
    }
    else
    {
        stream << " "<<'-'<<" ";
        if(theComplex.getImaginary()==-1)
            stream<<'i';
        else
            stream<<-theComplex.getImaginary()<<'i';
    }



    return stream;
}

猜你喜欢

转载自blog.csdn.net/Liusyu6688/article/details/80638775