鱼C论坛

 找回密码
 立即注册
查看: 1738|回复: 1

复数加减运算

[复制链接]
发表于 2021-3-14 23:22:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以复数类型抽象数据结构计算两个复数的和与差
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 08:42:09 | 显示全部楼层
本帖最后由 xieglt 于 2021-3-15 09:17 编辑
#ifndef H_COMPLEX
#define H_COMPLEX

#include <math.h>
//+ , - , * , += , -= , *= 都有
typedef struct tagComplex  
{
        double        X;
        double        Y;

        tagComplex()
        {
                X = 0;
                Y = 0;
        }

        tagComplex(const struct tagComplex & c)
        {
                X = c.X;
                Y = c.Y;
        }

        tagComplex(double x,double y = 0.0)
        {
                X = x;
                Y = y;
        }
        
        struct tagComplex & operator = (double x)
        {
                X = x;
                Y = 0;
                return * this;
        }

        
        struct tagComplex & operator = (const tagComplex & c)
        {
                X = c.X;
                Y = c.Y;
                return * this;
        }
        
        struct tagComplex & operator += (const tagComplex & c)
        {
                X += c.X;
                Y += c.Y;
                return * this;
        }
        
        struct tagComplex & operator -= (const tagComplex & c)
        {
                X -= c.X;
                Y -= c.Y;
                return * this;
        }
        
        struct tagComplex & operator *= (const tagComplex & c)
        {
                COMPLEX t = * this;
                X = t.X * c.X - t.Y * c.Y;
                Y = t.X * c.Y + t.Y * c.X;
                return * this;
        }
        
        struct tagComplex & operator /= (double n)
        {
                X /= n;
                Y /= n;
                return * this;
        }
        
        void Mul(const tagComplex & a,const tagComplex & b)
        {
                * this = a;
                * this *= b;
        }
        
        operator double()
        {
                return sqrt(X*X + Y*Y);
        }

        operator long()
        {
                return (long)(X + 0.4);
        }

        friend struct tagComplex operator + (const struct tagComplex & a, const struct tagComplex & b)
        {
                struct tagComplex c = a;
                c += b;
                return c;
        }

        friend struct tagComplex operator - (const struct tagComplex & a, const struct tagComplex & b)
        {
                struct tagComplex c = a;
                c -= b;
                return c;
        }

        friend struct tagComplex operator * (const struct tagComplex & a, const struct tagComplex & b)
        {
                struct tagComplex c = a;
                c *= b;
                return c;
        }
}COMPLEX, *LPCOMPLEX;

#endif
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 06:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表