| | |
converting postfix to Infix help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
I need help trying to convert a postfix expression to Infix. I have tried to search books and internet material to help me take on this task but no luck. Below is what I created, but I know it is far from right which is why I appreciate if someone can help me.
Targeted Function:
header file:
main.cpp
#include <iostream>
#include <stdlib.h>
#include "stack.h"
using namespace std;
int main()
{
toInfix(string expression);
system("PAUSE");
return 0;
}
Targeted Function:
C++ Syntax (Toggle Plain Text)
void toInfix(string expression) { stackType<string> stack,here; string Infix; string priority; string number, hold, again; again=expression; ifstream inData("input.txt"); ofstream outData("output.txt"); while (getline( inData, expression )) { cout << expression; } for (int a=0; a < expression.length(); a++) { number = here = expression[a]; cout << "The expression " << here << endl; cout << "The Infix " << Infix << endl; if ( expression[a] == '+' || expression[a] == '-') { if (priority <= 2 ) { here.pop(); here.push(); } else { here.push(); } } else if ( expression[a] == '*' || expression[a] == '/' || expression[a] == '%' ) { if (priority == 1 ) { here=stack[0]; stack[0]=expression[a]; Infix = Infix + here; } else { here.push; } } else if ( expression[a] == '(' ) { here.push(); } else if ( expression[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 + hold; // number already equals the string here ++b; } infix = infix + ' ' + number; a=b-1; } else { infix = infix + ' ' + number; } } // end of else if cout << "\nINFIX "<< infix << endl; } // end of for Infix = Infix + ' ' + stack; } #endif
header file:
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>&); }; 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 toInfix(string expression) { stackType<string> stack,here; string Infix; string priority; string number, hold, again; again=expression; ifstream inData("input.txt"); // declaration of inData ofstream outData("output.txt"); // declaration of outData while (getline( inData, expression )) // reads words in file using getline to capture all spaces { /*something to break up line into individual character*/ cout << expression; } for (int a=0; a < expression.length(); a++) { number = here = expression[a]; cout << "The expression " << here << endl; cout << "The Infix " << Infix << endl; if ( expression[a] == '+' || expression[a] == '-') { if (priority <= 2 ) { here.pop(); here.push(); } else here.push(); } else if ( expression[a] == '*' || expression[a] == '/' || expression[a] == '%' ) { if (priority == 1 ) { here=stack[0]; stack[0]=expression[a]; Infix = Infix + here; } else here.push; } else if ( expression[a] == '(' ) here.push(); else if ( expression[a] == ')' ) here.pop(); else if ( isdigit(again[a]) ) { // for dealing with multiple digits and single digit numbers if ( isdigit(again[a+1]) ) { int b=a+1; while ( isdigit(again[b]) ) { hold=again[b]; number = number + hold; // number already equals the string here ++b; } infix = infix + ' ' + number; a=b-1; } else { infix = infix + ' ' + number; } } // end of else if cout << "\nINFIX "<< infix << endl; } // end of for Infix = Infix + ' ' + stack; } #endif
main.cpp
#include <iostream>
#include <stdlib.h>
#include "stack.h"
using namespace std;
int main()
{
toInfix(string expression);
system("PAUSE");
return 0;
}
Last edited by NinjaLink; 32 Days Ago at 1:26 am.
![]() |
Similar Threads
- stacks - verify well-formed equations, InFix to PostFix (Java)
- "Infix-Postfix & Postfix-Infix" codes problem (C)
- help with a second infix to postfix and reversing infix to postfix (Java)
- infix to postfix ( homework help - Urgent ) (C++)
- Converting equation from infix to postfix using stack (C++)
- Help for Binary Tree Traversal for infix to postfix conversion (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: Do...while string help
- Next Thread: need help with classes
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





