Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that works as a simple RPN (Reverse Polish Notation) calculator.

ID: 3573718 • Letter: W

Question

Write a program that works as a simple RPN (Reverse Polish Notation) calculator. RPN calculators work slightly different than normal calculators. Because they work on the model of a stack, it is never necessary to use parenthesis to express a mathematical equation, regardless of how complex the equation is. Many scientists, mathematicians and engineers consider RPN to be a much better model for using a calculator than the more common calculator model, which uses parenthesis and equal signs.

The main concept to understand about how RPN calculators work is that they use a stack (last in, first out) and postfix binomial operators. As new data is entered, it is pushed onto the top of the stack, pushing the previously entered data further down the stack. Binomial operators are entered in postfix manner after the two numbers have been entered. When an operator value is entered, the top two values on the stack are used to determine a result, which is stored at the top of stack.

To add the numbers 4 and 5 to get a sum of 9, with an infix calculator, one would enter:

On an RPN calculator, this would be expressed as:

When the [enter] key is pressed, the 4 is pushed onto the stack. The 5 is then put at the top of the stack. When the calculator processes the +, it takes the top two stack values (4 and 5) adds them together and stores a 9 at the top of the stack. Whatever is the top stack value is what is displayed on the screen.

Note

For the input of a computer to work exactly like a real calculator, the input would need to be processed in raw mode rather than the normal buffered, also called cooked, mode. We will not tackle that complexity for this problem, so for the above example, the keys pressed on the keyboard would need to be as follows:

Data should be read from the keyboard as strings. If the data entered is one of { +, -, *, /}, then the operation of add, subtract, multiply or divide should be performed on the top two stack values with the result being stored to the stack. Entered numeric data should be pushed onto the stack as a floating point number. Any other data (e.g., text characters), should be treated as a user error with an error message displayed. The special sentinel key of a grave accent ` should used to cause the program to exit.

The stack should be implemented using a linked list. Depending on your implementation, you may wish to implement the following functions before you can really begin implementing the calculator.

Based on code in the study guide, here is some code to get you started working with a stack. stack.c

Hint

Use the ReadLine function to reliably read in the data. (String Example - ReadLine)

It may simplify your programming if you use a fixed Stack data structure to always hold the top value. The next pointer of this top structure can point to the top of the actual stack.

See Example Use of strtod and strtol. You will want to use this example function to convert the string data to doubles and to determine if numeric data was entered or not.

Function Use void *push( Stack *, double ); Add a number to the top of the stack double pop( Stack * ); Return and remove the top stack item double get( Stack * ); Return the top stack item void replace(Stack *, double); Replace the top stack value void show( Stack * ); Display the top stack value Whee a program that works as a simple RPN Roverse Polsh Notaton) calculator RPN calculators work sighty diferentthan nommal caicuators Because they work on the modol or a stack, tis never necessary to use parenthesis to express a mathematical equation rogardless of how complex the equation is Many scientists, mathematicians and engineers consider RPN to be a much bener model for using a calculator than the more common calculator model which uses parenthesisand equal signs The main concept to understand about how RPN calculators workisthat they use a stack last in, test out) and postfix binomial operators As new data s entered, pushed onto the top of the stack pushing the previously entered data further down the stack Binomial operators are entered inpostfak manner aner the two numbers have beenentered When an operator values entered the top two valueson the stack ate used to determine a resut whichis stored at the top of stack To add the numbers 4 and 5togetasum of 9 with aninfix calculator, one would enter On an RPN calculator this would be expressed as When the key is pressed the aispushed onto the stack. Thes is then put at the top ofthe stack When the calculator processes the .ttakes the top two stack values (4 and 5)adds them together and stores a9 at the top of the stack Whalover s the top stack valuais what displayed on the screen Note: For the input of a computer to workexactylke a real calculator the input would noed to be processed in raw mode rather than the normal buffered also called cooked mode We wilnot tackdo that complexity forthis problem, so for the above example, the keys pressed on the koyboard would need to be as follows Data should be read from the keyboard as strings the data entered is one of then the operation of add subtract multiply or divide should be performed on the top two stack values with the result being stored to the stack Entered numeric data should be pushed onto the stack as a foatng pontnumber Any other data (e g.text characters), should be treated as a user emor wth an error message displayed The special sentnolkey of a grave accent should used to cause the program to exit The stack should be implemented using a inked list Depending on your implementaton, you may wish to impiementthefolowing tunctions before you can realy begin implamenting the calculator Function void push Stack double) Add a number to the top of stack double pop(Stack Return and removethe top stack double get Stack Return the top stack term void replace(Stack double) Replace the top stack value void show Stock Display the 10p stack value Based on code in the study guide, here ssome code to get you started working with a stack itaoue 1. Use the Readline functon toreiably read in the dala Shing Example ReadLine) 2 may simply your programming if you use a fixed Stack data structure to always holdthe top value The next pointer of this top structure can point to the top of the actual stack 3 See Example Use of strtod and strtol You will want to use this example functon to convert the strng data to doubles and to determineinumeric data was entered or not

Explanation / Answer

Code:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>


typedef struct stack
{
double data;
struct stack * next;
}Stack;


Stack *push(Stack *st,double d)
{

Stack *tmp;

if(st==NULL)
{
   st= (Stack *) malloc(sizeof(Stack));
   st->data=d;
   st->next=NULL;
}
else
{
   tmp=(Stack *)malloc(sizeof(Stack));
   tmp->data=d;
   tmp->next=st;
   st=tmp;
   printf("st->next->data:%f ",st->next->data);
}

printf("Pushed value is:%f ",st->data);

return st;

}

double pop(Stack ** st){

Stack * tmp;
double i;
   if(*st==NULL)
   {
       printf("Stack is empty ");
       return -1;
   }

tmp=*st;
*st=(*st)->next;

i=tmp->data;
free(tmp);

return i;
}

double get(Stack *st){

if(st == NULL)
{
   printf("Nothing to get. Stack is empty ");
   return -1;
}


return st->data;
}

void replace(Stack *st, double d)
{
Stack *tmp;

if(st==NULL)
{
   st=(Stack *)malloc(sizeof(Stack));
   st->next=NULL;
   st->data=d;
}
else
{
   st->data=d;
}
}

void show(Stack * st)
{

printf("Stack top value is:%f ", get(st));
}


int main(void)
{

Stack *st=NULL;
double d,d2,d3;
char ch[20];
char *ptr;

printf("Enter expression ");
while(1)
{
   scanf("%s",ch);
   d=strtod(ch,&ptr);

   if(d!=0.0)
   {
   printf("The entered data is:%f ",d);
   st=push(st,d);
   continue;
   }
   else if(ch[0]=='`')
   break;
   else if(isalpha(ch[0]))
   {
   printf("Please enter numeric digits or +,-,*,/,` characters only ");
   continue;
   }

   printf("Operation %c ", ch[0]);
   switch(ch[0]){
   case '+':
       d2=pop(&st);
       d3=pop(&st);

       d=d2+d3;

       st=push(st,d);
       printf("The value after operation is:%f ",get(st));
       break;
   case '-':
       d2=pop(&st);
       d3=pop(&st);

       d=d2-d3;

       st=push(st,d);
       printf("The value after operation is:%f ",get(st));
       break;
   case '*':
       d2=pop(&st);
       d3=pop(&st);

       d=d2*d3;

       st=push(st,d);
       printf("The value after operation is:%f ",get(st));
       break;
   case '/':
       d2=pop(&st);
       d3=pop(&st);

       d=d2/d3;

       st=push(st,d);
       printf("The value after operation is:%f ",get(st));
       break;
      
   }
}
return 0;
}

Output:

Enter expression
4
The entered data is:4.000000
Pushed value is:4.000000
5
The entered data is:5.000000
st->next->data:4.000000
Pushed value is:5.000000
/
Operation /
Pushed value is:1.250000
The value after operation is:1.250000
6
The entered data is:6.000000
st->next->data:1.250000
Pushed value is:6.000000
12
The entered data is:12.000000
st->next->data:6.000000
Pushed value is:12.000000
+
Operation +
st->next->data:1.250000
Pushed value is:18.000000
The value after operation is:18.000000
-
Operation -
Pushed value is:16.750000
The value after operation is:16.750000
4
The entered data is:4.000000
st->next->data:16.750000
Pushed value is:4.000000
8
The entered data is:8.000000
st->next->data:4.000000
Pushed value is:8.000000
*
Operation *
st->next->data:16.750000
Pushed value is:32.000000
The value after operation is:32.000000
/
Operation /
Pushed value is:1.910448
The value after operation is:1.910448

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote