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

  • SEARCH
  • Home
  • 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 9197555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:06:00+00:00 2026-06-17T22:06:00+00:00

I was given a snippet of code to decipher, explain and offer any recommendations

  • 0

I was given a snippet of code to decipher, explain and offer any recommendations for improvement. I was told it works and we can’t run the code to test it. I pretty much understand it but just need to run it by someone to make sure what I do understand about it is correct and please get any help with explaining what I don’t understand.I have been doing a ton of research and still have some questions.

The code is implementing used to read a postfix expression that only uses multiplication and addition. Then evaluate the expression while saving the results onto a stack. Then it prints out the results. Operands are pushed onto the stack and then when it reads an operator, it pops the top 2 operands from the stack performs the calculation and stores the result back into the stack.

The program assumes that the integers and operators are delimited by some kind of character like a blank space or something, but doesn’t check the legality of the input at all.

 public static void main(String[] args)
{
    char[] a = args[0].toCharArray();
    int N =a.length;
    intStack s = new intStack();
    for (int i = 0; i<N; i++)
    {
        if (a[i]=='+')
        {
            s.push(s.pop() + s.pop());
        }
        if (a[i]=='*')
        {
            s.push(s.pop() * s.pop());
        }
        if ((a[i] >= '0') && (a[i] <= '9'))
        {
            s.push(0);
        }
        while ((a[i] >= '0') && (a[i] <= '9'))
        {
            s.push(10*s.pop() + (a[i++]-'0'));
        }
        Out.println(s.pop() + "");
    }
}

Post fix expression example: 2 3 5 + * = 16

I am confused when it comes to last if statement and the while loop.

So the first time it pushes a 0-9 number character it will store a # 0, then pop out that 0, multiple it by 10 and add it to the next number character (if there is one) that is converted into an int and push the result back into the stack?
If so, why is a 0 pushed onto the stack?

Shouldn’t it convert the first 0-9 numbered character to an int datatype, push it onto the stack and then go to the while loop?

then in the While loop, read the the array and keep converting 0-9 numbered characters to int datatypes and pushing them on the stack until a character is read that is different?

I also don’t see where it is incrementing int i in the while loop or breaking out of the while loop to advance to the next character?

  • 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-17T22:06:01+00:00Added an answer on June 17, 2026 at 10:06 pm

    The initial push of zero is a little confusing. The key to understanding what’s going on there is to observe that i is not incremented. When the code sees, say, '4', which is a first digit in "42", the if statement pushes zero; then the loop immediately pops it, multiplies by ten, adds 4, and pushes back. i is advanced to the next character (i.e. 2), and then the loop pops 4, multiplies by ten, adds 2, and stores back 42 – the desired result.

    There is an error in the code: when the expression ends in a number, you get an index out of bounds exception. For example, 42 is a valid postfix expression (having no operators is OK, right?) the inner loop would advance past the end of the string, causing an exception.

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

Sidebar

Related Questions

The code snippet is given as: char *s[] = {program, test, load, frame, stack,
I'm looking for code which can do the following. Given a snippet of XML,
Given the following code snippet below, using GetPropValue(MyComponent,'MySubComponent.Prop1') raises an EPropertyError exception. How can
Given this code snippet that can be readily pasted into Linqpad (or slightly modified
Given the following code snippet: $i= 11; function get_num() { global $i; return (--$i
I am creating a form in CakePHP.A snippet of the code is given below.
Given the below setup and code snippets, what reasons can you come up with
Why does following code snippet doesn't work? It doesn't gives any error, but also
The following code snippet works fine in 1.8.7 on Mac OS X, but not
Hi everybody and Merry Christmas. Please see this snippet of code. As you can

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.