Pregunta

I am a beginner in JavaScript.

This code is supposed to evaluate a postfix expression. But it doesn't work. I think that the code is very logical and I don't really know what's wrong with it.

If someone could help me, I would be grateful!

<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>
¿Fue útil?

Solución

There is some errors :

  1. your first condition should be separated by and (&&) operators instead of or since you are using negative condition

  2. since you convert string to numbers don't forget to convert the characters into integers (with something like +postArr[i])

  3. you should push the result and not a string with a comment (stack.push(result) and not stack.push("result = " + result)).

See this jsfiddle : http://jsfiddle.net/scaillerie/GwDTM/2/.

Otros consejos

I'm not sure if it's the only problem but the condition this code:

if((postArr[i]!="^")||(postArr[i]!= "+")||(postArr[i]!= "-")|| (postArr[i]!= "*")|| postArr[i]!= "/"))

Will always evaulate to true because postArr[i] will always not be one of the options. You probably want something like:

var ch = postArr[i];

if (! ((ch == "^") || (ch == "+") || (ch == "-")…

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top