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.
Aucun commentaire:
Enregistrer un commentaire