#include
#include

long int c[2][100]={0};
int k=0;

void add(long int x[2][100],long int y[2][100]);
void type(long int a[2][100],int l);
void search(long int a[2][100],int h);

int main()
{

 int i=0,j=0,l=-1,r=-1,w;
 long int a[2][100]={0},b[2][100]={0},e[2][100]={0},f[2][100]={0};

 clrscr();
 printf("\n\n\t   A(x) :a(n)X^e(n) + a(n-1)X^e(n-1) + ... + a(1)X^e(1) \n");
 printf("\n\t\t\t For exit : ( 0 & 0 ) ");
 printf("\n\t***********************************************************\n\n");
 do
 {
    printf(" Coeff  %i  of A(x) : ",i+1);
    scanf("%li",& a[1][i]);
    printf("\n Square  %i  of A(x) : ",i+1);
    scanf("%li",& a[0][i]);
    printf(" ----------------------------------------------------------------------------\n");
    i++;l++;
 }while(a[1][i-1]!=0 );
 search(a,l);
 printf("\n\n\tA(x): ");
 type(a,l);
 printf("\n\n\n\n\t\t     Press any key to continue ...");
 getch();
 clrscr();
 printf("\n\n\t   B(x) :b(n)X^e(n) + b(n-1)X^e(n-1) + ... + b(1)X^e(1) \n");
 printf("\n\t\t\t For exit : ( 0 & 0 ) ");
 printf("\n\t***********************************************************\n\n");
 do
 {
    printf(" Coeff  %i  of B(x) : ",j+1);
    scanf("%li",& b[1][j]);
    printf("\n Spuare  %i  of B(x) : ",j+1);
    scanf("%li",& b[0][j]);
    printf(" ----------------------------------------------------------------------------\n");
    j++;r++;
 }while(b[1][j-1]!=0);
 search(b,r);
 printf("\n\n\tB(x): ");
 type(b,r);
 printf("\n\n\n\n\t\t     Press any key to continue ...");
 getch();
 clrscr();
 printf("  ***************************************************************************");
 printf("\n\n    A(x) =");
 type(a,l);
 printf("\n\n  ***************************************************************************");
 printf("\n\n    B(x) =");
 type(b,r);
 add(a,b);
 printf("\n\n  ***************************************************************************");
 printf("\n\n    A(x) + B(x) =");
 type(c,k);
 for(i=0;i {
     c[1][i]=0;
     c[0][i]=0;
 }
 for(i=0;i {
     for(j=0;j     {
 k=0;
 e[1][j]=a[1][i]*b[1][j];
 e[0][j]=a[0][i]+b[0][j];
     }
     add(e,f);
     for(w=0;w     {
 f[1][w]=c[1][w];
 f[0][w]=c[0][w];
     }
 }
 printf("\n\n  ***********************************************************");
 printf("\n\n    A(x) * B(x) =");
 type(c,k);
 printf("\n\n  ***************************************");
 printf("\n\n\n\n\n\t\t\t Press any key to exit ...");
 getch();
 return(0);
}

/*************** FUNCTION ADD *****************************/

void add(long int x[2][100],long int y[2][100])
{

  int i=0,j=0;

  do
  {
  if(x[0][i]==y[0][j])
   {
     c[0][k]=x[0][i];
     c[1][k]=x[1][i]+y[1][j];
     k++;i++;j++;
   }
  if(x[0][i] > y[0][j])
   {
     c[0][k]=x[0][i];
     c[1][k]=x[1][i];
     k++;i++;
   }
  if(x[0][i] < y[0][j])
   {
     c[0][k]=y[0][j];
     c[1][k]=y[1][j];
     k++;j++;
   }
  }while(x[1][i]!=0 || y[1][j]!=0);
}

/********* FUNCTION TYPE ******************/

void type(long int a[2][100],int l)
{

   int i=0;

   if(a[1][0]==0)
       printf(" 0");
   while(a[1][i]!=0)
   {
    for(i=0;i    {
    if(a[1][i]>0 && a[1][i]!=a[1][0])
 printf(" +");
    if(a[0][i]==1 && a[1][i]==1)
 printf(" x");
    if(a[0][i]==1 && a[1][i]==-1)
 printf(" - x");
    if(a[1][i]==1 && a[0][i]!=0 && a[1][i]!=0 && a[0][i]!=1)
 printf(" x^%li",a[0][i]);
    if(a[1][i]==-1 && a[0][i]!=0 && a[1][i]!=0 && a[0][i]!=1)
 printf(" - x^%li",a[0][i]);
    if(a[0][i]==0 && a[1][i]!=0)
 printf(" %li",a[1][i]);
    if(a[0][i]==1 && a[1][i]!=0 && a[1][i]!=1 && a[1][i]!=-1)
 printf(" %lix",a[1][i]);
    if(a[1][i]!=1 && a[1][i]!=-1 && a[0][i]!=0 && a[1][i]!=0 && a[0][i]!=1)
 printf(" %lix^%li",a[1][i],a[0][i]);
    }
   }
}

/************************** FUNCTION SEARCH ***************/

void search(long int a[2][100],int h)
{

 int i,j,g,item1,item2;

 for(i=h;i>0;i--)
    for(j=0;j       if(a[0][j]       {
   item1=a[0][j];
   a[0][j]=a[0][j+1];
   a[0][j+1]=item1;
   item2=a[1][j];
   a[1][j]=a[1][j+1];
   a[1][j+1]=item2;
       }

 for(i=0;i    for(j=0;j       if(a[0][j]==a[0][j+1])
       {
    a[1][j]+=a[1][j+1];
    for(g=j+1;g    {
        a[1][g]=a[1][g+1];
        a[0][g]=a[0][g+1];
    }
       }
}

/*********** END PROGRAM *****************/