1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include "exposant_multiple.h"
unsigned int max(unsigned int i, unsigned int j)
{
return (i>j)?i:j;
}
exposant_multiple::exposant_multiple()
{
//ctor
nombre_de_variables=0;
exposants=NULL;
}
exposant_multiple& exposant_multiple::operator= (exposant_multiple& other)
{nombre_de_variables=other.nombre_de_variables;
unsigned int * nouveaux_exposants=new unsigned int [nombre_de_variables];
for (unsigned int i=0;i<nombre_de_variables;i++)
nouveaux_exposants[i]=other.exposants[i];
delete [] exposants;
exposants=nouveaux_exposants;
return *this;
}
exposant_multiple::exposant_multiple(int n)
{
//ctor
nombre_de_variables= n;
if (!n)
exposants=NULL;
else
exposants= new unsigned int [n];
}
exposant_multiple::exposant_multiple(exposant_multiple& other)
{
//ctor
nombre_de_variables= other.nombre_de_variables;
if (!nombre_de_variables)
exposants=NULL;
else
exposants = new unsigned int [nombre_de_variables];
for (unsigned int i=1;i<=nombre_de_variables;i++)
(*this)[i]=other[i];
}
exposant_multiple::~exposant_multiple()
{
if(exposants)
delete[] exposants;
}
bool exposant_multiple::operator==(exposant_multiple& other)
{
if (nombre_de_variables!=other.nombre_de_variables)
return false;
else
for (unsigned int i=1; i<=nombre_de_variables;i++)
if ((*this)[i]!=other[i]) return false;
return true;
}
unsigned int& exposant_multiple::operator[] (unsigned int i)
{
static unsigned int zero=0;
if (i<=nombre_de_variables)
return exposants[i-1];
else
return zero;
}
exposant_multiple& exposant_multiple::operator+ (exposant_multiple &other)
{
unsigned int n=max(nombre_de_variables,other.nombre_de_variables);
exposant_multiple * result =new exposant_multiple(n);
if (n>0)
{
for (unsigned int i=1;i<=n;i++)
(*result)[i]=(*this)[i]+other[i];
}
return *result;
}
ostream& operator<< (ostream& os, exposant_multiple e)
{
char in[3];
char exp[3];
if (!e.nombre_de_variables)
os<<string("1");
else
{
for (unsigned i=1;i<=e.nombre_de_variables;i++)
{
if (e[i])
{
os<<string("X");
sprintf(in,"%d",i);
os<<string(in);
if (e[i]>1)
{
sprintf(exp,"%d",e[i]);
os<<string("^")<<string(exp);
}
}
}
}
return os;
} |