Thread: C help with this reverse Polish Notation algorithm

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    2

    Question C help with this reverse Polish Notation algorithm

    some sums are going wrong like 321--
    ------------------------------------------------------------
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #define limit 10
    
    
    int main() {
        const char cont[100];
        char temp[1], temp2[2];
        float pilha_numeros[limit], pilha_numeros_final[limit], result;
           int i, a = 0, what_byte, other_byte;
        bool check = false;
        float pilha_numero_int[100];
    
    
        printf("INSERT: ");
        scanf("%s", &cont);
    
    
        for(i = 0; i < strlen(cont); i++) {
    
    
            if(isdigit(cont[i])) {
                //printf("now : %f\n", cont[i]);
                pilha_numeros[a] = cont[i];
                temp2[0] = pilha_numeros[a];
                pilha_numero_int[a] = atof(temp2);
                printf("%d= %f\n", (i+1),pilha_numero_int[a]);
                a++;
            } else {
                what_byte = i - a +1;
                if(check) {
                    other_byte = 0;
                } else {
                     other_byte = what_byte - 1;
                }
                switch(cont[i]) {
                    case('+'):
                        //printf("\n        first = %f[%f] | second = %f", pilha_numero_int[qual_byte], qual_byte, pilha_numero_int[other_byte]);
                        if(check) {
                            result = pilha_numero_int[what_byte] + pilha_numeros_final[other_byte];
                        } else {
                            result = pilha_numero_int[what_byte] + pilha_numero_int[other_byte];
                        }
                        pilha_numeros_final[0] = result;
                        //printf("\ntemp result = %f", result);
    
    
                    break;
                    case('-'):
                        if(check) {
                            result = pilha_numero_int[what_byte] - pilha_numeros_final[other_byte];
                        } else {
                            result= pilha_numero_int[what_byte] - pilha_numero_int[other_byte];
                        }
                        pilha_numeros_final[0] = result;
                    break;
                    case('*'):
                        if(check) {
                            result = pilha_numero_int[what_byte] * pilha_numeros_final[other_byte];
                        } else {
                            result = pilha_numero_int[what_byte] * pilha_numero_int[other_byte];
                        }
                        pilha_numeros_final[0] = result;
                    break;
                    case('/'):
                        if(check) {
                            result = pilha_numero_int[what_byte] / pilha_numeros_final[other_byte];
                        } else {
                            result = pilha_numero_int[what_byte] / pilha_numero_int[other_byte];
                        }
                        pilha_numeros_final[0] = result;
                    break;
                }
                check = true;
            }
        }
    
    
        printf("\n\nResult = %f\n", pilha_numeros_final[0]);
    }
    Last edited by Kojin; 03-31-2018 at 05:12 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,121
    > const char cont[100];
    Why is this const?

    > char temp[1], temp2[2];
    Temp1 is never used, and temp2 has no obvious \0 marking the end of the string, which will make your atof return garbage.

    All your check, whatbyte and otherbyte are unnecessary.
    RPN is a stack with only two operators push and pop.

    Each number pushes onto the stack.
    Each operator pops two numbers and pushes the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2018
    Posts
    2
    i'm trying to make this using only isdigit and atof, without pop and push

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,121
    Push is simply
    stack[pos++] = value;

    Pop is
    value = stack[--pos];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing to Reverse Polish Notation
    By patrink in forum C Programming
    Replies: 14
    Last Post: 07-09-2011, 01:40 AM
  2. Reverse Polish Notation Calculator
    By jazzyqueen in forum C Programming
    Replies: 1
    Last Post: 09-02-2009, 03:55 AM
  3. Reverse Polish Notation
    By Lucid15 in forum C Programming
    Replies: 7
    Last Post: 02-16-2009, 10:05 PM
  4. infix vs Reverse Polish notation
    By dionys in forum C Programming
    Replies: 1
    Last Post: 05-10-2004, 02:25 PM
  5. Reverse Polish Notation???
    By Wizard_D in forum C Programming
    Replies: 6
    Last Post: 11-01-2001, 12:20 AM

Tags for this Thread