Cod sursa(job #507095)

Utilizator grozaviorelGroza Viorel Mihai grozaviorel Data 5 decembrie 2010 14:44:34
Problema Reuniune Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 2.23 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define RECTS_COUNT (3)

typedef struct
	{
		int left;
		int top;
		int right;
		int bottom;
	}
	RECT;	
	

	
int GetUnionInfo(int RectsCount, RECT* pRects, int* UnionPerimeter, int* UnionArea)
{
	(*UnionPerimeter) = 0;
	(*UnionArea) = 0;
	
	RECT UnionRectangle;
	memset(&UnionRectangle, 0, sizeof(UnionRectangle));
	
	int RectIndex = 0;
	
	if (RectsCount > 0)
	{
		UnionRectangle.left = (pRects[0].left);
		UnionRectangle.top = (pRects[0].top);
		UnionRectangle.right = (pRects[0].right);
		UnionRectangle.bottom = (pRects[0].bottom);
		
		for (RectIndex = 1; RectIndex < RectsCount; RectIndex++)
		{
			if (UnionRectangle.left > (pRects[RectIndex].left))
				UnionRectangle.left = pRects[RectIndex].left;
			if (UnionRectangle.top > (pRects[RectIndex].top))
				UnionRectangle.top = pRects[RectIndex].top;
			if (UnionRectangle.right < (pRects[RectIndex].right))
				UnionRectangle.right = pRects[RectIndex].right;
			if (UnionRectangle.bottom < (pRects[RectIndex].bottom))
				UnionRectangle.bottom = pRects[RectIndex].bottom;
		}
	}
	
	(*UnionPerimeter) = 2 * ((UnionRectangle.right - UnionRectangle.left) + (UnionRectangle.bottom - UnionRectangle.top));
	(*UnionArea) = (UnionRectangle.right - UnionRectangle.left) * (UnionRectangle.bottom - UnionRectangle.top);
	
	return 0;
}

int main()
{	
	FILE* fInput  = NULL;
	FILE* fOutput = NULL;
	
	fInput  = fopen("reuniune.in", "r");
	if (fInput == NULL)
		return 0;
	
	fOutput = fopen("reuniune.out", "w");
	if (fOutput == NULL)
		return 0;
	
	int RectsCount = RECTS_COUNT;
	RECT* pRects = (RECT*)malloc(RectsCount * sizeof(RECT));
	memset(pRects, 0, RectsCount * sizeof(RECT));
	
	int RectIndex = 0;
	for (RectIndex = 0; RectIndex < RectsCount; RectIndex++)
	{
		fscanf(fInput, "%d%d%d%d", &(pRects[RectIndex].left), &(pRects[RectIndex].top), &(pRects[RectIndex].right), &(pRects[RectIndex].bottom));
	}
	
	int UnionPerimeter = 0;
	int UnionArea = 0;
	
	GetUnionInfo(RectsCount, pRects, &UnionPerimeter, &UnionArea);
	
	fprintf(fOutput, "%d %d", UnionArea, UnionPerimeter);
		
	free(pRects);
	pRects = NULL;
	
	fclose(fInput);
	fclose(fOutput);
	
	return 0;
}