I am trying to built the program for handling polynomial... using linklist.... and so far i tried to implement in the Class to add the 2 polynomial using the operator overloading i got messed up with the some objects.... i have commented the parts please help.....

#include<iostream>

using namespace std;

#define Linux //Remove this for running in windows

#ifdef Linux
	#define cls system("clear")	
	
	#include <termios.h>
	#include <unistd.h>

	int getch();
	int getch()

	{

		struct termios oldt, newt;
		int ch;
		tcgetattr(STDIN_FILENO, &oldt);
		newt= oldt;

		newt.c_lflag &= ~(ICANON| ECHO);
		tcsetattr(STDIN_FILENO, TCSANOW, &newt);
		ch= getchar();
		tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

		return ch;
	}
#else
	#include<conio.h>
	#define cls system("cls")
#endif

#include<stdio.h>

//#include<string.h>

#include<math.h>


typedef struct node *Nodeptr;

struct node

{

	int cof;

	int pow;

	node *link;

};



struct polptr

{

	Nodeptr front,

			rear;

};


class Polynomial

{

 private:

	polptr pptr;

	

	void add_rear(int, int);

	

 public:

	Polynomial();

	void feed();

	void display();

	//char pol_modi(, int,int);

	Polynomial operator + (Polymonial P2);

};



Polynomial::Polynomial()

{

	pptr.front=NULL;

	pptr.rear=NULL;

}



void menu();



int main()

{

	menu();

	return 0;

}



#define Extra_Options1 case 'f': case 'F':



void menu()

{

	Polynomial P1, P2;



	for(;;)

		{

			cls;

			cout<<"Polynomial Puppetting\n"

				<<"\n1. Feed P1"

				<<"\n2. Feed P2"

				<<"\n3. Display P1"

				<<"\n4. Display P2"

				<<"\n5. Add P1=P1+P2"

				<<"\n6. Analyse"

				<<"\n7. Solve"

				<<"\n8. Plot"

				<<"\n\nEnter your choice: ";

			switch(getch())

				{

					case '1': Extra_Options1;

						cls;

						P1.feed();

						break;

					

					case '2':

						cls;

						P2.feed();

						break;

					

					case '3': case 'd': case 'D':

						cls;

						P1.display();

						getch();

						break;

					

					case '4':

						cls;

						P2.display();

						getch();

						break;

					

					case '5':

						cls;

						//add(P1, P2);

						break;

					

					case 27: return;

				}

		}

}



void Polynomial::add_rear(int info=0, int pow=0)

{

	if(info==0)

	{

		cout<<"Error: add_rear>>info feed=NULL";

		return;

	}

	

	Nodeptr temp=new node;

	temp->cof=info;

	temp->pow=pow;

	temp->link=NULL;

	

	if(pptr.front==NULL) pptr.front=temp;

	else pptr.rear->link=temp;

	pptr.rear=temp;

}



/*
Building later.. on

char pol_modi(char pos, int info=0, int pow=0)

{

 switch(pos)

    {

     case 'r':

	  rear->cof=info;

	  rear->pow=pow;

	  break;

     case 'p':   //info=default

	  rear->pow=pow;

	  return 0;

    }

 return 0;

}

*/



void Polynomial::feed()

{

	char str[30], catchy[6], flag_pow=0;

	int no=0, sign=+1, i;

	cout<<"Enter the expression: ";

	scanf("%s",&str);

	for(i=0; str[i]!='\0'; i++)

	{

		if(str[i]>='0' && str[i]<='9')

			{

				no=no*10+(str[i]-48);

				continue;

			}

	

		/*
		//building it later after it completes....

		if(strcmp(catchy,"ln"));

		if(strcmp(catchy,"log"));

		if(strcmp(catchy,"sin"));

		if(strcmp(catchy,"sin"));

		if(strcmp(catchy,"cos"));

		if(strcmp(catchy,"tan"));

		if(strcmp(catchy,"cot"));

		if(strcmp(catchy,"sec"));

		if(strcmp(catchy,"cosec"));

		*/

	

		switch(str[i])

		{

			case 'x': case 'y':  case 'z':

				if(no==0) no=1;

				add_rear(sign*no,1);

				no=0;

				break;

			

			case '^':

				flag_pow='p';

				no=0;

				break;

			

			case 'e': break;

			

			case '-':

			case '+':

				if(no!=0)

				{

					if(flag_pow=='p')

					{

						pptr.rear->pow=no;

						flag_pow=0;

					}

					else add_rear(sign*no);

					no=0;

				}

				if(str[i]=='-') sign=-1;

				else sign=+1;

				break;

		};

	}

	if(str[i]=='\0' && no!=0)

	{

		if(flag_pow=='p')

		{

			pptr.rear->pow=no;

			flag_pow=0;

		}

		else add_rear(sign*no);

	}

}


//Problem... in this 

Polynomial operator + (Polymonial P2);

{

	if(pptr.front==NULL && P2.pptr.front==NULL)

	{

		cout<<"Polynomial are empty !!";

		getch();

		return Polynomial; //Temporary Instance

	}

	else

	if(pptr.front==NULL && P2.pptr.front!=NULL) 

	{

		cout<<"P2 is void";

		getch();

		return P2; //pointer copied not address so 

	}

	else

	if(pptr.front!=NULL && P2.pptr.front==NULL)

	{

		//Problem in it	how to return the same objects.....	
		cout<<"P1 is void";

		getch();

		return ;

	}

	else

	{

//Not yet corrected		
		Nodeptr temp1, temp2, store;

		for(temp1=P1.front; temp1!=NULL; temp1=temp1->link)

			{

				for(store=temp2=P2.front; temp2!=NULL; temp2=temp2->link)

				{

					if(temp1->pow==temp2->pow)

					{

						temp1->cof=temp1->cof+temp2->cof;

						//comparition if its the first node or not

						if(temp2==P2.front) P2.front->link=temp2->link;

						else store->link=temp2->link;

						delete temp2;

						break;

					}

					store=temp2;

				}

				temp2=P2.front;

			}

		while(temp2!=NULL)

			{

				P1.rear->link=temp2;

				temp2=temp2->link;

			}

		

		cout<<"Polynomial successfully added";

		getch();

	}

}



void Polynomial::display()

{

	if(pptr.front==NULL)

	{

		cout<<"Data feed void!!!";

		return;

	}

	Nodeptr temp=pptr.front;

	while(temp!=NULL)

	{

		if(temp->cof>0) cout<<'+';

		switch(temp->pow)

		{

			case 0: cout<<temp->cof; break;

			case 1:

				if(temp->cof!=1) cout<<temp->cof;

				cout<<'x';

				break;

			default:

				if(temp->cof!=1) cout<<temp->cof;

				cout<<"x^"<<temp->pow;

		}

		temp=temp->link;

	}

}

You shouldn't roll your own linked lists. Use std::list or std::vector (since there's little reason for prefering linked lists).

You shouldn't roll your own linked lists. Use std::list or std::vector (since there's little reason for prefering linked lists).

I made this program for the educational purpose ... to learn the detail about the linklists and pointer and some thing about the Operator overloading so .... it created my own linklist... :X

Okay. In that case, it makes sense the separate the linked-listy parts of your objects from the polynomial parts of your objects, so that you only have to worry about one part of the problem at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.