Building a Polynomial Expression Structure
$10-30 USD
S-au achitat serviciile după ce au fost prestate
A common way to represent a polynomial expression (e.g. 3x^5 – 2.2x^3 + 4x – 1.713) is with linked structures that
have three members:
a) A member to represent the coefficient (3)
b) A member to represent the exponent (5)
c) A pointer to the next term in the expression
Write a program that does the following:
1) Write a function input_poly(ptr1) that reads in two polynomials at a time with coefficient and exponent
from a file cp7_in.txt. The file will contain 2n polynomials, where n is an integer that is 10 or less. Space
for the polynomial will be allocated in run time and there is no limit to the size of the polynomial, e..g the
first two lines of the file is
3 5 -2.2 3 4 1 -1.713 0
3 100 -2.2 3 14 55 3.1 101
(Note the terms may be in any order)
Then, two polynomials will be created
3x^5 – 2.2x^3 + 4x – 1.713 and
3.1x^101 + 3x^100 + 14x^55 – 2.2x^3
2) Write a function add_poly(ptr1, ptr2) that expects pointers ptr1 and ptr2 to two lists that represent
polynomials and returns a pointer to a third list that represents the sum of polynomials. You will find the
sum of every pair of polynomials.
3) Write a function mult_poly(ptr1, ptr2) that expects pointers ptr1 and ptr2 to two lists that represent
polynomials and returns a pointer to a third list that represents the product of polynomials. You will find
the product of every pair of polynomials.
4) Write a function print_poly(ptr1) that prints the polynomial pointed to by ptr1 to a file, cp7.out. The terms
must be printed in descending order of exponent. That is to say, e.g. 3x5 – 2.2x3 + 4x – 1.713 is correct but
e.g.– 2.2x^3 + 3x^5 + 4x – 1.713 is not.
From the function main(), you may call the function print_poly(ptr1) to print out the following to [login to view URL]:
The sum of polynomials::
3x^5 – 2.2x^3 + 4x – 1.713
3.1x^101 + 3x^100 + 14x^55 – 2.2x^3
is
3.1x^101 + 3x^100 + 14x^55 + 3x^5 – 4.4x^3 + 4x – 1.713
The product of polynomials::
3x^5 – 2.2x^3 + 4x – 1.713
3.1x^101 + 3x^100 + 14x^55 – 2.2x^3
is 9.3x^106 + 9x^105 -6.82x^104 – 6.6x^103 + 12.4x^102 + 6.690x^101 - 5.139x^100 + 42x^60 – 30.8x^58 + 56x^56 – 23.982x^55 – 6.6x^8 + 4.84x^6 – 8.8x^4 + 3.769x^3
Precision up to 3 digits after the decimal is enough. Do not need more and do not print more (think printf
format). Remember to documentation (comments). Be sure you include the specified initial comments, describe each variable, and in the
body describe each logical block.
Input:
1 2 1 1 3 3 5 0
1 2 1 1 4 3 -5 0 1 4
3 5 -2.2 3 4 1 -1.713 0
3 100 -2.2 3 14 55 3.1 101
Output:
The sum of polynomials::
3x^3 +x^2 + x +5
4x^3 + x^2 + x - 5
is
x^4 +7x^3 + 2x^2 + 2x
The product of polynomials::
3x^3 +x^2 + x +5
x^4 +4x^3 + x^2 + x - 5
is
3x^7+13x^6+8x^5+13x^4+7x^3+x^2-25
The sum of polynomials::
3x^5 – 2.2x^3 + 4x – 1.713
3.1x^101 + 3x^100 + 14x^55 – 2.2x^3
is
3.1x^101 + 3x^100 + 14x^55 + 3x^5 – 4.4x^3 + 4x + 1.713
The product of polynomials::
3x^5 – 2.2x^3 + 4x – 1.713
3.1x^101 + 3x^100 + 14x^55 – 2.2x^3
is
9.3x^106 + 9x^105 -6.82x^104 – 6.6x^103 + 12.4x^102 + 6.690x^101 - 5.139x^100 + 42x^60 – 30.8x^58 + 56x^56 – 23.982x^55 – 6.6x^8 + 4.84x^6 – 8.8x^4 + 3.769x^3
Grading Instruction:
(1) The program outputs each polynomial that was input correctly
(in descending order of exponent, as power of x, o.e.x^3 for x3, with + in between terms)
(2) Correct outputs (multiple examples, score distributed equally among all examples)
Add two polynomials, print out the sum correctly, as shown in [login to view URL]
Add two polynomials, but the terms are no in correct order
Multiply two polynomials, print out the sum correctly, as shown in [login to view URL]
Multiply two polynomials, but the terms are not in correct order
B. From all papers
inadequate comments
at beginning of source, student name, program purpose, etc.
for each function and variables (1 or 2); also for functions you create such as delete_node etc
ID Proiect: #36477027