Project Rosalynde for KAIST student
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define EXP_BOUND 10 // ¡ˆºˆ¥¬ 0~EXP_BOUND
#define COEF_BOUND 20 // ∞˺ˆ¥¬ -COEF_BOUND/2 ~ +COEF_BOUND/2
#define TERMS_NUM 40 // terms Array index ∞πºˆ¥¬ TERMS_NUM∞≥
#define SUB_TERMS_NUM 10 // Polynomial B¿« ª©±‚ ≈◊Ω∫∆Æø°º≠¿« ∞πºˆ
#define ADD_TERMS_NUM 10 // Polynomial B¿« ¥ı«œ±‚ ≈◊Ω∫∆Æø°º≠¿« ∞πºˆ
#define INTEG_CONST 20 // ¿˚∫– ªÛºˆ
using namespace std;
class Term
{
private:
double coef; // ∞˺ˆ
int exp; // ¬˜ºˆ
// you can add your own private member here
public:
// constructor
Term(); // default constructor. ∞˺ˆøÕ ¬˜ºˆ ∏µŒ 0¿∏∑Œ √ ±‚»≠
Term(double c, int e); // ∞˺ˆ∏¶ c∑Œ, ¬˜ºˆ∏¶ e∑Œ √ ±‚»≠
// setter, getter
double GetCoef(); // coef ∞™¿ª ∏Æ≈œ
int GetExp(); // exp ∞™¿ª ∏Æ≈œ
void SetCoef(double c); // coef∞™¿ª c∑Œ º≥¡§
void SetExp(int e); // exp∞™¿ª e∑Œ º≥¡§
//print term. (coef)x^(exp) e.g 3x^2
void Print();
};
Term::Term()
{
coef = 0;
exp = 0;
}
Term::Term(double c, int e)
{
coef = c;
exp = e;
}
double Term::GetCoef()
{
return coef;
}
int Term::GetExp()
{
return exp;
}
void Term::SetCoef(double c)
{
coef = c;
}
void Term::SetExp(int e)
{
exp = e;
}
void Term::Print()
{
if(coef == 0)
return;
else if(exp == 0)
cout<<coef;
else if(exp == 1)
cout<<coef<<"x";
else
cout<<coef<<"x^"<<exp;
}
// Node class. linked list¿« node
// Term ∞¥√º «œ≥™øÕ linked list∏¶ ¿ß«— ∆˜¿Œ≈Õ next, prev ∏¶ ∞°¡¯¥Ÿ.
class Node
{
// Node class º≥∞Ë«—¥Ÿ.
// ∞£∆Ì«— ±∏«ˆ¿ª ¿ß«ÿ member ∏µŒ public øµø™ø° ¡§¿««ÿµµ ªÛ∞¸æ¯¿Ω.
public:
Term t;
Node* prev;
Node* next;
};
class Polynomial
{
private:
Node* head; // dummy Node head
Node* tail; // dummy Node tail
// you can add your own private member here.
public:
Polynomial(); // default constructor. head, tail node∏∏ ∞°¡¯ Polynomial ª˝º∫
// copy constructor. Polynomial pøÕ µø¿œ«— «◊µÈ¿ª ∞°¡¯ Polynomial ª˝º∫
Polynomial(Polynomial& p);
~Polynomial(); // destructor
void Add(Term t); // Polynomialø° Term t∏¶ ¥ı«‘
void Sub(Term t); // Polynomialø° Term t∏¶ ª≠
// Polynomial¿« size(«◊¿«∞≥ºˆ) ∏Æ≈œ.
// ∞˺ˆ∞° 0¿Œ «◊(ªÛºˆ«◊0µµ∆˜«‘)µÈ¿∫ «◊¿«∞≥ºˆø° ∆˜«‘滵 .
int Size();
// Polynomial¿ª default ªÛ≈¬∑Œ √ ±‚»≠.
// ¡Ô Polynomial¥¬ head, tail node∏∏ ∞°¡¯¥Ÿ
void Reset();
// "this"øÕ "p" µ°º¿∞·∞˙∏¶ ¿˙¿Â«— Polynomial return
Polynomial Add(Polynomial p);
// "this"øÕ "p" ª¨º¿ ∞·∞˙∏¶ ¿˙¿Â«— Polynomial return
Polynomial Sub(Polynomial p);
// "this"øÕ "p" ∞ˆº¿ ∞·∞˙∏¶ ¿˙¿Â«— Polynomial return
Polynomial Mul(Polynomial p);
// "this"¿« πÃ∫– ∞·∞˙∏¶ ¿˙¿Â«— Polynomial return
Polynomial Diff();
// "this"¿« ¿˚∫– ∞·∞˙∏¶ ¿˙¿Â«— Polynomial return
Polynomial Integ(double integ_const);
// Polynomial¿ª xø° ∞¸«— ≥ª∏≤¬˜º¯¿∏∑Œ√‚∑¬
void Print();
};
Polynomial::Polynomial()
{
head = new Node();
tail = new Node();
head->next = tail;
head->prev = NULL;
tail->prev = head;
tail->next = NULL;
}
Polynomial::Polynomial(Polynomial &p)
{
head = new Node();
tail = new Node();
head->next = tail;
head->prev = NULL;
tail->prev = head;
tail->next = NULL;
Node* cursor = p.head->next;
while(cursor != p.tail)
{
Add(cursor->t);
cursor = cursor->next;
}
}
Polynomial::~Polynomial()
{
}
void Polynomial::Add(Term t)
{
Node* cursor=head->next;
while(cursor!=tail)
{
if(cursor->t.GetExp()==t.GetExp())
{
cursor->t.SetCoef(cursor->t.GetCoef()+t.GetCoef());
if(cursor->t.GetCoef()==0)
{
Node* del=cursor;
cursor->prev->next=cursor->next;
cursor->next->prev=cursor->prev;
delete del;
}
return;
}
if(cursor->t.GetExp() < t.GetExp()) break;
cursor=cursor->next;
}
Node* tmp=new Node();
tmp->t=t;
tmp->prev=cursor->prev;
tmp->next=cursor;
cursor->prev->next=tmp;
cursor->prev=tmp;
return;
}
void Polynomial::Sub(Term t)
{
Node* cursor=head->next;
while(cursor!=tail)
{
if(cursor->t.GetExp()==t.GetExp())
{
cursor->t.SetCoef(cursor->t.GetCoef()-t.GetCoef());
if(cursor->t.GetCoef()==0)
{
Node* del=cursor;
cursor->prev->next=cursor->next;
cursor->next->prev=cursor->prev;
delete del;
}
return;
}
if(cursor->t.GetExp() < t.GetExp()) break;
cursor=cursor->next;
}
Node* tmp=new Node();
Term* newt=new Term(t.GetCoef()*(-1),t.GetExp());
tmp->t=*newt;
tmp->prev=cursor->prev;
tmp->next=cursor;
cursor->prev->next=tmp;
cursor->prev=tmp;
return;
}
int Polynomial::Size()
{
int size = 0;
Node* tmp=head->next;
while(tmp!=tail)
{
size++;
tmp=tmp->next;
}
return size;
}
void Polynomial::Reset()
{
if(Size()!=0)
{
Node* tmp= head->next;
while(tmp!=tail)
{
Node* del=tmp;
tmp=tmp->next;
delete del;
}
}
head->next=tail;
tail->next=head;
}
Polynomial Polynomial::Add(Polynomial p)
{
Polynomial result;
Node* cursor=p.head->next;
while(cursor!=p.tail)
{
result.Add(cursor->t);
cursor=cursor->next;
}
cursor=head->next;
while(cursor!=tail)
{
result.Add(cursor->t);
cursor=cursor->next;
}
return result;
}
Polynomial Polynomial::Sub(Polynomial p)
{
Polynomial result;
Node* cursor=p.head->next;
while(cursor!=p.tail)
{
result.Sub(cursor->t);
cursor=cursor->next;
}
cursor=head->next;
while(cursor!=tail)
{
result.Add(cursor->t);
cursor=cursor->next;
}
return result;
}
Polynomial Polynomial::Mul(Polynomial p)
{
Polynomial result;
return result;
}
Polynomial Polynomial::Diff()
{
Polynomial result;
return result;
}
Polynomial Polynomial::Integ(double integ_const)
{
Polynomial result;
return result;
}
void Polynomial::Print()
{
Node* cursor = head->next;
if(Size() == 0)
{
cout<<"0"<<endl;
return;
}
while(cursor != tail->prev)
{
cursor->t.Print();
cout<<" + ";
cursor = cursor->next;
}
cursor->t.Print();
cout<<endl;
}
int main()
{
Term terms[20] = {Term(-5, 2), Term(-3, 1), Term(2, 5), Term(0, 3), Term(3, 0), Term(-74, 4), Term(17, 3), Term(31, 2), Term(-184, 3), Term(9, 1), Term(15, 1), Term(-11, 2), Term(21, 5), Term(51, 3), Term(19, 4), Term(15, 1), Term(), Term(-31, 2), Term(-19, 0), Term(0, 4)};
Polynomial A;
cout<<"A: "; A.Print(); // A= 0
cout<<"Size of A : "<<A.Size()<<endl; // Size of A : 0
for(int i=0; i<20; i++){A.Add(terms[i]);}
cout<<"A: "; A.Print(); // A=
cout<<"Size of A : "<<A.Size()<<endl; // Size of A :
Polynomial _A(A);
for(int i=0; i<20; i++){_A.Sub(terms[i]);}
cout<<"_A: "; _A.Print(); // _A= 0
cout<<"Size of _A: "<<_A.Size()<<endl;// Size of A : 0
Polynomial A_(A); A_.Reset();
Polynomial B(A); B.Sub(terms[3]); B.Sub(terms[5]); B.Sub(terms[7]); B.Sub(terms[9]); B.Sub(terms[11]);
Polynomial C(A); C.Add(terms[12]); C.Add(terms[14]); C.Add(terms[16]); C.Add(terms[18]); C.Add(terms[19]);
cout<<"A: "; A.Print(); // A=
cout<<"Size of A: "<<A.Size()<<endl; // Size of A :
cout<<"A_: "; A_.Print(); // A_=
cout<<"Size of A_: "<<A_.Size()<<endl; // Size of A_ :
cout<<"B: "; B.Print(); // B=
cout<<"Size of B: "<<B.Size()<<endl; // Size of B :
cout<<"C: "; C.Print(); // C=
cout<<"Size of C: "<<C.Size()<<endl; // Size of C :
Polynomial AB;
AB = A.Add(B);
Polynomial AA_;
AA_= A.Add(A_);
Polynomial A_C;
A_C= A.Sub(C);
Polynomial A_A_;
A_A_= A.Sub(A_);
Polynomial AxB;
AxB= A.Mul(B);
Polynomial AxC;
AxC= A.Mul(C);
Polynomial AxA_;
AxA_= A.Mul(A_);
Polynomial BxC;
BxC= B.Mul(C);
Polynomial Diff_A;
Diff_A= A.Diff();
Polynomial Diff_A_;
Diff_A_= A_.Diff();
cout<<"A+B: "; AB.Print();
cout<<"A+A_: "; AA_.Print();
cout<<"A-C: "; A_C.Print();
cout<<"A-A_: "; A_A_.Print();
cout<<"AxB: "; AxB.Print();
cout<<"AxC: "; AxC.Print();
cout<<"AxA_: "; AxA_.Print();
cout<<"BxC: "; BxC.Print();
cout<<"Diff_A: "; Diff_A.Print();
cout<<"Diff_A_: "; Diff_A_.Print();
return 0;
}