Cod sursă (job #98821)

Utilizator avatar Claudiu Dan Claudiu Claudiu IP ascuns
Problemă Bizar (clasele 9-10) Compilator cpp | 1,43 kb
Rundă Tema 10 clasele 9-10 2014/15 Status evaluat
Dată 17 dec. 2014 09:11:16 Scor 63
#include<stdio.h>
using namespace std;
FILE *in = fopen ("bizar.in", "r"),
	*out = fopen ("bizar.out", "w");
char c;
int expresie(int numar);
int factor(int ct, int nr);
int expresie(int numar)
{
	int ct = 1, rez = factor(ct, numar);
	if (rez < 0)
		return -rez;
	return rez;
}

int factor(int ct, int nr)
{
	while (c == ' ')
		c = fgetc(in);

	int numar = 0, aux;
	while (c >=  '0' && c <= '9')
	{
		numar = numar * 10 + c - '0';
		c = fgetc(in);
	}
	int nr2 = 0;
	while (c == ' ')
		c = fgetc(in);
	while (c == ',')
	{
		while (c == ' ')
			c = fgetc(in);
		c = fgetc(in);
		nr2 = factor (ct + 1, nr);
		if (c != ',')
		{
		if (nr2 > 0 && ((nr % nr2 == ct) || (nr % nr2 == 0)))
			return -numar;
		else
			return nr2;
		}
	}
	while (c == ' ')
		c = fgetc(in);
	
	if (c == '(')
	{
		c = fgetc(in);
		aux = expresie(numar);
		numar = aux;
	}
			
	while (c == ' ')
		c = fgetc(in);
	if(c == ')')
	{
		c = fgetc(in);
		while (c == ' ')
			c = fgetc(in);
		if (c == ',')
		{
			c = fgetc(in);
			nr2 = factor (ct + 1, nr);
		}
		if (nr2 <0)
			return nr2;
		if (nr % ct == 0 )
			return -numar;
		return ct;
	}
		while (c == ' ')
		c = fgetc(in);

	while (c == '(')
	{
		c = fgetc(in);
		aux = expresie(numar);
		numar = aux;
	}
	while (c == ' ')
		c = fgetc(in);
	c = fgetc(in);
	return -numar;
}
int main ()
{
	c = fgetc(in);
	fprintf(out, "%d", -factor(1, -1));
	return 0;
}