第8周-项目1-复数类中的运算符重载(续)-++、--、>>、<<

问题及代码:

/*   
*Copyright (c)2015,烟台大学计算机与控制工程学院   
*All rights reserved.   
*文件名称:Complex.cpp   
*作    者:单昕昕   
*完成日期:2015年5月5日   
*版 本 号:v1.0   
*问题描述:在复数类中的运算符重载基础上
(1)再定义一目运算符 -,-c相当于0-c。
(2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
*程序输入:两个复数。
*程序输出:复数运算结果。
*/ 
//Sample Input
//3+4i 5-10i

/*
对于两个复数(a+bi)和(c+di)的运算结果
执行加法运算:(a+c),(b+d)i
执行减法运算:(a-c),(b-d)i
执行乘法运算:(a*c-b*d),(c*b+a*d)i
执行除法运算:(a*c+b*d)/(c*c+d*d),(a*d+b*c)/(c*c+d*d)
*/
#include<iostream>
using namespace std;
class Complex
{
public:
    Complex()
    {
        real=0;
        imag=0;
    }
    Complex(double r,double i)
    {
        real=r;
        imag=i;
    }
    Complex operator+(const Complex &c2);
    Complex operator-(const Complex &c2);
    Complex operator*(const Complex &c2);
    Complex operator/(const Complex &c2);
    Complex operator-();
    friend istream& operator>>(istream&input,Complex &c);
    friend ostream& operator<<(ostream&output,const Complex &c);
private:
    double real;
    double imag;
};
//下面定义成员函数
Complex Complex:: operator+(const Complex &c2)
{
    return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex:: operator-(const Complex &c2)
{
    return Complex(real-c2.real,imag-c2.imag);
}
Complex Complex:: operator*(const Complex &c2)
{
    return Complex(real*c2.real-imag*c2.imag,real*c2.imag+imag*c2.real);
}
Complex Complex:: operator/(const Complex &c2)
{
    return Complex((real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),

(real*c2.imag+imag*c2.real)/(-c2.real*c2.real+c2.imag*c2.imag));
}
Complex Complex:: operator-()
{
    return Complex(-real,-imag);
}
//流插入
istream& operator>>(istream&input,Complex &c)
{
    int a,b;
    char sign,i;
    do
    {
        cout<<"please input a complex number(a+bi or a-bi):";
        input>>a>>sign>>b>>i;
    }
    while(!((sign=='+'||sign=='-')&&i=='i'));
    c.real=a;
    c.imag=(sign=='+')?b:-b;
    return input;
}
//流提取
ostream& operator<<(ostream&output,const Complex &c)
{
    output<<"("<<c.real;
    if(c.imag>=0)
        output<<"+";
    output<<c.imag<<"i)";
    return output;
}
//下面定义用于测试的main()函数
int main()
{
    Complex c1,c2,c3;
    cin>>c1>>c2;
    cout<<"c1="<<c1<<endl;
    cout<<"c2="<<c2<<endl;
    c3=-c1;
    cout<<"-c1="<<c3<<endl;
    c3=c1+c2;
    cout<<"c1+c2="<<c3<<endl;
    c3=c1-c2;
    cout<<"c1-c2="<<c3<<endl;
    c3=c1*c2;
    cout<<"c1*c2="<<c3<<endl;
    c3=c1/c2;
    cout<<"c1/c2="<<c3<<endl;
    return 0;
}


运行结果:



知识点总结:
重载单目运算符。

学习心得:

流插入和流提取真的超级好用~~省去了以前总是要定义各种类型的变量来满足需要的麻烦~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
import java.io.*; public class Book{ double sb; double xb; Book(double x,double y){ this.sb=x; this.xb=y; } Book(){ } public static void main(String args[]){ System.out.println("请输入数据:"); double a=0; double b=0; double c=0; double d=0; String s; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入第一个复数的实部:"); try{ s = in.readLine(); a=Double.parseDouble(s); } catch(IOException e) { System.out.println("抛掷异常");} System.out.println("请输入第一个复述的虚部:"); try{ s = in.readLine(); b =Double.parseDouble(s); } catch(IOException e) { System.out.println("抛掷异常");} System.out.println("请输入第二个复述的实部:"); try{ s = in.readLine(); c =Double.parseDouble(s); } catch(IOException e) { System.out.println("抛掷异常");} System.out.println("请输入第二个复述的虚部:"); try{ s = in.readLine(); d =Double.parseDouble(s); } catch(IOException e) { System.out.println("抛掷异常");} Book h; h=new Book(a,b); Book j; j=new Book(c,d); System.out.println("您输入的一个数为:"); toString(h); System.out.println("您输入的二个数为:"); toString(j); Book k; k=new Book(); char z='y'; do{ System.out.println("请选择您要进行的计算:"); System.out.println("1 :进行加法运算"); System.out.println("2 :进行减法运算"); System.out.println("3 :进行修改"); System.out.println("4 :进行乘法运算"); System.out.println("5 :进行除法运算"); System.out.println("6 :查看修改结果"); int i=0; try{ i= Integer.parseInt(in.readLine()); } catch(IOException e) { System.out.println("抛掷异常");} switch(i) { case 1: k.sb=jia(h.sb,j.sb); k.xb=jia(h.xb,j.xb); System.out.println("计算结果的实部为:"+k.sb); System.out.println("计算结果的虚部为:"+k.xb); toString(k); break ; case 2: k.sb=jian(h.sb,j.sb); k.xb=jian(h.xb,j.xb); System.out.println("计算结果的实部为:"+k.sb); System.out.println("计算结果的虚部为:"+k.xb); toString(k); brea

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值