| | |
Need help with a function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
I need help with my function that is in my stack.h file which converts postfix expression to infix. My main.cpp reads the postfix expression from a file and outputs the expression to an output file. My main.cpp is currently working. I just need someone to help me with my toInfix function at the bottom of my stack.h. I tried to look for more information, but all I can find is Infix to Postfix. I appreciate if someone can lead me to the right direction.
main.cpp
stack.h
main.cpp
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <stdlib.h> #include "stack.h" #include <sstream> using namespace std; int main() { stringstream ss; stackType<string> stack; string Infix; string line; string word; ifstream inData("input.txt"); ofstream outData("output.txt"); while (getline( inData, line )) { ss << line; } while ( ss >> word) { stack.push(word); } outData<<line; inData.close(); outData.close(); system("PAUSE"); return 0; }
stack.h
C++ Syntax (Toggle Plain Text)
#ifndef H_StackType #define H_StackType #include <iostream> #include <fstream> #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>&); void toInfix(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 == 0); } 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>::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); } void stackType<Type>::toInfix(const stackType<Type>& otherStack) { string Infix; string isDigit; string again; int number; string word; string line; string here; for (int a=0; a < line.length(); a++) { number = word = expression[a]; if ( line[a] == '+' || line[a] == '-') { if (priority <= 2 ) { word.pop(); word.push(); } else { word.push(); } } else if ( line[a] == '*' || line[a] == '/' || line[a] == '%' ) { if (priority == 1 ) { word=stack[0]; word[0]=line[a]; Infix = Infix + word; } else { word.push; } } else if ( line[a] == '(' ) { word.push(); } else if ( line[a] == ')' ) { here.pop(); } else if ( isdigit(again[a]) ) { if ( isdigit(again[a+1]) ) { int b=a+1; while ( isdigit(again[b]) ) { hold=again[b]; number = number + word; ++b; } infix = infix + ' ' + number; a=b-1; } else { infix = infix + ' ' + number; } } } Infix = Infix + ' ' + stack; } #endif
![]() |
Similar Threads
- Function Pointer (C++)
- sign function (C#)
- Newbie function question (Python)
- My python program/function! (Python)
- Code Snippet: Function plotting in C# (C#)
Other Threads in the C++ Forum
- Previous Thread: Problem with TCHAR/CHAR
- Next Thread: How do i create a simple game using c++??
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





