/* test_conversion.c
   auteurs :
   groupe : 
   date :
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

void cartesien_vers_polaire(double x,double y, double r, double theta/* a modifier*/)
  {
  /* a completer */
  }

void polaire_vers_cartesien(double r, double theta,double x, double y/* a modifier*/) 
  {
  /* a completer */
  }



void initialiser_hasard(void);
double reel_uniforme_0_1(void);
double reel_uniforme(double borne_inf, double borne_sup);
int hasard_Bernouilli(double proba);

void usage(char *argv0);


int main(int argc,char* argv[]) 
  {
  double x,y,x2,y2;
  double rho,phi;
  int i,nb_erreurs,nb_tests=30;

  if (argc==4 && strcmp(argv[1],"cp")==0)
	{
	x=atof(argv[2]);
	y=atof(argv[3]);
        cartesien_vers_polaire(x,y, rho,phi/* a modifier*/);
        printf("(x,y) = [%lg,%lg] ===> (rho,phi) = (%lg,%lg)\n",
		x,y,rho,phi);
  	}

  else if (argc==4 && strcmp(argv[1],"pc")==0)
	{
	rho=atof(argv[2]);
	phi=atof(argv[3]);
        polaire_vers_cartesien(rho,phi,x,y /* a modifier*/);
        printf("(x,y) = [%lg,%lg] <=== (rho,phi) = (%lg,%lg)\n",
		x,y,rho,phi);
  	}

  else if (argc==2 && strcmp(argv[1],"autotest")==0)
	{
	initialiser_hasard();
	nb_erreurs=0;
	for (i=0;i<nb_tests;i++)
		{
		x=reel_uniforme(-10.0,10.0) * hasard_Bernouilli(0.8);
		y=reel_uniforme(-10.0,10.0) * hasard_Bernouilli(0.8);
	        cartesien_vers_polaire(x,y, rho,phi /* a modifier*/);
	        printf("(x,y)   = [%lg,%lg] ===> (rho,phi) = (%lg,%lg)\n"
			,x,y,rho,phi);
		polaire_vers_cartesien(rho,phi,x2,y2/* a modifier */);
        	printf("(x2,y2) = [%lg,%lg] <=== (rho,phi) = (%lg,%lg)\n",
			x2,y2,rho,phi);

		if (fabs(x2-x)+fabs(y2-y)>1e-4)
			{			
			printf ("ERREUR\n");
			nb_erreurs++;
			}
		printf("\n");
		}
	printf("Resultat du test : %d erreurs / %d tests\n",
		nb_erreurs, nb_tests);
  	}

  else
	
	{
	usage(argv[0]);
	return 1;
	}


  return 0;
  }

void usage(char *argv0)
  {
  printf("usage :\n"
		"    %s cp <x> <y> \n"
		"    %s pc <r><phi> \n"
		"    %s autotest\n",
		argv0,argv0,argv0);
  }


void initialiser_hasard(void)
  {
  srand(time(NULL));
  }

double reel_uniforme_0_1(void) 
  {
  return rand()/(RAND_MAX+1.0);
  }

double reel_uniforme(double borne_inf, double borne_sup)
  {
  return reel_uniforme_0_1()*(borne_sup-borne_inf)+borne_inf;
  }

int hasard_Bernouilli(double proba)
  {
  if (reel_uniforme_0_1()<proba)
	return 1;
  else
	return 0;
  }

