怎么实现复数矩阵的加减乘除,共轭转置,利用函数重载
2条回答 默认 最新
- CSDN专家-link 2021-07-10 23:23关注
#include <iostream> #include<cstdio> using namespace std; class Complex { public: Complex(double r = 0,double i = 0)//构造函数 { real=r; imag=i; } ~Complex() { } friend Complex operator+(Complex &c1,Complex &c2); //重载为友员函数 friend Complex operator*(Complex &c1,Complex &c2); Complex operator -(Complex&);//重载为成员函数 Complex operator /(Complex&); friend istream& operator>>(istream&, Complex&); friend ostream& operator<<(ostream&, Complex&); friend bool operator==(Complex &c1,Complex &c2); friend bool operator!=(Complex &c1,Complex &c2); void display( ); private: double real; double imag; }; Complex operator + (Complex &c1,Complex &c2) { return Complex(c1.real+c2.real, c1.imag+c2.imag); } Complex operator * (Complex &c1,Complex &c2) { return Complex(c1.real*c2.real, c1.imag*c2.imag); } Complex Complex::operator-(Complex &c) { return Complex(real-c.real,imag-c.imag); } Complex Complex::operator/(Complex &c) { return Complex(real/c.real,imag/c.imag); } istream& operator>>( istream& in, Complex &c ) { in >> c.real >> c.imag; return in; } ostream& operator<<( ostream& out, Complex &c ) { out << c.real << "+" << c.imag << "i\n"; return out; } bool operator == (Complex &c1,Complex &c2) { if(c1.real==c2.real&&c1.imag==c2.imag) { return true; } else { return false; } } bool operator != (Complex &c1,Complex &c2) { if(c1.real!=c2.real||c1.imag!=c2.imag) { return true; } else { return false; } } void Complex::display( ) { cout<<real<< "+" <<imag<<"i\n"<<endl; } int Menu() { int t; cout << endl; cout<<"=================="<<endl; cout<<"1.输入复数"<<endl; cout<<"2.查看输入的复数"<<endl; cout<<"3.复数相加"<<endl; cout<<"4.复数相减"<<endl; cout<<"5.复数相乘"<<endl; cout<<"6.复数相除"<<endl; cout<<"7.输出结果"<<endl; cout<<"0.退出"<<endl; cout<<"=================="<<endl; cout<<"请选择(0-7):"; cin>>t; return t; } int main() { int iChoice =1; Complex c1,c2,c3,c4; while (iChoice!=0) { iChoice = Menu(); switch (iChoice) { case 1: { cout<<"请输入一个复数:"<<endl; cin>>c1; getchar(); break; } case 2: { //c1.display(); cout<<c1; break; } case 3: { cout<<"原有的复数:"<<endl; cout<<c1; cout<<"请再输入一个复数相加:"<<endl; cin>>c2; getchar(); c3=c1+c2; break; } case 4: { cout<<"原有的复数:"<<endl; cout<<c1; cout<<"请再输入一个复数相减:"<<endl; cin>>c2; getchar(); c3=c1-c2; break; } case 5: { cout<<"原有的复数:"<<endl; cout<<c1; cout<<"请再输入一个复数相乘:"<<endl; cin>>c2; getchar(); c3=c1*c2; break; } case 6: { cout<<"原有的复数:"<<endl; cout<<c1; cout<<"请再输入一个复数相除:"<<endl; cin>>c2; getchar(); c3=c1/c2; break; } case 7: { cout<<"运算的结果:"<<endl; cout<<c3; break; } case 0: { break; } } } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 R语言Rstudio突然无法启动
- ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
- ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
- ¥15 用windows做服务的同志有吗
- ¥60 求一个简单的网页(标签-安全|关键词-上传)
- ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
- ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
- ¥100 为什么这个恒流源电路不能恒流?
- ¥15 有偿求跨组件数据流路径图
- ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值