本篇内容主要讲解“用C++实现小型复数计算器”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“用C++实现小型复数计算器”吧!
小型复数计算器项目设计,供大家参考,具体内容如下
1、实现复数的设置和显示。
2、声明一个复数类Complex,重载运算符 “+”、 “-”、 “*”、 “/”,使之能用于复数的加、减、乘、除,运算符重载函数作为Complex类的成员函数。
3、声明一个复数类Complex,重载运算符 “+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2,i+c1, c1+i,均合法(设i为整数,c1,c2为复数),分别求两个复数之和、整数和复数之和,并显示运算结果。
4、实现用运算符 = = 进行复数的相等比较,并显示比较结果。
5、在程序中还应该有构造函数,析构函数,并要用到友元函数。
废话不说,直接代码,欢迎指正。
#include <iostream> #include <process.h> using namespace std;//标准命名空间 void home_page()//打印首页 { cout<<"\t\t\t--------------欢迎来到复数四则计算器----------------"<<endl; cout<<"\t\t\t| |"<<endl; cout<<"\t\t\t| 服务类型:(1~4): |"<<endl; cout<<"\t\t\t| 1.实数与实数运算; |"<<endl; cout<<"\t\t\t| 2.实数与虚数运算; |"<<endl; cout<<"\t\t\t| 3.虚数与虚数运算; |"<<endl; cout<<"\t\t\t| 4.退出程序。 |"<<endl; cout<<"\t\t\t| |"<<endl; cout<<"\t\t\t----------------------------------------------------"<<endl; } void second_page()//打印次页 { cout<<"\t\t\t----------------------------------------------------"<<endl; cout<<"\t\t\t| |"<<endl; cout<<"\t\t\t|选择你要进行的运算类型(1~6): |"<<endl; cout<<"\t\t\t| 1.进行加法运算; |"<<endl; cout<<"\t\t\t| 2.进行减法运算; |"<<endl; cout<<"\t\t\t| 3.进行乘法运算; |"<<endl; cout<<"\t\t\t| 4.进行除法运算; |"<<endl; cout<<"\t\t\t| 5.进行比较运算; |"<<endl; cout<<"\t\t\t| 6.返回上层目录; |"<<endl; cout<<"\t\t\t| |"<<endl; cout<<"\t\t\t----------------------------------------------------"<<endl; } void third_page()//程序退出提示语句 { cout<<"程序已安全退出,感谢您的光顾,为达您的满意,我们力求下次做得更好!"<<endl; } class Complex { private: double real;//定义虚数的实部 double imag;//定义虚数的虚部 public: Complex(double r=0,double i=0):real(r),imag(i) {} //构造函数初始化虚数的实部和虚部 friend Complex operator +(Complex &c1,Complex &c2)//实现两虚数(或实数)相加 { return Complex(c1.real+c2.real,c1.imag+c2.imag); } friend Complex operator -(Complex &c1,Complex &c2)//实现两虚数(或实数)相减 { return Complex(c1.real-c2.real,c1.imag-c2.imag); } friend Complex operator *(Complex &c1,Complex &c2)//实现两虚数(或实数)相乘 { return Complex(c1.real*c2.real-(c1.imag*c2.imag), c1.real*c2.imag+c2.real*c1.imag); } friend Complex operator /(Complex &c1,Complex &c2)//实现两虚数(或实数)相除 { return Complex(((c1.real*c2.real)-(c1.imag*c2.imag))/ ((c2.real*c2.real)+(c2.imag*c2.imag)), ((c1.real*c2.imag)+(c2.real*c1.imag))/ ((c2.real*c2.real)+(c2.imag*c2.imag))); } friend bool operator ==(Complex &c1,Complex &c2)//实现两虚数(或实数)相比较 { if(c1.real==c2.real&&c1.imag==c2.imag) { return true; } else return false; } void set_real()//实现给实部赋值 { cin>>real; } void set_imag()//实现给虚部赋值 { cin>>imag; } void display()//显示运算结果 { if(imag==0) { cout<<"结果为:"<<real<<endl; } else cout<<"结果为:"<<real<<"+"<<imag<<"i"<<endl; } ~Complex(){}//析构函数 } ; int main()//主函数 { int choice,choice1;//定义选择标志 while(1) { home_page(); cout<<"请选择所需的服务:"<<endl; cin>>choice; switch(choice) { case 1: second_page(); cout<<"请选择你要进行的运算:"<<endl; cin>>choice1; switch(choice1) { case 1: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个实数"<<endl; c2.set_real(); c3=c1+c2;//+号为已重载运算符,下同 c3.display(); return main();//返回主函数,类似于递归,下同 } break; case 2: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个实数"<<endl; c2.set_real(); c3=c1-c2; c3.display(); return main(); } break; case 3: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个实数"<<endl; c2.set_real(); c3=c1*c2; c3.display(); return main(); } break; case 4: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个实数"<<endl; c2.set_real(); c3=c1/c2; c3.display(); return main(); } break; case 5: { Complex c1(0.0,0.0),c2(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个实数"<<endl; c2.set_real(); if(c1==c2) cout<<"两数相等"<<endl; else cout<<"两数不相等"<<endl; return main(); } case 6: { return main(); } } break; case 2: second_page(); cout<<"请选择你要进行的运算:"<<endl; cin>>choice1; switch(choice1) { case 1: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); c3=c1+c2; c3.display(); return main(); } break; case 2: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); c3=c1-c2; c3.display(); return main(); } break; case 3: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); c3=c1*c2; c3.display(); return main(); } break; case 4: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); c3=c1/c2; c3.display(); return main(); } break; case 5: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个实数"<<endl; c1.set_real(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); if(c1==c2) cout<<"两数相等"<<endl; else cout<<"两数不相等"<<endl; return main(); } break; case 6: { return main(); } } case 3: second_page(); cout<<"请选择你要进行的运算:"<<endl; cin>>choice1; switch(choice1) { case 1: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个虚数的实部"<<endl; c1.set_real(); cout<<"请输入第一个虚数的虚部"<<endl; c1.set_imag(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); c3=c1+c2; c3.display(); return main(); } case 2: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个虚数的实部"<<endl; c1.set_real(); cout<<"请输入第一个虚数的虚部"<<endl; c1.set_imag(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); c3=c1-c2; c3.display(); return main(); } break; case 3: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个虚数的实部"<<endl; c1.set_real(); cout<<"请输入第一个虚数的虚部"<<endl; c1.set_imag(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); c3=c1*c2; c3.display(); return main(); } break; case 4: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个虚数的实部"<<endl; c1.set_real(); cout<<"请输入第一个虚数的虚部"<<endl; c1.set_imag(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); c3=c1/c2; c3.display(); return main(); } break; case 5: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"请输入第一个虚数的实部"<<endl; c1.set_real(); cout<<"请输入第一个虚数的虚部"<<endl; c1.set_imag(); cout<<"请输入第二个虚数的实部"<<endl; c2.set_real(); cout<<"请输入第二个虚数的虚部"<<endl; c2.set_imag(); if(c1==c2) cout<<"两数相等"<<endl; else cout<<"两数不相等"<<endl; return main(); } case 6://返回上层目录 { return main(); } } break; case 4://退出程序 third_page(); break; } break; } return 0; }
到此,相信大家对“用C++实现小型复数计算器”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。