Starting from:

$30

Lab12. Polynomials

EE231002 Introduction to Programming
Lab12. Polynomials

The linked list is an ideal structure to store polynomials. An n-th degree polynomial has the
following form.
Pn(x) = anx
n + an−1x
n−1 + · · · + a1x + a0 (12.1)
And it can be stored using the following structure for the nodes of a linked list.
typedef struct sPoly {
int degree; // the degree of the node
double coef; // the coefficient of the node
struct sPoly *next; // pointer to the next node
} POLY;
Using the structure defined above to represent polynomials, please write the following functions.
POLY *oneTerm(int degree, double coef);
This function creates a 1-term polynomial of the form coef×x
degree and returns the new polynomial.
POLY *add(POLY *p1, POLY *p2);
This function adds two polynomials p1 and p2 to form a new polynomial and return the new polynomial.
POLY *sub(POLY *p1, POLY *p2);
This function subtract polynomial p2 from p1 to form a new polynomial and return the new polynomial.
POLY *mply(POLY *p1, POLY *p2);
This function multiplies two polynomials p1 and p2 to form a new polynomial and return the new
polynomial.
void print(POLY *p1);
This function prints out the polynomial p1 in human readable form. See the example output given below
for more details.
void release(POLY *p1);
This function releases all nodes of the polynomial p1 and returns them to the heap space.
To facilitate polynomial manipulations, all polynomials in this lab should be arranged in degreedescending order. Thus, the first node of a degree n polynomial starts with anx
n
and the first node has
degree= n and coef= an. And to take advantage of the linked list’s dynamic property, no term of zero
coefficient should be stored. Of course, your functions should not have memory leak problem.
1
Using the functions given above, your main function implements the following pseudo code.
Let X be a one-term polynomial and X= x,
and One be a one-term polynomial with One=1.
Polynomial A =X+One,
Polynomial A2 = A × A,
Polynomial A3 = A2 × A,
Polynomial A4 = A3 × A,
Polynomial A5 = A4 × A,
printf("A = ");
print(A),
printf("A2 = ");
print(A2),
Polynomial B =X−One,
Polynomial B2 = B × B,
Polynomial B3 = B2 × B,
Polynomial B4 = B3 × B,
Polynomial B5 = B4 × B,
Polynomial C = A + B,
Polynomial C2 = A2 × B2,
Polynomial C3 = A3 × B3,
Polynomial C4 = A4 × B4,
Polynomial C5 = A5 × B5,
printf("C = ");
print(C),
printf("C2 = ");
print(C2),
printf("C3 = ");
print(C3),
printf("C4 = ");
print(C4),
printf("C5 = ");
print(C5).
And the first 4 lines of output look like:
A = x +1
A2 = x^2 +2 x +1
C = 2 x
C2 = x^4 -2 x^2 +1
Note that if a coefficient is one, the it would not be printed unless it is a constant term. The degree
of 1 is also not printed. Please make sure your program output follows the example exactly.
Notes.
1. Create a directory lab12 and use it as the working directory.
2
2. Name your program source file as lab12.c.
3. The first few lines of your program should be comments as the following.
/* EE231002 Lab12. Polynomials
ID, Name
Date:
*/
4. After you finish verifying your program, you can submit your source code by
$ ∼ee2310/bin/submit lab12 lab12.c
If you see a ”submitted successfully” message, then you are done. In case you want to check which
file and at what time you submitted your labs, you can type in the following command:
$ ∼ee2310/bin/subrec lab12
It will show the last few submission records.
5. You should try to write the program as efficient as possible. The format of your program should be
compact and easy to understand. These are part of the grading criteria.
3

More products