Please give me a full program and ill give a 5 star rating !! Thanks A polynomia
ID: 3544385 • Letter: P
Question
Please give me a full program and ill give a 5 star rating !! Thanks
A polynomial can be represented as a linked list, where each node called a polyNode contains the coefficient and the exponent of a term of the polynomial. For example, the polynomial 4x3 + 3x2 - 5 would be represented as the linked list. Write a Polynomial class that has methods for creating a polynomial, reading and writing a polynomial, and adding a pair of polynomials. In order to add 2 polynomials, traverse both lists. If a particular exponent value is present in either one, it should also be present in the resulting polynomial unless its coefficient is zero.Explanation / Answer
import java.util.Vector
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class polyNode {
public:
double coefficient;
int exponent
polyNode next;
polyNode(double coeff, int ep, polyNode _next) {
coefficient(coeff);
exponent(exp);
next(_next);
}
}
public class Polynomial {
public:
vector<double> coeffs;
vector<int> exps;
polyNode head;
Polynomial() {
head = null;
}
public void readPolynomial() {
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
do {
double coef; int expo;
System.out.println("Enter coeff:");
coef = Double.parseDouble(bufferRead.readLine());
System.out.println("Enter exponent:");
coef = Int.parseInt(bufferRead.readLine());
coeffs.add(coef);
exps.add(expo);
System.out.println("Want to continue:Yes(1) or No(0):");
int continue = Int.parseInt(bufferRead.readLine());
} while (continue);
}
void createPolynomial() {
polyNode next = null;
polyNode curr = null;
for(int i = coeff.size(); i >0; i--) {
curr = new polyNode(coeffs[i], expo[i], next);
prev = curr;
}
head = curr;
}
Polynomial addPolynomial(Polynomial poly2) {
polyNode head1 = this.head;
polyNode head2 = poly2.head;
Polynomial sumpoly = new Polynimial();
while (!(head1 == null && head2 == null)) {
while (head1.exponent > head2.exponent) {
sumpoly.coeffs.add(head1.coefficient);
sumpoly.exps.add(head1.exponent);
}
if (head1.exponent == head2.exponent) {
sumpoly.coeffs.add(head1.coefficient + head2.coefficient);
sumpoly.exps.add(head1.exponent);
}
while (head2.exponent > head1.exponent) {
sumpoly.coeffs.add(head2.coefficient);
sumpoly.exps.add(head2.exponent);
}
}
sumpoly.createPolynomial();
return sumpoly;
}
}
public static void main(String[] args) {
Polymial poly1,poly2;
poly1.readPolynomial();
poly2.readPolynomial();
Polynomial poly3 = poly1.addPolynomial(poly2);
}