please help me to rewrite this.

Reply

Join Date: Nov 2004
Posts: 13
Reputation: missy is an unknown quantity at this point 
Solved Threads: 0
missy missy is offline Offline
Newbie Poster

please help me to rewrite this.

 
0
  #1
Apr 1st, 2005
how do u rewrite the infix to postfix conversion using class. i have my code from struct stack type.

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. const int SIZE = 20;
  5.  
  6. private: int top;
  7. char num [SIZE];
  8.  
  9. public: void stack_init (stack_type&);
  10. void push (stack_type& , char);
  11. void pop (stack_type&, char& );
  12. int is_empty ( stack_type&);
  13. int is_full (stack_type&);
  14. }
  15.  
  16. int main ()
  17. {
  18. char infix [20], postfix [20]="", ch;
  19. int size, i, j=-1;
  20. stack_type stack;
  21. stack_init (stack);
  22. cout << "Enter an infix string with no embedded blanks";
  23. cin >> infix;
  24. size = strlen(infix);
  25. for ( i = 0; i < size ; i++ )
  26. {
  27. if (infix [i] == '+' || infix [i] == '-' ||
  28. infix [i] == '*' || infix [i] == '/' )
  29. // if current char is an operator *************
  30. if ( infix [i] == '+' || infix [i] == '-')
  31. { if (!is_empty(stack))
  32. {pop (stack, ch);
  33. while (ch=='+'|| ch=='-'||ch=='*'||ch=='/')
  34. { j++;
  35. postfix [j] = ch;
  36. ch = ' '; // blank out ch for later testing
  37. if (!is_empty(stack))
  38. pop (stack, ch);
  39. }
  40. if (ch != ' ')
  41. push (stack, ch );
  42. push (stack,infix[i]);
  43. }
  44. else // if stack is empty
  45. push (stack, infix[i]);
  46. }
  47. else // if current operand is a high prec operand
  48. { if (!is_empty(stack))
  49. {pop (stack, ch);
  50. while (ch=='*'||ch=='/')
  51. { j++;
  52. postfix [j] = ch;
  53. ch = ' '; // blank out ch for later testing
  54. if (!is_empty(stack))
  55. pop (stack, ch);
  56. }
  57. if (ch != ' ')
  58. push (stack, ch );
  59. push (stack,infix[i]);
  60. }
  61. else // if stack is empty
  62. push (stack, infix[i]);
  63. }
  64.  
  65. else // current char is operand or parenthesis
  66. if (infix[i] == '(')
  67. push (stack, infix[i]);
  68. else
  69. if (infix[i] == ')')
  70. {pop (stack,ch);
  71. while (ch != '(')
  72. { j++;
  73. postfix[j] = ch;
  74. pop (stack,ch);
  75. }
  76. }
  77. else // current is an operand
  78. {j++;
  79. postfix[j] = infix[i];
  80. }
  81. } // end of for i loop
  82. while (!is_empty(stack))
  83. {j++;
  84. pop (stack,ch);
  85. postfix[j] = ch;
  86. }
  87. cout << postfix << endl;
  88. return 0;
  89. }
  90.  
  91.  
  92.  
  93. void stack_init (stack_type& s )
  94. { s.top = -1;
  95. }
  96. void push (stack_type& s, char item)
  97. {
  98. if (s.top+1 < SIZE)
  99. { s.top ++;
  100. s.num [s.top] = item;
  101. }
  102. }
  103. void pop (stack_type & s , char& item )
  104. { if (s.top > -1 )
  105. { item = s.num[s.top];
  106. s.top --;
  107. }
  108. }
  109. int is_empty ( stack_type& s )
  110. { if (s.top > -1 )
  111. return 0;
  112. else
  113. return 1;
  114. }
  115. int is_full (stack_type& s )
  116. { if (s.top == SIZE-1)
  117. return 1;
  118. else
  119. return 0;
  120. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC