温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何用C++实现小型复数计算器

发布时间:2022-04-14 17:10:54 来源:亿速云 阅读:189 作者:zzz 栏目:编程语言

这篇文章主要讲解了“如何用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++实现小型复数计算器这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI