您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何用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++實現小型復數計算器”的內容了,經過本文的學習后,相信大家對如何用C++實現小型復數計算器這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。