Copies of necessary files are available in ~cs60/hw3/ on CSIL,
along with an executable version of our solution (testpoly ).
|
poly.c
to implement the functions declared in poly.h
.
testpoly.c
in your
working directory for this project (and mv poly.c-skeleton poly.c
).
Do not change poly.h
or testpoly.c
- you will not turn in these files.
poly.h
, and
think about how these types can be used to form linked lists.
Also see the four functions that are already implemented in poly.c
:
newTerm
- creates a new Term
structure by dynamically
allocating memory for it; stores copies of the coeff
and
exponent
parameters in the structure's members; and returns a pointer
to this structure (or returns NULL
if there is insufficient
memory to store the node). You must use this function whenever your solution
creates a new polynomial term.
freeTerm
- frees the memory used by the parameter. You must use this function
to free any temporary polynomial terms that you create (the freePoly
function must use freeTerm
too).
termsCreated
and termsFreed
are used
by testpoly.c
to evaluate memory management.
addTerm
- adds a term to the polynomial - either by changing the coefficient
of an existing term, or by inserting a new term;
maintains the terms in order from greatest to least exponent. This is the most difficult one
(hints).
value
- returns the double value of the first parameter (the Poly
)
at the value of the second parameter. In other words, plug in the second parameter as
the value for x, and return the result of this calculation.
degree
- returns degree of the parameter (greatest exponent value).
count
- returns the number of terms in the parameter.
sum
- returns a new polynomial equal to the sum of the 2 parameters.
diff
- returns a new polynomial equal to the difference between the two
parameters, as first Poly
minus second Poly
.
mult
- returns a new polynomial equal to the first parameter times the
second parameter.
freePoly
- frees all memory space used by the polynomial,
and sets pointer to NULL
. You must use freeTerm
to free
each term (do not use free
directly).
testpoly.c
.
stdin
until it reads an EOF
- it does not prompt the user for input. The program just echoes
comment lines (first non-blank character is '#'
) and blank lines.
add p c e
, value p x
, degree p
,
count p
, sum
, diff p p
, mult
, clear p
, and print p
,
where p
is either 1 or 2 to represent polynomial 1 or 2, c
is a double coefficient
value, e
is an integer exponent greater than or equal to 0, and x
is a double value.
poly.c
. Here is the text of a suitable
Makefile
for this purpose.
poly.c
from your
engineering account as follows:
turnin hw3@cs60 poly.c