第九周【项目1】在第8周项1的基础上(1)再定义一目运算符 -,-c相当于0-c(2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然
2014-04-22 14:54
936 查看
/* * 程序的版权和版本声明部分 * Copyright (c)2012, 烟台大学计算机学院学生 * All rightsreserved. * 作者: 初瑞 * 完成日期: 2013年 4 月 26 日 * 版本号: v1.0 * 输入描述:无 * 问题描述:无 * 程序输出:无 */ #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 &c); Complex operator-(Complex &c); Complex operator*(Complex &c); Complex operator/(Complex &c); friend Complex operator+(double &d,Complex &c);//实数加复数必须用友原函数 Complex operator+(double &d);//复数加实数一般函数 friend Complex operator-(double &d,Complex &c);//实数加复数 Complex operator-(double &d);//复数加实数 friend Complex operator*(double &d,Complex &c);//实数加复数 Complex operator*(double &d);//复数加实数 friend Complex operator/(double &d,Complex &c);//实数加复数 Complex operator/(double &d);//复数加实数 friend Complex operator-(Complex &c);//相反数相当于(0-c),即实数减复数,所以用友原函数 friend istream&operator>>(istream&,Complex&); friend ostream&operator<<(ostream&,Complex&); private: double real; double imag; }; Complex Complex::operator+(Complex &c) { return Complex(real+c.real,imag+c.imag); } Complex Complex::operator-(Complex &c) { return Complex(real-c.real,imag-c.imag); } Complex Complex::operator*(Complex &c) { return Complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real); } Complex Complex::operator/(Complex &c) { double d; d=c.real*c.real+c.imag*c.imag; return Complex((real*c.real+imag*c.imag)/d,(imag*c.real-real*c.imag)/d); } Complex operator+(double &d,Complex &c)//实数加复数,友原函数 { return Complex(d+c.real,c.imag); } Complex Complex::operator+(double &d)//复数加实数,一般函数 { return Complex(real+d,imag); } Complex operator-(double &d,Complex &c)//实数加复数,友原函数 { return Complex(d-c.real,c.imag); } Complex Complex::operator-(double &d)//复数加实数,一般函数 { return Complex(real-d,imag); } Complex operator*(double &d,Complex &c)//实数加复数,友原函数 { return Complex(d*c.real,d*c.imag); } Complex Complex::operator*(double &d)//复数加实数,一般函数 { return Complex(real*d,imag*d); } Complex operator/(double &d,Complex &c)//实数加复数,友原函数 { return Complex(d/c.real,d/c.imag); } Complex Complex::operator/(double &d)//复数加实数,一般函数 { return Complex(real/d,imag/d); } Complex operator-(Complex &c)//相反数,友原函数 { return Complex(-c.real,-c.imag); } istream&operator>>(istream&input,Complex &c)//知道 { cout<<"请输入复数的实部和虚部:"; input>>c.real>>c.imag; return input; } ostream&operator<<(ostream &output,Complex &c)//知道 { output<<"("<<c.real; if(c.imag>=0) output<<"+"; output<<c.imag<<"i)"<<endl; return output; } int main() { double d=9; Complex c1,c2,c3; cin>>c1; cout<<"c1="; cout<<c1; cin>>c2; cout<<"c2="; cout<<c2; c3=c1+c2; cout<<"c1+c2="; cout<<c3; c3=c1-c2; cout<<"c1-c2="; cout<<c3; c3=c1*c2; cout<<"c1*c2="; cout<<c3; c3=c1/c2; cout<<"c1/c2="; cout<<c3; c3=d+c1; cout<<"d+c1="; cout<<c3; c3=c1+d; cout<<"c1+d="; cout<<c3; c3=d-c1; cout<<"d-c1="; cout<<c3; c3=c1-d; cout<<"c1-d="; cout<<c3; c3=d*c1; cout<<"d*c1="; cout<<c3; c3=c1*d; cout<<"c1*d="; cout<<c3; c3=d/c1; cout<<"d/c1="; cout<<c3; c3=c1/d; cout<<"c1/d="; cout<<c3; c3=-c1; cout<<"-c1="; cout<<c3; return 0; }
运算结果:
心得体会:对定义Complex类中的<<和>>运算符的重载还是不太熟悉啊,怎么觉得特别别扭啊。
相关文章推荐
- 第九周实验报告 任务2 定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 《第九周任务一》定义Complex类中的《《和》》运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然
- 《第九周任务二》定义Time类中的<<和>>运算符的重载,实现时间输入输出,改造源程序中对运算结果的显示方式,使程序读起来更自然
- 第九周实验任务二--定义Complex类中的<<和>>运算符的重载,实现输入和输出,使程序读起来更自然
- 第九周实验报告任务 1 定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 《C++第九周实验报告1-1》--------接第8周任务1,定义Complex类中的<<和>>运算符的重载,实现输入和输出
- 第九周实验报告任务3 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 《第九周任务三》定义分数类中《和》运算符重载,实现分数的输入输出,改造源程序中对运算结果显示方式,使程序读起来更自然
- 《第九周任务三》定义分数类中《和》运算符重载,实现分数的输入输出,改造源程序中对运算结果显示方式,使程序读起来更自然
- 第九周试验任务1--定义Time类中的<<和>>运算符的重载,实现输入和输出。
- 定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
- 《C++第九周实验报告2-1》---接第8周任务2,定义Time类中的<<和>>运算符重载,实现时间的输入输出
- C++第九周【任务一】定义Complex类中的<<和>>运算符的重载,实现输入和输出
- 第九周任务一(定义Complex类中的<<和>>运算符的重载,实现输入和输出)
- 第九周 C++任务一。定义Complex类中的<<和>>运算符的重载,实现输入和输出
- 第九周任务三--定义分数类中<<和>>运算符重载,实现分数的输入输出,使程序读起来更自然。
- 第九周C++上机报告(定义Complex类中的<<和>>运算符的重载)
- 第九周 项目一:定义复数类的<<和>>运算符的重载