944,108 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2219
  • C++ RSS
Apr 1st, 2005
0

please help me to rewrite this.

Expand Post »
how do u rewrite the infix to postfix conversion using class. i have my code from struct stack type.

C++ Syntax (Toggle Plain Text)
  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 >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
missy is offline Offline
13 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pleae help!!! Homework woes!
Next Thread in C++ Forum Timeline: VC++ Stage 3 project help me!!!!!!1





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC