复数计算器

基本要求:

        为复数定义一个类。重载输入和输出运算符>>和<<。实现复数的+,-,*,/等运算。输出复数时,应对实部和虚部的各种情况加以讨论。以便能输出2,2+3i,2-3i,5i,-8i,i与-i这样的复数。

        复数的四则运算可以按如下方式进行:


#include<iostream>
using namespace std;
#include<windows.h>

//复数类
class complexnumber
{
private:
    float real;
    float imag;
public:
    friend istream& operator >>(istream& in,complexnumber &temple);//重载输入运算符
    friend ostream& operator <<(ostream& out,complexnumber &temple);//重载输出运算符
    friend complexnumber operator +(complexnumber t1,complexnumber t2);//重载加法运算符
    friend complexnumber operator -(complexnumber t1,complexnumber t2);//重载减法运算符
    friend complexnumber operator *(complexnumber t1,complexnumber t2);//重载乘法运算符
    friend complexnumber operator /(complexnumber t1,complexnumber t2);//重载除法运算符
};

//重载输入运算符
istream& operator >>(istream& in,complexnumber &temple)
{
    cout<<"实部    虚部"<<endl;
    in>>temple.real>>temple.imag;
    return in;
}

//重载输出运算符
ostream& operator <<(ostream& out,complexnumber &temple)
{
    if(temple.real==0)
    {
        if(temple.imag==0)
        {
            cout<<0<<endl;
        }
        else if(temple.imag==1)
        {
            out<<"i"<<endl;
        }
        else if(temple.imag==-1)
        {
            out<<"-i"<<endl;
        }
        else
        {
            out<<temple.imag<<"i"<<endl;
        }
    }
    else
    {
        out<<temple.real;
        if(temple.imag>0)
        {
            out<<"+";
        }
        if(temple.imag!=0)
        {
            out<<temple.imag<<"i"<<endl;
        }
    }
    return out;
}

//重载加法运算符
complexnumber operator +(complexnumber t1,complexnumber t2)
{
    complexnumber temple;
    temple.real=t1.real+t2.real;
    temple.imag=t1.imag+t2.imag;
    return temple;
}

//重载减法运算符
complexnumber operator -(complexnumber t1,complexnumber t2)
{
    complexnumber temple;
    temple.real=t1.real-t2.real;
    temple.imag=t1.imag-t2.imag;
    return temple;
}

//重载乘法运算符
complexnumber operator *(complexnumber t1,complexnumber t2)
{
    complexnumber temple;
    temple.real=(t1.real*t2.real)-(t1.imag*t2.imag);
    temple.imag=(t1.imag*t2.real)+(t1.real*t2.imag);
    return temple;
}

//重载除法运算符
complexnumber operator /(complexnumber t1,complexnumber t2)
{
    complexnumber temple;
    temple.real=((t1.real*t2.real)+(t1.imag*t2.imag))/((t2.real*t2.real)+(t2.imag*t2.imag));
    temple.imag=((t1.imag*t2.real)-(t1.real*t2.imag))/((t2.real*t2.real)+(t2.imag*t2.imag));
    return temple;
}

int main()
{
    system("color f3");
    int select;
    complexnumber ob1,ob2,ob;
    do
    {
        cout<<"*******************************"<<endl;//菜单
        cout<<"*         复数计算器         *"<<endl;
        cout<<"*******************************"<<endl;
        cout<<"*1------------------------加法*"<<endl;
        cout<<"*2------------------------减法*"<<endl;
        cout<<"*3------------------------乘法*"<<endl;
        cout<<"*4------------------------除法*"<<endl;
        cout<<"*0------------------------退出*"<<endl;
        cout<<"*******************************"<<endl;
        cout<<"请输入你的选择:";
        cin>>select;
        switch(select)
        {
            case 1: cout<<endl<<"加法:"<<endl;
                    cout<<"请输入第一个复数的值:"<<endl;
                    cin>>ob1;
                    cout<<"第一个复数为:"<<ob1<<endl;
                    cout<<"请输入第二个复数的值:"<<endl;
                    cin>>ob2;
                    cout<<"第二个复数为:"<<ob2<<endl;
                    system("pause");
                    ob=ob1+ob2;
                    system("cls");
                    cout<<"第一个复数为:"<<ob1<<endl;
                    cout<<"第二个复数为:"<<ob2<<endl;
                    cout<<"两数之和为:"<<ob<<endl;
                    system("pause");
                    system("cls");
                break;
            case 2: cout<<endl<<"减法:"<<endl;
                    cout<<"请输入第一个复数的值:"<<endl;
                    cin>>ob1;
                    cout<<"第一个复数为:"<<ob1<<endl;
                    cout<<"请输入第二个复数的值:"<<endl;
                    cin>>ob2;
                    cout<<"第二个复数为:"<<ob2<<endl;
                    system("pause");
                    ob=ob1-ob2;
                    system("cls");
                    cout<<"第一个复数为:"<<ob1<<endl;
                    cout<<"第二个复数为:"<<ob2<<endl;
                    cout<<"两数之差为:"<<ob<<endl;
                    system("pause");
                    system("cls");
                break;
            case 3: cout<<endl<<"乘法:"<<endl;
                    cout<<"请输入第一个复数的值:"<<endl;
                    cin>>ob1;
                    cout<<"第一个复数为:"<<ob1<<endl;
                    cout<<"请输入第二个复数的值:"<<endl;
                    cin>>ob2;
                    cout<<"第二个复数为:"<<ob2<<endl;
                    system("pause");
                    ob=ob1*ob2;
                    system("cls");
                    cout<<"第一个复数为:"<<ob1<<endl;
                    cout<<"第二个复数为:"<<ob2<<endl;
                    cout<<"两数之积为:"<<ob<<endl;
                    system("pause");
                    system("cls");
                break;
            case 4: cout<<endl<<"除法:"<<endl;
                    cout<<"请输入第一个复数的值:"<<endl;
                    cin>>ob1;
                    cout<<"第一个复数为:"<<ob1<<endl;
                    cout<<"请输入第二个复数的值:"<<endl;
                    cin>>ob2;
                    cout<<"第二个复数为:"<<ob2<<endl;
                    system("pause");
                    ob=ob1/ob2;
                    system("cls");
                    cout<<"第一个复数为:"<<ob1<<endl;
                    cout<<"第二个复数为:"<<ob2<<endl;
                    cout<<"两数之商为:"<<ob<<endl;
                    system("pause");
                    system("cls");
                break;
        }
    }while(select!=0);
    system("cls");
    cout<<"欢迎使用!!"<<endl;
    return 0;
}


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值