oj(一)s
来源:互联网 发布:linux增加空间 编辑:程序博客网 时间:2024/05/22 03:38
//定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,//使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编写程序,分别求两个复数之和、差、积和商。#include <iostream>#include <iomanip>using namespace std;class Complex{public: Complex(); Complex(double r,double i); Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display();private: double real; double imag;};/* C++代码 */Complex::Complex(){}Complex::Complex(double r,double i){ real=r; imag=i;}Complex Complex::operator+(Complex &c2){ return Complex(this->real+c2.real,this->imag+c2.imag);}Complex Complex::operator-(Complex &c2){ return Complex(this->real-c2.real,this->imag-c2.imag);}Complex Complex::operator*(Complex &c2){ return Complex(this->real*c2.real-this->imag*c2.imag,this->imag*c2.real+this->real*c2.imag);}Complex Complex::operator/(Complex &c2){ return Complex((this->real*c2.real+this->imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(this->imag*c2.real-this->real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));}void Complex::display(){ cout<<"("<<real<<","<<imag<<"i)"<<endl;}//****************int main(){ double real,imag; cin>>real>>imag; Complex c1(real,imag); cin>>real>>imag; Complex c2(real,imag); cout<<setiosflags(ios::fixed); cout<<setprecision(2); Complex 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;}
结果:
0 0
- oj(一)s
- oj(一)r
- oj(一)t
- oj(一)u
- oj(一)v
- oj(一)w
- oj(一)x
- oj(一)y
- OJ (一)B
- OJ专题(一)
- 华为OJ(一)
- 二哥OJ(一)
- 取石子(一)-OJ
- OJ---Peter's Smoke
- OJ 1.S
- Eva's Balance-OJ
- 南阳oj S + T
- Struts2中的标签介绍(一) s:property s:set s:bean s:push s:date
- 冒泡排序
- IT公司中最流行的10种编程语言
- 使用java反射机制遍历实体类的属性和类型以及值
- object-c中NSString与int和float的相互转换
- 重新发现Oracle太美之awr相关脚本简单介绍
- oj(一)s
- 集合的嵌套
- Java中jdbc的使用
- leetcode:Edit Distance 编辑距离
- CentOS6.4安装Oracle11g
- oj(一)t
- Swift基本语法
- DWZ富客户端框架(jQuery RIA framework), 是中国人自己开发的基于jQuery实现的Ajax RIA开源框架.
- oj(一)u