数据结构作业--复数的四则运算

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
//1.定义一个复数的结构体 
typedef struct
{
    double real; //复数的实部 
    double imag; //复数的虚部 
}Complex;

//2.初始化函数:开辟空间赋给指针 
void InitComplex (Complex*& z)
{
    z = new Complex;
}

//3.构建复数函数
void AssignComplex(Complex*& z, double v1, double v2)
{
    z->real = v1;
    z->imag = v2;
}

//4.组合复数函数
void DispComplex(Complex* z)
{
    if (z->real != 0)
    {
        if (z->imag < 0 && z->imag != -1)
            cout << z->real << z->imag << "i";
        else if (z->imag == -1)
            cout << z->real << "-i";
        else if (z->imag == 0)
            cout << z->real;
        else if (z->imag == 1)
            cout << z->real << "+i";
        else
            cout << z->real << "+" << z->imag << "i";
    }
    else
    {
        if (z->imag < 0 && z->imag != -1)
            cout << z->imag << "i";
        else if (z->imag == -1)
            cout << "-i";
        else if (z->imag == 0)
            cout << " error";
        else if (z->imag == 1)
            cout << "i";
        else
            cout <<z->imag << "i";
    }

}

//5.定义四则运算类
class Foperation {
public:
    void Add(Complex* z1, Complex* z2, Complex*& ans)
    {
        ans->real = z1->real + z2->real; //a+b 
        ans->imag = z1->imag + z2->imag; //c+d
    }
    void sub(Complex* z1, Complex* z2, Complex*& ans)
    {
        ans->real = z1->real - z2->real; //a-b
        ans->imag = z1->imag - z2->imag; //c-d
    }
    void mul(Complex* z1, Complex* z2, Complex*& ans)
    {
        ans->real = (z1->real * z2->real) - (z1->imag * z2->imag); //a*c-b*d
        ans->imag = (z1->real * z2->imag) + (z1->imag * z2->real);    //a*d+b+c
    }
    void div(Complex* z1, Complex* z2, Complex*& ans)
    {
    
            double i = (z1->real * z2->real) + (z1->imag * z2->imag); //实部分子 
            double j = (z2->real * z1->imag) - (z1->real * z2->imag); //虚部分子 
            double k = (z2->real * z2->real) + (z2->imag * z2->imag); //分母 
            ans->real = i / k;
            ans->imag = j / k;
    }
};

//6.定义销毁函数
void DestroyComplex(Complex*& z)
{
    free(z);
}

int main()
{
    double a, b, c, d;
    Complex* ans;
    InitComplex(ans);
    Foperation Fop;

    //创建复数1
    cout << "请输入第一个复数的实部:"; cin >> a;
    cout << "请输入第一个复数的虚部:"; cin >> b;
    Complex* z1;
    InitComplex(z1);
    cout << "第一个复数为:";
    AssignComplex(z1, a, b);
    DispComplex(z1);
    cout << endl;

    //创建复数2 
    cout << "请输入第二个复数的实部:"; cin >> c;
    cout << "请输入第二个复数的虚部:"; cin >> d;
    Complex* z2;
    InitComplex(z2);
    cout << "第二个复数为:";
    AssignComplex(z2, c, d);
    DispComplex(z2);
    cout << endl;

    //计算两数之和 
    Fop.Add(z1, z2, ans);
    cout << "他们的和为:"; DispComplex(ans);
    cout << endl;

    //计算两数之差 
    Fop.sub(z1, z2, ans);
    cout << "他们的差为:"; DispComplex(ans);
    cout << endl;

    //计算两数之积 
    Fop.mul(z1, z2, ans);
    cout << "他们的积为:"; DispComplex(ans);
    cout << endl;

    //计算两数之商 
    if (c != 0 || d != 0)
    {
        Fop.div(z1, z2, ans);
        cout << "他们的商为:"; DispComplex(ans);
        cout << endl;
    }
    else cout << "他们的商为:error" << endl;
    //释放空间
    DestroyComplex(z1);
    DestroyComplex(z2);
    DestroyComplex(ans);

    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值