waltermilner

Untitled

Apr 29th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4.   <script>
  5.     'use strict';
  6.  
  7.     // define Complex type objects
  8.     function Complex(a, b)
  9.     {
  10.         // construct a Complex number..
  11.         this.re = a;
  12.         this.imag = b;
  13.  
  14.  
  15.         // display function:
  16.         this.show = function ()
  17.         {
  18.             if (this.imag < 0)
  19.                 document.writeln(this.re + " " + this.imag + "i <br>");
  20.  
  21.             else
  22.                 document.writeln(this.re + " + " + this.imag + "i <br>");
  23.         };
  24.  
  25.         // add
  26.         this.add = function (other)
  27.         {
  28.             return new Complex(this.re + other.re, this.imag + other.imag);
  29.         };
  30.  
  31.         // subtract
  32.         this.sub = function (other)
  33.         {
  34.             return new Complex(this.re - other.re, this.imag - other.imag);
  35.         };
  36.  
  37.         // multiply
  38.         this.mult = function (other)
  39.         {
  40.             return new Complex(this.re * other.re - this.imag * other.imag, this.re * other.imag + this.imag * other.re);
  41.         };
  42.  
  43.         this.div = function (other)
  44.         {
  45.             var denom = other.re * other.re + other.imag * other.imag;
  46.             var real = this.re * other.re + this.imag * other.imag;
  47.             var im = this.imag * other.re - this.re * other.imag;
  48.             return new Complex(real / denom, im / denom);
  49.         };
  50.  
  51.     }
  52.  
  53.  
  54.  
  55.     var c1 = new Complex(2, 1);
  56.     var c2 = new Complex(0, 1);
  57.     var c3 = c1.add(c2);
  58.     c3.show();
  59.   </script>
  60.   <head>
  61.     <title>Complex</title>
  62.   </head>
  63.   <body>
  64.   </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment