如何用C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器

這篇文章主要講解了“如何用C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何用C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器”吧!

成都創(chuàng)新互聯(lián)是一家專業(yè)從事成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站的網(wǎng)絡(luò)公司。作為專業(yè)網(wǎng)絡(luò)公司,成都創(chuàng)新互聯(lián)依托的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營經(jīng)驗(yàn),為您提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣及網(wǎng)站設(shè)計(jì)開發(fā)服務(wù)!

一、問題描述及功能要求

1、實(shí)現(xiàn)復(fù)數(shù)的設(shè)置和顯示。
2、聲明一個復(fù)數(shù)類Complex,重載運(yùn)算符 “+”、 “-”、 “*”、 “/”,使之能用于復(fù)數(shù)的加、減、乘、除,運(yùn)算符重載函數(shù)作為Complex類的成員函數(shù)。
3、聲明一個復(fù)數(shù)類Complex,重載運(yùn)算符 “+”,使之能用于復(fù)數(shù)的加法運(yùn)算。參加運(yùn)算的兩個運(yùn)算量可以都是類對象,也可以其中有一個是整數(shù),順序任意。例如:c1+c2,i+c1, c1+i,均合法(設(shè)i為整數(shù),c1,c2為復(fù)數(shù)),分別求兩個復(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ù)。

二、代碼實(shí)現(xiàn) 帶有注釋

廢話不說,直接代碼,歡迎指正。

#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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個實(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個實(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個實(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個實(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個實(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個實(shí)數(shù)"<<endl;
                c1.set_real();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個虛數(shù)的實(shí)部"<<endl;
                c1.set_real();
                cout<<"請輸入第一個虛數(shù)的虛部"<<endl;
                c1.set_imag();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個虛數(shù)的實(shí)部"<<endl;
                c1.set_real();
                cout<<"請輸入第一個虛數(shù)的虛部"<<endl;
                c1.set_imag();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個虛數(shù)的實(shí)部"<<endl;
                c1.set_real();
                cout<<"請輸入第一個虛數(shù)的虛部"<<endl;
                c1.set_imag();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個虛數(shù)的實(shí)部"<<endl;
                c1.set_real();
                cout<<"請輸入第一個虛數(shù)的虛部"<<endl;
                c1.set_imag();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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<<"請輸入第一個虛數(shù)的實(shí)部"<<endl;
                c1.set_real();
                cout<<"請輸入第一個虛數(shù)的虛部"<<endl;
                c1.set_imag();
                cout<<"請輸入第二個虛數(shù)的實(shí)部"<<endl;
                c2.set_real();
                cout<<"請輸入第二個虛數(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ì)算器”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何用C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

網(wǎng)站標(biāo)題:如何用C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器
文章URL:http://muchs.cn/article48/jejjhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、App開發(fā)、ChatGPT網(wǎng)站收錄、App設(shè)計(jì)網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)