// cMoeda.cpp   
// 17 de Janeiro de 2001  , Aula de LP1

#include "cMoeda.h"
#include <string.h>
#include <stdlib.h>

// Classe cMoeda

cMoeda::cMoeda( double Val )
{
	Valor = Val;
}

double cMoeda::GetValor()
{
	return Valor;
}

ostream& operator<<( ostream& o, cMoeda& Moeda )
{
	return Moeda.Escrever(o);
}

istream& operator>>( istream& i, cMoeda& Moeda )
{
	return Moeda.Ler(i);
}


// Classe cEscudo


cEscudo::cEscudo( double Val ) : cMoeda( Val )
{

}

cEscudo::cEscudo( cEuro & Euro )
{
	Valor = 200.482 * Euro.GetValor() ; 
}


ostream& cEscudo::Escrever( ostream& o )
{
	int  decimal, sign, i;   
	char *buffer;
    

	buffer = _fcvt( Valor, 2, &decimal, &sign );

	i=0;
	while ( i < decimal ) 
	{
		o << buffer[i];
		i = i+1;
	}

	o << '$';

	o << buffer [i] << buffer[i+1] << "Esc"; 
	

	
	return o;
}



istream& cEscudo::Ler( istream &i ) 
{
	char s[15];
	char *p;

	cout << "Esc : ";
	i >> s;

	p = strchr( s, '$' );

	if ( p ) 
	{
		*p = '.';
		Valor = atof(s);
	}

	return i;
}



// Classe Euro


cEuro::cEuro( double Val ) : cMoeda( Val )
{

}


ostream& cEuro::Escrever( ostream& o )
{
	int  decimal, sign, i;   
	char *buffer;
    

	buffer = _fcvt( Valor, 2, &decimal, &sign );

	i=0;
	while ( i < decimal ) 
	{
		o << buffer[i];
		i = i+1;
	}

	o << ',';

	o << buffer [i] << buffer[i+1] << " Euros" ;
	

	
	return o;
}





istream& cEuro::Ler( istream &i )
{
	char s[15];
	char *p;

	cout << "Euro : ";
	i >> s;

	p = strchr( s, ',' );

	if ( p ) 
	{
		*p = '.';
		Valor = atof(s);
	}

	return i;

}


cEuro::cEuro( cEscudo & Escudo )
{
	Valor = Escudo.GetValor() / 200.482;
}





