I am currently at Kutztown University enrolled in Computer Science II. I am struggling to finish writing the actual cpp for the program, the implementation and header files are finished.
Here are the instructions of the program:
Recall that a polynomial is a sum of terms in the form, f(x) = a0x0 + a1x1 + a2x2 + … + anxn. You are to write a program that will manipulate polynomials. The application should instantiate polynomials as linked lists of terms. The application should allow the user to do the following
1. Build a polynomial by entering it term by term. The function must first check to see if there is a term with that exponent in the polynomial (find). If there is not, the function will insert the term. If there is, the function will first remove the existing term and then insert the new term.
2. Add the two polynomials to produce a new polynomial. The function will be passed two polynomials and return their sum.
3. Print out the three polynomials in the form
term1 + term2 + … + termN
+ term1 + term2 + … + termN
term1 + term2 + … + termN
4. Evaluate the polynomial that was the result of 2 given a value from the user
5. Clear the polynomials and start again
The user should be able to repeat the process above as many times as he or she wants.
Now I have finished the implementation and the header files:
//File: linkedList.h
//Author: Prof. Day
//Course: CSC 136
//Date: Spring 2009
//Purpose: Declaration of linked list class
#include <iostream>
using namespace std;
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
template <class T>
class linkedList
{
public:
//sets the head to null
linkedList() {head = NULL;}
//The Big 3
//Copy constructor does a deep copy
linkedList(const linkedList &list);
//Assignment operator does a deep copy
linkedList& operator=(const linkedList &list);
//Destructor deletes all the nodes in the list
~linkedList();
//returns true if the list is empty, false otherwise
bool empty() const;
//clears out all the nodes in the list and sets head to NULL
void clear();
//inserts the value in the list to maintain ascending order
//insert fails if the value is already in the list
bool insert(T value);
//removes the value from the list
//remove fails if there value is not found in the list
bool remove(T value);
//find looks for the value in the list. If found, it copies
// the list component into the parameter. This is in case
// the component is not a simple type.
//Return: true if found, falser otherwise
bool find(T &value);
//for traversing
//moves to the beginning of the list
void start();
//returns the next value in the list and advances along
//getNext fails if it is at the end of the list
bool getNext(T &value);
private:
class node
{
public:
node() {next = NULL;}
node(T value) {data = value; next = NULL;}
T data;
node *next;
};
//attributes
node *head; //points to the beginning of the list
node *current; //keeps track of where the traversal is
};
#endif
template class linkedList<int>;
//File: linkedList.cpp
//Author: Prof. Day
//Course: CSC 136
//Date: Spring 2009
//Purpose: Declaration of linked list class
#include <iostream>
#include "linkedList.h"
using namespace std;
//The Big 3
//Copy constructor does a deep copy
template <class t>
linkedList<t>:: linkedList(const linkedList &list)
{
node *create;
for (node *ptr = list.head; ptr != NULL; ptr = ptr->next)
if (head == NULL)
{
head = new node(ptr->data);
create = head;
}
else
{
create->next = new node(ptr->data);
create = create->next;
}
}
//Assignment operator does a deep copy
template <class t>
linkedList<t>& linkedList<t>:: operator=(const linkedList &list)
{
if (head != NULL)
delete this;
node *create;
for (node *ptr = list.head; ptr != NULL; ptr = ptr->next)
if (head == NULL)
{
head = new node(ptr->data);
create = head;
}
else
{
create->next = new node(ptr->data);
create = create->next;
}
}
//Destructor deletes all the nodes in the list
template <class t>
linkedList<t>:: ~linkedList()
{
for (node *ptr = head; ptr != NULL; )
{
ptr = ptr->next;
delete ptr;
}
head = NULL;
}
//returns true if the list is empty, false otherwise
template <class t>
bool linkedList<t>::empty() const
{
if (head == NULL)
return true;
return false;
}
//clears out all the nodes in the list and sets head to NULL
template <class t>
void linkedList<t>::clear()
{
current = head;
while (current != NULL)
{
head = current->next;
delete current;
current = head;
}
head = NULL;
}
//inserts the value in the list to maintain ascending order
//insert fails if the value is already in the list
template <class t>
bool linkedList<t>::insert(t value)
{
node *insertloc = new node(value);
for (node *ptr = head; ptr != NULL; ptr = ptr->next)
{
if (ptr->data == value)
return false;
else if ((value > ptr->data) && (value < ptr->next->data))
{
insertloc->next = ptr->next;
ptr->next = insertloc;
return true;
}
}
}
//removes the value from the list
//remove fails if there value is not found in the list
template <class t>
bool linkedList<t>::remove(t value)
{
for (node *ptr = head; ptr !=NULL; ptr = ptr -> next)
{
if (ptr->data == value)
{
if (ptr == head)
head = ptr->next;
else
current -> next = ptr -> next;
delete ptr;
return true;
}
current = ptr;
}
return false;
}
//find looks for the value in the list. If found, it copies
// the list component into the parameter. This is in case
// the component is not a simple type.
//Return: true if found, falser otherwise
template <class t>
bool linkedList<t>::find(t &value)
{
for (node *ptr = head; ptr != NULL; ptr = ptr -> next)
if (ptr -> data == value)
value = ptr -> data;
return true;
return false;
}
//for traversing
//moves to the beginning of the list
template <class t>
void linkedList<t>::start()
{
current = head;
}
//returns the next value in the list and advances along
//getNext fails if it is at the end of the list
template <class t>
bool linkedList<t>::getNext(t &value)
{
if (current == NULL)
return false;
value = current -> data;
current = current -> next;
return true;
}
This is the part I need help with, the actual program.. This is what I have so far:
//File:linkedListApp.cpp
// Insructions to follow while coding:
/*/ if (there is a term with exponent(t2) in the polynomial (using find)
remove existing term then insert the term;
else
insert the term;
add two polynomials to produce a new polynomial;
function will be passed two polynomails and return the sum;
print out polynomials in the form as on paper;
evaluate the polynomial that was the result of 2 given a value from the user;
clear the polynomials and start again; /*/
// Program Starts:
#include "linkedList.h"
#include <iostream>
using namespace std;
int main()
{
int t1,t2;
linkedList<int>p1,p2;
cout<<"Please enter a polynomial term by term: (Ex. 12 3 ) \n";
cin >> t1 >> t2;;
return 0;
}
I finished most of the program with the assistance of a tutor and seeing as it's Saturday and the program is due by 12:00 Sunday night im in a bind.
Thanks for any help!
-Bill