Wednesday, 16 October 2013

polynomial

Addition of polynomial


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class poly
{
int exp;
int coeff;
poly *next;
public:
void input1();
void input2();
void display();
void add();
};
poly *head1,*head2,*head3;

void poly::input1() //Input for Polynomial 1
{
int degree;
cout<<endl<<"degree of d polynomial : ";
cin>>degree;
int c,e,i;
for(i=0;i<=degree;i++)
{
m:cout<<endl<<"exponent : ";
cin>>e;
if(e>degree)
{
cout<<endl<<"invalid exp!!! ";
goto m;
}
cout<<endl<<"coeff : ";
cin>>c;
poly *temp1;
temp1=head1;
if(temp1==NULL)
{
temp1=new poly;
temp1->exp=e;
temp1->coeff=c;
temp1->next=NULL;
head1=temp1;
}
else
{
temp1=head1;
poly *r;
r=new poly;
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
r->exp=e;
r->coeff=c;
temp1->next=r;
r->next=NULL;
}
}
}

void poly::input2()//Input for Polynomial 2
{
int degree;
cout<<endl<<"degree of d polynomial : ";
cin>>degree;
int c,e,i;
for(i=0;i<=degree;i++)
{
m:cout<<endl<<"exponent : ";
cin>>e;
if(e>degree)
{
cout<<endl<<"invalid exp!!! ";
goto m;
}
cout<<endl<<"coeff : ";
cin>>c;
poly *temp2;
temp2=head2;
if(temp2==NULL)
{
temp2=new poly;
temp2->exp=e;
temp2->coeff=c;
temp2->next=NULL;
head2=temp2;
}
else
{
temp2=head2;
poly *r;
r=new poly;
while(temp2->next!=NULL)
{
temp2=temp2->next;
}
r->exp=e;
r->coeff=c;
temp2->next=r;
r->next=NULL;
}
}
}

void poly::add() //Addition of the polynomials
{
if(head1==NULL)
head3=head2;
if(head2==NULL)
head3=head1;
if((head1!=NULL)&&(head2!=NULL))
{
poly *t1,*t2;
t1=head1;
t2=head2;
int newcoeff,newexp;
while((t1!=NULL)&&(t2!=NULL))
{
if(t1->exp==t2->exp)
{
newcoeff=t1->coeff+t2->coeff;
newexp=t1->exp;
t1=t1->next;
t2=t2->next;
}
if(t1->exp<t2->exp)
{
newcoeff=t2->coeff;
newexp=t2->exp;
t2=t2->next;
}
if(t1->exp>t2->exp)
{
newcoeff=t1->coeff;
newexp=t1->exp;
t1=t1->next;
}
if(newcoeff!=0)
{
poly *r,*t3;
t3=head3;
r=new poly;
r->coeff=newcoeff;
r->exp=newexp;
r->next=NULL;
if(head3==NULL)
{
head3=r;
cout<<endl<<head3->coeff<<"    "<<head3->exp<<endl;
}
else
{
t3=head3;
while(t3->next!=NULL)
{
t3=t3->next;
}
t3->next=r;
cout<<endl<<t3->coeff<<" "<<t3->exp<<endl;
}
}
}
}
}

void poly::display() //Display the results
{
if(head1!=NULL)
{
poly *temp;
temp=head1;
cout<<endl<<"--poly1--"<<endl<<endl;
while(temp->next!=NULL)
{
cout<<temp->coeff<<"y^"<<temp->exp<<" + ";
temp=temp->next;
}
cout<<temp->coeff<<"y^"<<temp->exp;
}
if(head2!=NULL)
{
poly *temp1;
temp1=head2;
cout<<endl<<endl<<"--poly2--"<<endl<<endl;
while(temp1->next!=NULL)
{
cout<<temp1->coeff<<"y^"<<temp1->exp<<" + ";
temp1=temp1->next;
}
cout<<temp1->coeff<<"y^"<<temp1->exp;
}
if(head3!=NULL)
{
poly *temp3;
temp3=head3;
cout<<endl<<endl<<"--sum--"<<endl<<endl;
while(temp3->next!=NULL)
{
cout<<temp3->coeff<<"y^"<<temp3->exp<<" + ";
temp3=temp3->next;
}
cout<<temp3->coeff<<"y^"<<temp3->exp;
}
}

void main()
{
poly t;
char ch;
clrscr();
cout<<"do u want enter poly expression 1 (y/n) :";
cin>>ch;
if(ch=='y')
    t.input1(); //function call
cout<<"do u want enter poly expression 2 (y/n) :";
cin>>ch;
if(ch=='y')
    t.input2();
t.add();
t.display();
getch();

}






#include <iostream.h> #include <string.h> #include <ctype.h> const int MAX = 50 ; class infix { private : char target[MAX], stack[MAX] ; char *s, *t ; int top, l ; public : infix( ) ; void setexpr ( char *str ) ; void push ( char c ) ; char pop( ) ; void convert( ) ; int priority ( char c ) ; void show( ) ; } ; infix :: infix( ) { top = -1 ; strcpy ( target, "" ) ; strcpy ( stack, "" ) ; l = 0 ; } void infix :: setexpr ( char *str ) { s = str ; strrev ( s ) ; l = strlen ( s ) ; * ( target + l ) = '\0' ; t = target + ( l - 1 ) ; } void infix :: push ( char c ) { if ( top == MAX - 1 ) cout << "\nStack is full\n" ; else { top++ ; stack[top] = c ; } } char infix :: pop( ) { if ( top == -1 ) { cout << "Stack is empty\n" ; return -1 ; } else { char item = stack[top] ; top-- ; return item ; } } void infix :: convert( ) { char opr ;   while ( *s ) { if ( *s == ' ' || *s == '\t' ) { s++ ; continue ; }   if ( isdigit ( *s ) || isalpha ( *s ) ) { while ( isdigit ( *s ) || isalpha ( *s ) ) { *t = *s ; s++ ; t-- ; } }   if ( *s == ')' ) { push ( *s ) ; s++ ; }   if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '-' || *s == '$' ) { if ( top != -1 ) { opr = pop( ) ;   while ( priority ( opr ) > priority ( *s ) ) { *t = opr ; t-- ; opr = pop( ) ; } push ( opr ) ; push ( *s ) ; } else push ( *s ) ; s++ ; }   if ( *s == '(' ) { opr = pop( ) ; while ( ( opr ) != ')' ) { *t = opr ; t-- ; opr = pop ( ) ; } s++ ; } }   while ( top != -1 ) { opr = pop( ) ; *t = opr ; t-- ; } t++ ; } int infix :: priority ( char c ) { if ( c == '$' ) return 3 ; if ( c == '*' || c == '/' || c == '%' ) return 2 ; else { if ( c == '+' || c == '-' ) return 1 ; else return 0 ; } } void infix :: show( ) { while ( *t ) { cout << " " << *t ; t++ ; } }   void main( ) { char expr[MAX] ; infix q ;   cout << "\nEnter an expression in infix form: " ; cin.getline ( expr, MAX ) ;   q.setexpr( expr ) ; q.convert( ) ;   cout << "The Prefix expression is: " ; q.show( ) ; } - See more at: file:///F:/new%20ds%20prog/C++%20program%20to%20convert%20an%20Expression%20from%20Infix%20expression%20to%20Prefix%20form%20%20%20electrofriends.com.htm#sthash.PP3OczdP.dpuf

No comments:

Post a Comment