Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Operation() {
- this.operands = arguments;
- }
- Operation.prototype.evaluate = function () {
- var operResult = [];
- for (var i = 0; i < this.operands.length; i++) {
- operResult.push(this.operands[i].evaluate.apply(this.operands[i], arguments));
- }
- return this.count.apply(null, operResult);
- };
- Operation.prototype.diff = function (name) {
- var operResult = [];
- for (var i = 0; i < this.operands.length; i++) {
- operResult.push(this.operands[i]);
- operResult.push(this.operands[i].diff(name));
- }
- return this.countDiff.apply(null, operResult);
- };
- Operation.prototype.toString = function () {
- var resString = '';
- for (var i = 0; i < this.operands.length; i++) {
- resString += this.operands[i].toString() + ' ';
- }
- return resString + this.operation;
- };
- /* Constant class implementation */
- function Const(a) {
- this.value = a;
- }
- Const.prototype.evaluate = function () {
- return this.value;
- };
- Const.prototype.diff = function (name) {
- return new Const(0);
- };
- Const.prototype.toString = function () {
- return this.value.toString();
- };
- const vars = ['x', 'y', 'z'];
- function Variable(name) {
- this.index = vars.indexOf(name);
- }
- Variable.prototype.evaluate = function () {
- return arguments[this.index];
- };
- Variable.prototype.diff = function (name) {
- if (vars[this.index] === name) {
- return new Const(1);
- }
- else {
- return new Const(0);
- }
- };
- Variable.prototype.toString = function () {
- return vars[this.index];
- };
- function Add(a, b) {
- Operation.call(this, a, b);
- this.operation = '+'
- }
- Add.prototype = Object.create(Operation.prototype);
- Add.prototype.count = function (a, b) {
- return a + b;
- }
- Add.prototype.countDiff = function (a, a_dir, b, b_dir) {
- return new Add(a_dir, b_dir);
- };
- function Subtract(a, b) {
- Operation.call(this, a, b);
- this.operation = '-'
- }
- Subtract.prototype = Object.create(Operation.prototype);
- Subtract.prototype.count = function (a, b) {
- return a - b;
- };
- Subtract.prototype.countDiff = function (a, a_dir, b, b_dir) {
- return new Subtract(a_dir, b_dir);
- };
- function Multiply(a, b) {
- Operation.call(this, a, b);
- this.operation = '*'
- }
- Multiply.prototype = Object.create(Operation.prototype);
- Multiply.prototype.count = function (a, b) {
- return a * b;
- };
- Multiply.prototype.countDiff = function (a, a_dir, b, b_dir) {
- return new Add(new Multiply(a, b_dir), new Multiply(a_dir, b));
- };
- function Divide(a, b) {
- Operation.call(this, a, b);
- this.operation = '/'
- }
- Divide.prototype = Object.create(Operation.prototype);
- Divide.prototype.count = function (a, b) {
- return a / b;
- };
- Divide.prototype.countDiff = function (a, a_dir, b, b_dir) {
- return new Divide(new Subtract(new Multiply(a_dir, b), new Multiply(a, b_dir)), new Multiply(b, b));
- };
- function Negate(a) {
- Operation.call(this, a);
- this.operation = 'negate'
- }
- Negate.prototype = Object.create(Operation.prototype);
- Negate.prototype.count = function (a) {
- return -a;
- };
- Negate.prototype.countDiff = function (a, a_dir) {
- return new Negate(a_dir);
- };
- function ArcTan(a) {
- Operation.call(this, a);
- this.operation = 'atan';
- }
- ArcTan.prototype = Object.create(Operation.prototype);
- ArcTan.prototype.count = function (a) {
- return Math.atan(a);
- };
- ArcTan.prototype.countDiff = function (a, da) {
- return new Divide(da, new Add(new Const(1), new Multiply(a, a)));
- };
- function ArcTan2(a, b) {
- Operation.call(this, a, b);
- this.operation = 'atan2';
- }
- ArcTan2.prototype = Object.create(Operation.prototype);
- ArcTan2.prototype.count = function (a, b) {
- return Math.atan2(a, b);
- };
- ArcTan2.prototype.countDiff = function (a, b, da, db) {
- return new Divide(new Subtract(new Multiply(da, b), new Multiply(a, db)), new Add(new Multiply(a, a), new Multiply(b, b)));
- };
- function applyToConstructor(constructor, argArray) {
- var args = [null].concat(argArray);
- var factoryFunction = constructor.bind.apply(constructor, args);
- return new factoryFunction();
- }
- function is_digit(c) {
- return c >= '0' && c <= '9';
- }
- function parse(expr) {
- var OPERATIONS = {
- '+': [Add, 2],
- '-': [Subtract, 2],
- '*': [Multiply, 2],
- '/': [Divide, 2],
- 'negate': [Negate, 1],
- 'atan': [ArcTan, 1],
- 'atan2': [ArcTan2, 2]
- };
- var stack = [];
- var tokens = expr.split(/\s+/);
- for (var i = 0; i < tokens.length; i++) {
- var token = tokens[i];
- if (token in OPERATIONS) {
- var args = [];
- for (var j = 0; j < OPERATIONS[token][1]; j++)
- args.unshift(stack.pop());
- stack.push(applyToConstructor(OPERATIONS[token][0], args));
- } else if (vars.indexOf(token) !== -1) {
- stack.push(new Variable(token));
- } else if (is_digit(tokens[i][0]) || (tokens[i][0] === '-' && tokens[i].length !== 1)) {
- stack.push(new Const(parseInt(token)));
- }
- }
- return stack.pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement