Scampada,
примерно так без скобок и синусов
function calc(str)
{
str = str.replace(/\s+/g, '');
var re = /(\d+(\.\d+)*)\!/;
for (; re.test(str);) {str = str.replace(re,
function (a, b)
{
var temp = 1;
b = parseInt(b);
while(b) temp *= b--;
return temp.toFixed(10).toString();
}
)
};
re = /(\d+\.?\d*)\^(\d+\.?\d*)/;
for (; re.test(str);) {str = str.replace(re,
function (a, b, c)
{
return Math.pow(b, c).toFixed(10).toString();
}
)
};
re = /(-*\d+\.?\d*)\/(-*\d+\.?\d*)/;
for (; re.test(str);) {str = str.replace(re,
function (a, b, c)
{
return (b / c).toFixed(10).toString();
}
)
};
re = /(-*\d+\.?\d*)\*(-*\d+\.?\d*)/;
for (; re.test(str);) {str = str.replace(re,
function (a, b, c)
{
return (b * c).toFixed(10).toString();
}
)
};
re = /(-*\d+\.?\d*)\-(-*\d+\.?\d*)/;
for (; re.test(str);) {str = str.replace(re,
function (a, b, c)
{
return (b - c).toFixed(10).toString();
}
)
};
re = /(-*\d+\.?\d*)\+(-*\d+\.?\d*)/;
for (; re.test(str);) {str = str.replace(re,
function (a, b, c)
{
return (+b + +c).toFixed(10).toString();
}
)
};
return str
}
alert([calc('2^2^2'),calc('2^2 + 2! -3^1'),calc('-5 + -5')])
|