Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

for some reason my GUI is not evaluating does this look familar to anyone so I c

ID: 641903 • Letter: F

Question

for some reason my GUI is not evaluating does this look familar to anyone so I can check it against the correct answer?

/**
     * Evaluates the expression and returns the result as a Double.
     *
     * PSEUDOCODE: Declare and create a Stack object named
     * operatorStack Declare and create a Stack object named
     * operandStack While mTokenQueue is not empty Do Declare and create a Token
     * object named token assigning getTokenQueue().dequeue() to it If token
     * instanceof Operand Then Push token onto the operand stack (type cast
     * token to Operand) ElseIf token instanceof LeftParen Then Push token onto
     * the operator stack (type cast token to LeftParen) ElseIf token instanceof
     * RightParen Then While the operator on the top of the operator stack is
     * not an instanceof LeftParen Do Call topEval(operatorStack, operandStack)
     * End While Pop the top operator from the operator stack -- removes the
     * LeftParen Else Declare Operator object named operator and assign token to
     * it (type cast to Operator) While keepEvaluating(operatorStack, operator)
     * is true Do Call topEval(operatorStack, operandStack) EndWhile Push
     * operator onto the operator stack End If End While While the operator
     * stack is not empty Do Call topEval(operatorStack, operandStack) End While
     * Pop the top Operand from the operand stack and return its value (call
     * getValue() on the Operand).
     */
    public Double evaluate() {
        Stack operatorStack = new Stack<>();
        Stack operandStack = new Stack<>();
        while (!getTokenQueue().isEmpty()) {
            Token token = getTokenQueue().dequeue();
            if (token instanceof Operand) {
                operandStack.push((Operand) token);
            } else if (token instanceof LeftParen) {
                operatorStack.push((LeftParen) token);
            } else if (token instanceof RightParen) {
                while (!(operatorStack.peek() instanceof LeftParen)) {
                    topEval(operatorStack, operandStack);
                }
                operatorStack.pop();
            } else {
                Operator operator = (Operator) token;
                while (keepEvaluating(operatorStack, operator)) {
                    topEval(operatorStack, operandStack);
                }
                operatorStack.push(operator);
            }
        }
        while (!operatorStack.isEmpty()) {
            topEval(operatorStack, operandStack);
        }
        return operandStack.pop().getValue();
    }

Explanation / Answer

#include #include #include #include #include #include class stack { public: double arr[100]; /* declaring the stack of a particular size */ int top; int start , full ; int valid; public: stack() { top = start = -1; /* initially the top and the starting point are same */ full = 100 ; /* the size of the array is initiallized to mark the end */ valid = 1; /* valid is used as a flag to mark wether overflow or underflow of the stack */ } void push(double a); double pop(); double oper(char ,double,double); }; void stack::push(double a) { if(top == full-1 ) { printf(" the stack is full cannot enter any more values"); } else { top++ ; arr[top] = a ; } } double stack::pop() { double i; if(top == start) { printf(" the stack is empty"); printf(" a garbage value is printed"); valid = 0; } else { i = arr[top]; top--; valid = 1; } if(valid == 1) { return i; } } double stack::oper(char c,double op1,double op2) { switch(c) { case '+': return(op1+op2); case '-': return(op1-op2); case '*': return(op1*op2); case '/': return(op1/op2); case '$': return(pow(op1,op2)); default : printf("illegal operation"); return 0; } } void main() { clrscr(); stack s; char exp[50] , i , pos=0; int flag = 1 , count = 0,k; double op1,op2; double value; printf(" enter you expression"); while((exp[pos++]=getchar()) != ' '); exp[--pos] = ''; printf(" the expession is %s",exp); for(k=0 ; exp[k] != '' ; k++) { if(isdigit(exp[k])) /* isdigit is used to check wether the character is a digit*/ s.push((double)(exp[k]-'0')); else { op1 = s.pop(); op2 = s.pop(); value = s.oper(exp[k],op1,op2); s.push(value); printf("%lf",value); } } getch(); }