91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用C++實現小型復數計算器

發布時間:2021-06-17 16:24:07 來源:億速云 閱讀:274 作者:chen 欄目:開發技術

本篇內容主要講解“用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++實現小型復數計算器”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

c++
AI

曲水县| 阿拉尔市| 浠水县| 泰州市| 安阳县| 方城县| 池州市| 拉萨市| 周宁县| 镇巴县| 邮箱| 汾阳市| 镇原县| 成武县| 宜阳县| 获嘉县| 临沭县| 九江市| 开平市| 内江市| 广宁县| 建湖县| 辽宁省| 定襄县| 兴隆县| 三河市| 千阳县| 沁阳市| 丰台区| 梓潼县| 闵行区| 黎城县| 光山县| 丁青县| 定远县| 双柏县| 清新县| 陕西省| 古交市| 秦皇岛市| 龙川县|