Question

String hello = "44+"
int result=0, top=-1;
int []stack = new int[hello.length()];
for(int i=0 ; i<hello.length() ; i++)
{
    char c = s.charAt(i);
    if(c >= '0' && c <= '9')
        stack[++top]=(int)(c-'0');
    else
    {
        int x=stack[top--]; //pop
        int y=stack[top--]; //pop
        switch(c)
        {
            case '+' : result = x + y;
                break;
            case '-' : result = y - x;
                break;
            case '*' : result = x * y;
                break;
            case '/' : result = y / x;
                break;
            default : result = 0;
        }
        stack[++top]=result; //push
    }
}
result = stack[top--]; //pop
return result;

44+ stores 8 -> return result happens and when I print it on the main I get 8 as a output.. which is correct

if I replace this code

    stack[++top]=(int)(c-'0');

with this one

    stack[++top]=(int)(c);

the output looks like this = 104.. the second code looks correct to me but it doesn't give me the correct output

My questions are

  1. why c-'0' is used and not a c only ?
  2. why case '-' : result = y - x is used and not x - y ?
  3. why case '/' : result = y / x is used and not x / y ?

Thanks in advance

Was it helpful?

Solution

A string (for example "44+") contains a set of characters. Each character in a string is represented by a specific code or value. The string "ABC" is represented by the values 65,66,67 (in decimal). The string "ABC0123456789" is represented by the values 65,66,67,48,49,50,51,52,53,54,55,56,57 respectively. Therefore, to get the numerical value of the digit characters ('0' to '9') you have to subtract 48 from the character's code.

Regarding your 2nd and 3rd questions: Y is the first number pushed on the stack and X is the second number pushed on the stack. Since the operation is supposed to be between the first and second digits, it should be Y-X and Y/X (it should also be Y+X and Y*X but the order doesn't change the result in this case).

Note that this code won't work well with more than two digits or when the string isn't formatted exactly as in the example.

OTHER TIPS

48 is ASCII of '0'. The characters '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' have the ASCII values 48,49,50, ... , 57

Therefore you can convert the character of a digit to its value by subtracting 48 (which is the same as -'0')

c - '0' is the same as c- 48

c is character code, which in C are basically integer. So '0' is character zero, but it is also number 48 (ASCII code of zero). So, to convert a number character into a C language integer, code like c - '0' is often used.

To put it other way, char c = '0'; and char c = 48; are exactly equivalent in C (on computers, which use ASCII encoding).

About order of x and y, well, x was at the top of stack, in other words pushed last, and y was 2nd topmost, so which ever order you want to use... If you want to the normal order, that is string "yx/" means y / x, then you need to write it like that.

This code has bugs, one of them is:
the line:

char c = s.charAt(i);

should be:

char c = hello.charAt(i);

This code supposes to take a Postfix Expression and calculate the total

44+  is 4+4 

in order to calculate the total you should use a stack (which is demonstrated in the code above):

44+ is taken from the stack by two pop operations (x=4 and y=4) and the operations is the third pop +

Since the numbers are taken as chars from a string it needs to be converted to int, if you'll check the ascii table you'll see that '0' is 48 so when you take the char '4' which is 52 and calculate '4'-'0' you get 52-48 which is 4 as requested.

As for the order of the operations, according to Postfix Expression the operation should be done in one order:

xy+ --> x+y
xy- --> x-y
xy/ --> x/y
xy* --> x*y

doing it the other way (y/x for example) is a bug.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top