Question

In: Computer Science

Write in C++ programming language Given the following postfix string, from left to right, use the...

Write in C++ programming language

Given the following postfix string, from left to right, use the stack push operation to put items on the stack and the pop operation whenever an operator is encountered and to get two items and apply the operator to the operands then push back the result to evaluate the expression … rewrite the entire stack as it appears before each operator is applied.

12 3  / 5  + 32 2  3  ^  2  *  /  -

Solutions

Expert Solution

Here is your required code in C++

#include<bits/stdc++.h>

using namespace std;

class MyStack
{
double stack[100];
int top;

public:   
//constructor
MyStack()
{
top=-1;
}
  
//add element to stack
void push(double data)
{
top++;
stack[top] = data;
}
  
//remove element from stack
double pop()
{
return stack[top--];
}
  
//get top element from stack
double peek()
{
return stack[top];
}
};


int main(int argc, char *argv[])
{
MyStack stack;
string line;
// Taking inputs
cout<<"\nEnter postfix expression: \n";
getline(cin, line);
  
stringstream expression(line);
string token;
while (expression >> token) {
if (isdigit(token[0])) {
stack.push(atof(token.data()));
}
else if (token == "+") { // Addition Operation
double x = stack.pop();
double y = stack.pop();
stack.push(y + x);
}
else if (token == "-") { // Subtraction Operation
double x = stack.pop();
double y = stack.pop();
stack.push(y - x);
}
else if (token == "*") { // Multiplication Operation
double x = stack.pop();
double y = stack.pop();
stack.push(y * x);
}
else if (token == "/") { // Division Operation
double x = stack.pop();
double y = stack.pop();
stack.push(y / x);
}
else if(token == "^"){ // Power Operation
double x = stack.pop();
double y = stack.pop();
stack.push(pow(y, x));
}
}
  
// Displaying result
cout<<"\nExpression evaluation result: \n";
cout << stack.peek()<<"\n";

return 0;
}


Please refer to the screenshot of the code to understand the indentation of the code:


Here is the Output for the sample Test Cases:


Related Solutions

in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword...
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword Cipher algorithm. Note: The encryption only works on alphabets. Numbers and symbols such as 1 to 9, -, &, $ etc remain unencrypted. Input:     Zombie Here     secret     where: First line represents the unencrypted string S. Second line represents the keyword string K. Output:     ZLJEFT DTOT Explanation: We used "secret" keyword there. Plain Text: A B C D E F G H I J K...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP...
1a) Write a program in C programming language to determine *pass* or *fail*. Use the GP ( 0.00 - 1.49 -> fail 1.50 - 4.00 -> pass ) 1b) Write a program in C to display month in Islamic Calendar.
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
The C language permits a variable to be left- or right-shifted by a nonconstant amount (e.g....
The C language permits a variable to be left- or right-shifted by a nonconstant amount (e.g. i >> j where i and j are variables), but the MIPS instruction set only supports shifts by a constant value (e.g. i >> 2). In words rather than code, describe how a variable-length right shift could be performed using MIPS instructions.
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT