第九周上机任务(一)
来源:互联网 发布:sip服务器端口 编辑:程序博客网 时间:2024/05/21 19:39
/** Copyright (c) 2012, 烟台大学计算机学院* All rights reserved.* 作 者:孙永* 完成日期:2013 年 05 月 08 日* 版 本 号:v1.0* 输入描述:* 问题描述:定义Complex类中的<<和>>运算符的重载,实现输入和输出, 改造原程序中对运算结果显示方式,使程序读起来更自然。* 程序输出:* 问题分析:* 算法设计:略*/#include <iostream>#include <Cstring>using namespace std;class Complex{public: Complex( ){real=0;imag=0;} //Complex(double r,double i){real=r;imag=i;} friend Complex operator+(Complex &c1,Complex &c2); friend Complex operator-(Complex &c1,Complex &c2); friend Complex operator*(Complex &c1,Complex &c2); friend Complex operator/(Complex &c1,Complex &c2); friend istream& operator>>(istream &cin,Complex&c); friend ostream& operator<<(ostream &cout,Complex&c);private: double real; double imag;};Complex operator+(Complex &c1,Complex &c2){ Complex c; c.real=c1.real+c2.real; c.imag=c1.imag+c2.imag; return c;}Complex operator-(Complex &c1,Complex &c2){ Complex c; c.real=c1.real-c2.real; c.imag=c1.imag-c2.imag; return c;}Complex operator*(Complex &c1,Complex &c2){ Complex c; c.real=c1.real*c2.real-c1.imag*c2.imag; c.imag=c1.imag*c2.real+c1.real*c2.imag; return c;}Complex operator/(Complex &c1,Complex &c2){ Complex c; c.real=(c1.real/c2.real+c1.imag/c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(c1.imag/c2.real-c1.real/c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c;}istream & operator>>(istream&cin,Complex&c){ cout<<"请输入复数的实部与虚部"<<endl; cin>>c.real>>c.imag; return cin;}ostream & operator<<(ostream &cout,Complex&c){ cout<<"("; cout<<c.real; cout<<","<<c.imag<<"i)"<<endl; return cout;}int main( ){ Complex c1,c2,c3; cin>>c1>>c2; cout<<c1<<c2<<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;}
运行结果:
- 第九周上机任务(一)
- 第九周上机任务
- 个人所得税计算器(第九周上机任务)
- 个人所得税计算器(第九周上机任务)
- 第九周上机任务(个人所得税)
- 第九周上机任务(二)
- 第九周上机任务一,计算分段函数的值.
- 第九周上机任务1
- 第九周上机任务2
- 第九周上机任务3
- 第九周上机任务4
- 第九周上机任务5
- 第九周任务(一)
- 第九周任务(一)
- 第九周任务(一)
- 第九周任务(一)
- 二次函数求解(第九周上机任务)
- 利息计算器(第九周上机任务3)。
- Ruby之Class
- CodeForces 73C LionAge II Dp
- visio生成pdf然后去掉白边距
- 一天的活动
- Flex4的Application中设置背景图像
- 第九周上机任务(一)
- linux计划任务
- storm版本
- Linux使用shell编程自动切换IP
- Chrome 获取屏幕位置问题
- JQuery插件实现相册效果(数据库调用图片)
- 字节大小端判断
- POJ 2195 Going Home
- linux下常用函数及命令