Pagini recente » Cod sursa (job #1720026) | Cod sursa (job #1415588) | Cod sursa (job #2125143) | Cod sursa (job #452437) | Cod sursa (job #2048553)
#include<fstream>
#include<cstring>
#include<stack>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
int P[130], n;
char s[100005];
stack<char> stiva;
stack< pair<int,int> > v;
int main(){
P[(int)('+')] = P[(int)('-')] = 1;
P[(int)('*')] = P[(int)('/')] = 2;
fin >> s;
n = strlen(s);
for( int i = 0; i < n; i++ ){
if( s[i] == '(' ){
stiva.push( s[i] );
continue;
}
if( s[i] == ')' ){
while( stiva.top() != '(' ){
v.push( make_pair( 0, (int)( stiva.top() ) ) );
stiva.pop();
}
stiva.pop();
continue;
}
if( s[i] == '+' ){
if( i == 0 || s[i - 1] == '(' )
v.push( make_pair( 1, 0 ) );
while( !stiva.empty() && P[(int)(stiva.top())] >= P[(int)(s[i])] ){
v.push( make_pair( 0, (int)( stiva.top() ) ) );
stiva.pop();
}
stiva.push( s[i] );
continue;
}
if( s[i] == '-' ){
if( i == 0 || s[i - 1] == '(' )
v.push( make_pair( 1, 0 ) );
while( !stiva.empty() && P[(int)(stiva.top())] >= P[(int)(s[i])] ){
v.push( make_pair( 0, (int)( stiva.top() ) ) );
stiva.pop();
}
stiva.push( s[i] );
continue;
}
if( s[i] == '*' ){
while( !stiva.empty() && P[(int)(stiva.top())] >= P[(int)(s[i])] ){
v.push( make_pair( 0, (int)( stiva.top() ) ) );
stiva.pop();
}
stiva.push( s[i] );
continue;
}
if( s[i] == '/' ){
while( !stiva.empty() && P[(int)(stiva.top())] >= P[(int)(s[i])] ){
v.push( make_pair( 0, (int)( stiva.top() ) ) );
stiva.pop();
}
stiva.push( s[i] );
continue;
}
int nr = 0;
while( s[i] >= '0' && s[i] <= '9' ){
nr = nr * 10 + ( s[i] - '0' );
i++;
}
v.push( make_pair( 1, nr ) );
i--;
}
for( int i = 0; i < v.size(); i++ )
if( v[i].first == 0 )
fout << (int)()
return 0;
}