| | |
C++ postfix to infix help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
Hello, I need some advice to help me write a program to convert postfix to infix. However, I am having difficulty getting started. I tried to search for more information, but all I got was infix to postfix results. This is what I have so far, but I want to make sure I am on the right track. Any help is appreciated.
main.cpp
header file:
main.cpp
C++ Syntax (Toggle Plain Text)
stackType<string>s1; char letter; ifstream inData("input.txt"); // declaration of inData ofstream outData("output.txt"); // declaration of outData while (getline( inData, letter )) { if (letter == 'A'|| letter == 'B' || letter == 'C' || letter == 'D' || letter == 'E') { s1.push(); } if (letter == '*'|| letter == '-' || letter == '+' || letter == '/') { s1.pop(); } inData.close(); // closes inData outData.close(); // closes outData system("pause"); return 0; }
header file:
C++ Syntax (Toggle Plain Text)
#ifndef H_StackType #define H_StackType #include <iostream> #include <cassert> using namespace std; template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType<Type>& otherStack); ~stackType(); bool operator== (const stackType<Type>&); bool operator!= (const stackType<Type>&); private: int maxStackSize; int stackTop; Type *list; void copyStack(const stackType<Type>& otherStack); bool isEqual(const stackType<Type>&); }; template<class Type> void stackType<Type>::copyStack(const stackType<Type>& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; } template<class Type> stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); } template<class Type> const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack) { if (this != &otherStack) copyStack(otherStack); return *this; } template<class Type> void stackType<Type>::initializeStack() { stackTop = 0; } template<class Type> void stackType<Type>::destroyStack() { stackTop = 0; } template<class Type> bool stackType<Type>::isEmptyStack() { return(stackTop == maxStackSize); } template<class Type> bool stackType<Type>::isFullStack() { return(stackTop == maxStackSize); } template<class Type> void stackType<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cerr<<"Cannot add to a full stack."<<endl; } template<class Type> Type stackType<Type>::top() { assert(stackTop != 0); return list[stackTop - 1]; } template<class Type> void stackType<Type>::pop() { if(!isEmptyStack()) { stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl; } template<class Type> stackType<Type>::stackType(int stackSize) { if(stackSize <= 0) { cerr<<"The size of the array to hold the stack must " <<"be positive."<<endl; cerr<<"Creating an array of size 100."<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = new Type[maxStackSize]; } template<class Type> stackType<Type>::~stackType() { delete [] list; } //template<class Type> //bool stackType<Type>::operator== (const stackType<Type>& otherStack) //{ //if (otherStack.isFullStack() != isFullStack()) //cerr<<"They are not the same size"<<endl; //for(int j = 0 ; j < isFullStack(); ++j) //{ //if (otherStack.list[j] != list[j]) //return false; //else //return true; //} //} //template<class Type> //bool stackType<Type>::operator!= (const stackType<Type>& otherStack) //{ //return !(*this == otherStack); //} template<class Type> bool stackType<Type>::isEqual(const stackType<Type>& otherStack) { bool bRet = false; if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop){ bRet = true; for (int j = 0; j < stackTop; ++j) { if (otherStack.list[j] != list[j]) { // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j]; bRet = false; break; } } } return bRet; } template<class Type> bool stackType<Type>::operator==(const stackType<Type>& otherStack) { return isEqual(otherStack); } template<class Type> bool stackType<Type>::operator!=(const stackType<Type>& otherStack) { return !isEqual(otherStack); //!(*this == otherStack); }
Last edited by NinjaLink; 32 Days Ago at 4:03 am.
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
//TO READ POSTFIX POLISH NOTATION FROM FILE void readFile(ifstream& read) { char in = '0'; while (in != '\n') { read.get(in); if ((in >='0') && (in <='9')) { ExprNode<char> EN(in); LS.Push(EN); } else { ExprTree<char> ET; ExprNode<char> EN1 = LS.Pop(); ExprNode<char> EN2 = LS.Pop(); ExprNode<char> EN(in); ET.Insert(EN.getData()); ET.Insert(EN2.getData(), EN2.getLeft(), EN2.getRight()); ET.Insert(EN1.getData(), EN1.getLeft(), EN1.getRight()); LS.Push(ET.getRoot()); } read.get(in); } } //TO PRINT INFIX EXPRESSION void InOrder(ExprNode<T> *node, ofstream& write) { if (node != NULL) { if (node->left && node->right) { write<<"("; } InOrder(node->left, write); write<<node->data; InOrder(node->right, write); if (node->left && node->right) { write<<")"; } } }
Last edited by tsfaridi; 31 Days Ago at 5:33 am. Reason: was not in code snippet
![]() |
Similar Threads
- Postfix calculator (C++)
- infix to postfix conversion (C++)
- "Infix-Postfix & Postfix-Infix" codes problem (C)
- infix to postfix to answer pls help!! (C++)
- help with a second infix to postfix and reversing infix to postfix (Java)
- infix to postfix ( homework help - Urgent ) (C++)
- Please AnyBody Can Help Me To Build Assembly Code Infix To Postfix It So Hard T_T (Assembly)
Other Threads in the C++ Forum
- Previous Thread: Extract the file name from the full file path.
- Next Thread: BigInt addition: Is this correct?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





