//#include<bits/stdc++.h>
#include <stack>
#include <string>
#include <iostream>
using namespace std;
string ONP(string w)
{
stack<char> stos;
stos.push('#');
string onp="";

for (int i=0; i<w.size(); i++)
{
	if (w[i]=='(') {stos.push('('); continue;}
	//push - umieszczenie elementu na stosie
	if (w[i]==')') 
	   {while (stos.top()!='(')
	       {onp=onp+stos.top(); stos.pop();} stos.pop(); continue;}       
	//top - odczytanie watosci elementu bedacego na wierzcholku stosu
	//pop - zdjecie elementu ze stosu
	if (w[i]=='+' || w[i]=='-')
	   {while (stos.top()!='#' && stos.top()!='(')
	   {onp=onp+stos.top(); stos.pop();} stos.push(w[i]); continue;}
	if (w[i]=='*' || w[i]=='/')
		{if (stos.top()=='*' || stos.top()=='/')
		{onp=onp+stos.top(); stos.pop();} stos.push(w[i]);} else onp=onp+w[i];
}
while (stos.top()!='#') {onp=onp+stos.top(); stos.pop();}
stos.pop();
return onp;
}
//empty - sprawdzenie czy stos jest pusty
int main()
{
   string w, tekst;
   cout<<"Wprowadz wyrazenie algebraiczne: ";
   getline (cin, w);
   cout<<ONP(w);
   return 0;
}