Javascript mintapélda - Log in

komplex számok osztálya

/* konstruktor */
function Complex(r, i) {
	if (r) {
		this.real = r;
	} else {
		this.real = 0.0;
	}
	if (i) {
		this.imag = i;
	} else {
		this.imag = 0.0;
	}

	/* metódusok */
	this.setReal = function(r) { this.real = r; };
	this.setImag = function(i) { this.imag = i; };
	this.getReal = function() { return this.real; };
	this.getImag = function() { return this.imag; };
	this.abs = function() {
		return Math.sqrt(this.real * this.real + this.imag * this.imag);
	};
	this.conjugalt = function() {
		var w = this.real;
		this.real = this.imag;
		this.imag = w;
	};
	this.add = function(c) {
		this.real += c.real;
		this.imag += c.imag;
	}
	this.toString = function() {
		return this.real + '+' + this.imag + 'i';
	}
}

Komplex osztály használata

var a = new Complex();
document.write('a=' + a.toString() + '<br>');
var b = new Complex(1.0);
document.write('b=' + b.toString() + '<br>');
var c = new Complex(0.5, 2.4);
document.write('c=' + c.toString() + '<br>');
var d = new Complex();
d = c; d.conjugalt();	// vigyazat ez elrontja c értékét is! REFERENCIA
			// d = Complex(c.real, c.imag) esetén c nem változik!
document.write('d=' + d.toString() + '<br>');
document.write('de !!! c=' + c.toString() + '<br>');
var e = new Complex();
e.setReal(4.1); e.setImag(1.8);
document.write('e=' + e.toString() + '<br>');
d.add(e);
document.write('d=' + d.toString() + '<br>');

Last modified: Thursday, 9 March 2017, 3:05 PM