다항식 클래스 (class Polynomial)
Source Code
polynomial.h
cpp
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Term
{
public:
int exp;
double coef;
void Display(bool is_first_term);
};
class Polynomial
{
private:
int max_exp;
int capacity;
public:
Term *termArray;
int num_of_terms;
Polynomial(int given_capacity);
~Polynomial();
int NewTerm(double given_coef, int given_exp);
Polynomial* Add(Polynomial* b);
Polynomial* Mult(Polynomial* b);
void Display();
double Evaluate(double value);
};
#endif
polynomial.cpp
cpp
#include <iostream>
#include "polynomial.h"
#include <cmath>
void Term::Display(bool is_first_term)
{
if(coef == 0)
return;
if(coef > 0)
{
if(!is_first_term)
{
std::cout << " + " << coef;
}
else
{
std::cout << coef;
}
}
if(coef < 0)
std::cout << " - " << -coef;
if(exp != 0)
std::cout << "x";
if(exp > 1)
std::cout << "^";
if(exp > 1)
std::cout << exp;
}
Polynomial::Polynomial(int given_capacity)
{
max_exp = -1;
num_of_terms = 0;
capacity = given_capacity;
termArray = new Term[given_capacity];
}
Polynomial::~Polynomial()
{
delete [] termArray;
}
int Polynomial::NewTerm(double given_coef, int given_exp)
{
if(given_exp < 0)
return -1;
if(num_of_terms == capacity)
return -1;
for(int i=0;i<capacity;i++)
{
if(termArray[i].exp==given_exp)
return -1;
}
termArray[num_of_terms].coef = given_coef;
termArray[num_of_terms].exp = given_exp;
num_of_terms++;
if(max_exp < given_exp)
max_exp = given_exp;
return 0;
}
void Polynomial::Display()
{
if(num_of_terms > 0)
{
int i,j;
for(i=max_exp;i>=0;i--)
{
for(j=0;j<num_of_terms;j++)
{
if(termArray[j].exp==i)
break;
}
if(j != num_of_terms)
termArray[j].Display(i==max_exp);
}
}
std::cout << std::endl;
}
double Polynomial::Evaluate(double value)
{
double result = 0;
for(int i=0;i<num_of_terms;i++)
{
result += termArray[i].coef*pow(value,termArray[i].exp);
}
return result;
}
Polynomial* Polynomial::Add(Polynomial *b)
{
Polynomial* result_poly;
int num_of_term_in_result = num_of_terms + b->num_of_terms;
int num_of_duplicate_exp=0;
int i,j;
for(i=0;i<num_of_terms;i++)
{
for(j=0;j<b->num_of_terms;j++)
{
if(termArray[i].exp == b->termArray[j].exp)
num_of_duplicate_exp++;
}
}
num_of_term_in_result -= num_of_duplicate_exp;
result_poly = new Polynomial(num_of_term_in_result);
for(i=0;i<num_of_terms;i++)
{
result_poly->NewTerm(termArray[i].coef,termArray[i].exp);
}
for(i=0;i<b->num_of_terms;i++)
{
for(j=0;j<result_poly->num_of_terms;j++)
{
if(result_poly->termArray[j].exp == b->termArray[i].exp)
{
result_poly->termArray[j].coef += b->termArray[i].coef;
break;
}
}
if(j == result_poly->num_of_terms)
{
result_poly->NewTerm(b->termArray[i].coef, b->termArray[i].exp);
}
}
return result_poly;
}
Polynomial* Polynomial::Mult(Polynomial *b)
{
Polynomial* result_poly;
Polynomial* temp_poly;
result_poly = new Polynomial(b->num_of_terms);
int i,j;
for(i=0;i<num_of_terms;i++)
{
temp_poly = new Polynomial(b->num_of_terms);
for(j=0;j<b->num_of_terms;j++)
{
temp_poly->NewTerm(termArray[i].coef*b->termArray[j].coef, termArray[i].exp+b->termArray[j].exp);
}
result_poly = result_poly->Add(temp_poly);
delete temp_poly;
}
return result_poly;
}