code contest

         Water Allocation

A. First Edition
This is the first problem of a code contest and I am frustrated at the fact that I cannot even finish the very
first problem within three hours.  
B.The problem

PROB1: WATER

DESCRIPTION

INPUT: prob1.dat

OUTPUT: prob1.out

 

C.The idea of program
 

At first, I didn't notice the problem of double quotes which is quite annoying. I really dislike "fstream" as it

can only do the very simple job and for the more detailed job, you have to use "FILE" pointer.

Then, I repeatedly forget to initialize all data of "states" such as "rank", "requires"... which cost me another

half hour for debugging, maybe longer than that?

D.The major functions
There are few points to mention.
 
E.Further improvement
 
F.File listing
1. water.cpp
 
file name: water.cpp
#include <iostream>
#include <fstream>
using namespace std;

const int MaxNameLength=80;
const int MaxContractors=30;

struct Contractor
{
	char name[MaxNameLength];
	int priority;
	long quota;
	long allocated;
};

//end is the bound of ending, exclusive
void allocate(int start, int end, long curVol, long requires);
void readName(char* buffer);
//void readNumber(int& number);
void readNumber(long& number);

Contractor groups[MaxContractors];
char buffer[MaxNameLength];
FILE* in;


int main()
{
	char resource[MaxNameLength];
	//bool newStart=false;
	long totalVolume=0;
	int rank=1;
	long requires=0;
	long curVol;
	int index=0, start=0;
	ofstream out;
	in=fopen("prob1.dat", "r");
	out.open("prob1.out", ios::out);
	out<<"program 1 by team "<<0<<"\n";
	while (!feof(in))
	{
		readName(resource);
		//fscanf(in, "%d,", totalVolume);
		if (strcmp(resource, "\"END\"")==0)
		{
			break;
		}
		readNumber(totalVolume);
		//in>>resource;
		//in>>totalVolume;
		
		curVol=totalVolume;
		while (!feof(in))
		{
			readName(groups[index].name);
			if (strcmp(groups[index].name, "\"END\"")!=0)
			{				
				//fscanf(in, "%,d,", groups[index].priority);
				readNumber((long&)groups[index].priority);
				//fscanf(in, "%,d\n", groups[index].quota);
				readNumber(groups[index].quota);

				if (groups[index].priority>rank)
				{
					allocate(start, index, curVol, requires);
					curVol-=requires;
					if (curVol<0)
					{
						curVol=0;
					}
					requires=groups[index].quota;
					start=index;
					rank=groups[index].priority;
					index++;			
				}
				else
				{
					requires+=groups[index].quota;
					index++;
				}
			}
			else
			{
				allocate(start, index, curVol, requires);
				break;
			}
		}
		out<<resource<<" , "<<totalVolume<<"\n";
		for (int i=0; i<index; i++)
		{
			out<<groups[i].name<<" ,"<<groups[i].allocated<<"\n";
		}
		index=0;
		start=0;
		rank=1;
		requires=0;
	}

	return 0;
}



void readNumber(long& number)
{
	char buffer[MaxNameLength];
	char ch;
	int index=0;
	bool started=false;
	while (!feof(in))
	{
		if (started)
		{
			if (ch>='0'&&ch<='9')
			{
				buffer[index++]=ch;
			}
			else
			{
				buffer[index]='\0';
				break;
			}
		}
		if (!started&&ch<='9'&&ch>='0')
		{
			buffer[index++]=ch;
			started=true;
		}
		ch=fgetc(in);
	}
	number=atoi(buffer);
}




void readName(char* buffer)
{
	int index=0;
	char ch;
	bool started=false;
	ch=fgetc(in);

	while (!feof(in))
	{
		
		if (started)
		{	
			if (ch=='"')
			{
				buffer[index++]=ch;
				buffer[index]='\0';
				break;
			}
			else
			{
				buffer[index++]=ch;
			}
		}

			
		if (!started&&ch=='"')
		{
			index=0;
			started=true;
			buffer[index++]=ch;			
		}
		ch=fgetc(in);
	}
}


		



void allocate(int start, int end, long curVol, long requires)
{
	bool percent;
	percent=requires>curVol;
	
	for (int i=start; i<end; i++)
	{
		if (curVol>0)
		{
			if (percent)
			{
				groups[i].allocated=curVol*((double)groups[i].quota/requires);
			}
			else
			{
				groups[i].allocated=groups[i].quota;
			}
		}
		else
		{
			groups[i].allocated=0;
		}

	}
}


  
The input file "prob1.dat" is like following:

"Le River",1000000
"People",1,500000
"Farms",1, 250000
"Mine",2,700000
"Golf Course",2,300000
"Car Wash", 3,100000
"END",0,0
"Le Lake",1000000
"A",1,500000
"B",1,250000
"D",2,300000
"E",3,100000
"END",-1,0
"END",0




The result is like following:
program 1 by team 0
"Le River" , 1000000
"People" ,500000
"Farms" ,250000
"Mine" ,175000
"Golf Course" ,75000
"Car Wash" ,0
"Le Lake" , 1000000
"A" ,500000
"B" ,250000
"D" ,250000
"E" ,0
 
			
				 back.gif (341 bytes)       up.gif (335 bytes)         next.gif (337 bytes)