Pagini recente » Cod sursa (job #2334029) | Istoria paginii runda/3_martie_simulare_oji_2024_clasa_9/clasament | Istoria paginii runda/9_martie_simulare_oji_2024_clasa_10 | Cod sursa (job #118107) | Cod sursa (job #2669583)
#include <iostream>
#include <fstream>
#include <cstring>
#include <stack>
using namespace std;
char s[100001];
stack <char> semne;
stack <int> numere;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
int calcul(int a, int b, char o)
{
if(o=='*')
return a*b;
if(o=='-')
return a-b;
if(o=='+')
return a+b;
if(o=='/')
return a/b;
}
int ordine(char x)
{
if(x==')')
return -1;
if(x=='(')
return 0;
if(x=='+' || x=='-')
return 1;
if(x=='*' || x=='/')
return 2;
}
void formare()
{
int n=strlen(s),ok=1,i=0;
while(i<n)
{
if(s[i]<'0' || s[i]>'9')
{
if(semne.empty() || s[i]=='(' || ordine(s[i])>ordine(semne.top()))
semne.push(s[i]);
else
{
if(s[i]==')')
{
while(semne.top()!='(')
{
int a=numere.top();
numere.pop();
int b=numere.top();
numere.pop();
numere.push(calcul(b,a,semne.top()));
semne.pop();
}
semne.pop();
}
else
{
int a=numere.top();
numere.pop();
int b=numere.top();
numere.pop();
numere.push(calcul(b,a,semne.top()));
semne.pop();
semne.push(s[i]);
}
}
}
else
{
int nr=0;
while((s[i]>'0' || s[i]=='0') && (s[i]<'9' || s[i]=='9'))
nr=nr*10+(s[i]-'0'), i++;
i--;
numere.push(nr);
}
i++;
}
int r;
while(!semne.empty())
{
int a=numere.top();
numere.pop();
int b=numere.top();
numere.pop();
r=(calcul(b,a,semne.top()));
numere.push(r);
semne.pop();
}
g<<r;
///(1+2*3)*5-(2*3)*(1+1)/3
}
int main()
{
f.get(s,100001);
cout<<s;
formare();
return 0;
}
//if((s[i]>'0' || s[i]=='0') && (s[i]<'9' || s[i]=='9'))