Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <script>
- 'use strict';
- // define Complex type objects
- function Complex(a, b)
- {
- // construct a Complex number..
- this.re = a;
- this.imag = b;
- // display function:
- this.show = function ()
- {
- if (this.imag < 0)
- document.writeln(this.re + " " + this.imag + "i <br>");
- else
- document.writeln(this.re + " + " + this.imag + "i <br>");
- };
- // add
- this.add = function (other)
- {
- return new Complex(this.re + other.re, this.imag + other.imag);
- };
- // subtract
- this.sub = function (other)
- {
- return new Complex(this.re - other.re, this.imag - other.imag);
- };
- // multiply
- this.mult = function (other)
- {
- return new Complex(this.re * other.re - this.imag * other.imag, this.re * other.imag + this.imag * other.re);
- };
- this.div = function (other)
- {
- var denom = other.re * other.re + other.imag * other.imag;
- var real = this.re * other.re + this.imag * other.imag;
- var im = this.imag * other.re - this.re * other.imag;
- return new Complex(real / denom, im / denom);
- };
- }
- var c1 = new Complex(2, 1);
- var c2 = new Complex(0, 1);
- var c3 = c1.add(c2);
- c3.show();
- </script>
- <head>
- <title>Complex</title>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment