Question:
RPN calc help, c programming!?
NyQuil
2013-07-30 04:43:17 UTC
I need help with making RPN calculator in c programming that shows the step by step on how it is solve.

for example.
Please enter postfix: 1 2 3 + - 4 5 / *
and once I enter, the output should be like this:
2 + 3 = 5
1 - 5 = -4
4 * 5 = 20
-4 / 20 = -0.2

The one that I made only shows the final result.
I need to know the flow or the algorithm on how I should code this.
Many thanks to those who can help!
Three answers:
husoski
2013-07-30 05:25:06 UTC
Make yourself a stack. Lots of problems get simpler with the right data structure.



Orgainize code however you like, or however your instructor likes is more to the (grade) point, but I like to define subroutines before they are used ("main-at-the-bottom" style) and keep all definitions that go together in the same part of the source file.



/* Value stack implementaiton */



#define VALSTACK_SIZE 100 /* at most 100 operands waiting for postfix operators */



double valstack[VALSTACK_SIZE];

int valstack_index=0;



/* push x onto the stack */

void push_value(double x)

{

assert(valstack_index < VALSTACK_SIZE);

valstack[valstack_index++] = x;

}



/* pop a value from the stack and return it */

double pop_value(void)

{

assert(valstack_index > 0);

return valstack[--valstack_index;

}



/* return true (nonzero) if stack is empty */

int stack_empty()

{ return valstack_index == 0; }





To use the assert macro, you need to #include . assert() is a good way to insert statements that do nothing if your program is running correctly, but halt with an error if not. If the argument is true, no action is taken, otherwise you get an error message and a halt. I used those to prevent pushing too many values or popping values from an empty stack. A stack removes values in last-in-first-out (LIFO) order.



When your code sees a number, call push_value(x).



When your code sees the / operator:



y = pop_value();

x = pop_value();

assert (y != 0.0); /*TODO: Add a friendly error message here. */

z = x / y;

push_value(z);

printf("%g / %g = %g\n", x, y, z);



Notice the order: y (the second operand) is popped first because it was pushed last. (LIFO)



Also notice how assert can be used. You can halt on an error condition to give basic functionality during testing, but flag the source as a reminder that there's still work to be done. Comments beginning with TODO are easy to search for, and commonly used for that purpose. Some IDEs will automatically list those for you.
?
2016-12-08 19:52:29 UTC
Rpn Calculator C
Jonathan
2013-07-30 12:25:54 UTC
If you want to live dangerously, the following code will work. There are no checks for errors of any kind. It assumes you are accurate and not ornery. But it gets the point across about the structure. You can add error checking, easily. Adding error checks will increase the size of the code and reduce its readability, which is why I simply avoided doing any here since I was focused on showing you the basic conceptual structure. Take careful note of the use of 'continue' and 'break' in the for/switch() structure.



#include

#include

#include

int main( void ) {

    char buf[200], c, *w, p= 0;

    double stack[50], a, b;

    static const char *d= " \n\r\t";

        printf( "Please enter postfix: " );

        fgets( buf, sizeof(buf), stdin );

        for( w= strtok( buf, d ); w != NULL; w= strtok( NULL, d ) ) {

            switch ( c= *w ) {

            case '+':

                b= stack[--p]; a= stack[--p]; stack[p++]= a + b;

                break;

            case '-':

                b= stack[--p]; a= stack[--p]; stack[p++]= a - b;

                break;

            case '*':

                b= stack[--p]; a= stack[--p]; stack[p++]= a * b;

                break;

            case '/':

                b= stack[--p]; a= stack[--p]; stack[p++]= a / b;

                break;

            default:

                stack[p++]= strtod( w, NULL );

                continue;

            }

            printf( "%lg %c %lg = %lg\n", a, c, b, stack[p-1] );

        }

    return 0;

}



See link below for a run using your input.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Search results for 'RPN calc help, c programming!?' (Questions and Answers)
3
replies
C programming help, RPN calc?
started 2013-08-02 12:27:29 UTC
programming & design
Loading...