#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;
}
Mr Bin 0 Newbie Poster
Mr Bin 0 Newbie Poster
VernonDozier 2,218 Posting Expert Featured Poster
iamthwee commented: Four very good questions! +11
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.