Question

In: Computer Science

Use stack to evaluate postfix expressions : 5 4 2 1 + 6 * + 3...

Use stack to evaluate postfix expressions :
5 4 2 1 + 6 * + 3 + *

Solutions

Expert Solution

Lets use simple C Programming

CODE:

#include<stdio.h>
int stack[20];
int top = -1;
void push(int x){
   stack[++top] = x;
}
int pop(){
return stack[top--];
}
int main(){
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0'){
if(isdigit(*e)){
num = *e - 48;
push(num);
}
else{
n1 = pop();
n2 = pop();
switch(*e){
   case '+':{
   n3 = n1 + n2;
   break;
   }
   case '-':{
   n3 = n2 - n1;
   break;
   }
   case '*':{
   n3 = n1 * n2;
   break;
   }
   case '/':{
   n3 = n2 / n1;
   break;
   }
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}

SCREENSHOT OF THE CODE:

OUTPUT:

Thank you. Please ask me if you have any doubt.


Related Solutions

ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5...
ID X Y 1 2 3 2 3 6 3 4 6 4 5 7 5 8 7 6 5 7 7 6 7 8 8 8 9 7 8 10 12 11 Test the significance of the correlation coefficient. Then use math test scores (X) to predict physics test scores (Y).  Do the following: Create a scatterplot of X and Y. Write the regression equation and interpret the regression coefficients (i.e., intercept and slope). Predict the physics score for each....
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Find mean, median, mode, variance, standard deviation, coefficient of variation, range, 70th percentile, 3rdquartile of the data and skewness and define what each of these statistics measure. For example, mean is a measure of the central tendency, what about the rest? Use Chebyshev’s rule to find...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4...
[4 5 5 2 4 4 6 3 3 7 5 3 6 3 4 4 6 5 4 5 3 7 5 5 4 2 6 5 6 6] This is my dataset Split the dataset in two equal parts. You have 30 datavalues. If you split the data in two equal parts each part will contain 15 data values.  Call the first part Y and second part X.Draw scatter plot of the 2 datasets, X being on the horizontal...
The following postfix expression 2 6 + 3 5 - / evaluates to Group of answer...
The following postfix expression 2 6 + 3 5 - / evaluates to Group of answer choices -1 1 -4 4 this is not a postfix expression In an array implementation of the StackInterface where push() and pop() are O(1), the top of the stack is Group of answer choices the first entry in the array the last occupied entry in the array the second entry in the array the entry before the last ocuppied entry in the array the...
Convert the expression into postfix notation 19 + 2 ∗ 5  + (1 - 6/(1 ∗ 2))
Convert the expression into postfix notation 19 + 2 ∗ 5  + (1 - 6/(1 ∗ 2))
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3...
tens Units 1 5 2 3 4 8 5 2 5 6 9 6 1 3 5 4 7 9 7 0 0 4 5 6 9 9 8 1 3 5 6 8 9 9 0 1 2 3 5 9 The table represent a random sample of 31 test scores taken from a large lecture class. Find the following [round to 2 decimal points X. XX] a) [2 pts] Find the 5 number summary [L, Q1, Q2, Q3,...
Matrix A2= [1 2 3; 4 5 6; 7 8 9; 3 2 4; 6 5...
Matrix A2= [1 2 3; 4 5 6; 7 8 9; 3 2 4; 6 5 4; 9 8 7] Note: TA2 is defined to be a linear transformation that maps any vector x to A2* x. That is TA2 = A2*x. Also the range of the Linear transformation represented by A2 is the same as the column space of A2. l) Find a basis for the null(TA2). m) Find nullity of A2, TA2 and A2tA2. n) Find rank(A2), rank(A2t),...
X 1 3 5 3 4 4 Y 2 5 4 3 4 6 A: Plot...
X 1 3 5 3 4 4 Y 2 5 4 3 4 6 A: Plot the date B: find the line of best fit C: determine ŷ AT x=3 D: Find r and r^2 E: explain r and r^2
2. Consider functions f : {1, 2, 3, 4, 5, 6} → {1, 2, 3, 4,...
2. Consider functions f : {1, 2, 3, 4, 5, 6} → {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. (a) How many of these functions are strictly increasing (i.e. f(1) < f(2) < f(3) < f(4) < f(5) < f(6))? Hint: How many different possibilities are there for the range of f? For each range of f, how many strictly increasing functions are there? (b) How many of these functions are non-decreasing (i.e. f(1) ≤ f(2) ≤...
Year Promisedcash flows Expectedcash flows 1 6 5 2 6 5 3 6 5 4 6...
Year Promisedcash flows Expectedcash flows 1 6 5 2 6 5 3 6 5 4 6 5 5 6 5 6 6 5 7 6 5 8 6 5 9 6 5 10 106 95 What is the present value today of promised cash flows at 6%? If the price of the bond is 80, what is the yield to maturity of the bond using the promised cash flows? What is the present value today of the expected cash flows...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT