1 Replies - 4394 Views - Last Post: 13 July 2009 - 03:32 PM Rate Topic: -----

#1 mejohnm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-January 09

polynomial program

Post icon  Posted 13 July 2009 - 07:30 AM

I am writing a code which the user has options to enter a polynomial, see the degree of the polynomial, change the coefficient, and see what the coefficient is at a certain xth power. I am using ADT stucture with linked list. I am a bit stuck cause I made my main but the functions are not going to the .cpp functions. It just shows the main screen. Here is my code. I know I did something stupid wrong here, just can't figure it out. Once I figure this out I will keep on writing out the .cpp file.

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


Is This A Good Question/Topic? 0
  • +

Replies To: polynomial program

#2 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: polynomial program

Posted 13 July 2009 - 03:32 PM

I am not familiar with this line &polynomial::displayPolynomial;, but class member functions are usually called like so:
polynomial classInstance;
classInstance.displayPolynomial(); // for example



I just noticed that in your switch statement you are not passing any of the required arguments to your functions.

This post has been edited by apw5020: 13 July 2009 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1