| | |
Please help me check my code to convert Infix to Postfix
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 0
#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;
}
#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;
}
•
•
Join Date: May 2009
Posts: 4
Reputation:
Solved Threads: 0
0
#2 Oct 8th, 2009
C Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- Help pplz. . convert infix to postfix. . (C++)
- infix to postfix conversion (C++)
- Infix to postfix using array stacks (Java)
- infix to postfix to evaluate (Java)
- Code Snippet: Infix to Postfix (Java)
- "Infix-Postfix & Postfix-Infix" codes problem (C)
- help with a second infix to postfix and reversing infix to postfix (Java)
- Any Boolean Expression to Sum of Minterms (C++)
Other Threads in the C++ Forum
- Previous Thread: Help implementing functions into code
- Next Thread: Change constant in a exe compiled with Visual Studio C++ 2005
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






