Cod sursa(job #560986)

Utilizator alyn_acalin alin alyn_ac Data 18 martie 2011 19:45:04
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include<fstream.h>
#include<iostream.h>
typedef struct
{
	char c[22];
	int k;
}ingredient;
char a[1005];
char b[5];
int main()
{
	ifstream fin("reteta2.in");
	ofstream fout("reteta2.out");
	int n,x=0,i=0,s=0,p=0,y=0,j;
	ingredient el[200],aux;
	fin.get(a,1005);
	n=strlen(a);
	while(i<n)
	{
		while(i<n&&(a[i]<'a' || a[i]>'z') )
			i++;
		y=0;
		while(a[i]>='a' && a[i]<='z' )
		{
			el[p].c[y]=a[i]; i++; y++;
		}
		el[p].c[y]='\0';
		while(a[i]==' ')
			i++;
		while(a[i]<='9' &&a[i]>='0')
		{
			b[x]=a[i]; x++; i++;
		}
		b[x]='\0'; x=0; el[p].k=atoi(b); p++;
	}
	//for(i=0;i<p-1;i++)
		//cout<<el[i].c<<' '<<el[i].k;
	for(i=0;i<p-2;i++)
		for(j=i+1;j<p-1;j++)
		{
			if(strcmp(el[i].c,el[j].c)>0)
			{
				aux=el[i]; el[i]=el[j]; el[j]=aux;
			}
		}
	
	i=0;
	x=0;
	while(i<n)
	{
		while(a[i]!=')')
			i++;
		i++;
		while(a[i]==' ')
			i++;
		
		while(a[i]>='0' && a[i]<='9')
		{
			b[x]=a[i]; i++; x++;
		}
		b[x]='\0'; s+=atoi(b); x=0;
	}
	fout<<s<<'\n';
	x=0;
	for(i=0;i<p-1;i++)
	{
		x=i;
		while(strcmp(el[i].c,el[i+1].c)==0)
			{ el[x].k+=el[i+1].k;i++; }
		fout<<el[x].c<<" "<<el[x].k<<'\n';
	}
	return 0;
}