| | |
please i need your help.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 13
Reputation:
Solved Threads: 0
i need help to rewrite this in infix to postfix using class. it was in a struct format at first, now i need to format this in a class version.
Code tags added. -Narue
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> using namespace std; const int SIZE = 20; private: int top; char num [SIZE]; public: void stack_init (stack_type&); void push (stack_type& , char); void pop (stack_type&, char& ); int is_empty ( stack_type&); int is_full (stack_type&); } int main () { char infix [20], postfix [20]="", ch; int size, i, j=-1; stack_type stack; stack_init (stack); cout << "Enter an infix string with no embedded blanks"; cin >> infix; size = strlen(infix); for ( i = 0; i < size ; i++ ) { if (infix [i] == '+' || infix [i] == '-' || infix [i] == '*' || infix [i] == '/' ) // if current char is an operator ************* if ( infix [i] == '+' || infix [i] == '-') { if (!is_empty(stack)) {pop (stack, ch); while (ch=='+'|| ch=='-'||ch=='*'||ch=='/') { j++; postfix [j] = ch; ch = ' '; // blank out ch for later testing if (!is_empty(stack)) pop (stack, ch); } if (ch != ' ') push (stack, ch ); push (stack,infix[i]); } else // if stack is empty push (stack, infix[i]); } else // if current operand is a high prec operand { if (!is_empty(stack)) {pop (stack, ch); while (ch=='*'||ch=='/') { j++; postfix [j] = ch; ch = ' '; // blank out ch for later testing if (!is_empty(stack)) pop (stack, ch); } if (ch != ' ') push (stack, ch ); push (stack,infix[i]); } else // if stack is empty push (stack, infix[i]); } else // current char is operand or parenthesis if (infix[i] == '(') push (stack, infix[i]); else if (infix[i] == ')') {pop (stack,ch); while (ch != '(') { j++; postfix[j] = ch; pop (stack,ch); } } else // current is an operand {j++; postfix[j] = infix[i]; } } // end of for i loop while (!is_empty(stack)) {j++; pop (stack,ch); postfix[j] = ch; } cout << postfix << endl; return 0; } void stack_init (stack_type& s ) { s.top = -1; } void push (stack_type& s, char item) { if (s.top+1 < SIZE) { s.top ++; s.num [s.top] = item; } } void pop (stack_type & s , char& item ) { if (s.top > -1 ) { item = s.num[s.top]; s.top --; } } int is_empty ( stack_type& s ) { if (s.top > -1 ) return 0; else return 1; } int is_full (stack_type& s ) { if (s.top == SIZE-1) return 1; else return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: How do i fix this?
- Next Thread: Simple array/class problem (dot operator)???
| 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






