Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getPolska(input) {
- priority = {'(' : 0, ')' : 1, '+' : 2, '-' : 2, '*' : 3, '/' : 3, '^' : 4};
- stack = new Array();
- out = "";
- function inputNumber() {
- while ((input.charAt(i) >= '0' && input.charAt(i) <= '9') || (input.charAt(i) == '.')) {
- out += input.charAt(i);
- i++
- }
- i--;
- out += " ";
- }
- for (i = 0; i < input.length; i++) {
- symbol = input.charAt(i);
- switch (symbol) {
- case ' ' :
- break;
- case '(' :
- stack.push('(');
- break;
- case ')':
- while (stack[stack.length - 1] != '(') {
- out += stack.pop();
- }
- stack.pop();
- break;
- case '+' :
- case '-' :
- case '*' :
- case '/' :
- case '^' :
- if (/\(\-/.test(input.charAt(i-1) + symbol)) {
- i++;
- out += " -";
- inputNumber();
- }
- else if (priority[symbol] > priority[stack[stack.length - 1]]) {
- stack.push(symbol);
- }
- else {
- while (priority[symbol] <= priority[stack[stack.length - 1]]) {
- out += stack.pop();
- }
- stack.push(symbol);
- }
- break;
- default :
- if (symbol >= '0' && symbol <= '9') {
- out += " ";
- inputNumber();
- }
- }
- }
- while (stack.length > 0)
- out += stack.pop();
- return out;
- }
- function calculate(polska) {
- stack = new Array();
- justDoIT = "";
- for (i=0; i<polska.length; i++) {
- symbol = polska.charAt(i);
- if (symbol == ' ') {
- i++;
- num = "";
- while (polska.charAt(i) != " ") {
- num+=polska.charAt(i);
- i++;
- }
- stack.push(num);
- }
- else {
- op2=stack.pop();
- op1=stack.pop();
- if (symbol!='^') {
- justDoIT = op1 + symbol + op2;
- }
- else {
- justDoIT = "Math.pow(" + op1 + ',' + op2 + ')';
- }
- stack.push(eval(justDoIT));
- }
- }
- var result = stack.pop();
- return result;
- }
- function main(){
- //WScript.Echo("Vvedi virazhenie")
- //var input = WScript.StdIn.Readline();
- input = '(-1)*6.5+3';
- polska = getPolska(input);
- WScript.Echo("eto polska zapis")
- WScript.Echo(polska);
- result = calculate(polska);
- WScript.Echo("eto resultat")
- WScript.Echo(result);
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement