/*****************************************************/
/*                 lecture d'un polynome             */
/*                    dans un fichier                */
/*****************************************************/

#include "lecture_polynome.h"


polynome lecture_polynome(char *nom)
{
  FILE *flot;
  int x, d, c = '+';
  polynome P = NULL;
  
  if ((flot = fopen(nom, "r")) == NULL)
    {
      fprintf(stderr,"\nErreur: impossible d'ouvrir le fichier %s\n", nom);
      return(NULL);
    }
  do
    {
      if (c == '-')
	ungetc(c, flot);
      c = getc(flot);
      if (c == 'x')
	{
	  x = 1;
	  c = getc(flot);
	  if (c == '^')
	    {
	      fscanf(flot, "%d", &d);
	      c = getc(flot);
	    }
	  else
	    d = 1;
	}
      else
	{
	  ungetc(c, flot);
	  fscanf(flot, "%d", &x);
	  c = getc(flot);
	  if (c == 'x')
	    {
	      c = getc(flot);
	      if (c == '^')
		{
		  fscanf(flot, "%d", &d);
		  c = getc(flot);
		}
	      else
		d = 1;
	    }
	  else
	    d = 0;
	}
      P = constructeur(x, d, P);
    }
  while(c == '+' || c == '-');
  return(P);
}
