您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“用C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“用C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器”吧!
小型復(fù)數(shù)計(jì)算器項(xiàng)目設(shè)計(jì),供大家參考,具體內(nèi)容如下
1、實(shí)現(xiàn)復(fù)數(shù)的設(shè)置和顯示。
2、聲明一個(gè)復(fù)數(shù)類Complex,重載運(yùn)算符 “+”、 “-”、 “*”、 “/”,使之能用于復(fù)數(shù)的加、減、乘、除,運(yùn)算符重載函數(shù)作為Complex類的成員函數(shù)。
3、聲明一個(gè)復(fù)數(shù)類Complex,重載運(yùn)算符 “+”,使之能用于復(fù)數(shù)的加法運(yùn)算。參加運(yùn)算的兩個(gè)運(yùn)算量可以都是類對象,也可以其中有一個(gè)是整數(shù),順序任意。例如:c1+c2,i+c1, c1+i,均合法(設(shè)i為整數(shù),c1,c2為復(fù)數(shù)),分別求兩個(gè)復(fù)數(shù)之和、整數(shù)和復(fù)數(shù)之和,并顯示運(yùn)算結(jié)果。
4、實(shí)現(xiàn)用運(yùn)算符 = = 進(jìn)行復(fù)數(shù)的相等比較,并顯示比較結(jié)果。
5、在程序中還應(yīng)該有構(gòu)造函數(shù),析構(gòu)函數(shù),并要用到友元函數(shù)。
廢話不說,直接代碼,歡迎指正。
#include <iostream> #include <process.h> using namespace std;//標(biāo)準(zhǔn)命名空間 void home_page()//打印首頁 { cout<<"\t\t\t--------------歡迎來到復(fù)數(shù)四則計(jì)算器----------------"<<endl; cout<<"\t\t\t| |"<<endl; cout<<"\t\t\t| 服務(wù)類型:(1~4): |"<<endl; cout<<"\t\t\t| 1.實(shí)數(shù)與實(shí)數(shù)運(yùn)算; |"<<endl; cout<<"\t\t\t| 2.實(shí)數(shù)與虛數(shù)運(yùn)算; |"<<endl; cout<<"\t\t\t| 3.虛數(shù)與虛數(shù)運(yùn)算; |"<<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|選擇你要進(jìn)行的運(yùn)算類型(1~6): |"<<endl; cout<<"\t\t\t| 1.進(jìn)行加法運(yùn)算; |"<<endl; cout<<"\t\t\t| 2.進(jìn)行減法運(yùn)算; |"<<endl; cout<<"\t\t\t| 3.進(jìn)行乘法運(yùn)算; |"<<endl; cout<<"\t\t\t| 4.進(jìn)行除法運(yùn)算; |"<<endl; cout<<"\t\t\t| 5.進(jìn)行比較運(yùn)算; |"<<endl; cout<<"\t\t\t| 6.返回上層目錄; |"<<endl; cout<<"\t\t\t| |"<<endl; cout<<"\t\t\t----------------------------------------------------"<<endl; } void third_page()//程序退出提示語句 { cout<<"程序已安全退出,感謝您的光顧,為達(dá)您的滿意,我們力求下次做得更好!"<<endl; } class Complex { private: double real;//定義虛數(shù)的實(shí)部 double imag;//定義虛數(shù)的虛部 public: Complex(double r=0,double i=0):real(r),imag(i) {} //構(gòu)造函數(shù)初始化虛數(shù)的實(shí)部和虛部 friend Complex operator +(Complex &c1,Complex &c2)//實(shí)現(xiàn)兩虛數(shù)(或?qū)崝?shù))相加 { return Complex(c1.real+c2.real,c1.imag+c2.imag); } friend Complex operator -(Complex &c1,Complex &c2)//實(shí)現(xiàn)兩虛數(shù)(或?qū)崝?shù))相減 { return Complex(c1.real-c2.real,c1.imag-c2.imag); } friend Complex operator *(Complex &c1,Complex &c2)//實(shí)現(xiàn)兩虛數(shù)(或?qū)崝?shù))相乘 { 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)//實(shí)現(xiàn)兩虛數(shù)(或?qū)崝?shù))相除 { 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)//實(shí)現(xiàn)兩虛數(shù)(或?qū)崝?shù))相比較 { if(c1.real==c2.real&&c1.imag==c2.imag) { return true; } else return false; } void set_real()//實(shí)現(xiàn)給實(shí)部賦值 { cin>>real; } void set_imag()//實(shí)現(xiàn)給虛部賦值 { cin>>imag; } void display()//顯示運(yùn)算結(jié)果 { if(imag==0) { cout<<"結(jié)果為:"<<real<<endl; } else cout<<"結(jié)果為:"<<real<<"+"<<imag<<"i"<<endl; } ~Complex(){}//析構(gòu)函數(shù) } ; int main()//主函數(shù) { int choice,choice1;//定義選擇標(biāo)志 while(1) { home_page(); cout<<"請選擇所需的服務(wù):"<<endl; cin>>choice; switch(choice) { case 1: second_page(); cout<<"請選擇你要進(jìn)行的運(yùn)算:"<<endl; cin>>choice1; switch(choice1) { case 1: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)實(shí)數(shù)"<<endl; c2.set_real(); c3=c1+c2;//+號為已重載運(yùn)算符,下同 c3.display(); return main();//返回主函數(shù),類似于遞歸,下同 } break; case 2: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)實(shí)數(shù)"<<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<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)實(shí)數(shù)"<<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<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)實(shí)數(shù)"<<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<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)實(shí)數(shù)"<<endl; c2.set_real(); if(c1==c2) cout<<"兩數(shù)相等"<<endl; else cout<<"兩數(shù)不相等"<<endl; return main(); } case 6: { return main(); } } break; case 2: second_page(); cout<<"請選擇你要進(jìn)行的運(yùn)算:"<<endl; cin>>choice1; switch(choice1) { case 1: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<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<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<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<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<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<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<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<<"請輸入第一個(gè)實(shí)數(shù)"<<endl; c1.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<endl; c2.set_imag(); if(c1==c2) cout<<"兩數(shù)相等"<<endl; else cout<<"兩數(shù)不相等"<<endl; return main(); } break; case 6: { return main(); } } case 3: second_page(); cout<<"請選擇你要進(jìn)行的運(yùn)算:"<<endl; cin>>choice1; switch(choice1) { case 1: { Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0); cout<<"請輸入第一個(gè)虛數(shù)的實(shí)部"<<endl; c1.set_real(); cout<<"請輸入第一個(gè)虛數(shù)的虛部"<<endl; c1.set_imag(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<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<<"請輸入第一個(gè)虛數(shù)的實(shí)部"<<endl; c1.set_real(); cout<<"請輸入第一個(gè)虛數(shù)的虛部"<<endl; c1.set_imag(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<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<<"請輸入第一個(gè)虛數(shù)的實(shí)部"<<endl; c1.set_real(); cout<<"請輸入第一個(gè)虛數(shù)的虛部"<<endl; c1.set_imag(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<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<<"請輸入第一個(gè)虛數(shù)的實(shí)部"<<endl; c1.set_real(); cout<<"請輸入第一個(gè)虛數(shù)的虛部"<<endl; c1.set_imag(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<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<<"請輸入第一個(gè)虛數(shù)的實(shí)部"<<endl; c1.set_real(); cout<<"請輸入第一個(gè)虛數(shù)的虛部"<<endl; c1.set_imag(); cout<<"請輸入第二個(gè)虛數(shù)的實(shí)部"<<endl; c2.set_real(); cout<<"請輸入第二個(gè)虛數(shù)的虛部"<<endl; c2.set_imag(); if(c1==c2) cout<<"兩數(shù)相等"<<endl; else cout<<"兩數(shù)不相等"<<endl; return main(); } case 6://返回上層目錄 { return main(); } } break; case 4://退出程序 third_page(); break; } break; } return 0; }
到此,相信大家對“用C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。