Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ programming infix to postfix then evaluete getting your input from an external file and using template stack. Am needing help to fix my program

C++ programming

infix to postfix then evaluete getting your input from an external file and using template stack.

Am needing help to fix my program so that it can do what the professor wnats

convert infix to postfix using template stacks and perform the evalustion

thank you;

here is my program but need help fixing it.

//header file

#ifndef STACK_H

#define STACK_H

template class stack;//forward declaration

template

class Stacknode

{

friend class stack;

public:

Stacknode(const T &, Stacknode *);

T getData()const;

//set pointer to next node

void setNextPtr (stackNode *nPtr)

{

nextPtr = nPtr;

}//end function

Stacknode *getNextPtr () const

{

return nextPtr;

}

private: T data;

stackNode *nextptr

};

//member function defination

template

stackNode::stackNode(const T &^d, stackNode*ptr)

{

data = d;

nextPtr = ptr;

}//end constructor

//get data

template

stackNode::getData()const

{

return data;

}//end fubnction get data

#endif // STACK_H

#endif // STACK_H

#ifndef STACK_H

#define STACK_H

#include

using std::cout;

#include

#include "stackNode.h"

template

class stack

{

public:

stack();

~stack();

void push(T &);

T pop;

bool isEmpty()const;

T stackTop()const;

void print()const;

stackNode *getTopPtr()()const

{

return topPtr;

}

private: StackNode *topPtr;

};//end class template

//member function defination

template

Stack::Stack()

{

topPTR=0;

}

template

Stack::~Stack()

{

StackNode *tempPtr, *currentPtr=ttopPtr;

while(currentPtr!=0)

{

tempPtr=currentPtr;

currentPtr=currentPtr->getNextPtr();

delete tempPtr;

}//endwhile

}//end destructor

//push node

template

void Stack::push(T &d)

{

stackNode *newPtr=new stackNode(d, topPtr);

topPtr=newPtr;

}//end function push

//pop node

template

T Stack::pop()

{

stackNode *tempPtr=topPtr;

topPtr=topPrt->nextPtr;

T poppedValue = tempPtr->data;

delete tempPtr;

returnpoppedValue;

}

//is stack empty

template

bool Stack::isEmpty()const

{

return topPtr==0;

}

template

T Stack::stackTop()const

{

return

!isEmpty()?getTopPtr()->getData():static_cast (0);

}//end fuction stacktop

//display stack

template

void Stack:: *currentPtr=topPtr;

if(isEmpty())

cout<<"stack is empty";

else

{

cout<<"The stack is : ";

while(currentPtr!=)

{

cout<data<<"";

currentPtr=currentPtr->nextptr;

}//end while

cout<<" ";

}//end function print

#endif // STACK_H

#include "stack.h"

//function prototype

void convertToPostfix(char *char, char *const);

bool isOperater(char)

bool precedence(char,char);

int main ()

{

const int MAXSIZE = 50

char c;

char inFix[MAXSIZE]

char postFix[MAXSIZE]

int pos = 0;

cout<<"Enter the input file to read from"<

string inFix;

ifstream file;

file.open("inputFile.txt")

while(!file.eof())

{

getline(file, inFix);

int size = Infix.size();

convertToPostfix(inFix,postfix);

cout<<"The expression in postfix is"<

return 0;

}

void convertToPostfix(char *const infix, char *const postfix)

{

Stack charStack;

int infixCount;

int postfixCount;

bool higher;

char popValue

char leftparen = '(';

charStack.push(leftparen);

charStack.print();

strcat(infix,")");

//convert the infix expresion to postfix;

for(infixCount=0, postfixCount=o;

charStack.stackTop();infixCount++)

{

if(isdigit(infix[infixCount]))

postfix[postfiCount++]==infix[infixCount];

else if(infix[infixCount]=='(')

{

charStack.push(leftparen);

charStack.print()

}

else if (isOperator(infix[infixCount]))

{

higher = true;

while(higher)

{

if(isOperator(charStack.stackTop()))

if (presedence (charStack.stackTop(),infix[infixCount]))

{

postfi[postfixCount++]=charStack.pop();

}

else

higher = false;

}

postfix[postfixCount]='\0';

}

bool isOperator (char c)

}

if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')

return true;

else

return false;

}

bool precedence(char operator1, char operator2)

{

if(operator1 =='^')

return true;

else if(operator2=='^')

return false;

else if (operator1=='*'||operator1=='/')

return true;

else if(operator1=='+'||operator1=='/')

{

if(operator2=='+'||operator1=='/')

return false;

else

return true;

}

return false

}

}

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

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

0764532545, 978-0764532542

More Books

Students also viewed these Databases questions