Cod sursa(job #2642273)

Utilizator am_I_reallymeMircea Filat am_I_reallyme Data 14 august 2020 13:42:54
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
struct my_timer{
	chrono::time_point<chrono::high_resolution_clock> start;
	my_timer():
	start(chrono::high_resolution_clock::now()){}
	~my_timer(){
		std::chrono::duration<double> diff = chrono::high_resolution_clock::now() - start;
		clog<<diff.count()<<"s\n";
	}
};

int main()
{
	freopen("evaluare.in","r",stdin);
	freopen("evaluare.out","w",stdout);
	{
		char ord[128];
		ord['+']=ord['-']=0;
		ord['*']=ord['/']=1;
		ord['('] = -1;
		queue<ll> val;
		stack<ll> op;
		int c=getchar();
		while(c!=EOF){
			if(isdigit(c)){
				int num = c&15;
				while(isdigit(c=getchar())){
					num = (num << 3) + (num << 1) + (c & 15);
				}
				val.emplace(num);
				if(c==EOF)
					break;
			}
			if(c==')'){
				while(op.top() != '('){
					val.emplace((1ll<<62) | op.top());
					op.pop();
				}
				op.pop();
			}
			else{
				while(!op.empty() && ord[op.top()] >= ord[c]){
					val.emplace((1ll<<62) | op.top());
					op.pop();
				}
				op.emplace(c);
			}
			c=getchar();
		}
		while(!op.empty()){
			val.emplace((1ll<<62) | op.top());
			op.pop();
		}
		while(!val.empty()){
			if(val.front() & (1ll << 62)){
				ll a = op.top();
				op.pop();
				switch(val.front() & ~(1ll<<62)){
					case '+':
						op.top() += a;
						break;
					case '-':
						op.top() -= a;
						break;
					case '*':
						op.top() *= a;
						break;
					case '/':
						op.top() /= a;
						break;
				}
			}else{
				op.emplace(val.front());
			}
			val.pop();
		}
		printf("%lld",op.top());
	}
}