Pagini recente » Cod sursa (job #382710) | Borderou de evaluare (job #1569276) | Cod sursa (job #3251901) | Cod sursa (job #2762119) | Cod sursa (job #3131110)
#include <bits/stdc++.h>
using namespace std;
stack <char> op;
vector < vector<char> > polo(100005);
char s[1005];
int prioritate(char x)
{
switch(x)
{
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
}
return 0;
}
int main()
{
int n;
char aux;
string s;
cin >> s;
n = s.size();
int cnt = 0;
for(int i = 0; i < n; ++i)
{
if(s[i] >= '0' && s[i] <= '9')
{
polo[cnt].push_back(s[i]);
if(i == n - 1 || (s[i + 1] < '0' || s[i + 1] > '9'))
cnt++;
}
else
if(s[i] == ')')
{
while(op.top()!='(')
{
aux = op.top();
polo[cnt].push_back(aux);
cnt++;
op.pop();
}
op.pop();
}
else
if(op.empty() || s[i] == '(' || prioritate(s[i]) > prioritate(op.top()))
op.push(s[i]);
else
{
while(!op.empty() && prioritate(s[i]) <= prioritate(op.top()))
{
aux = op.top();
polo[cnt].push_back(aux);
cnt++;
op.pop();
}
op.push(s[i]);
}
}
while(!op.empty())
{
polo[cnt].push_back(op.top());
cnt++;
op.pop();
}
///forma prefixata
for(int i = 0; i < cnt ; ++i)
{
for(auto u : polo[i])
cout << u;
cout << " ";
}
/*
for(int i = 0; i < cnt ; ++i)
{
int nr = 0;
for(auto u : polo[i])
{
}
}
*/
return 0;
}