-1

I have a Parse program that parsing arithmetic expressions in the expression class there is a function parse here is it;

//function parse
void express::parse(){
    char ch;
    char lastVal;
    char lastop;

    for(int j=0;j<len;j+=1){
        ch = pStr[j];

        if(ch>= '0' && ch <= '9'){
            s.push(ch - '0');
        }else if(ch =='+' || ch == '-' || ch == '/' || ch == '*'){
            if(s.getTop() == 1){
                s.push(ch);
            }else{
                lastVal = s.pop(); // number
                lastop = s.pop(); // operator
                //if ch is * or /  && lastop + or -
                if((ch == '*' || ch == '/') && (lastop == '+' || lastop == '-')){
                    s.push(lastop);
                    s.push(lastVal);
                
                }else{
                    switch(lastop){
                    case '+': s.push(s.pop() + lastVal);break;
                    case '-': s.push(s.pop() - lastVal);break;
                    case '*': s.push(s.pop() * lastVal);break;
                    case '/': s.push(s.pop() / lastVal);break;
                    default: cout << "Unkowen Number"; exit(1);break;
                    }//end switch
                }//end if
                s.push(ch);
            }//end if 
        }//end if
    }//end for
};

and the s.pop() that is a stack class to hold the character whether it a number or an operator and to help me make the operation first of the '*' , '/' so I need to know what is the that statement means s.push(ch - '0');? is it about the ASCII code or what i don't know but when I erase it the result change.

shehab ahmed
  • 29
  • 1
  • 6
  • 1
    If `ch` is a numeric digit (`'0'` .... `'9'`) it converts that digit to the corresponding numerical value (`'0'` to `0`, `'1'` to `1`, ... `'9'` to `9`). If `ch` is not a numeric digit, the result is implementation defined. – Peter Jul 19 '20 at 11:02

2 Answers2

1
s.push(ch - '0');
  • ch here is a character of the input string. It is of type char which in C++ is an integral type (having a width of CHAR_BITS bits, which is 8 bits on almost every system you would encounter nowadays), so you can do arithmetic operations on it.
  • '0' is a character representing digit zero "0".
  • ch - '0' converts a character to a corresponding integer number. '0' -> 0; '1' -> 1 and so on. It uses the fact that C++ standard requires digits to occupy sequential codes, see [lex.charset/3]: "In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous." In ASCII character encoding digits occupy sequential codes (see https://en.wikipedia.org/wiki/ASCII) starting from 48: character '0' has code 48, '1' has code 49, etc.
  • s.push() pushes a resulting integer onto a stack.
Ilya Popov
  • 3,765
  • 1
  • 17
  • 30
1

What you are doing by ch - '0' is that you are effectively converting the character (e.g. '9') to the actual numerical value (the number 9)!

Hossein
  • 24,202
  • 35
  • 119
  • 224