Pagini recente » Cod sursa (job #2691310) | Cod sursa (job #850416) | Cod sursa (job #821833) | Cod sursa (job #599816) | Cod sursa (job #2683456)
//ALEXANDRU MICLEA
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>
#include <assert.h>
using namespace std;
using ll = long long;
#include <fstream>
//ifstream cin("input.in"); ofstream cout("output.out");
ifstream cin("evaluare.in"); ofstream cout("evaluare.out");
//VARIABLES
string s;
//FUNCTIONS
bool isOp(char c) {
return c == '+' || c == '-' || c == '/' || c == '*';
}
int priority(char op) {
if (op == '/' || op == '*') return 2;
if (op == '-' || op == '+') return 1;
return -1;
}
void process_op(stack <int>& st, char op) {
int r = st.top(); st.pop();
int l = st.top(); st.pop();
switch (op) {
case '+': st.push(l + r); break;
case '-': st.push(l - r); break;
case '/': st.push(l / r); break;
case '*': st.push(l * r); break;
}
}
int evaluate(string& s) {
stack <int> st;
stack <char> op;
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] == '(') {
op.push('(');
}
else if (s[i] == ')') {
while (op.top() != '(') {
process_op(st, op.top());
op.pop();
}
op.pop();
}
else if (isOp(s[i])) {
char curr_op = s[i];
while (!op.empty() && priority(op.top()) >= priority(curr_op)) { ///de inteles
process_op(st, op.top());
op.pop();
}
op.push(curr_op);
}
else {
//citesc ce numar am in fata
int num = 0;
while (i < (int)s.size() && isalnum(s[i]))
num = num * 10 + s[i++] - '0';
--i;
st.push(num);
}
}
while (!op.empty()) {
process_op(st, op.top());
op.pop();
}
return st.top();
}
//MAIN
int main() {
cin >> s;
cout << evaluate(s);
return 0;
}