<数据结构>复数四则运算
2016-12-28 15:51
351 查看
#include <iostream> using namespace std; //之所以用类模板 是因为输入不知道类型 template<class Elem>class Complex{ private: Elem reality; Elem falsehood; public: Complex(Elem r, Elem f){ reality = r; falsehood = f; } ~Complex(){ } Complex operator + (const Complex& c1){ return Complex(reality + c1.reality, falsehood + c1.falsehood); } Complex operator - (const Complex& c1){ return Complex(reality - c1.reality, falsehood + c1.falsehood); } Complex operator * (const Complex& c1){ return Complex(reality*c1.reality - falsehood*c1.falsehood, reality*c1.falsehood + falsehood*c1.reality); } Complex operator / (const Complex& c1){ if (falsehood != 0) return Complex((reality * c1.reality + falsehood + c1.falsehood) / (c1.reality*c1.reality + c1.falsehood * c1.falsehood), (falsehood * c1.reality - reality * c1.falsehood) / (c1.reality * c1.reality + c1.falsehood * c1.falsehood)); else cout << "分母不能为0,请重新输入" << endl; } Elem Reality(){ return reality; } Elem Falsehood(){ return falsehood; } void Conjugate(){ falsehood = -falsehood; } void Output(){ if (falsehood>0) { cout << "(" << reality << "+" << falsehood << "i" << ")" << endl; } else cout << "(" << reality << falsehood << "i" << ")" << endl; } }; int main() //测试 { float a, b, c, d; cin >> a >> b >> c >> d; Complex<float> c1(a, b); Complex<float> c2(c, d); cout << "测试结果:" << endl; cout << "两个复数分别为:(1+2i)和(2+3i)" << endl; cout << "(1+2i)+(2+3i)="; (c1 + c2).Output(); cout << "\n(1+2i)-(2+3i)="; (c1 - c2).Output(); cout << "\n(1+2i)*(2+3i)="; (c1*c2).Output(); cout << "\n(1+2i)/(2+3i)="; (c1 / c2).Output(); cout << "\nc1的实部是:" << c1.Reality() << endl; cout << "\nc1的虚部是:" << c1.Falsehood() << endl; cout << "\nc2的实部是:" << c2.Reality() << endl; cout << "\nc2的虚部是:" << c2.Falsehood() << endl; cout << "\nc1的共轭复数是:"; c1.Conjugate(); c1.Output(); cout << "\nc2的共轭复数是:"; c2.Conjugate(); c2.Output(); system("pause"); return 0; }
相关文章推荐
- <数据结构>线性表.顺序存储结构
- <数据结构>-树
- <数据结构>二叉树的实现
- <转>关于SparkMLlib的基础数据结构Spark-MLlib-Basics
- <数据结构>孩子兄弟表示法家谱
- <数据结构>栈-表达式求值
- <数据结构-队列>
- 数据结构___实验一 复数的四则运算
- <DataStructure_1>用栈实现四则运算(c语言)
- <perl> 复杂数据结构 (Reference & Dereference)
- <<数据结构第二章>>总结
- <数据结构 课程设计> 文件目录结构显示(Java 版)
- <数据结构>线性表.链式存储结构
- <C/C++基础> 设计一个复数类(拷贝函数的使用)
- 为什么Oracle的伪列rownum不支持>,>=,=,between...and,而都用<比较
- <ios>调用系统的短信和发送邮件功能,实现短信分享邮件分享
- <C 程序设计语言>__第 1 章 导言
- <转>Developing Custom Draw Controls in Visual C++
- asp.net的<% %>总结
- xml<></> 和</>的区别