Complex Again

来源:互联网 发布:如何上传淘宝宝贝图片 编辑:程序博客网 时间:2024/05/29 18:23

Complex Again

标签(空格分隔): 程序设计实验 c++

本人学院

  • Complex Again
    • 标签空格分隔 程序设计实验 c
    • 本人学院
    • description
    • file provided
    • understanding
    • my answer
    • the standard answer
    • rethinking


description

I know you have learned operator overloading. Your task is to finish this easy class Complex according to the function main.

class Complex {

  //A friend function to print Complex numbers like a+bi where a is the real part and b is the imaginary part

public:
Complex(double = 0.0, double = 0.0);

//Some function about operator overloading.

void SetReal(double re){real = re;}
void SetImag(double im){imag = im;}
private:
double real;
double imag;
};

tip:

You should first enter the real part and then the imaginary part.

That is , cin >> x1 >> y1; Complex C(x1,y1); x1 is the real part and y1 is the imaginary part.

Sample Input

4 6 1 1

2 3

Sample Output

2+3i
4+6i
1+1i
5+7i
3+5i
-2+10i
5+1i
5+7i
4+6i
-2+10i
4+6i
1
0

file provided

main.vpp

#include <iostream>#include "Complex.h"using namespace std;int main(int argc, const char *argv[]) {    double x1, y1, x2, y2;    cin >> x1 >> y1 >> x2 >> y2;    // x1 and x2 are the real part, y1 and y2 are the imaginary part    Complex a(x1, y1), b(x2, y2);    Complex c = a;    double c1, c2;    // c1 is the real part and c2 is the imaginary part    cin >> c1 >> c2;    c.SetReal(c1);    c.SetImag(c2);    cout << c << endl;    cout << a << endl;    c = b;    cout << c << endl;    // a and b are not changed.    cout << (a + b) << endl;    cout << (a - b) << endl;    cout << (a * b) << endl;    cout << (a / b) << endl;    a += b;    cout << a << endl;    a -= b;    cout << a << endl;    a *= b;    cout << a << endl;    a /= b;    cout << a << endl;    cout << (a == a) << endl;    cout << (a != a) << endl;    return 0;}

understanding

my answer

Complex.h

#ifndef complex_h#define complex_h#include<iostream>using namespace std;class Complex {    friend ostream& operator<<(ostream& cout, const Complex& a);    public:      Complex(double = 0.0, double = 0.0);      Complex operator+(const Complex &b);      Complex operator-(const Complex &b);      Complex operator*(const Complex &b);      Complex operator/(const Complex &b);      Complex operator+=(const Complex &b);      Complex operator-=(const Complex &b);      Complex operator*=(const Complex &b);      Complex operator/=(const Complex &b);      bool operator==(const Complex &b);      bool operator!=(const Complex &b);      void SetReal(double re) {real = re;}      void SetImag(double im) {imag = im;}    private:      double real;      double imag;};#endif

Complex.cpp

#include<iostream>#include"Complex.h"using namespace std;ostream& operator<<(ostream& cout, const Complex& a) {    if (a.real == 0) {        if (a.imag == 0) {            cout << "0";            return cout;        } else {            cout << a.imag << "i";        }    } else {        cout << a.real << "+" << a.imag << "i";    }    return cout;}Complex::Complex(double x, double y) {    real = x;    imag = y;}Complex Complex::operator+(const Complex &b) {    Complex result;    result.real = real + b.real;    result.imag = imag + b.imag;    return result;}Complex Complex::operator-(const Complex &b) {    Complex result;    result.real = real - b.real;    result.imag = imag - b.imag;    return result;}Complex Complex::operator*(const Complex &b) {    Complex result;    result.real = real * b.real - imag * b.imag;    result.imag = real * b.imag + imag * b.real;    return result;}Complex Complex::operator/(const Complex &b) {    Complex result;    double divi = b.real * b.real + b.imag * b.imag;    result.real = (real * b.real + imag * b.imag) / divi;    result.imag = (imag * b.real - real * b.imag) / divi;    return result;}Complex Complex::operator+=(const Complex &b) {    *this = *this + b;    return *this;}Complex Complex::operator-=(const Complex &b) {    *this = *this - b;    return *this;}Complex Complex::operator*=(const Complex &b) {    *this = *this * b;    return *this;}Complex Complex::operator/=(const Complex &b) {    *this = *this / b;    return *this;}bool Complex::operator==(const Complex &b) {    if (real == b. real && imag == b.imag) return true;    return false;}bool Complex::operator!=(const Complex &b) {    if (*this == b) return false;    return true;}

the standard answer

Complex.h

#ifndef COMPLEX_H#define COMPLEX_H#include <iostream>using namespace std;class Complex {  friend ostream& operator <<(ostream&, const Complex&); public:  Complex(double = 0, double = 0);  Complex(const Complex& otherComplex);  Complex& operator = (const Complex& otherComplex);  Complex& operator += (const Complex& otherComplex);  Complex& operator -= (const Complex& otherComplex);  Complex& operator *= (const Complex& otherComplex);  Complex& operator /= (const Complex& otherComplex);  Complex operator +(const Complex&);  Complex operator -(const Complex&);  Complex operator *(const Complex&);  Complex operator /(const Complex&);  bool operator == (const Complex& otherComplex);  bool operator != (const Complex& otherComplex);  void SetReal(double re) {    real = re;  }  void SetImag(double im) {    imag = im;  } private:  double real;  double imag;};#endif

Complex.cpp

#include <iostream>#include "Complex.h"using namespace std;Complex::Complex(double real, double imag) {  this->real = real;  this->imag = imag;}Complex::Complex(const Complex& otherComplex) {  real = otherComplex.real;  imag = otherComplex.imag;}ostream& operator <<(ostream& out, const Complex& c) {  out << c.real << "+" << c.imag << "i";  return out;}Complex Complex:: operator +(const Complex& c) {  double tmpreal = real + c.real;  double tmpimag = imag + c.imag;  Complex tmp(tmpreal, tmpimag);  return tmp;}Complex Complex:: operator -(const Complex& c) {  double tmpreal = real - c.real;  double tmpimag = imag - c.imag;  Complex tmp(tmpreal, tmpimag);  return tmp;}Complex Complex:: operator *(const Complex& c) {  double tmpreal = real * c.real - imag * c.imag;  double tmpimag = imag * c.real + real * c.imag;  Complex tmp(tmpreal, tmpimag);  return tmp;}Complex Complex:: operator /(const Complex& c) {  double t = c.real * c.real + c.imag * c.imag;  double tmpreal = (real * c.real + imag * c.imag) / t;  double tmpimag = (imag * c.real - real * c.imag) / t;  Complex tmp(tmpreal, tmpimag);  return tmp;}Complex& Complex:: operator = (const Complex& otherComplex) {  real = otherComplex.real;  imag = otherComplex.imag;  return *this;}Complex& Complex:: operator += (const Complex& otherComplex) {  *this = *this + otherComplex;  return *this;}Complex& Complex:: operator -= (const Complex& otherComplex) {  *this = *this - otherComplex;  return *this;}Complex& Complex:: operator *= (const Complex& otherComplex) {  *this = *this * otherComplex;  return *this;}// a / b, b != 0Complex& Complex:: operator /= (const Complex& otherComplex) {  *this = *this / otherComplex;  return *this;}bool Complex:: operator == (const Complex& otherComplex) {  if (real == otherComplex.real && imag == otherComplex.imag)    return true;  else    return false;}bool Complex:: operator != (const Complex& otherComplex) {  return !(*this == otherComplex);}

rethinking

按照一般人的理解.我觉得复数如果复部小于零,应该表示为例如:2-3i;
而不是标答输出的2+-3i
下面是我觉得格式更正确的代码.

#include<iostream>#include"Complex.h"using namespace std;ostream& operator<<(ostream& cout, const Complex& a) {    if (a.real == 0) {        if (a.imag == 0) {            cout << "0";            return cout;        } else {            cout << a.imag << "i";        }    } else {        cout << a.real;        if (a.imag > 0) cout << "+";        if (a.imag < 0) cout << a.imag << "i";    }    return cout;}Complex::Complex(double x, double y) {    real = x;    imag = y;}Complex Complex::operator+(const Complex &b) {    Complex result;    result.real = real + b.real;    result.imag = imag + b.imag;    return result;}Complex Complex::operator-(const Complex &b) {    Complex result;    result.real = real - b.real;    result.imag = imag - b.imag;    return result;}Complex Complex::operator*(const Complex &b) {    Complex result;    result.real = real * b.real - imag * b.imag;    result.imag = real * b.imag + imag * b.real;    return result;}Complex Complex::operator/(const Complex &b) {    Complex result;    double divi = b.real * b.real + b.imag * b.imag;    result.real = (real * b.real + imag * b.imag) / divi;    result.imag = (imag * b.real - real * b.imag) / divi;    return result;}Complex Complex::operator+=(const Complex &b) {    *this = *this + b;    return *this;}Complex Complex::operator-=(const Complex &b) {    *this = *this - b;    return *this;}Complex Complex::operator*=(const Complex &b) {    *this = *this * b;    return *this;}Complex Complex::operator/=(const Complex &b) {    *this = *this / b;    return *this;}bool Complex::operator==(const Complex &b) {    if (real == b. real && imag == b.imag) return true;    return false;}bool Complex::operator!=(const Complex &b) {    if (*this == b) return false;    return true;}}

2.在重载操作符时,遇到一个错误:must take either zero or one argument

因为我在参数列表中传进两个参数了.

网络回答:
用成员方式重载运算符, 不能改变参数的个数
二元运算符用成员重载时, 只需要一个参数, 另一个参数由this指针传入
所以, 像
rmb::operator+(rmb&, rmb&)
都要改成
rmb::operator+(rmb&)
第一个参数由this指针自动传入到函数中去的.

3.c++ 重载 >>(输入) 、<< (输出) 操作符 1
输入输出流重载与一般操作符略有区别.
重载输出操作符 <<

为了与标准库IO操作一致,重载 << 操作符函数应把ostream&作为其第一个参数,对类类型const对象的引用作为第二个参数,并返回对ostream形参的引用。

ostream& operator<<(ostream& cout, const Sales_item& s){    cout << s.isbn << “\t” << s.units_sold << “\t”        << s.revenue << “\t” << s.avg_price();    return cout;}

一般而言,“<<”应输出对象的内容,进行最小限度的格式化,尤其不应输出换行符,而让用户自己控制输出细节。

“<<”操作符应定义为非成员函数。因为对于类的成员函数,左操作数为该类类型的对象,这样只能将第二个参数作为ostream&,使用时:

Sales_item item;
item << cout;
这与“<<”的正常使用习惯相反。所以“<<”操作符应定义为非成员函数,并将其作为所操作类的友元。

重载输入操作符 >>

与输出操作符类似,输入操作符函数的第一个形参为流的引用,第二个形参为类类型对象的引用(非const,因为 >> 的目的就是将数据读入到该对象中)。

istream& operator>>(istream& cin, Sales_item& s){    double price;    cin >> s.isbn >> s.units_sold >> price;    // check that the inputs succeeded    if (cin)        s.revenue = s.units_sold * price;    else        s = Sales_item(); // input failed: reset object to default state    return cin;}

输入操作符要检测和处理在读入数据时可能发生的错误。如果输入失败,则确保对象处于可用和一致的状态,比如将形参恢复为空的Sales_item对象。除此之外,还可以设置流的条件状态。用户如果想知道输入是否成功,可以测试流。
4. += 这类重载,是要改变左操作数,即this对象的.
5. 而一般的 + - 等不改变操作数,返回一个新的对象.
6. 注意友元函数关键字friend只需要在类的声明里出现. 而且友元函数不是类的成员函数,在实现的时候不需要加作用域符
7.重载 =, += 之类的运算符,返回类型Complex& 用别名. 修改*this,返回*this
/若函数的返回值类型为Complex,则此语句会引起 对拷贝构造函数的调用;若函数的返回值类型为 Complex&,则不调用拷贝构造函数/
8.重载操作符, 参数列表加 const!!!!


  1. http://www.cnblogs.com/zhuyf87/archive/2013/02/25/2932441.html ↩
0 0
原创粉丝点击