Transform only ascii numbers 0-9 to int

Asked

Viewed 34 times

0

I have the following code

string t_postfix = getPostfix();
Stack<int> operandos;    //pilha dos operandos
for(int i=0;i<n;i++){    //loop para pegar operando e realizar operações

        if(isdigit(t_postfix[i])){    // verifica se é um digito ou operando
            to_int = (t_postfix[i]-'0');
            operandos.push(to_int);    //em caso positivo salva na pilha operandos
        }
        else{    //em caso de um operador

            operandos.pop(v1);    //pega o primeiro elemento da pilha e remove


            operandos.pop(v2);    //pega o próximo elemento da pilha e remove

however when I use the values of v1 and v2(int), do an operation and return the result to the stack at the top, if the values are between 0-9 the answer comes in ascii but if it comes <0 or >9 ai comes normally as int, would have some way of I define which characters are the ones that have to switch from ascii to int (0-9) and the ones that don’t? because I’ve used

v1=v1-'0';

but then he changed the numbers on the accounts

  • For what reason you do: to_int = (t_postfix[i]-'0'); if you do not use the result for anything?

  • a yes I will fix but I take these values from a string so to ensure that the numbers will be saved as integers because it comes as a result of a function that must be returned in string

  • I didn’t understand it very well, but when you do if(isdigit(t_postfix[i])), then the character existing in t_postfix[i] is a decimal digit. Otherwise (if’s Else) is not a digit.

1 answer

0

by making Edit a writing error of mine as noted "to_int = (t_postfix[i]-'0')" and not using after, it worked out and fixed the problem, the above code posted is already working correctly after edited

Browser other questions tagged

You are not signed in. Login or sign up in order to post.