In: Computer Science
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined.
p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head) ● Constructor ● Public Function to create a Node ● Public function to insert the Node to the linked list (sorted polynomial according to the exponent). ● Public function to print the polynomial in the elegant format: 23x 9 + 18x 7+3 ● Overloaded public function to allow adding two polynomials poly3=poly1+poly2 (23x 9 + 9x 7+3)+(2x 4+3x 7+8x 2 -6) =23x 9 +12 x 7+2x 4+8x 2 -3 ● Overloaded public function to allow negating (!) the sign of any polynomial poly3=!poly1 2x 4+3x 7+8x 2 -6 =- 2x 4 -3x 7+8x 2+6 ● Overloaded public function to allow multiplying two polynomials ● Public function to evaluate polynomial based on an input If x=1, then the value of this polynomial 2x 4+3x 7+8x 2 -6 should be 2(1) 4+3(1) 7+8(1) 2 -6 =7
Main menu to test the following tasks ○ cout << "1. Create polynomial \n"; ○ cout << "2. Print polynomial \n"; ○ cout << "3. Add two polynomilas \n"; ○ cout << "4. Negate polynomial \n"; ○ cout << "5. Multiply two polynomials \n "; ○ cout << "6. Evaluate polynomial \n "; ○ cout << "7. Exit \n";
ANSWER :-
GIVEN THAT :-
//poly.h
class Node{
private:
double coefficient;
int exponent;
Node *next;
Node *prev;
public:
Node();
Node(double, int);
double get_coefficient();
int get_exponent();
Node *get_next();
Node *get_previous();
void set_coefficient(double);
void set_exponent(int);
void set_next(Node*);
void set_previous(Node*);
};
class LinkedList{
private:
Node *head;
public:
LinkedList();
LinkedList(Node*);
Node* create_node();
void insert();
void print();
int operator=(const int& x );
// LinkedList operator+(const LinkedList& pol);
// LinkedList* operator*(const LinkedList& pol);
LinkedList operator!(void);
};
// poly.cpp
// ###################### NOT COMPLETE
########################
// # need to implement polynomial addtion, complementation,
multiplication, valuation.
#include<iostream>
#include<math.h>
#include "poly.h"
using namespace std;
// node class
Node::Node(){}
Node::Node(double c, int e){
coefficient = c;
exponent = e;
next = NULL;
prev = NULL;
}
double Node::get_coefficient(){
return coefficient;
}
int Node::get_exponent(){
return exponent;
}
Node* Node::get_next(){
return next;
}
Node* Node::get_previous(){
return prev;
}
void Node::set_coefficient(double c){
coefficient = c;
}
void Node::set_exponent(int e){
exponent = e;
}
void Node::set_next(Node*n){
next = n;
}
void Node::set_previous(Node*p){
prev = p;
}
// LinkedList Class
LinkedList::LinkedList(){
this->head = NULL;
}
LinkedList::LinkedList(Node*head){
this->head = head;
}
Node* LinkedList::create_node(){
double c;
int e;
cout<<"Enter Coefficient? ";
cin>>c;
cout<<"Enter Exponent? ";
cin>>e;
return new Node(c, e);
}
void LinkedList::insert(){
// insert at tail
Node *node = create_node();
if(!head){
head = node;
return;
}
Node *temp = head;
while(temp->get_next()){
temp = temp->get_next();
}
node->set_previous(temp);
temp->set_next(node);
}
void LinkedList::print(){
Node *temp = head;
while(temp){
cout<<temp->get_coefficient();
if(temp->get_exponent()){
cout<<"x^"<<temp->get_exponent();
}
if(temp->get_next()){
cout<<" + ";
}
temp = temp->get_next();
}
cout<<endl;
}
int LinkedList::operator=(const int& x){
cout<<"\nPolynomial : ";
this->print();
Node *temp = head;
double result = 0;
while(temp){
result += temp->get_coefficient()*pow(x,
temp->get_exponent());
temp = temp->get_next();
}
cout<<"at x = "<<x<<" is "<< result;
}
LinkedList LinkedList::operator!(void){
head->set_coefficient(head->get_coefficient()*-1);
return *this;
}
#include<iostream>
#include "poly.cpp"
using namespace std;
// main.cpp
int main(){
LinkedList p1, p2, p3;
p1.insert();
p1.insert();
p1.insert();
p1.print();
// p2.insert();
// p2.insert();
// p2.insert();
// p2.print();
// p3 = p1 + p2;
// p3.print();
// p3 = p1*p2;
// p3.print();
p1 = 1;
p3 = !p1;
cout<<"\np3=!p1\n";
p3.print();
p1.print();
// p3 = p1+p2;
// p3.print();
return 0;
}
poly:main.o poly.o
g++ main.o -o poly
main.o: main.cpp
g++ -c main.cpp
poly.o: poly.cpp
g++ -c poly.cpp
clean:
rm *.o
poly
# Note: save as makefile without extension.
OUTPUT :-