In C++ Objectives : • Implement a linked list using classes • Create multiple cl
ID: 3734267 • Letter: I
Question
In C++
Objectives: • Implement a linked list using classes • Create multiple classes that interact • Implement recursive procedures on a simple data structure Problem: Gearbox Software in Frisco is working on a new physics engine to use with future games. As a recently hired intern, you have been assigned to the team building the physics engine. Your job is to create a program to evaluate polynomial expressions used by the engine.
Pseudocode Details: • Describe your logic for parsing the lines in the file • Describe your logic for calculating the value of the expression • Describe the logic of your main function Details: • Each expression will be a simple polynomial expression. • The only operators will be addition (+) and subtraction (-) • Spaces will surround the operators • The expression may not be in standard form (exponents ordered from largest to smallest) • Each expression will contain one or more terms. • The term may not have a coefficient. o If no coefficient is present, the coefficient is 1. • The coefficient may be an integer or floating point. • If a coefficient is present, it will immediately be followed by the variable x. • The term may not have an exponent (constant value) • If an exponent is present o Variable will be followed by a carat (^) – no space o The exponent will be an integer value • Expressions may contain terms with the same exponent o If so, combine like terms
• Store each expression in a linked list (-10 points if linked list not used) • Each node of the linked list will contain one term of the expression • Comment your code generously. Refer to the grading rubric for more details. • Use as few variables as possible. Don’t waste memory holding a calculation just so you can print it out one time.
Class Details • Node class (-5 points if not implemented)
o Attributes Coefficient Exponent Next pointer
o Methods Default constructor Overloaded constructor • Pass in coefficient and exponent Accessors Mutators •
Linked List (-5 points if not implemented)
o Attributes Head – node pointer
o Default constructor
o Overloaded constructor Takes node and assigns head to point at the node passed in o Destructor Delete linked list EXTRA CREDIT – delete the list recursively (+5 points)
o Accessor
o Mutator
o Insert Add node to linked list You can build the function to insert anywhere in the linked list • Or reuse the code from project 3 to insert and sort the list o Print Recursively print out the list to the file EXTRA CREDIT – implement an overloaded << operator (+5 points) • The overloaded operator is not recursive
User Interface: There will be no user interface for this program. All I/O will be file based.
Input: • All input will be read from a file named poly.txt. • Each line of the file will contain a single expression • The expression will be formatted as follows: o f()= • The number in parentheses may be integer or floating-point. • The number in parentheses will be used to evaluate the expression.
Output: • All output will be written to a file named answers.txt. • Each line will contain the polynomial expression and the evaluated answer. • Write the original expression in standard form (highest to lowest exponent). • The format of the line will be as follows: o = • All values will be rounded to 3 decimals.
Explanation / Answer
1. Objective-
for this section we provide c++ class implementation of linked list with logic,and to evaluate polynomial expressions. we provide code in the last section.
2.pesudocode,class details,linked list-
explaination- 1.in singly link list every node has next pointer.
2. next pointer is used for connecting other node to any node on link list.
3. we provide a head node to linked list ,it shows the starting node or addressed to 1st node of linkedd list.
4.if linked list is empty head beacome null.
5.there are some predefined operations for linked list
1-create list 2-add new node in the starting 3.-add new node in the ending 4.-add new node between two nodes 5.delete node with given position.
6.a linked list simply contain nodes.
7.every node consist 2 thing- data ,pointing address.
8.in the data field it stores data of any kind,and in address field it store address of next node attached to it.
9.for creation of list we first make a node head having null address.
10. then add new node on the be-half of requirement.
11. we use some values of cofficient as per reqirement for polynomial.
12. in any list or in any node of a linked list have more then one data field.
13.total no. of pointer field in a node depend upon type of linked list- ex. 1.singly linked list have 1 pointer field
2.doubly linked list have 2 pointer field.
code/////////-
c++
#include <iostream>
using namespace std;
/* class representation of link list for polynomial */
class polyll {
private:
struct polynode { // defining a node
float coeff; //data field of node for coefficient
int exp; // data field for power or exp val
polynode* link; // pointer to another node
} * p;
public:
polyll();
void append(float c, int e); // to add another node in the list
void display_poly(); // to display polynomial
~polyll();
};
polyll::polyll()
{
p = NULL;
}
void polyll::append(float c, int e) // function to add nodes in linked list
{
polynode* temp = p;
if (temp == NULL) {
temp = new polynode;
p = temp;
}
else {
while (temp->link != NULL)
temp = temp->link;
temp->link = new polynode;
temp = temp->link;
}
temp->coeff = c;
temp->exp = e;
temp->link = NULL;
}
void polyll::display_poly() // function to display polynomial
{
polynode* temp = p;
int f = 0;
cout << endl; while (temp != NULL) { if (f != 0) { if (temp->coeff > 0)
cout << " + ";
else
cout << " "; } if (temp->exp != 0)
cout << temp->coeff << "x^" << temp->exp;
else
cout << temp->coeff;
temp = temp->link;
f = 1;
}
}
polyll::~polyll()
{
polynode* q;
while (p != NULL) {
q = p->link;
delete p;
p = q;
}
}
int main()
{ // taking coefficient and exp value by own
polyll p1;
p1.append(1, 5);
p1.append(1.5, 4);
p1.append(0, 3);
p1.append(-4, 2);
p1.append(2.5, 1);
p1.append(12, 0);
cout << " this is your polynomial:"; // providing polynomial
p1.display_poly();
return 0;
}
output- 1x^5 + 1.5x^4 0x^3 -4x^2 + 2.5x^1 + 12