用js写一个简单的复数类

function Complex(R , I){
  if( isNaN( R ) || isNaN( I )) { throw new TypeError('Complex params require Number'); }
  this.r = R;                          
  this.i = I;                         
}
// 加法
Complex.prototype.add = function (that) {
  return new Complex(this.r + that.r, this.i + that.i);
};
// 负运算
Complex.prototype.neg = function () {
  return new Complex(-this.r, -this.i);
};
// 乘法
Complex.prototype.multiply = function (that) {
  if (this.r === that.r && this.i + that.i === 0) {
    return this.r * this.r + this.i * this.i
  }
  return new Complex(this.r * that.r - this.i * that.i, this.r * that.i + this.i * that.r);
};
// 除法
Complex.prototype.divide = function (that) {
  var a = this.r;
  var b = this.i;
  var c = that.r;
  var d = that.i;
  return new Complex((a * c + b * d) / (c * c + d * d), (b * c - a * d) / (c * c + d * d));
};
// 模长
Complex.prototype.mo = function () {
  return Math.sqrt(this.r * this.r + this.i * this.i);
};
Complex.prototype.toString = function () {
  return "{" + this.r + "," + this.i + "}";
};
// 判断两个复数相等
Complex.prototype.equal = function (that) {
  return that !== null && that.constructor === Complex && this.r === that.r && this.i === that.i;
};
Complex.ZERO = new Complex(0, 0);
Complex.ONE  = new Complex(1, 0);
Complex.I    = new Complex(0, 1);
// 从普通字符串解析为复数
Complex.parse = function (s) {
  try {
    var execres = Complex.parseRegExp.exec(s);
    return new Complex(parseFloat(execres[1]), parseFloat(execres[2]));
  } catch (e) {
    throw new TypeError("Can't parse '" + s + "'to a complex");
  }
};
Complex.parseRegExp = /^\{([\d\s]+[^,]*),([\d\s]+[^}]*)\}$/;
// console.log(/^\{([\d\s]+[^,]*),([\d\s]+[^}]*)\}$/.exec('{2,3}'));
// 示例代码
var c = new Complex(2, 3);
var d = new Complex(2, 5);

console.log(c.add(d).toString());
console.log(Complex.parse(c.toString()).add(c.neg()).equal(Complex.ZERO));
console.log(c.divide(d).toString(), Complex.parse('{2h, 09d}').mo())
// 共轭复数 得出的结果是普通实数了
console.log(new Complex(2, 3).multiply(new Complex(2, -3)))
{4,8}
true
{0.6551724137931034,-0.13793103448275862} Complex { r: 2, i: 9 }
13
(base) leooo-de-mbp:leo-py mac$ node js-complex.js 
{4,8}
true
{0.6551724137931034,-0.13793103448275862} 9.219544457292887
13
复制代码

转载于:https://juejin.im/post/5d0219dce51d4550a629b264

猜你喜欢

转载自blog.csdn.net/weixin_34290631/article/details/93168196