#include"stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class LinkList
{
protected:
struct node
{
int exp;
int info;
struct node* next;
}; typedef struct node * NODEPTR;
NODEPTR listPtr;
public:
LinkList(){
listPtr=0;
}
int IsEmpty();
void PushPoly(int cof,int ex);
//void Mult(list);
void Add(LinkList Poly1 , LinkList Poly2);
void Display(int num);
};
void LinkList::PushPoly(int cof , int ex){
NODEPTR p;
p = new node;
p->info=cof;
p->exp=ex;
p->next=listPtr;
listPtr=p;
}
void LinkList::Add(LinkList Poly1,LinkList Poly2){
int count1 , count2;
LinkList PolyR;
NODEPTR p,q,r;
for(p=Poly1.listPtr; p!=0 ; p=p->next) {
count1++;
}
for(q=Poly2.listPtr; q!=0 ; q=q->next)
count2++;
if(count1>count2)
{
for(int i=0;i<=count1;i++)
PolyR.PushPoly(0,0);
}
else if(count2>count1)
{
for(int i=1;i<=count2;i++)
PolyR.PushPoly(0,0);
}
else
{
for(int i=1;i<=count1;i++)
PolyR.PushPoly(0,0);
}
for(p=Poly1.listPtr, q=Poly2.listPtr,r=PolyR.listPtr; p!=0 && q!=0&&r!=0; p=p->next, q=q->next, r=r->next)
{
if(p->exp==q->exp)
{
r->info = p->info + q->info;
r->exp=p->exp;
}
else
{
r->info = p->info;
r->exp=p->exp;
}
}
PolyR.Display(3);
cout<<"result "<<endl;
}
void LinkList::Display(int num){
NODEPTR p;
cout<<"Polynomial "<<num<<endl<<endl;
for(p=listPtr;p!=0;p=p->next){
cout <<p->info<<" x^"<<p->exp<<(p->next!=0?" + ":" ");
}cout<<endl;
}
int main(){
LinkList poly1,poly2;
poly1.PushPoly(4,3);
poly1.PushPoly(5,2);
poly1.PushPoly(6,1);
poly2.PushPoly(6,3);
poly2.PushPoly(5,2);
poly2.PushPoly(7,1);
poly1.Display(1);
cout<<endl;
poly2.Display(2);
cout<<endl;
poly1.Add(poly1,poly2);
//cout <<endl<<endl;
//poly1.multi(poly2);
//poly1.evaluate();
getch();
return 0;
}
My addition function not work y ?????Help me plxxxx