home *** CD-ROM | disk | FTP | other *** search
- (***********************************************************)
- (* PROGRAM Poly which will serve as a main program for *)
- (* various operations on polynomials. Several of these *)
- (* are currently implemented as stubs. *)
- (* by PRB 2/91 *)
- (***********************************************************)
- PROGRAM Poly(Input,Output);
-
- TYPE ExpType = 0..Maxint;
- TermPtr = ^TermType;
- TermType = RECORD
- Coef: Integer;
- Exp: ExpType;
- Next: TermPtr;
- END;
-
- VAR Poly1,Poly2,Sum,Prod,Sub,Deriv,Mon,
- Work,Temp,MonPoly1,MonPoly2,p1,q1,p2,q2: TermPtr;
-
- (***********************************************************)
- (* Auxilliary procedure for spacing output *)
- (***********************************************************)
- PROCEDURE PrintBlankLines(N:Integer);
- VAR I: Integer;
- BEGIN
- FOR I:= 1 TO N DO Writeln;
- END;
- (*********************************************************)
- (* Creates a term with given coefficient and exponent *)
- (*********************************************************)
- PROCEDURE MakeTerm(Coef:Integer; Exp:ExpType);
- BEGIN
- END;
- (********************************************************)
- (* Pre: The monomial has been created *)
- (* Post: The monomial is written to the screen *)
- (********************************************************)
- PROCEDURE WriteMon(Mon:TermPtr);
- BEGIN
- IF (Mon = nil) THEN Writeln(' 0');
- IF (Mon^.coef>0) THEN (* WRITE SIGN *)
- Write('+')
- ELSE
- Write(' - ');
- WITH Mon^ DO
- IF Exp = 0 THEN (* WRITE TERM *)
- Write(Abs(Coef):1)
- ELSE
- IF Exp = 1 THEN
- Write(Abs(Coef):1,'*X')
- ELSE
- Write(Abs(Coef):1,'*X^',Exp:1);
- Writeln;
- END;
- (*********************************************************)
- (* Pre: The polynomial has been created *)
- (* Post: The polynomial is written to the screen *)
- (*********************************************************)
- PROCEDURE WritePoly(Poly:TermPtr);
- VAR P: TermPtr;
-
- BEGIN
- P:=Poly^.Next;
- IF (P = nil) THEN Writeln(' 0');
- WHILE (P<> NIL) DO
- BEGIN
- IF (P^.Coef>0) THEN (* Write sign *)
- Write(' + ')
- ELSE
- Write(' - ');
- WITH P^ DO
- IF Exp = 0 THEN (* Write term *)
- Write(Abs(Coef):1)
- ELSE
- IF Exp = 1 THEN
- Write(Abs(Coef):1,'*X')
- ELSE
- Write(Abs(Coef):1,'*X^',Exp:1);
- P:= P^.Next;
- END; (** WHILE **)
- Writeln;
- END;
- (***********************************************************)
- (* Pre: Zero Poly must exist. *)
- (* Post: The user has supplied the coefficients *)
- (* for a polynomial. *)
- (***********************************************************)
- PROCEDURE ReadPoly(VAR Poly: TermPtr);
- VAR P: TermPtr;
- Coef: Integer;
- Exp: ExpType;
-
- BEGIN
- Writeln;
- Writeln('Enter the coefficients and Exps of the polynomial');
- Writeln('in descending order. To exit, enter a zero coeficient.');
- Writeln('Coef, Exp');
- Readln(Coef,Exp);
- P:= Poly;
- WHILE Coef<>0 DO
- BEGIN
- New(P^.Next);
- P:= P^.Next;
- P^.Coef:= Coef;
- P^.Exp:= Exp;
- Writeln('Coef, Exp');
- Readln(Coef, Exp);
- END;
- P^.Next:= nil;
- END;
- (******************************************************)
- (* Pre: Poly1, Poly2, exist *)
- (* Post: The user suplied the monomial *)
- (******************************************************)
- PROCEDURE ReadMon(VAR Mon:TermPtr);
- VAR M : TermPtr;
- coef : Integer;
- Exp : Exptype;
- BEGIN
- Writeln;
- Writeln('Enter the coefficient and Exp of the monomial');
- Writeln('Coef, Exp');
- Readln(Coef,Exp);
- New(M);
- M := Mon;
- M^.Coef := Coef;
- M^.Exp := Exp;
- M^.Next := nil;
- END;
- (******************************************************)
- (* Pre: Poly1, Poly2, and Sum exist. *)
- (* Post: Sum has been formed from Poly1, Poly2 *)
- (******************************************************)
- PROCEDURE AddPolys(Poly1,Poly2:TermPtr; VAR Sum:TermPtr);
- VAR P1,P2,S: TermPtr;
- Sumcoef: Integer;
-
- BEGIN (* Initialize *)
- P1:= Poly1^.Next;
- P2:= Poly2^.Next;
- S:= Sum;
-
- WHILE (P1<>nil) AND (P2<>nil) DO
- BEGIN
- IF P1^.Exp = P2^.Exp THEN
- BEGIN (* Add Like terms *)
- Sumcoef:= P1^.coef + P2^.coef;
- IF (Sumcoef<>0) THEN
- BEGIN
- New(S^.Next);
- S:= S^.Next;
- S^.coef:= Sumcoef;
- S^.Exp:= P1^.Exp;
- END;
- P1:= P1^.Next;
- P2:= P2^.Next;
- END
- ELSE
- BEGIN (* Lone term cases *)
- new(S^.Next); (* copy a single term to sum *)
- S:= S^.Next;
- IF P1^.Exp > P2^.Exp THEN
- BEGIN
- S^:= P1^;
- P1:= P1^.Next;
- END
- ELSE
- BEGIN
- S^:= P2^;
- P2:= P2^.Next;
- END;
- END;
- END;
-
- WHILE (P1<>nil) DO (* Append any remaining *)
- BEGIN (* terms to sum *)
- New(S^.Next);
- S:= S^.Next;
- S^:= P1^;
- P1:= P1^.Next;
- END;
- WHILE (P2<>nil) DO
- BEGIN
- New(S^.Next);
- S:= S^.Next;
- S^:= P2^;
- P2:= P2^.Next;
- END;
- END;
-
- (***************************************************************)
- (* Pre: Poly1, Poly2 and Sum exist. *)
- (* Post: Sum has been formed from Poly1, Poly2 *)
- (***************************************************************)
- (* Subtract Procedure *)
- PROCEDURE SubPolys(Poly1,Poly2:TermPtr; VAR Sub:TermPtr);
- VAR P1,P2,Sb: TermPtr;
- Sumcoef: Integer;
-
- BEGIN (* Initialize *)
- P1:= Poly1^.Next;
- P2:= Poly2^.Next;
- Sb:= Sub;
-
- WHILE (P1<>nil) AND (P2<>nil) DO
- BEGIN
- IF P1^.Exp = P2^.Exp THEN
- BEGIN (* Subtract Like terms *)
- Sumcoef:= P1^.coef - P2^.coef;
- IF (Sumcoef<>0) THEN
- BEGIN
- New(Sb^.Next);
- Sb:= Sb^.Next;
- Sb^.coef:= Sumcoef;
- Sb^.Exp:= P1^.Exp;
- END;
- P1:= P1^.Next;
- P2:= P2^.Next;
- END
- ELSE
- BEGIN (* Lone term cases *)
- new(Sb^.Next); (* copy a single term to sub *)
- Sb:= Sb^.Next;
- IF P1^.Exp > P2^.Exp THEN
- BEGIN
- Sb^:= P1^;
- P1:= P1^.Next;
- END
- ELSE
- BEGIN
- P2:= P2^.Next;
- END;
- END;
- END;
-
- WHILE (P1<>nil) DO (* Append any remaining *)
- BEGIN (* terms to sub *)
- New(Sb^.Next);
- Sb:= Sb^.Next;
- Sb^:= P1^;
- P1:= P1^.Next;
- END;
- WHILE (P2<>nil) DO
- BEGIN
- New(Sb^.Next);
- Sb:= Sb^.Next;
- Sb^:= P2^;
- P2:= P2^.Next;
- END;
- END;
- (***********************************************************)
- (* Multiplies monomial and a polynomial. The product is *)
- (* pointed to by Prod1 and Prod2. *)
- (***********************************************************)
- PROCEDURE MultMon(Poly1,Poly2,Mon : TermPtr; VAR MonPoly1,MonPoly2 : TermPtr);
- VAR P1,P2,Prod1,Prod2:TermPtr;
- Prodcoef : Integer;
- BEGIN
- P1 := Poly1^.Next;
- P2 := Poly2^.Next;
- Prod1 := MonPoly1;
- Prod2 := MonPoly2;
- While (P1 <> nil) OR (P2 <> nil) DO
- BEGIN
- IF (P1 <> nil) THEN
- BEGIN
- Prodcoef := Mon^.coef * P1^.coef;
- IF (Prodcoef <> 0) THEN
- BEGIN
- New(Prod1^.Next);
- Prod1 := Prod1^.Next;
- Prod1^.coef := Prodcoef;
- Prod1^.Exp := P1^.Exp + Mon^.Exp;
- END; {IF}
- P1 := P1^.Next;
- END; { IF }
- IF (P2 <> nil) THEN
- BEGIN {IF}
- Prodcoef := Mon^.coef * P2^.coef;
- IF (Prodcoef <> 0) THEN
- BEGIN {IF}
- New(Prod2^.Next);
- Prod2 := Prod2^.Next;
- Prod2^.coef := Prodcoef;
- Prod2^.Exp := P2^.Exp + Mon^.Exp;
- END; {IF }
- P2 := P2^.Next;
- END;{IF}
- END; {WHILE}
- END;
- (***********************************************************)
- (* Multiplies two polynomials pointed to by *)
- (* Poly1 and Poly2. The product is pointed *)
- (* to by Prod. *)
- (***********************************************************)
-
- PROCEDURE MultPolys(VAR Poly1,Poly2,Prod:TermPtr);
- VAR P1,P2,hold,H,temp: TermPtr;
- Prodcoef,Pexe :integer;
-
- BEGIN {MultPolys}
- New(temp);
- New(H);
- New(hold);
- H := hold;
- P2 := Poly2^.next;
-
- WHILE P2 <> NIL DO
- BEGIN
- P1 := Poly1^.next;
- WHILE P1 <> NIL DO
- BEGIN
- Pexe := P1^.exp + P2^.exp; (* add the exponents *)
- Prodcoef := P1^.coef * P2^.coef; (* multiply the coeficents *)
- temp^.exp := Pexe;
- temp^.coef := Prodcoef;
- hold^.next := temp;
- P1 := P1^.next;
- Addpolys(H,Prod,Prod); (* Simplify by adding *)
- END;
- P2 := P2^.next;
- END;
- END; {MultPolys}
-
- (***********************************************************)
- (* Pre: None. *)
- (* Post: All Polynomials have been initialized to zero *)
- (***********************************************************)
- PROCEDURE Initialize(VAR Poly1,Poly2,Sum,Prod,Deriv,Mon: TermPtr);
- BEGIN
- New(Poly1);
- New(Poly2);
- New(Sum);
- New(Sub);
- New(Prod);
- New(Deriv);
- New(Mon);
- New(MonPoly1);
- New(MonPoly2);
-
- Poly1^.Next:= nil;
- Poly2^.Next:= nil;
- Sum^.Next:= nil;
- Sub^.Next:= nil;
- Prod^.Next:= nil;
- Deriv^.Next:= nil;
- Mon^.Next:= nil;
- MonPoly1^.Next := nil;
- MonPoly2^.Next := nil;
- END;
- (***********************************************************)
- (*************************** MAIN **************************)
- BEGIN
- Initialize(Poly1,Poly2,Sum,Prod,Deriv,Mon);
- (* Describe *)
- PrintBlankLines(5);
- ReadPoly(Poly1);
- Writeln('The first polynomial is:');
- WritePoly(Poly1);
- Writeln;
- ReadPoly(Poly2);
- Writeln('The second polynomial is:');
- WritePoly(Poly2);
- Writeln('Enter Monomial.');
- ReadMon(Mon);
- Writeln('The Monomial is:');
- WriteMon(Mon);
- PrintBlankLines(5);
-
- AddPolys(Poly1,Poly2,Sum);
- Writeln('The sum of the two Polynomials is:');
- Writepoly(Sum);
- PrintBlankLines(2);
-
- SubPolys(Poly1,Poly2,Sub);
- Writeln('The subtraction of the two Polynomials is:');
- Writepoly(Sub);
- PrintBlankLines(2);
-
- MultMon(Poly1,Poly2,Mon,MonPoly1,MonPoly2);
- Writeln('The Product of the monomial and the polynomial #1 is:');
- WritePoly(MonPoly1);
- PrintBlankLines(2);
- Writeln('The Product of the monomial and the polynomial #2 is:');
- WritePoly(MonPoly2);
- PrintBlankLines(2);
-
- MultPolys(Poly1,Poly2,Prod);
- Writeln('The Product of the two polynomials is:');
- WritePoly(Prod);
- PrintBlankLines(1);
-
- END.
- (********************************************************)
-