Wednesday, August 21, 2013

Represent polynomial using structures and write a menu driven program to perform Addition, Multiplication and Evaluation


/*--------------------------------------------------------------------------------------------------
Program    : Represent polynomial using structures and write a menu
                     driven program to perform Addition, Multiplication and
                     Evaluation.

Created By : JABIR DAUD PATHAN
Branch        : COMPUTER ENGINEERING
----------------------------------------------------------------------------------------------------*/


/*--------------------<<  Header File  >>------------------------*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

/*-------------<< Global Declaration of Functions  >>------------*/

void accept(void);
void add(void);
void multiply(void);
void evaluate(void);

struct poly
 {
  int coef;
  int expo;
 }p1[10],p2[10],p3[10],p4[100],p5[100];

int t1,t2,i,j,k,no1,no2,ch;  //Global Declaration of variables

/*----<< function defination for accept two polynomials >>-------*/

void accept()
 {
  printf("\nEnter how many terms you want in p1==>");
  scanf("%d",&t1);
  printf("\nEnter the coef and expo of p1==>\n");
  for(i=0;i<t1;i++)
   {
    scanf("%d%d",&p1[i].coef,&p1[i].expo);
                                           //accept coef and expo of poly p1
   }
  printf("\nPolynomial p1 is==>");
  for(i=0;i<t1;i++)
   {
    printf(" %dx^(%d)",p1[i].coef,p1[i].expo); //display poly p1
    if(i<t1-1)
     {
      printf(" +");
     }
   }
  printf("\n\nEnter how many terms you want in p2==>");
  scanf("%d",&t2);
  printf("\nEnter the coef and expo of p2==>\n");
  for(j=0;j<t2;j++)
   {
    scanf("%d%d",&p2[j].coef,&p2[j].expo);
                                          //accept coef and expo of poly p2
   }
  printf("\nPolynomial p2 is==>");
  for(j=0;j<t2;j++)
   {
    printf(" %dx^(%d)",p2[j].coef,p2[j].expo);//display poly p2
    if(j<t2-1)
     {
      printf(" +");
     }
   }
 }

/*----<< function defination for addition of two polynomials >>----*/

void add()
 {
  int i=0,j=0,k=0;
  printf("\n\nAddition of polynomial is==>\n\n");
  while(i<t1&&j<t2)
   {
    if(p1[i].expo==p2[j].expo) //check expo of both poly are equal
     {
       p3[k].coef=p1[i].coef+p2[j].coef; //add the coef of both poly
       p3[k].expo=p1[i].expo;  //assign expo of p1 to p3
       i++;
       j++;
       k++;
     }
  else if(p1[i].expo>p2[j].expo)//check expo of p1 is greater than p2
            {
             p3[k].coef=p1[i].coef; //assign coef of p1 to p3
             p3[k].expo=p1[i].expo; //assign expo of p1 to p3
             i++;
             k++;
            }
  else if(p1[i].expo<p2[j].expo)//check expo of p1 is less than p2
            {
             p3[k].coef=p2[j].coef; //assign coef of p2 to p3
             p3[k].expo=p2[j].expo; //assign expo of p2 to p3
             j++;
             k++;
            }
   }
   while(i<t1)
    {
      p3[k].coef=p1[i].coef;
      p3[k].expo=p1[i].expo;
      k++;
      i++;
    }
   while(j<t2)
    {
      p3[k].coef=p2[j].coef;
      p3[k].expo=p2[j].expo;
      j++;
      k++;
    }
   for(i=0;i<k;i++)
    {
     printf(" %dx^(%d)",p3[i].coef,p3[i].expo); //display poly p3
     if(i<k-1)
      {
       printf(" +");
      }
    }
 }

/*--<<function defination for multiplication of two polynomials>>--*/

void multiply()
 {
  for(i=0;i<t1;i++)
   {
    for(j=0;j<t2;j++)
     {
      p4[k].coef=p2[j].coef*p1[i].coef;  //multiply coef of p1 & p2
      p4[k].expo=p2[j].expo+p1[i].expo;  //add expo of p1 & p2
      k++;
     }
   }
  for(i=1;i<k;i++)
   {
    for(j=i+1;j<k;j++)
     {
      if(p4[i].expo==p4[j].expo)
       {
               p4[i].coef=p4[i].coef+p4[j].coef;
               p4[j].coef=0;
       }
     }
   }
  printf("\n\nMultiplication of polynomial is==>\n\n");
  for(i=0;i<k;i++)
   {
    if(p4[i].coef!=0)
     {
      printf("%dx^(%d)",p4[i].coef,p4[i].expo); //display poly p4
      if(i<k-1)
       {
                printf("+");
       }
     }
   }
 }

/*---<< function defination for evaluation of two polynomials >>---*/

void evaluate()
 {
  int i,x,y,sum=0;
  printf("\n\nenter the value of x for p1:");
  scanf("%d",&x); //accept the value of x from user
  for(i=0;i<t1;i++)
   {
    sum=sum+p1[i].coef*pow(x,p1[i].expo);
                         //formula for evaluation of polynomial
   }
  printf("\np1[for x=%d]=%d",x,sum);
  sum=0;
  printf("\n\nenter the value of x for p2:");
  scanf("%d",&y);
  for(i=0;i<t2;i++)
   {
    sum=sum+p2[i].coef*pow(y,p2[i].expo);
   }
  printf("\np2[for x=%d]=%d",y,sum);
}





/*----------<<  Main Function Start  >>-------------*/

void main()
 {
  char ans; //Variable declaration
  do
   {
    clrscr(); //Clear the text mode of window
    printf("\n\t\t\t------<<  MENU  >>------");
    printf("\n\n\t\t\t1.Add polynomial");
    printf("\n\t\t\t2.Multiply polynomial");
    printf("\n\t\t\t3.Evaluate polynomial");
    printf("\n\t\t\t4.Exit");
    printf("\n\n\t\t\t------------------------");
    printf("\n\n\t\t\tEnter your choice==>");
    scanf("%d",&ch); //accept choice from user
    switch(ch)
     {
      case 1 :
                      accept(); //function call
                      add();    //function call
                      break;

      case 2 :
                      accept();
                      multiply();
                      break;

      case 3 :
                      accept(); 
                      evaluate();
                      break;

      case 4 :
                      exit(0);
                      break;

      default:
                      printf("\n\tInvalid choice");
                      break;

     }
     printf("\n\n\t\t Do you Want to Continue(Y/N)?==>");
     ans=getch();
   }while(ans=='y'||ans=='Y');
    getch(); 
 }
/*----------<<  End Of Main Function  >>-----------*/






/*-----------------<< OUTPUT SCREEN  >>-------------------*/


                        ------<<  MENU  >>------
                        1.Add polynomial
                        2.Multiply polynomial
                        3.Evaluate polynomial
                        4.Exit
                        ------------------------

                        Enter your choice==>1

Enter how many terms you want in p1==>4

Enter the coef and expo of p1==>
23
6
11
4
9
1
5
0

Polynomial p1 is==> 23x^(6) + 11x^(4) + 9x^(1) + 5x^(0)

Enter how many terms you want in p2==>3

Enter the coef and expo of p2==>
3
2
9
1
6
0

Polynomial p2 is==> 3x^(2) + 9x^(1) + 6x^(0)

Addition of polynomial is==>

 23x^(6) + 11x^(4) + 3x^(2) + 18x^(1) + 11x^(0)

                 Do you Want to Continue(Y/N)?==>Y



                        ------<<  MENU  >>------
                        1.Add polynomial
                        2.Multiply polynomial
                        3.Evaluate polynomial
                        4.Exit
                        ------------------------

                        Enter your choice==>2

Enter how many terms you want in p1==>4

Enter the coef and expo of p1==>
2
6
3
4
9
1
5
0

Polynomial p1 is==> 2x^(6) + 3x^(4) + 9x^(1) + 5x^(0)

Enter how many terms you want in p2==>3

Enter the coef and expo of p2==>
2
2
3
1
6
0

Polynomial p2 is==> 2x^(2) + 3x^(1) + 6x^(0)

Multiplication of polynomial is==>

4x^(8)+6x^(7)+18x^(6)+9x^(5)+18x^(4)+18x^(3)+37x^(2)+69x^(1)+30x^(0)


                 Do you Want to Continue(Y/N)?==>Y





                        ------<<  MENU  >>------
                        1.Add polynomial
                        2.Multiply polynomial
                        3.Evaluate polynomial
                        4.Exit
                        ------------------------

                        Enter your choice==>3

Enter how many terms you want in p1==>3

Enter the coef and expo of p1==>
2
2
3
1
2
0

Polynomial p1 is==> 2x^(2) + 3x^(1) + 2x^(0)

Enter how many terms you want in p2==>3

Enter the coef and expo of p2==>
3
2
2
1
4
0

Polynomial p2 is==> 3x^(2) + 2x^(1) + 4x^(0)

enter the value of x for p1:1

p1[for x=1]=7

enter the value of x for p2:2

p2[for x=2]=20


                 Do you Want to Continue(Y/N)?==>Y



                        ------<<  MENU  >>------
                        1.Add polynomial
                        2.Multiply polynomial
                        3.Evaluate polynomial
                        4.Exit
                        ------------------------

                        Enter your choice==>4







No comments:

Post a Comment