首页 > 其他分享 >43.复数运算

43.复数运算

时间:2023-05-27 18:55:04浏览次数:35  
标签:real return 运算 imag 43 complex 复数 inline const

complex_test.cpp

#pragma once

#include <iostream>
#include "complex.h"

using namespace std;

ostream&
operator << (ostream& os, const complex& x)
{
	return os << '(' << real(x) << ',' << imag(x) << ')';
}

int main()
{
	complex c1(2, 1);
	complex c2(4, 0);

	cout << c1 << endl;
	cout << c2 << endl;

	cout << c1 + c2 << endl;
	cout << c1 - c2 << endl;
	cout << c1 * c2 << endl;
	cout << c1 / 2 << endl;

	cout << conj(c1) << endl;
	cout << norm(c1) << endl;
	cout << polar(10, 4) << endl;

	cout << (c1 += c2) << endl;

	cout << (c1 == c2) << endl;
	cout << (c1 != c2) << endl;
	cout << +c2 << endl;
	cout << -c2 << endl;

	cout << (c2 - 2) << endl;
	cout << (5 + c2) << endl;

	return 0;
}

complex.h

#pragma once
#ifndef __MYCOMPLEX__
#define __MYCOMPLEX__

class complex;
complex&
__doapl(complex* ths, const complex& r);
complex&
__doami(complex* ths, const complex& r);
complex&
__doaml(complex* ths, const complex& r);


class complex
{
public:
	complex(double r = 0, double i = 0) : re(r), im(i) { }
	complex& operator += (const complex&);
	complex& operator -= (const complex&);
	complex& operator *= (const complex&);
	complex& operator /= (const complex&);
	double real() const { return re; }
	double imag() const { return im; }
private:
	double re, im;

	friend complex& __doapl(complex*, const complex&);
	friend complex& __doami(complex*, const complex&);
	friend complex& __doaml(complex*, const complex&);
};


inline complex&
__doapl(complex* ths, const complex& r)
{
	ths->re += r.re;
	ths->im += r.im;
	return *ths;
}

inline complex&
complex::operator += (const complex& r)
{
	return __doapl(this, r);
}

inline complex&
__doami(complex* ths, const complex& r)
{
	ths->re -= r.re;
	ths->im -= r.im;
	return *ths;
}

inline complex&
complex::operator -= (const complex& r)
{
	return __doami(this, r);
}

inline complex&
__doaml(complex* ths, const complex& r)
{
	double f = ths->re * r.re - ths->im * r.im;
	ths->im = ths->re * r.im + ths->im * r.re;
	ths->re = f;
	return *ths;
}

inline complex&
complex::operator *= (const complex& r)
{
	return __doaml(this, r);
}

inline double
imag(const complex& x)
{
	return x.imag();
}

inline double
real(const complex& x)
{
	return x.real();
}

inline complex
operator + (const complex& x, const complex& y)
{
	return complex(real(x) + real(y), imag(x) + imag(y));
}

inline complex
operator + (const complex& x, double y)
{
	return complex(real(x) + y, imag(x));
}

inline complex
operator + (double x, const complex& y)
{
	return complex(x + real(y), imag(y));
}

inline complex
operator - (const complex& x, const complex& y)
{
	return complex(real(x) - real(y), imag(x) - imag(y));
}

inline complex
operator - (const complex& x, double y)
{
	return complex(real(x) - y, imag(x));
}

inline complex
operator - (double x, const complex& y)
{
	return complex(x - real(y), -imag(y));
}

inline complex
operator * (const complex& x, const complex& y)
{
	return complex(real(x) * real(y) - imag(x) * imag(y),
		real(x) * imag(y) + imag(x) * real(y));
}

inline complex
operator * (const complex& x, double y)
{
	return complex(real(x) * y, imag(x) * y);
}

inline complex
operator * (double x, const complex& y)
{
	return complex(x * real(y), x * imag(y));
}

complex
operator / (const complex& x, double y)
{
	return complex(real(x) / y, imag(x) / y);
}

inline complex
operator + (const complex& x)
{
	return x;
}

inline complex
operator - (const complex& x)
{
	return complex(-real(x), -imag(x));
}

inline bool
operator == (const complex& x, const complex& y)
{
	return real(x) == real(y) && imag(x) == imag(y);
}

inline bool
operator == (const complex& x, double y)
{
	return real(x) == y && imag(x) == 0;
}

inline bool
operator == (double x, const complex& y)
{
	return x == real(y) && imag(y) == 0;
}

inline bool
operator != (const complex& x, const complex& y)
{
	return real(x) != real(y) || imag(x) != imag(y);
}

inline bool
operator != (const complex& x, double y)
{
	return real(x) != y || imag(x) != 0;
}

inline bool
operator != (double x, const complex& y)
{
	return x != real(y) || imag(y) != 0;
}

#include <cmath>

inline complex
polar(double r, double t)
{
	return complex(r * cos(t), r * sin(t));
}

inline complex
conj(const complex& x)
{
	return complex(real(x), -imag(x));
}

inline double
norm(const complex& x)
{
	return real(x) * real(x) + imag(x) * imag(x);
}

#endif   //__MYCOMPLEX__

参考资料:

侯捷C++面向对象高级开发

标签:real,return,运算,imag,43,complex,复数,inline,const
From: https://www.cnblogs.com/codemagiciant/p/17437167.html

相关文章

  • Java 中的运算符和流程控制(附面试题)
    算术运算符Java中的算法运算符,包括以下几种:算术运算符名称举例+加法1+2=3-减法2-1=1*乘法2*3=6/除法24/8=3%求余24%7=3++自增1inti=1;i++--自减1inti=1;i--我们本讲要重点讲的是“++”和“--”,其他的算术运算符相对比较简单直观,本讲就不花精力去讲解了,之所以要把“++”和......
  • 343. Integer Break刷题笔记
    题目描述难点主要是考虑整数拆成两个数之后不继续拆分的情况classSolution:defintegerBreak(self,n:int)->int:dp=[0]*(n+1)dp[2]=1foriinrange(3,n+1):forjinrange(1,i-1):dp[i]=max(dp[i],max......
  • BGP线路有什么优势?43.248.187.x
       1、消除南北访问障碍由于BGP可以将联通、电信、移动等运营商的线路“合并”,使得中国南北无障碍通讯成为可能,对接入层来说,可使“联通、电信”这类区别消失,更能使一个网站资源无限制的在全国范围内无障碍访问,而不需要在异地部署VPN或者异地加速站来实现异地无障碍访问2、高......
  • 基本逻辑运算与常用复合逻辑
    基本逻辑运算 且  或  非  与非 或非 异或 同或 与或非 真值表的列写方法   ......
  • AI换脸10分钟诈骗430万,黑产诈骗怎么防
    随着ChatGPT、Midjourney、StableDiffusion等现象级应用的广泛使用,大模型的安全性受到了学术界和产业界的广泛关注。现有的研究热点主要围绕两方面:(1)利用SFT与RLHF等技术将大模型与人类偏好对齐,从而提升大模型自身的安全性。(2)针对不同场景设计专用内容过滤器。除了大模型......
  • Codeforces 1439E - Cheat and Win
    模拟赛放了道*3500,结果全场都切了,非常恐怖。首先考虑怎么样的树是合法的,打个表发现SG函数值为\(\sum_{d}2^d·(\text{深度为d的点个数}\bmod2)\),换句话说后手必胜当且仅当每种深度的点数都是偶数。于是实际上我们只用建出虚树之后树上差分一下求出每个点被覆盖的情况,进而......
  • 基本运算符
    运算符类型运算符描述示例算术运算符+加3+4=7-减4-2=2*乘2*5=10/除8/4=2.0%取余9%4=1//整除9//4=2**幂2**3=8比较运算符==等于3==3!=不等于3!=4<小于3<4>大于4>3......
  • P8943 Deception Point 题解
    Description题目给的很详细了。Solution首先\(n\)个点\(n\)条边,我们很容易就想到基环树(比正常的树多了一条边,形成了一个环),不会也没关系,这题跟基环树其实关系不大。首先,我们可以发现题目中说明了这个环不是一个四元及以下的环,这代表着如果\(A\)提前进入了这个环,那么他......
  • 2023-05-25:给定一个正整数 x,我们将会写出一个形如 x (op1) x (op2) x (op3) x ... 的
    2023-05-25:给定一个正整数x,我们将会写出一个形如x(op1)x(op2)x(op3)x...的表达式其中每个运算符op1,op2,…可以是加、减、乘、除之一例如,对于x=3,我们可以写出表达式3*3/3+3-3,该式的值为3在写这样的表达式时,我们需要遵守下面的惯例:除运算符(/)返回有理数任何地......
  • 2023-05-25:给定一个正整数 x,我们将会写出一个形如 x (op1) x (op2) x (op3) x ... 的
    2023-05-25:给定一个正整数x,我们将会写出一个形如x(op1)x(op2)x(op3)x...的表达式其中每个运算符op1,op2,…可以是加、减、乘、除之一例如,对于x=3,我们可以写出表达式3*3/3+3-3,该式的值为3在写这样的表达式时,我们需要遵守下面的惯例:除运算符(/)返回有理数......