第八周项目一

*/

* Copyright (c) 2011, 烟台大学计算机学院

* All rights reserved.

* 作 者: 孙培培

* 完成日期:2013 年 4月22日 

* 版 本 号:v1.0 *

* 输入描述:略

问题描述:

* 程序输出:见下

* 问题分析:
 算法设计:略

*/
//【项目1】实现复数类中的运算符重载
//定义一个复数类重载运算符+、-、*、/,使之能用于复数的加减乘除。
//(1)任务一:请用类的成员函数完成运算符的重载;
#include <iostream>
using namespace std;
class Complex
{
public:
	Complex(){real=0;imag=0;}
	Complex(double r,double i){real=r;imag=i;}
	Complex operator+(Complex &C2);
	Complex operator-(Complex &C2);
	Complex operator*(Complex &C2);
	Complex operator/(Complex &C2);
	void display();
private:
	double real;
	double imag;
};
//下面定义成员函数

Complex Complex::operator+(Complex &C2)
{
    Complex C;
    C.real=real+C2.real;
    C.imag=imag+C2.imag;
    return C;
}
Complex Complex::operator-(Complex &C2)
{
    Complex C;
    C.real=real-C2.real;
    C.imag=imag-C2.imag;
    return C;
}
Complex Complex::operator*(Complex &C2)
{
    Complex C;
    C.real=real*C2.real-imag*C2.imag;
    C.imag=imag*C2.real+real*C2.imag;
    return C;
}
Complex Complex::operator/(Complex &C2)
{
        Complex C;
    C.real=(real*C2.real+imag*C2.imag)/(C2.real*C2.real+C2.imag*C2.imag);
    C.imag=(imag*C2.real-real*C2.imag)/(C2.real*C2.real+C2.imag*C2.imag);
    return C;
}
void Complex::display()
{
    cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
  Complex C1(3,5),C2(-5,10),C3;
  cout<<"C1=";
  C1.display();
  cout<<"C2=";
  C2.display();
  C3=C1+C2;
  cout<<"C1+C2=";
  C3.display();
  C3=C1-C2;
  cout<<"C1-C2=";
  C3.display();
  C3=C1*C2;
  cout<<"C1*C2=";
  C3.display();
  C3=C1/C2;
  cout<<"C1/C2=";
  C3.display();
   return 0;
}
//BB 平台上已经提供了用于测试的main()函数


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值