Cod sursa(job #1147539)

Utilizator L.DanielLungu Daniel L.Daniel Data 19 martie 2014 22:15:40
Problema Evaluarea unei expresii Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 2.02 kb
#include <fstream>
#include<string>
using namespace std;
#define MAXINT 10000000000
ifstream f("evaluare.in");
ofstream g("evaluare.out");
struct sir
{
	long int val;
	char operant;
}b[1000000];
struct lista
{
	lista*st, *dr;
	long int val;
	char op;
};
lista*prim, *ult;
char c;
long int k, v[1000000], l;
void add(long int);
long int operatie(long int x, long int y, char c) {
	switch (c) {
	case '+': return x + y;
	case '-': return x - y;
	case '*': return x*y;
	case '/': return x / y;
	}
	return 0;
}
void citire()
{
	while (!f.eof())
	{
		f >> c;
		if (f.eof()) break;
		if (c >= '0'&&c <= '9'&&b[k].val == 0)
		{
			b[k].val = c - '0';
			v[k] = MAXINT;
			if (c == '0')k++;
		}
		else if (c >= '0'&&c <= '9'&&b[k].val != 0)
		{
			b[k].val = b[k].val * 10 + c - '0';
		}
		else {
			if (b[k].val != 0)k++;
			b[k].operant = c;
			if (c == '(')
			{
				v[k] = 0;
				l = l + 10;
			}
			else if (c == ')')
			{
				v[k] = 0;
				l = l - 10;
			}
			else if (b[k].operant == '-' || b[k].operant == '+')v[k] = l + 1;
			else v[k] = l + 2;
			k++;
		}
	}
}
void RSD(long int st, long  int dr)
{
	long int x = MAXINT, i, l = -1;
	for (i = st; i <= dr; i++)
		if (x >= v[i] && v[i] != 0)
		{
			x = v[i];
			l = i;
		}
	if (l != -1)
	{
		add(l);
		RSD(st, l - 1);
		RSD(l + 1, dr);
	}
}
void add(long int x)
{
	lista*q = new lista;
	if (b[x].val != NULL)q->val = b[x].val;
	else q->op = b[x].operant;
	if (prim == NULL)
	{
		q->st = NULL;
		q->dr = NULL;
		prim = q;
		ult = q;
	}
	else
	{
		q->st = ult;
		q->dr = NULL;
		ult->dr = q;
		ult = q;
	}
}
void calcul()
{
	while (ult != prim)
	{
		lista*p = ult;
		while (p->op <= 0 && p->st != NULL)p = p->st;
		p->val = operatie(p->dr->val, p->dr->dr->val, p->op);
		p->op = NULL;
		if (p->dr->dr->dr == NULL)
		{
			ult = p;
			p->dr = NULL;
		}
		else {
			p->dr = p->dr->dr->dr;
			p->dr->st = p;
		}
	}
}
int main()
{
	citire();
	RSD(0, k);
	calcul();
	g << prim->val;
	return 0;
}