Program for multiplication of Polynomials

Source: Internet
Author: User

Exp is an exponent, info is a coefficient, and 0 is input.

# Include
# Include
# Include
Template
Class listob
{
Public:
Listob ()
{
Info = 0;
Next = NULL;
Prior = NULL;
}
Listob (tdata C, tdata D)
{
Info = C;
Exp = D;
Next = NULL;
Prior = NULL;
}
Friend void operator> (istream & stream, listob

& O );
Tdata info, exp;
Listob

* Next;
Listob

* Prior;
};
Template
Void operator> (istream & stream, listob

& O)
{
Cout <"enter information :";
Stream> O. Exp> o.info;
}
Template
Class list: Public listob

{
Public:
List ()
{Start = END = NULL ;}
Void store (listob

);
Void remove (listob

* Ob );
Void frwdlist ();
Listob

* Find (tdata C );
Listob

* Getstart ()
{Return start ;}
Listob

* Getend ()
{Return end ;}
Void setstart (listob

* P)
{Start = P ;}
Void setend (listob

* P)
{End = P ;}
Static void add (list

&, List

);
Static void sort (list

&);
Static void copy (list

&, List

);
PRIVATE:
Listob

* Start, * end;
};
Template
Void list

: Store (listob

F)
{
Listob

* P;
P = new listob

;
If (! P)
{
Cout <"Allocation Error./N ";
Exit (1 );
}
P-> info = f.info;
P-> exp = f. Exp;
If (START = NULL)
End = Start = P;
Else
{
P-> prior = end;
End-> next = P;
End = P;
}
}
Template
Void list

: Remove (listob

* Ob)
{
If (ob-> prior)
{
Ob-> prior-> next = ob-> next;
If (ob-> next)
Ob-> next-> prior = ob-> prior;
Else
End = ob-> prior;
}
Else
{
If (ob-> next)
{
Ob-> next-> prior = NULL;
Start = ob-> next;
}
Else
Start = END = NULL;
}

}
Template
Void list

: Frwdlist ()
{
If (START = NULL)
{Cout <"empty list/N ";}
Listob

* Temp;
Temp = start;
While (temp)
{
Cout < Exp <"," < Info <"";
Temp = temp-> next;
}
Cout <}
Template
Void list

: Add (list

& List1, list

List2)
{

Listob

* P, * q, * temp, * U;
P = list1.getstart (); q = list2.getstart ();
If (! P) {list

: Copy (list1, list2); return ;}
Int flag;
While (P & Q)
{
If (p-> exp Exp)
Flag = 1;
If (p-> exp = Q-> exp)
Flag = 2;
If (p-> exp> q-> exp)
Flag = 3;
Switch (FLAG)
{
Case 1:
{
Temp = P;
P = p-> next;
} Break;
Case 2:
{

P-> info + = Q-> Info;
If (! P-> info)
{
List1.remove (P );
}
Else
Temp = P;
P = p-> next;
Q = Q-> next;
} Break;
Case 3:
{
If (p-> prior)
{
U = Q-> next;
P-> prior-> next = Q;
P-> prior-> next-> prior = p-> prior;
P-> prior-> next = P;
P-> prior = Q;
}
Else
{
U = Q-> next;
List1.setstart (Q );
List1.getstart ()-> next = P;
List1.getstart ()-> prior = NULL;
P-> prior = list1.getstart ();
}
Q = u;
} Break;
}
}
If (q)
{Temp-> next = Q; q-> prior = temp ;}

}
Template
Void list

: Copy (list

& List1, list

List2)
{
Listob

* P;
List1.setstart (null );
P = list2.getstart ();
While (P)
{
List1.store (* P );
P = p-> next;
}
}
Template
Void list

: Sort (list

& List)
{
Tdata temp;
Listob

* P, * q;
P = List. getstart ();
While (P)
{
Q = P;
While (Q-> next)
{
If (Q-> next-> exp

Exp)
{
Temp = Q-> next-> exp;
Q-> next-> exp = p-> exp;
P-> exp = temp;
Temp = Q-> next-> Info;
Q-> next-> info = p-> Info;
P-> info = temp;
}
Q = Q-> next;
}
P = p-> next;
}
}
Void main ()
{
List List1, list2, list2save, listanswer;
Listob F, * P, * q;
Cin> F;
While (f.info! = 0)
{
List1.store (f );
Cin> F;
}
List : Sort (list1 );
List1.frwdlist ();
Cin> F;
While (f.info! = 0)
{
List2.store (f );
Cin> F;
}
List : Sort (list2 );
List : Copy (list2save, list2 );
List2.frwdlist ();
P = list1.getstart ();
Q = list2.getstart ();
If (P & Q)
{
While (P)
{
While (q)
{
Q-> exp + = p-> exp;
Q-> info * = p-> Info;
Q = Q-> next;
}
List : Add (listanswer, list2 );
List : Copy (list2, list2save );
Q = list2.getstart ();
P = p-> next;
}
Listanswer. frwdlist ();
}
Else
Cout <"empty list/N ";
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.