Question: I want to make a stack calculator based on these stack.h and stack.txx and calculator.h you can't change based stack.h and stack.txx and calculator.h but

I want to make a stack calculator based on these stack.h and stack.txx and calculator.h

you can't change based stack.h and stack.txx and calculator.h but can add it.

stack.h

#ifndef STACK_H #define STACK_H

#include

#include

#include

template

class Stack

{

public:

// Default constructor

Stack(){

capacity = 10;

top = -1;

array = new type[capacity];

};

// Destructor

~Stack();

// Return top element of stack

type& Top();

void Push(const type& item);

void Pop();

bool IsEmpty() const;

void Print() {

for(int i=0; i<=top; i++) std::cout << array[i] << std::endl;

};

int Size() { return top+1; }

private:

// Data

type *array;

int capacity;

int top;

};

#ifndef STACK_TXX

#define STACK_TXX

#include "stack.txx"

#endif

#endif

--------------------

stack.txx

template

Stack::~Stack()

{

delete [] array;

}

template

type& Stack::Top()

{

if(top < 0) assert(false);

return array[top];

}

template

void Stack::Push(const type& item)

{

if(top == capacity-1) // double capacity

{

type* temp = new type[capacity*2];

for(int i=0; i

{

temp[i] = array[i];

}

type* ret = array;

array = temp;

delete [] ret;

capacity *= 2;

}

array[ ++top ] = item;

}

template

void Stack::Pop()

{

if(top >= 0) top--;

}

template

bool Stack::IsEmpty() const

{

if(top < 0) return true;

else return false;

}

-----------------

calculator.h

double Eval(char* in)// i take 2 stacks, which is input=in, array {

}

----------

main.cpp

#include "stack.h"

#include "calculator.h"

#include

int main()

{

char str[] ="-10-((-2+(2+4*3))-12) + 122 * (123 + (120+888) - 300)";

std::cout << Eval(str);

return 0;

}

result is 101372

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!