#include <iostream>
#include <string>
using namespace std;
class Poly; // forward declaration
//=====================================================================================
class Node
{
friend class Poly;
private:
int coefficient;
int degree;
Node *next;
public:
Node(int c=0,int d = 0 , Node *n=NULL);
~Node() {};
};
Node::Node(int c,int d, Node *n)
{
coefficient = c;
degree = d;
next = n;
}
//=====================================================================================
class Poly
{
private:
Node *first;
public:
Poly(Node *f=NULL); // 생성자
~Poly(); // 소멸자
Poly(const Poly &poly); // 복사 생성자
void print(); // 리스트 출력
void init(int coe,int deg);
void Insert(string &str);
void init(int num);
void input(int num);
Poly& operator+(const Poly& poly); // 배정연산자
};
//=======================================================================================
Poly::Poly(const Poly &poly)
{
Node *current;
for( current = poly.first; current->next != NULL; current = current->next );
{
init(current->coefficient,current->degree);
}
}
Poly::Poly(Node *f)
{
first = f;
}
Poly::~Poly()
{
Node *current, *delnode;
current = first;
while( current != NULL )
{
delnode = current;
current = current->next;
delete delnode;
}
}
void Poly::init(int coe,int deg)
{
Node *newnode,*current;
newnode = new Node(coe,deg,NULL);
if( first == NULL)
first = newnode;
else
{
for( current = first; current->next != NULL; current = current->next );
current->next = newnode;
}
}
void Poly::input(int num)
{
if(num <= 0)
{
cout<<" 0개이하의 다항식을 입력하실수는 없습니다"<< endl;
return;
}
string poly;
for(int i = 0 ; i < num ; i ++)
{
cout<<i+1<<"번째 다항식 : ";
cin>>poly;
Insert(poly);
}
}
void Poly::Insert(string &str)
{
int xpos = str.find(\'x\')-1;
int length = str.length();
char coe[16] ="\\0";
char deg[16] ="\\0";
str.copy(coe,xpos,1);
str.copy(deg,xpos+3,xpos+3);
int coeR= 0;
int degR = 0;
int ss = 1;
for( int i = xpos-1 ; i > -1 ; i--)
{
coeR+=((int)coe[i]-48)*ss;
ss*=10;
}
if(str[0] == \'-\')
coeR*=-1;
ss = 1;
for ( int j = length-(xpos+4) ; j > -1 ; j--)
{
degR+=((int)deg[j]-48)*ss;
ss*=10;
}
init(coeR,degR);
}
void Poly::print()
{
Node *current;
if ( first == NULL )
cout << "비었어!";
else
{
for ( current = first; current != NULL; current = current->next )
{
if( current->coefficient> 0)
cout << "+";
cout << current->coefficient << "x^"<<current->degree;
}
}
cout << endl;
}
//================================================================================
Poly& Poly::operator+(const Poly& poly)
{
static Poly result;
Node *firstpoly, *secondpoly , *newnode ,*current;
int coe;
int aa;
string str;
firstpoly = first;
secondpoly = poly.first;
if(firstpoly->degree >= secondpoly->degree)
{
for(; firstpoly != NULL; firstpoly = firstpoly->next)
{
for( ; secondpoly != NULL; secondpoly = secondpoly->next)
{
if(firstpoly->degree == secondpoly->degree)
{
coe = firstpoly->coefficient + secondpoly->coefficient;
result.init(coe,firstpoly->degree);
}
else
{
coe = firstpoly->coefficient;
result.init(coe, firstpoly->degree);
}
}
}
}
else
{
for( ; secondpoly != NULL; secondpoly = secondpoly->next)
{
for(; firstpoly != NULL; firstpoly = firstpoly->next)
{
if(secondpoly->degree == firstpoly->degree)
{
coe = secondpoly->coefficient + firstpoly->coefficient;
result.init(coe,secondpoly->degree);
}
else
{
coe=secondpoly->coefficient;
result.init(coe,secondpoly->degree);
}
}
}
}
result.print();
return result;
}
//========================================================================================
int main()
{
Poly a, b,c;
int input;
//float r;
cout<< "[첫번째 다항식 입력 ]\\n항수 :";
cin>>input;
a.input(input);
a.print();
cout<<endl;
cout<< "[두번째 다항식 입력 ]\\n항수 : ";
cin>>input;
b.input(input);
b.print();
cout<<endl;
cout<<"첫번째 다항식 : ";a.print();
cout<<"두번째 다항식 : ";b.print();
c = a+b; //이것을 오버로딩 해야되는데 잘 안됩니다...
return 0;
}
//==============================================================================
연결리스트로 다항식을 만들고
그 다항식을 연산하려고 하는데
연결리스트에 다항식 쳐 넣는것까지는 했는데
다항식끼리 연산으로 연산자오버로딩을 할려고 하는데 이게 미치겠네요..
오버로딩을 간단히 사용하는건 많이 해보다가 조금 복잡한걸 하니 바로 막히는...
조언좀 부탁드리면 안될까요?
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.