HW 2

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

int IsZero(polynomial p)

{
for (int i = p.degree; i >= 0; i--)
{
if (p.coef[i] != 0) return 0;
else if (i == 0 ) return 1;
}
}

int COMPARE(int i, int j)


{
if (i < j)
return -1;

else if (i == j)
return 0;

else
return 1;
}

polynomial Attach(polynomial p, int coef, int expo)


{
if (p.degree < expo) p.degree = expo;
p.coef[expo] = coef;
return p;
}

polynomial Remove(polynomial p, int expo)//3 5 2 2


{

p.degree--;
return p;
}

void PrintPoly2(polynomial_term t[], int start, int finish)


{
polynomial_term a[2];
for (int i = start; i < finish; i++)
for (int j = i; j <= finish; j++)
if (t[i].expo < t[j].expo)
{
a[0] = t[i];
t[i] = t[j];
t[j] = a[0];
}
for (int i = start; i <= finish; i++)
{
if (i == start)
{
if (t[i].expo == 0)
printf("%d", t[i].coef);

else
printf("%dX^%d", t[i].coef, t[i].expo);
}
else
{
if (t[i].expo == 0)
printf(" + %d", t[i].coef);

else
printf(" + %dX^%d", t[i].coef, t[i].expo);
}
}

You might also like