<SCRIPT>
//string.split(search).join(replace) -- string.replace(search, replace)
var operators = Array(
"+", "-", "*", "/", "^"
);
function Calc(str){
//Раскрытие скобок:
var pos1 = 0;
while( pos1 || (pos1 = str.indexOf("(")) != -1){
var pos2 = str.indexOf(")",++pos1);
if(pos2>0){
var noBrackets = str.substring(pos1,pos2);
pos1 = str.indexOf("(",pos1);
if(pos1 > pos2 || pos1 < 0){
str = str.split("("+noBrackets+")").join( Calc_noBrackets(noBrackets,0) );
pos1 = 0;
}
}else{
trace("Error: No ending ')' !");
return;
}
}
return Calc_noBrackets(str,0);
}
function Calc_noBrackets(str,opId){
str = str.split("--").join("+");
str = str.split("+-").join("-");
var args = str.split(operators[opId]);
var result = 0;
for(var argId=0;argId<args.length;argId++){
var arg = args[argId];
for(var nextOpId=opId+1;nextOpId<operators.length;nextOpId++)
if(arg.indexOf(operators[nextOpId])>0){
arg = Calc_noBrackets(arg,nextOpId);
break;
}
arg = new Number(arg);
if(argId==0)
result = arg;
else switch(operators[opId]){
case "^": result = Math.pow(result,arg); break;
case "*": result *= arg; break;
case "/": result /= arg; break;
case "+": result += arg; break;
case "-": result -= arg; break;
}
}
return result;
}
</SCRIPT>
JavaScript to Visual C++
Mojete li vi etot primer napisat'dlya Visual C++.
Eto ochen srochno nujen mne.
Код:
Eto kalkulyator.