header file:
struct Node
{
int degree;
int coef;
Node *next;
};
class polynomial
{
public:
// constructors
polynomial();
void append(int degree, int coef);
void displayDegree(int coef);
void displayCoefficient(int degree);
void newCoefficient(int degree, int coef);
void displayPolynomial();
private:
int coef;
int degree;
};
.cpp file
#include <iostream>
#include "polynomial.h"
using namespace std;
// default constructor
polynomial::polynomial()
{
}
Node *start_ptr = NULL;
Node *temp, *temp2, *current;
void polynomial::append(int degree, int coef)
{
temp = new Node;
cout << "Please enter the coefficient: ";
cin >> temp->coef;
cout << "Please enter the \"x\" power: ";
cin >> temp->degree;
temp->next = NULL;
// Set up link to this node
if (start_ptr == NULL)
{ start_ptr = temp;
current = start_ptr;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->next != NULL)
{ temp2 = temp2->next;
// Move to next link in chain
}
temp2->next = temp;
}
}
// returns the coefficient at the xth power term.
void polynomial::displayCoefficient(int degree)
{
}
// returns the xth degree at a coefficient
void polynomial::displayDegree(int coef)
{
}
//replaces the coefficient of the "x"th power term with a new coefficient
void polynomial::newCoefficient(int degree, int coef)
{
}
//displays the current polynomial
void polynomial::displayPolynomial()
{
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "There is no polynomail entered!" << endl;
else
{
while (temp != NULL)
{ // Display details for what temp points to
cout << temp->coef << "x^";
cout << temp->degree << " + ";
cout << endl;
temp = temp->next;
}
}
}
main .cpp
#include <iostream>
#include "polynomial.h"
using namespace std;
void main()
{
int option = 0;
do
{
&polynomial::displayPolynomial;
// display_list();
cout << endl;
cout << "Please select an option : " << endl;
cout << "0. Exit the program." << endl;
cout << "1. Enter the polynomial." << endl;
cout << "2. Display the degree of the polynomial" << endl;
cout << "3. Display the coefficient of the \"x\"th power term." << endl;
cout << "4. Change the coefficient at a certain \"x\"th power term" << endl;
cout << endl << " >> ";
cin >> option;
switch (option)
{
case 1 : &polynomial::append; break;
case 2 : &polynomial::displayDegree; break;
case 3 : &polynomial::displayCoefficient; break;
case 4 : &polynomial::newCoefficient;
}
}
while (option != 0);
}
This post has been edited by mejohnm: 13 July 2009 - 11:09 AM

New Topic/Question
Reply




MultiQuote




|