/******************************************
*
* Cedric Pradalier 2001
* mail : http://cedric.pradalier.free.fr/mail.html
*
*****************************************/
#include "output.h"
#include <stdlib.h>
#include <stdio.h>
#include "horaire.h"
/*****************************************
Implementation des horaires
*****************************************/
/* Generic == 88:88 */
int CHoraire::IsGeneric()
{
return (heure == 88) && (minute == 88);
}
int CHoraire::IsValide()
{
return (((heure >= 0) && (heure <=24)
&& (minute >= 0) && (minute <= 59)))
|| IsGeneric();
}
bool CHoraire::operator>(CHoraire h2)
{
return ((heure > h2.heure) ||
((heure == h2.heure) && (minute>h2.minute)));
}
int CHoraire::operator<=(CHoraire h2)
{
return ((heure < h2.heure) ||
((heure == h2.heure) && (minute <= h2.minute)))
/* 23H00 est avant 00H30 */
|| ((heure > 3) && (h2.heure < 3))
/* l'horaire generic est inferieur a n'importe quoi */
|| IsGeneric();
}
int CHoraire::operator!=(CHoraire h2)
{
return (heure != h2.heure) || (minute != h2.minute);
}
CHoraire CHoraire::operator+(CHoraire h2)
{
CHoraire res;
int sigma = minute + h2.minute;
res.heure = heure + h2.heure + (sigma / 60);
res.minute = sigma % 60;
return res;
}
CHoraire CHoraire::operator-(CHoraire h2)
{
CHoraire res;
int sigma = minute - h2.minute;
if (sigma<0)
{
sigma+=60;
h2.heure++;
}
res.heure = heure - h2.heure - (sigma / 60);
if (res.heure<0)
res.heure+=24;
res.minute = sigma % 60;
return res;
}
CHoraire CHoraire::operator*(int lambda)
{
CHoraire res;
int sigma = lambda * minute;
res.heure = lambda * heure + (sigma / 60);
res.minute = sigma % 60;
return res;
}
CHoraire CHoraire::operator/(int lambda)
{
CHoraire res;
int sigma = (100 * (60 * heure + minute)) / lambda;
if ((sigma % 100) > 50)
sigma = (sigma / 100) + 1;
else
sigma /= 100;
res.heure = sigma / 60;
res.minute = sigma % 60;
return res;
}
/* interpolation lineaire entre 2 arrets . Formule : .h */
CHoraire CHoraire::Interpol(CHoraire h2,int mult,int div)
{
if (!(IsValide() && h2.IsValide()))
return HNULL;
return (*this) + ((h2-(*this))*mult)/div;
}
void CHoraire::Print()
{
if (IsValide())
if (IsGeneric())
{
sprintf(outbuf,"**:**");
FlushBuffer();
}
else
{
sprintf(outbuf,"%02i:%02i",heure,minute);
FlushBuffer();
}
else
{
sprintf(outbuf,"--:--");
FlushBuffer();
}
}
/*
void main()
{
CHoraire h2(0,0), h1(11,16), h3;
h1.Print();sprintf(outbuf,"\n");FlushBuffer();
h2.Print();sprintf(outbuf,"\n");FlushBuffer();
sprintf(outbuf,"Comparison h1 <= h2 : %i\n",h1 <= h2);FlushBuffer();
sprintf(outbuf,"Vive les shtroumpf!!!\n");FlushBuffer();
}
*/