| | |
please help me to rewrite this.
![]() |
•
•
Join Date: Nov 2004
Posts: 13
Reputation:
Solved Threads: 0
how do u rewrite the infix to postfix conversion using class. i have my code from struct stack type.
<< moderator edit: added [code][/code] tags >>
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; }
![]() |
Similar Threads
- URL Rewrite in PHP (PHP)
- mod rewrite problem (tomcat with od_jk as well) (Linux Servers and Apache)
- Mod rewrite for help desk knowledge base (Linux Servers and Apache)
- IPB 2.0.0 mod rewrite (Search Engine Optimization)
- Apache rewrite map (PHP)
- jsp will lost its session while subdomain-rewrite applied (JSP)
- php rewrite question (PHP)
- rewrite boot.ini file (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Pleae help!!! Homework woes!
- Next Thread: VC++ Stage 3 project help me!!!!!!1
| Thread Tools | Search this Thread |
action api array auto based binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets





