I got to do this stack program for class and im having a bit of a problem. We were supposed to use a custom "stack.h" template to make a stack and use it to find the result of an infix equation.
I used the normal c++ standard template library ( #include<stack>)
and when i changed it to the template that we have to use i keep getting an error.
I havn't worked with custom templates, so im a bit lost on this area.
Basically what should i do to make this actual code work with using the custom template
If i switch the code back to #include<stack> the code works perfectly.
Code
// Test data : 123*+4-
// answer should be : 3
// for problem 15, first change infix to postfix
#include<iostream>
#include "stack.h"
#include<string>
using namespace std;
int evaluate(int operand_1, int operand_2, char operate);
int evaluate_xpression(char ptr[], int size);
int main() {
string temp;
int size =0;// size of the array,
cout << " Please enter the postfix expression that you would like to evaluate" << endl;
getline( cin, temp);
char *ptr;
ptr = new char[temp.length()]; // creates a dynamic array based on the number of chars in the string input
for ( int i=0; i< temp.length(); i++)
{
ptr[i]=temp[i];
size +=1;
};
int answer = evaluate_xpression(ptr, size);
cout<<" The Answer to the expression is :"<<answer << endl;
delete[] ptr;
return 0;
}
int evaluate(int operand_1, int operand_2, char operate) {
switch (operate)
{
case '*':
return operand_2 * operand_1;
case '/':
return operand_2 / operand_1;
case '+':
return operand_2 + operand_1;
case '-':
return operand_2 - operand_1;
default :
return 0;
}
}
int evaluate_xpression(char ptr[], int size) {
Stack<int> stack;
int i = 0;
char character;
int answer = 0;
while (i < size)
{
character = ptr[i];
if (isdigit(character))
{
Stack.push(character-'0');
}
else
{
int operand_1 = stack.top();
stack.pop();
int operand_2 = stack.top();
stack.pop();
answer = evaluate(operand_1, operand_2, character);
stack.push(answer);
}
i++;
}
return answer;
}
The "stack.h" header file looks like this
Code
// stack.cpp -- the function definitions for the array implementation of a stack
template <class DataType>
Stack<DataType>::Stack( )
: elements( 2 ), top( -1 )
{
}
template <class DataType>
void Stack<DataType>::push( DataType elementToPush )
{
if ( ++top == elements.length( ) )
elements.changeSize( elements.length( ) << 1 );
elements[ top ] = elementToPush;
}
// removes an element from the top of the stack and returns it in poppedElement;
// returns false if called on an empty stack; otherwise, returns true
template <class DataType>
bool Stack<DataType>::pop( DataType & poppedElement )
{
if ( top == -1 )
return false;
poppedElement = elements[ top ];
top--;
int trysize = elements.length( );
while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;
if ( trysize < elements.length( ) ) {
try {
elements.changeSize( trysize );
}
catch( ... ) { }
}
return true;
}
// returns the element at the top of the stack in topElement without removing it
// returns false if called on an empty stack; otherwise, returns true
template <class DataType>
bool Stack<DataType>::peek( DataType & topElement )
{
if ( top == -1 )
return false;
topElement = elements[ top ];
return true;
}
template <class DataType>
bool Stack<DataType>::isEmpty( ) const
{
return top == -1;
}
template <class DataType>
void Stack<DataType>::makeEmpty( )
{
top = -1;
try {
elements.changeSize( 2 );
}
catch( ... ) { }
}
Im getting an error on line 63, that says" use of undeclared identifier stack";
Code
nt evaluate_xpression(char ptr[], int size) {
Stack<int> stack; <--------- This line
int i = 0;
char character;
int answer = 0;
while (i < size)
{
This post was edited by Pino38 on Feb 18 2015 01:21pm