Please help me check my code to convert Infix to Postfix

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 4
Reputation: Mr Bin is an unknown quantity at this point 
Solved Threads: 0
Mr Bin Mr Bin is offline Offline
Newbie Poster

Please help me check my code to convert Infix to Postfix

 
-1
  #1
Oct 7th, 2009
#include <stack>
#include <queue>
#include <iostream>
#include <fstream>

using namespace std;


char num[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char ope[4] = {'+', '-', '*', '/'};

bool isope(char);
bool isnum(char);
int prio(char);
queue<char> ReversePolish();
void write(queue<char> ch);

void main()
{
queue<char> q;
q = ReversePolish();
}

bool isope(char c)
{
int i = 0;
while ((i < 4)&&(c != ope[i]))
++i;
if (i < 4)
return true;
else
return false;
}

bool isnum(char c)
{
int i = 0;
while ((i < 10)&&(c != num[i]))
++i;
if (i < 10)
return true;
else
return false;
}

int prio(char c)
{
if (c == '$')
return 0;
if ((c == '(')||(c == ')'))
return 1;
else if ((c == '+')||(c == '-'))
return 2;
return 3;
}

queue<char> ReversePolish()
{
stack<char> stk;
stk.push('$');
queue<char> que;
char ch;

fstream fin("input.txt", ios::in);
if (!fin)
{
cout << "Khong tim thay file!!" ;
return que;
}

fin >> ch;

while (ch != '\n')
{
if (ch == '(')
stk.push(ch);
else if (isnum(ch))
que.push(ch);
else if (isope(ch))
{
while (stk.top() != '$' && prio(ch) <= prio(stk.top()))
{
que.push(stk.top());
stk.pop();
}
stk.push(ch);
}
else if (ch == ')')
{
while (stk.top() != '(' && (!stk.empty()))
{
que.push(stk.top());
stk.pop();
}
stk.pop();
}
while (!stk.empty())
{
que.push(stk.top());
stk.pop();
}
fin >> ch;
}
fin.close();
return que;
}
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 4
Reputation: Mr Bin is an unknown quantity at this point 
Solved Threads: 0
Mr Bin Mr Bin is offline Offline
Newbie Poster
 
0
  #2
Oct 8th, 2009
  1. #include <stack>
  2. #include <queue>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. char num[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  10. char ope[4] = {'+', '-', '*', '/'};
  11.  
  12. bool isope(char);
  13. bool isnum(char);
  14. int prio(char);
  15. queue<char> ReversePolish();
  16. void write(queue<char> ch);
  17.  
  18. void main()
  19. {
  20. queue<char> q;
  21. q = ReversePolish();
  22. }
  23.  
  24. bool isope(char c)
  25. {
  26. int i = 0;
  27. while ((i < 4)&&(c != ope[i]))
  28. ++i;
  29. if (i < 4)
  30. return true;
  31. else
  32. return false;
  33. }
  34.  
  35. bool isnum(char c)
  36. {
  37. int i = 0;
  38. while ((i < 10)&&(c != num[i]))
  39. ++i;
  40. if (i < 10)
  41. return true;
  42. else
  43. return false;
  44. }
  45.  
  46. int prio(char c)
  47. {
  48. if (c == '$')
  49. return 0;
  50. if ((c == '(')||(c == ')'))
  51. return 1;
  52. else if ((c == '+')||(c == '-'))
  53. return 2;
  54. return 3;
  55. }
  56.  
  57. queue<char> ReversePolish()
  58. {
  59. stack<char> stk;
  60. stk.push('$');
  61. queue<char> que;
  62. char ch;
  63.  
  64. fstream fin("input.txt", ios::in);
  65. if (!fin)
  66. {
  67. cout << "Khong tim thay file!!" ;
  68. return que;
  69. }
  70.  
  71. fin >> ch;
  72.  
  73. while (ch != '\n')
  74. {
  75. if (ch == '(')
  76. stk.push(ch);
  77. else if (isnum(ch))
  78. que.push(ch);
  79. else if (isope(ch))
  80. {
  81. while (stk.top() != '$' && prio(ch) <= prio(stk.top()))
  82. {
  83. que.push(stk.top());
  84. stk.pop();
  85. }
  86. stk.push(ch);
  87. }
  88. else if (ch == ')')
  89. {
  90. while (stk.top() != '(' && (!stk.empty()))
  91. {
  92. que.push(stk.top());
  93. stk.pop();
  94. }
  95. stk.pop();
  96. }
  97. while (!stk.empty())
  98. {
  99. que.push(stk.top());
  100. stk.pop();
  101. }
  102. fin >> ch;
  103. }
  104. fin.close();
  105. return que;
  106. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
1
  #3
Oct 8th, 2009
One, it's int main () , not void main () . Two, we need an input file. Three, what's the point of having a program with no output, either to the screen or to a file? How do you test it? If there is output, I missed it. Four, what's the question?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC