返回值是一个复数类的,有虚部和实部,

返回值是一个复数类的,有虚部和实部,代码的返回那块应该怎么写呢?

 public struct Complex
{
    public int real;
    public int imaginary;

    // Constructor.
    public Complex(int real, int imaginary)  
    {
        this.real = real;
        this.imaginary = imaginary;
    }

    // Specify which operator to overload (+), 
    // the types that can be added (two Complex objects),
    // and the return type (Complex).
    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
    }

    // Override the ToString() method to display a complex number 
    // in the traditional format:
    public override string ToString()
    {
        return (System.String.Format("{0} + {1}i", real, imaginary));
    }
}

class TestComplex
{
    static void Main()
    {
        Complex num1 = new Complex(2, 3);
        Complex num2 = new Complex(3, 4);

        // Add two Complex objects by using the overloaded + operator.
        Complex sum = num1 + num2;

        // Print the numbers and the sum by using the overridden 
        // ToString method.
        System.Console.WriteLine("First complex number:  {0}", num1);
        System.Console.WriteLine("Second complex number: {0}", num2);
        System.Console.WriteLine("The sum of the two numbers: {0}", sum);

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    First complex number:  2 + 3i
    Second complex number: 3 + 4i
    The sum of the two numbers: 5 + 7i
*/

返回一个数组不可以吗?

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#ifndef COMPLEX_H
#define COMPLEX_H
#include
#include
class Complex
{
public:
Complex(double _real,double _imag = 0.0):real(_real),imag(_imag){} //构造函数,初始化列表和默认参数
Complex(std::istream &is){is >> this;}; //输入构造函数,调用自身的>>操作符
void SetReal(double _real); //更改实部的值
void SetImag(double _imag); //更改虚部的值
void SetVal(double _real,double _imag); //更改整个复数
inline double GetReal() const; //获取实部,常成员函数
inline double GetImag() const; //获取虚部,常成员函数
Complex& operator+=(const Complex &val); //成员操作符+=
Complex& operator
=(const Complex &val); //成员操作符-=
friend bool operator==(const Complex &lhs,const Complex &rhs); //友元函数,需访问私有数据
friend std::istream& operator>>(std::istream &,Complex &); //友元输入操作符,需私有数据
friend std::ostream& operator<<(std::ostream &,const Complex &); //友元输出操作符,需私有数据
private:
double real;

double imag;
};

Complex operator+(const Complex &lhs, const Complex &rhs); //普通函数,实现两个复数+操作
Complex operator*(const Complex &lhs, const Complex &rhs); //普通函数,实现两个复数*操作

//========================分割线,此线上面为定义代码,此线下面是实现代码===============================
inline bool operator==(const Complex &lhs,const Complex &rhs)
{
return (lhs.real == rhs.real) && (lhs.imag == rhs.imag);
}

inline bool operator!=(const Complex &lhs,const Complex &rhs)
{
return !(lhs==rhs);
}

inline Complex& Complex::operator+=(const Complex &val)
{
real += val.real;
imag += val.imag;
return *this;
}

inline Complex operator+(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret += rhs;
return ret;
}

inline Complex& Complex::operator*=(const Complex &val)
{
double tReal = real;
double tImag = imag;
real = tReal*val.real - tImag*val.imag;
imag = tReal*val.imag + tImag*val.real;
return *this;
}

inline Complex operator*(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret *= rhs;
return ret;
}

inline std::istream& operator>>(std::istream &is,Complex &com)
{
std::cout << "请输入实数部分:" ;
is >> com.real;
if(is)
{
std::cout << "请输入虚数部分:" ;
is >> com.imag;
if(is)
{
return is;
}
else
{
com = Complex(0.0);
}
}
else
{
com = Complex(0.0);
}
return is;
}

inline std::ostream& operator<<(std::ostream &os, const Complex &com)
{
os << "复数为:" << com.real << std::showpos << com.imag << "i" << std::endl;
return os;
}

inline double Complex::GetReal() const
{
return real;
}

inline double Complex::GetImag() const
{
return imag;
}

void Complex::SetReal(double _real)
{
real = _real;
}

void Complex::SetImag(double _imag)
{
imag = _imag;
}

void Complex::SetVal(double _real,double _imag)
{
real = _real;
imag = _imag;
}
#endif

先定义一个理性 复数类 internal class complex{ int realpart;int virtualpart;} ,然后写个方法,方法返回的类型是complex不就行了

把实部跟虚部放到一个一维数组int[2]就行啦,用的时候从数组取出实部虚部就行了

可以建立一个无名临时对象返回,也可以直接给个名字返回,总之实质为返回一个对象。