Question

Implement the array-based stack class. Use it in the client code to convert and compute post-fix...

Implement the array-based stack class. Use it in the client code to convert and compute post-fix expressions. The postfix expressions are saved in a file named "postfix.txt", one expression each line. For simplicity, you may assume all numbers in the expressions are single digit numbers.

Submit:
1. Source code: the stack class and the client code;
2. Output screen capture: show the results for the following expressions (The errors in some expressions are intentional. Your code must detect it and output an error message):

3 7 +

3 7 2 * +

3 7 + 2 *

* 3 7 2 4 - * 5 + 2 * 9 + 4 8 - * + 1 –

3 7 2 4 - * 5 + 2 * 9 + 4 8 - * + 1 - +

3 7 2 4 - * 5 + * 2 * 9 + 4 8 - * + 1 –

3 7 2 4 - * 5 + 2 * 9 + 4 8 - * + 1 -

All files must be submitted individually. DO NOT ZIP.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include <iostream>

using namespace std;

class stack{

int arr;

int top;

int capacity;

public:

stack(int size){

capacity = size;

arr = new int[capacity];

top=-1;

}

bool isempty(){

return top==-1;

}

void push (int val){

arr[++top]=val;

}

void pop (){

top--;

}

int top(){

return arr[top];

}

};

int main(){

int n,num1,num2;

cout<<"Enter length of expression : ";

cin>>n;

char a[n];

cout<<"Enter expression : ";

for (int i=0;i<n;i++){

cin>>a[i];

}

stack s(n);

for(int i=0;i<n ;i++){

if(a[i]>='0' &&a[i]<='9') s.push(a[i]-'0');

else{

num2=s.top();

s.pop();

num1 = s._top();

s.pop();

if(a[i] == '+')s.push(num1 + num2);

if(a[i] == '-')s.push(num1 - num2);

if(a[i] == '*')s.push(num1 num2);

if(a[i] == '/')s.push(num1 / num2);

}

}

cout<<"\nvalue of expression : "<< s._top()<<"\n";

return 0;

}

//sample outputCAUsers\IshuManish\Documents\ poste.exe nter length o expression5 nter expression 3 7 2 * + value of expression 17 alue of expressionl? Process exited after 21.86 seconds with return value 0 Press any key to continue - .

Add a comment
Know the answer?
Add Answer to:
Implement the array-based stack class. Use it in the client code to convert and compute post-fix...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective To acquire expertise in stack manipulation and management, subroutine linkage and retur...

    Objective To acquire expertise in stack manipulation and management, subroutine linkage and return conventions, and recursive procedures. Description You are to create a MIPS programming assignment that returns postfix representation of the input and create an expression tree. Your MIPS program should make use of the expression tree to store the input and provide, by means of an adequate traversal, the value for the expression. Your solution should be structured according to the following steps: Step 1: Convert expression from...

  • Java please 5. Using the following postfix expressions, use a class) to stack (or the method...

    Java please 5. Using the following postfix expressions, use a class) to stack (or the method we used in (12 pts-6 eaeh) solve them producing a final result. (a) 69 5 725 7*+-18 25 5,2025 20%+ (b) 2 3+ 5-6 78+ 45 5 % 12 3/++ -

  • By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert...

    By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that...

    C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that is similar to www.cplusplus.com/forum/beginner/192479/ Parenthesis Balance Problem Description Compilers require parenthesis, braces, brackets, and angled brackets to be balanced. This means that every time one is opened it must also be close AND everything between the two are also balanced. Balanced: (00) oO100) (C0O Unbalanced: (DI This is easily determined by creating a stack and reading the input. When an open parenthesis is found,...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks....

    I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. Programming:...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT