EE 312 Project 6 - Polynomial
Module
You must complete this assignment by yourself. You may not use code
that you have not written by yourself. You may not give your code to
another student, or help another student with this project. If you
need help, seek help from the instructional staff for the class.
Description: The purpose of this assignment is to
1. Develop a module that represents polynomials and provides
polynomial operations.
2. Practice using linked lists and dynamic memory.
You will implement the functions described in Poly.h. Use the poly type defined
in the provided Poly.h and Poly.c files.
You cannot modify the Poly.h
file, or change the poly representation. In Poly.c, complete the definitions
of the functions that are declared in Poly.h. The provided tests in Proj6Test.c
are not intended to provide comprehensive testing. You must write
your own tests.
The format of the string that will be passed to your makePoly() function
will be similar to these strings below:
5x^3 + 2x + 4
-2x - 6
Each polynomial contains only the variable x. The ^ symbol is used to represent
exponentiation in the strings. Terms in the polynomial will be
separated from a + (for addition) or - (for subtraction) sign by a
single blank space, and there will not be whitespace embedded in a
monomial term. Terms with zero coefficients will not be listed. The
terms will be in decreasing order of degree, and there will not be
multiple terms with the same degree. All coefficients will be
integers (and of course, all exponents will be non-negative
integers.) This will be the format of the string argument for your makePoly() function.
Representation
Each polynomial will be represented as a linked list in which each
node represents a term of the polynomial. For a polynomial's linked
list, the list nodes will occur in decreasing order of degree. There
will be one node corresponding to any particular power of x which
has a non-zero coefficient in the polynomial. Zero coefficient terms
will not be included in the linked list representation of a
polynomial. A zero polynomial will be represented by a NULL
list pointer.
The struct for each node in the linked list will contain:
1. exp: the
exponent of x in the term
2. coeff: the
non-zero coefficient in the term
3. next:
pointer to the next node in the list
The functions that you will be implementing are the following.
- Poly
makePoly(char *src): This function takes a string
representation src of a polynomial, and returns a pointer to the
linked list representation of the polynomial. The linked list
must contain only terms with non-zero coefficients, and the
nodes for the terms must be listed in decreasing order of
degree. No two nodes can represent terms with the same power of
x.
- Poly
polyCopy(Poly src): This function creates a copy of the
polynomial that src references.
The original polynomial and the copy should not share nodes -
each node in the src
polynomial will be duplicated for the copy.
- double
polyEval(Poly src, double val): This function returns
the polynomial function's value at x = val.
- Poly polyAdd(Poly
one, Poly two): This function returns the sum of the
polynomial arguments. Neither of the arguments are modified. You
will be dynamically creating a new linked list to hold the
result of the operation.
- Poly
polyMult(Poly one, Poly two): This function returns the
product of the polynomial arguments. Neither of the arguments
are modified. You will be dynamically creating a new linked list
to hold the result of the operation.
- void addTerm(Poly
one, int coeff, int exponent): This function modifies
the poly one by adding the monomial term with the specified
coefficient and exponent. Note that this may require creating a
new node in the polynomial's linked list representation,
modifying the fields in an existing node, or doing nothing
(e.g., if the coefficient argument is 0 or the exponent is
negative.) If a high order term is added, the pointer to the
list will need to be updated.
- int
realRoots(Poly src): If the src polynomial is of degree 2 or
less, return the number of distinct real roots of the
polynomial. If the polynomial is of degree 3 or higher, return
-1.
- char
*stringPoly(Poly src): This function returns a string
representation of the polynomial. The non-zero terms should be
given in order of decreasing exponent, and the ^ symbol should
be used to indicate exponentiation. The portion of the string
that represents a term should not contain embedded space, and
each + or - (subtraction) in the polynomial should contain a
single blank space before and after it. You may assume that the
string representation of the polynomial is at most 100
characters. Do not print coefficients equal to 1 except in
constant terms. Negative coefficients should be represented in
your string as a subtraction in terms after the high-order term.
E.g., the string should be 3x^2 - 2x, not 3x^2 + -2x.
- bool
polyEqual(Poly one, Poly two): This function returns
true if the two polynomials are equal, and false otherwise.
- void
deletePoly(Poly one): Destroy the polynomial and
deallocate all memory associated with it.
You may need to write additional helper functions to reduce
redundancy. Do not modify Poly.h, or add any #includes or #defines to Poly.c. You may not use any
globals. You should not add anything to Poly.c except the implementation of
the functions listed above and any helper functions that you need
to reduce redundancy.
Use valgrind to check for memory leaks. You will lose points for
memory leaks.
At the top of your Poly.c
file, add the following header, with your name replacing
<NAME>.
/* Student information for project:
*
* Replace <NAME> with your name. (And then delete this line.)
*
* On my honor, <NAME>, this programming project is my own work
* and I have not provided this code to any other student.
*
* Name:
* email address:
* UTEID:
* Section 5 digit ID:
* Number of slip days used on this assignment:
*/
Style
At a minimum, you must include a comment right before each function, summarizing the behavior of the function. This is minimal - you must add comments to describe complex chunks of code within your functions, as well. Follow the style guide for EE 312.
You will submit your Poly.c file on Canvas. The #includes in your Poly.c file must not be modified. The Poly.c file should only be modified by adding comments and your implementation of the functions described above and any helper functions that you need.
If your code does not compile, you will receive a 0 on the project. If your file does not have the correct name, including capitalization, your project grade will be decreased by 5 points. If the header given above is not filled in completely and correctly, you will lose points. Please check your 5 digit section ID. Sections have been combined on Canvas, so you will not see the correct number there.
For each function that you implement, include the big-Oh of your function in the comment before your function. Assume the degree of the polynomial is N. For functions that take two polynomials, assume that both polynomials have degree N. Do a worst case analysis. The efficiency of your algorithms matters.
Academic Dishonesty on Projects
This is a reminder that copying code from someone else, giving your code to someone else, finding solutions on the internet, and walking another student through a solution are all examples of cheating. Please review the syllabus for my policy on cheating. If I catch you cheating on a project, you will fail the class and I will file a report with the dean of students office. For programming projects, I use MOSS (Measure Of Software Similarity), which was developed at Stanford. MOSS is extremely effective at detecting plagiarism on coding assignments. None of these have any effect on MOSS: adding or changing comments, changing function or variable names, rearranging functions and code chunks. I recognize that most EE 312 students do not and would not cheat. It is important to detect cheating in part to protect the value of your degree and your hard-earned grades.