OJ-练习-复数运算符重载
来源:互联网 发布:淘宝直通车推广要求 编辑:程序博客网 时间:2024/05/29 10:35
/* * Copyright (c) 2013, 烟台大学计算机学院* All rights reserved.* 作 者: 霍雨佳* 完成日期:2014 年5月20日* 版 本 号:v1.0* 问题描述:求两坐标间的距离* 样例输入:* 样例输出:* 问题分析:定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。*(1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数)。*(2)输出的算数,在复数两端加上括号,实部和虚部均保留两位小数,如(8.23+2.00i)、(7.45-3.40i)、(-3.25+4.13i)等。* 编写程序,分别求两个复数之和、整数和复数之和,并且输出*/#include <iostream>#include <iomanip>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+(double &);friend Complex operator+(double&,Complex &);friend ostream& operator << (ostream& output, const Complex& c);private:double real;double imag;};//将程序需要的其他成份写在下面,只提交begin到end部分的代码//******************** begin ********************Complex Complex::operator+(Complex &c2){ return Complex(c2.real+real,c2.imag+imag);}Complex Complex::operator+(double &c){ return Complex(c+real,imag);}Complex operator+(double&c,Complex &c1){ return Complex(c+c1.real,c1.imag);}ostream& operator << (ostream& output, const Complex& c){ cout<<setiosflags(ios::fixed)<<setprecision(2); output<<"("<<c.real; if(c.imag>=0)output<<"+"; output<<c.imag<<"i)"<<endl; return output;}//********************* end ********************int main(){//测试复数加复数double real,imag;cin>>real>>imag;Complex c1(real,imag);cin>>real>>imag;Complex c2(real,imag);Complex c3=c1+c2;cout<<"c1+c2=";cout<<c3;//测试复数加实数double d;cin>>real>>imag;cin>>d;c3=Complex(real,imag)+d;cout<<"c1+d=";cout<<c3;//测试实数加复数cin>>d;cin>>real>>imag;c1=Complex(real,imag);c3=d+c1;cout<<"d+c1=";cout<<c3;return 0;}
运行结果:
0 0
- OJ-练习-复数运算符重载
- 16 oj 复数类 重载运算符+
- 【OJ】---R---复数类--重载运算符+
- YTU-OJ- 复数类--重载运算符+
- YTU-OJ- 复数类--重载运算符+,-,*,/
- OJ --复数类 重载运算符+
- OJ-复数类-重载运算符+-*/
- OJ-复数类-重载运算符2
- OJ-复数类-重载运算符3+
- OJ复数类--重载运算符+
- oj--复数类--重载运算符3+
- 第十六周( OJ 练习一 3) ——复数运算符的重载。
- 16 oj 复数类 重载运算符3+
- 【OJ】---C---复数运算符重载(+与<<)
- △【OJ】---S---复数类,重载运算符+,-,*,/
- 【OJ】---T---复数类,重载运算符2+
- YTU-OJ- 复数类--重载运算符2+
- YTU-OJ-复数类--重载运算符3+
- 2014年4月操作系统份额:Windows XP仍然在26%以上 Linux微涨
- windows下安装phpunit进行单元测试
- 统计字符串中汉字的个数
- Nginx担当WebSockets代理
- java.lang.OutOfMemoryError: PermGen space解决办法
- OJ-练习-复数运算符重载
- C# Dictionary 的几种遍历方法
- 管理人员的组织和资源的使用
- trsjhnfgs
- Sql Server profiler 分析器
- Hive创建TABLE_PARAMS表失败
- Linux 2.6 中的文件锁
- 女大学生种蔬菜创业故事
- 翻译03