6.27 洗刷刷 1 oj 运算符重载 复数之和 (3+4i)+(5-10i)
2015-06-27 13:56
369 查看
题目描述
定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。
输入
两个复数
输出
复数之和
样例输入
3 4
5 -10
样例输出
(8.00,-6.00i)
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
Complex();
Complex(double r,double i);
double get_real();
double get_imag();
void display();
private:
double real;
double imag;
};
Complex::Complex()
{
real=0;
imag=0;
}
Complex::Complex(double r,double i)
{
real=r;
imag=i;
}
double Complex::get_real()
{
return real;
}
double Complex::get_imag()
{
return imag;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
Complex operator+(Complex &c1,Complex &c2)
{
return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
}
int main()
{
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
Complex c3=c1+c2;
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
c3.display();
return 0;
}
定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。
输入
两个复数
输出
复数之和
样例输入
3 4
5 -10
样例输出
(8.00,-6.00i)
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
public:
Complex();
Complex(double r,double i);
double get_real();
double get_imag();
void display();
private:
double real;
double imag;
};
Complex::Complex()
{
real=0;
imag=0;
}
Complex::Complex(double r,double i)
{
real=r;
imag=i;
}
double Complex::get_real()
{
return real;
}
double Complex::get_imag()
{
return imag;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
Complex operator+(Complex &c1,Complex &c2)
{
return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
}
int main()
{
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
Complex c3=c1+c2;
cout<<setiosflags(ios::fixed);
cout<<setprecision(2);
c3.display();
return 0;
}
相关文章推荐
- The type 'Microsoft.Office.Interop.Excel.ApplicationClass' has no constructors defined
- Linux入门篇之四:VMware虚拟网络配置
- Redis命令学习-Pub/Sub(发布/订阅)
- ADF中改变table每页展示数据的行数
- HDU_2069 Coin Change(dfs)
- 微信支付成功后,notify_url 没收到通知的解决
- 日语学习之沪江N3基础 20150627 -6
- java学习之旅27--键盘输入_Scanner类的使用_import简单入门
- 黑马程序员——jdk1.5新特性
- libcurl:开源的http库学习
- myeclipse7.5注册码
- ios tableView那些事(四)tableView添加标题,头尾和改变cell的宽度
- JVM解读(五):JVM垃圾收集
- Java开发牛人十大必备网站
- 项目笔记:4.引入CDN内容分发网络,实现网站静态元素加速
- 编程面试过程中常见的10大算法
- 用Redis实现分布式锁
- 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
- windows下copy批处理命令
- 基于Struts2+Hibernate的学生信息管理系统实例