我是JavaScript的初學者。JavaScript。插入Postfix
此代碼應該評估後綴表達式。 但它不起作用。我認爲代碼非常合乎邏輯,我不知道它有什麼問題。
如果有人能幫助我,我將不勝感激!
<html>
<head>
</head>
<body>
<!-- starting javaScript -->
<script type = "text/javascript">
var postStr = "123++"; //initial postfix expression
var postArr = new Array();
postArr = postStr.split(""); //string.split() turns string into array!
for(var i=0; i<postArr.length; i++)
document.write("value"+postArr[i]+"<br>");
var stack=[];
var result;
var firstNum;
var secNum;
//var k;
for(var i=0; i<postArr.length; i++)
{
if((postArr[i]!="^")||(postArr[i]!= "+")||(postArr[i]!= "-")|| (postArr[i]!= "*")||(postArr[i]!= "/"))
{
stack.push(postArr[i]);
//document.write("length" + stack.length);
}
else if((postArr[i]=="^")||(postArr[i]== "+")||(postArr[i]== "-")||(postArr[i]== "*")||(postArr[i]== "/"))
{
if(postArr[i]=='+')
{
firstNum=stack.pop();
secNum=stack.pop();
result = secNum + firstNum;
stack.push("result = " + result);
}
else if(postArr[i]=='*')
{
firstNum=stack.pop();
secNum=stack.pop();
result = secNum * firstNum;
stack.push("result = " + result);
}
else if(postArr[i]=='/')
{
firstNum=stack.pop();
secNum=stack.pop();
result = secNum/firstNum;
stack.push("result = " + result);
}
else if(postArr[i]=='-')
{
firstNum=stack.pop();
secNum=stack.pop();
result = secNum - firstNum;
stack.push("result = " + result);
}
}
}
var finalRes=stack.pop();
document.write(finalRes);
</script>
</body>
</html>
你知道哪個部件出問題了嗎? – Wiseguy