Cod sursa(job #2870767)

Utilizator Max_CalincuMaxim Calincu Max_Calincu Data 12 martie 2022 15:49:53
Problema Evaluarea unei expresii Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair<ll, ll>
#define sz(x) (int)((x).size())
#define all(a) (a).begin(), (a).end()

/*const ll maxn = 1e5;
int f[maxn],nf[maxn],inv[maxn];
const int M=998244353;
void init(){
    inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
    f[0]=nf[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
}
int C(int x,int y){return 1ll*f[x]*nf[y]%M*nf[x-y]%M;} */

const ll mod = 1e9+7;
ll n, k, m, mi, ma;

string s;
ll i = 0;

ll get(){

	ll ans = 0;
	while('0' <= s[i] && s[i] <= '9'){
		ans*=10;
		ans += (s[i] - '0');
		i++;
	}
	return ans;
}

ll process(){
	ll x = 0;
	ll cur;	
	if(s[i] == '('){
		i++;
		cur = process();
	} else cur = get();
	ll sign = 1;
	while(s[i] != ')'){
		char c = s[i];
		i++;
		ll y;
		
		if(s[i] == '(') {
			i++;
			y = process();
		} else y = get();
		
		if(c == '+' || c == '-'){
			x += sign*cur;
			cur = y;
			sign = 1;
			if(c == '-') sign = -1;
			continue;
		}
		if(c == '*') cur*=y; else cur/=y;
	}
	x += sign*cur;
	i++;
	return x; 
}

void solve(){
	
	ifstream cin("evaluare.out");
	ofstream cout("evaluare.out");
	cin >> s;
	n = sz(s);
	s = s + ')';
	cout << process() << "\n";

}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	//init();
	
	int t=1;
//	cin >> t;
	while(t--) solve();
	
	return 0;
}