Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ implement a program that can input an expression in postfix notation and output its value . A SIMPLE STACK CLASS main.cpp /******************************* * Week

C++ implement a program that can input an expression in postfix notation and output its value.

A SIMPLE STACK CLASS

main.cpp

/*******************************

* Week 2 lesson: *

* a simple Stack class *

*******************************/

#include

#include "stack.h"

using namespace std;

int main()

{

Stack s;

cout << "Insertion of 10 characters in s" << endl;

for (int i = 0; i < s.getSize(); i++)

{

char x = 32 + rand()%95;

cout << x << endl;

s.push(x);

}

cout << endl

<< "Displaying and deleting elements from s" << endl;

while(!s.isEmpty())

{

cout << "Item at the top: " << s.peek() << endl;

s.pop();

}

return 0;

}

Stack.cpp

/*******************************

* Week 2 lesson: *

* a simple Stack class *

*******************************/

#include "Stack.h"

/*

* Constructor. Initializes the stack.

*/

Stack::Stack()

{

top = 0;

}

/*

* Determines whether the stack is empty.

*

* Returns true if the stack is empty, false otherwise.

*/

bool Stack::isEmpty()

{

return top == 0;

}

/*

* Adds an element to the top of the stack.

*

* x: element to be added to the stack.

*/

void Stack::push(char x)

{

list[top] = x;

top++;

}

/*

* Removes the element at the top of the stack.

*/

void Stack::pop()

{

top--;

}

/*

* Returns the element at the top of the stack. Does not remove it.

*/

char Stack::peek()

{

return list[top-1];

}

/*

* Returns the size of the stack.

*/

int Stack::getSize()

{

return SIZE;

}

Stack.h

/*******************************

* Week 2 lesson: *

* a simple Stack class *

*******************************/

/*

* Class implementing a Stack ADT.

*/

class Stack

{

public:

Stack();

bool isEmpty();

void push(char);

void pop();

char peek();

int getSize();

private:

static const int SIZE = 10; //size of the queue array

char list[SIZE]; //array to store the stack items

int top; //amount of elements in the array

};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

Students also viewed these Databases questions

Question

What are the factors affecting organisation structure?

Answered: 1 week ago

Question

What are the features of Management?

Answered: 1 week ago

Question

Briefly explain the advantages of 'Management by Objectives'

Answered: 1 week ago