Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8680785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:20:20+00:00 2026-06-12T21:20:20+00:00

Here is my code so far: public class PostfixCalculator { private Stack<Float> stack; private

  • 0

Here is my code so far:

public class PostfixCalculator {

   private Stack<Float> stack;
   private float result;

   private Boolean isOperator (char op){
    boolean operator;
    switch (op){
        case '+':
        case '-':
        case '*':
        case '/':
        case '^':
            operator = true;
            break;
    default:
        operator = false;
        break;}
    return operator;
}

private Boolean isFunction (String func){
    String[] functions = {"sin", "cos", "max"};
    for (int i=0; i<functions.length; i++){
        if (func.equals(functions[i]))
            return true; }
    return false;
}

private void computeOperator (float op1, float op2, char op){
    switch (op){
        case '+': 
            result = op1 + op2;
            break; 
        case '-': 
            result = op1 - op2;
            break;
        case '/': 
            result = op1/op2;
            break;
        case '*': 
            result = op1 * op2; 
            break;
        case '^': 
            result = (float) Math.pow(op1, op2);
            break;

        default:
            break;
    }   
}

public float calculate(String expr) throws IllegalArgumentException {
    result = 0;
    String token;
    Float makeit;
    char operator;
    float op1, op2, pushFloat;
    StringTokenizer calc=new StringTokenizer(expr);

    stack = new Stack<Float>();

    while (calc.hasNextToken()){
        token=calc.getNextToken();
        operator=token.charAt(0);

        if (!(this.isOperator(operator))){
            if (this.isFunction(token)){
                if (token.equals("sin")){
                    op1=stack.pop();
                    result = (float) Math.sin(op1);
                    stack.push(result);
                }
                else if (token.equals("cos")){
                    op1=stack.pop();
                    result = (float) Math.cos(op1);
                    stack.push(result);
                }
                else if (token.equals("max")){
                    op1=stack.pop();
                    op2=stack.pop();
                    result=Math.max(op1, op2);
                    stack.push(result);
                }

            }
            else {
                makeit = new Float(token);
                pushFloat = makeit.floatValue();
                stack.push(pushFloat);
            }

        }
        else {
            op1 = stack.pop();
            op2 = stack.pop();
            computeOperator (op1, op2, operator);
            stack.push(result);


        }
    }
    return stack.pop();
}

}

I think I have the basics of it down, but how do I deal with postfix calculations with three digits in a row or more, like for example ‘2 3 4 * -‘? Any help would be appreciated. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:20:21+00:00Added an answer on June 12, 2026 at 9:20 pm

    Yep, a stack. And one can implement a simple stack easily with an array and a counter — no real need to use a fancy class.

    The array would be as large as the most deeply nested expression that you can handle (10 elements should be sufficient for virtually all “real” problems).

    Push is update A[i] and increment i. Pop is decrement i and reference A[i]. “8 9 +” is push(8), push(9), pop the top two elements, add them, push the result. Note that operators are never pushed, only operands/results.

    ‘2 3 4 * -‘ would be push(2), push(3), push(4), pop top two, multiply, push result, pop two, subtract, push result.

    “7 6 + cos” would be push(7), push(6), pop two, add, push result, pop one, perform “cos”, push result.

    Error if you need to pop an element and the counter is zero (eg, “7 +” — you’d want to pop twice but you can only pop once). Also an error if the user expresses “finished” and there is more than one element in the stack.

    Always “display” your top element, as that’s the last value pushed or the result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
Here is the C++ code public class First { public: virtual int firstmethod(int a,
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
Ok. here's the operations i successfully code so far thank's to your help: Adittion:
I'm a total newb to LINQ. Here is the code I have so far:
Here is what I got so far. Please read the comment in the code.
The script is on jsfiddle here : CODE What it does at the moment
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code my jqgrid editing through form. $(#DataEnergy).jqGrid('navGrid', '#pagergrid', {}, //options {editdata: {
Here my code, I need to insert into mysql database I have mysql,php,android 1)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.