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

ou, youl pateits and your grand- , tle thee becomes more interesting as the numb

ID: 3748471 • Letter: O

Question

ou, youl pateits and your grand- , tle thee becomes more interesting as the number of generations increases An RPN calculator utilizes a scheme whereby each new numerical value is followed by the operation that is to be performed between the new value and its predecessor. (RPN stands for "reverse Polish notation.") Thus, adding two numbers, say 3.3 and 4.8, would require the following keystrokes: 11.71 3. 3 enter) 4.8 The sum, 8. 1, would then be displayed in the calcutator's single visible register RPN calculators make use of a stack, typically containing four registers (four components), as illustrated in Fig. 11.7. Each new number is entered into the X register, causing all previously entered values to be pushed up in the stack. If the top register (i.e, the T register) was previously occupied, then the old number will be lost (it will be overwritten by the value that is pushed up from the Z register) NULL (T register) (Z register) (Y register) (X register) Fig. 11.7 Arithmetic operations are always carried out between the numbers in the X and Y registers. The result of such an operation will always be displayed in the X register, causing everything in the upper registers to drop down one level (thus "popping" the stack). This procedure is illustrated in Figs. 11.8(a) to 11.8(c) for the addition of the values 3.3 and 4.8, as described above. Write an interactive C program that will simulate an RPN calculator. Display the contents of the stack after each operation, as in Figs. 11.8(a) to 11.8c) Include a provision for carrying out each of the

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct RPNcalc{
double value;
struct RPNcalc *next;
};

struct RPNcalc *createnewNode(int v){
struct RPNcalc *newnode = (struct RPNcalc*)malloc(sizeof(struct RPNcalc));
newnode->value = v;
newnode->next = NULL;
return newnode;
}
  
int main() {
  
// creating 4 registers as creating 4 nodes of linked list
struct RPNcalc* headX = createnewNode(0.0); //register X
struct RPNcalc* secondY = createnewNode(0.0); //register Y
struct RPNcalc* thirdZ = createnewNode(0.0); //register Z
struct RPNcalc* lastT = createnewNode(0.0); //register T
  
headX -> next = NULL; // next of X is NULL
secondY -> next = NULL; // next to Y is NULL
thirdZ -> next = NULL; // next to Z is NULL
lastT -> next = NULL; // next to T is NULL
  
/* now operations */
int n; // number of operations
scanf("%d", &n);
int index = 0; // index is for tracing at which position the first elment be pushed in next op.
while(n--)

{
double val;
char str[6];
scanf("%lf", &val);
scanf("%s", str);
// printf("%f ", val);
if(str[0] == 'e'){
int i = 0;
if(index == 0){
headX->value = val;
index++;
}
else if(index == 1){
headX->next = secondY;
secondY->value = headX->value;   
headX->value = val;
index++;
}
else if(index == 2)
{
secondY->next = thirdZ;
thirdZ->value = secondY->value;
secondY->value = headX->value;
headX->value = val;
index++;
}
else if(index >= 3)
{
thirdZ->next = lastT;
lastT->value = thirdZ->value;
thirdZ->value = secondY->value;
secondY->value = headX->value;
headX->value = val;
index = 4;
}
  
}
else if(str[0] == '+')
{
if(index < 2)
{
printf("not valid ");
}
else{
printf("%d ", index);
double result;
result = headX->value+secondY->value;
headX->value = result;
if(index == 2)
{
headX->next = NULL;
secondY->value = 0.0;
}
else if(index == 3)
{
secondY->next = NULL;
secondY->value = thirdZ->value;
thirdZ->value = 0.0;
}
else
{
thirdZ->next = NULL;
secondY->value = thirdZ->value;
thirdZ->value = lastT->value;
lastT->value = 0.0;
}

}

}
else if(str[0] == '-')
{
if(index < 2){
printf("not valid ");
}
else{
double result;
result = secondY->value - headX->value;
headX->value = result;
if(index == 2)
{
headX->next = NULL;
secondY->value = 0.0;
}
else if(index == 3)
{
secondY->next = NULL;
secondY->value = thirdZ->value;
thirdZ->value = 0.0;
}
else
{
thirdZ->next = NULL;
secondY->value = thirdZ->value;
thirdZ->value = lastT->value;
lastT->value = 0.0;
}
}
}
  
else if(str[0] == '*')
{
if(index < 2){
printf("not valid ");
}
else{
double result;
result = secondY->value*headX->value;
headX->value = result;
if(index == 2)
{
headX->next = NULL;
secondY->value = 0.0;
}
else if(index == 3)
{
secondY->next = NULL;
secondY->value = thirdZ->value;
thirdZ->value = 0.0;
}
else
{
thirdZ->next = NULL;
secondY->value = thirdZ->value;
thirdZ->value = lastT->value;
lastT->value = 0.0;
}
}
}
else if(str[0] == '/')
{
if(index < 2){
printf("not valid ");
}
else{
double result;
result = secondY->value*1.0/headX->value;
headX->value = result;
if(index == 2)
{
headX->next = NULL;
secondY->value = 0.0;
}
else if(index == 3)
{
secondY->next = NULL;
secondY->value = thirdZ->value;
thirdZ->value = 0.0;
}
else
{
thirdZ->next = NULL;
secondY->value = thirdZ->value;
thirdZ->value = lastT->value;
lastT->value = 0.0;
}
}
}
printf("value after operation at register X: %.2f ", headX->value);
printf("value of register y: %.2f ", secondY->value);
printf("value of register z: %.2f ", thirdZ->value);
printf("value of register t: %.2f ", lastT->value);
}
return 0;
}