학교 과제

c++로 작성한 간단한 다항식 클래스

꾸준함. 2017. 12. 24. 12:00


자료구조 프로그래밍 과목을 배우면서 c++로 작성한 간단한 다항식 클래스입니다.(2가지 버전)

poly.in


polya.h

#ifndef POLYNOMIAL_H

#define POLYNOMIAL_H

 

#include <iostream>

using namespace std;

 

class Polynomial; //전방참조

 

class Term

{

private:

        float coef; //coefficient

        int exp; //exponent

        friend class Polynomial;

        friend ostream &operator<<(ostream &, Polynomial &);

        friend istream &operator>>(istream &, Polynomial &);

};

 

class Polynomial

{

private:

        Term *termArray;

        int capacity; //1로 초기화

        int terms; //저장된 항의 수로 0으로 초기화

public:

        Polynomial(); //construct a polynomial p(x)=0

        Polynomial operator+(Polynomial &); //다항식의 합을 반환

        void NewTerm(const float, const int);

        friend ostream &operator<<(ostream &, Polynomial &);

        friend istream &operator>>(istream &, Polynomial &);

};

 

#endif


polya.cpp

#include <iostream>

#include "polya.h"

using namespace std;

 

Polynomial::Polynomial() :capacity(1), terms(0)

{

        termArray = new Term[capacity];

}

 

void Polynomial::NewTerm(const float theCoeff, const int theExp)

{

        //Add a new term to the end of termArray

        if (terms == capacity)

        {

               //double capacity of termArray

               capacity *= 2;

               Term *temp = new Term[capacity]; //new array

               copy(termArray, termArray + terms, temp);

               delete[]termArray; //deallocate old memory

               termArray = temp;

        }

        termArray[terms].coef = theCoeff;

        termArray[terms++].exp = theExp;

}

 

Polynomial Polynomial::operator+(Polynomial &b)

{

        //Return the sum of the polynomials *this and b

        Polynomial c;

        int aPos = 0, bPos = 0;

        while ((aPos < terms) && (bPos < b.terms))

        {

               if (termArray[aPos].exp == b.termArray[bPos].exp)

               {

                       float t = termArray[aPos].coef + b.termArray[bPos].coef;

                       if (t)

                              c.NewTerm(t, termArray[aPos].exp);

                       aPos++;

                       bPos++;

               }

               else if (termArray[aPos].exp < b.termArray[bPos].exp)

               {

                       c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);

                       bPos++;

               }

               else

               {

                       c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);

                       aPos++;

               }

        }

        //add in remaining term of *this

        for (;aPos < terms;aPos++)

               c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);

        //add in remaining terms of b(x)

        for (;bPos < b.terms;bPos++)

               c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);

        return c;

}

 

istream &operator>>(istream &is, Polynomial &p)

{

        //terms and (coefficient, exponent) pair들을 읽어들인다

        //높은 차수의 항부터 저장되었다고 가정한다

        int noofterms;

        float coef;

        int exp;;

        is >> noofterms;

        for (int i = 0; i < noofterms; i++)

        {

               is >> coef >> exp; //계수와 지수 pair를 읽어들인다

               p.NewTerm(coef, exp);

        }

        return is;

}

 

ostream &operator<<(ostream &os, Polynomial &p)

{

        for (int i = 0; i < p.terms; i++)

        {

               if (p.termArray[i].coef && p.termArray[i].coef != 1 && p.termArray[i].coef != -1)

                       os << p.termArray[i].coef;

               if(p.termArray[i].exp!=0)

                       os << "x^";

               if(p.termArray[i].exp && p.termArray[i].exp!=1)

                       os<< p.termArray[i].exp;

               if (i != p.terms - 1 && p.termArray[i + 1].coef >= 0)

                       os << " +";

               else if (p.termArray[i + 1].coef == -1)

                       os << " -";

               else if (p.termArray[i + 1].coef < -1)

                       os << " ";

        }

        os << endl;

        return os;

}


hw2a.cpp

#include <iostream>

#include "polya.h"

using namespace std;

 

int main(void)

{

        Polynomial p1, p2;

 

        cin >> p1 >> p2; //2개의 다항식을 읽어들인다

        Polynomial p3 = p1 + p2;

        cout << p1 << p2 << p3;

        return 0;

}


polyb.h

#ifndef POLYNOMIAL_H

#define POLYNOMIAL_H

 

#include <iostream>

using namespace std;

 

class Polynomial; //전방참조

 

class Term

{

private:

        float coef; //coefficient

        int exp; //exponent

        friend class Polynomial;

        friend ostream &operator<<(ostream &, Polynomial &);

        friend istream &operator>>(istream &, Polynomial &);

};

 

class Polynomial

{

private:

        Term *termArray;

        int capacity; //1로 초기화

        int terms; //저장된 항의 수로 0으로 초기화

public:

        Polynomial(); //construct a polynomial p(x)=0

        Polynomial operator+(Polynomial &); //다항식의 합을 반환

        Polynomial operator*(Polynomial &); //다항식의 곱을 반환

        void NewTerm(const float, const int);

        void sortArray();

        friend ostream &operator<<(ostream &, Polynomial &);

        friend istream &operator>>(istream &, Polynomial &);

};

 

#endif


polyb.cpp

#include <iostream>

#include "polyb.h"

using namespace std;

 

Polynomial::Polynomial() :capacity(1), terms(0)

{

        termArray = new Term[capacity];

}

 

void Polynomial::NewTerm(const float theCoeff, const int theExp)

{

        //Add a new term to the end of termArray

        if (terms == capacity)

        {

               //double capacity of termArray

               capacity *= 2;

               Term *temp = new Term[capacity]; //new array

               copy(termArray, termArray + terms, temp);

               delete[]termArray; //deallocate old memory

               termArray = temp;

        }

        termArray[terms].coef = theCoeff;

        termArray[terms++].exp = theExp;

}

 

Polynomial Polynomial::operator+(Polynomial &b)

{

        //Return the sum of the polynomials *this and b

        Polynomial c;

        int aPos = 0, bPos = 0;

        while ((aPos < terms) && (bPos < b.terms))

        {

               if (termArray[aPos].exp == b.termArray[bPos].exp)

               {

                       float t = termArray[aPos].coef + b.termArray[bPos].coef;

                       if (t)

                              c.NewTerm(t, termArray[aPos].exp);

                       aPos++;

                       bPos++;

               }

               else if (termArray[aPos].exp < b.termArray[bPos].exp)

               {

                       c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);

                       bPos++;

               }

               else

               {

                       c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);

                       aPos++;

               }

        }

        //add in remaining term of *this

        for (;aPos < terms;aPos++)

               c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);

        //add in remaining terms of b(x)

        for (;bPos < b.terms;bPos++)

               c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);

        return c;

}

 

Polynomial Polynomial::operator*(Polynomial &b)

{

        Polynomial c; //*this b의 곱을 c에 저장하려고 한다

        int aPos, bPos;

        for (aPos = 0; aPos < terms; aPos++)

        {

               for (bPos = 0; bPos < b.terms; bPos++)

               {

                       int place = -1; //찾고자 하는 인덱스가 없는 경우 -1

                       int tempCoef = termArray[aPos].coef*b.termArray[bPos].coef;

                       int tempExp = termArray[aPos].exp+b.termArray[bPos].exp;

                       for (int i = 0; i < c.terms; i++)

                              if (c.termArray[i].exp == tempExp)

                                      place = i; //같은 지수인 인덱스가 있다면 place에 저장

                       if(place==-1)

                              c.NewTerm(tempCoef, tempExp); //같은 지수가 없다면 새로 추가하고

                       else

                              c.termArray[place].coef += tempCoef; //같은 지수가 있다면 더한다

               }

        }

        return c;

}

 

void Polynomial::sortArray()

{

        //버블 소트

        for(int i=0; i<terms; i++)

               for(int j=i+1; j<terms; j++)

                       if (termArray[i].exp < termArray[j].exp)

                       {

                              Term temp = termArray[i];

                              termArray[i] = termArray[j];

                              termArray[j] = temp;

                       }

}

 

istream &operator>>(istream &is, Polynomial &p)

{

        //terms and (coefficient, exponent) pair들을 읽어들인다

        //높은 차수의 항부터 저장되었다고 가정한다

        int noofterms;

        float coef;

        int exp;;

        is >> noofterms;

        for (int i = 0; i < noofterms; i++)

        {

               is >> coef >> exp; //계수와 지수 pair를 읽어들인다

               p.NewTerm(coef, exp);

        }

        return is;

}

 

ostream &operator<<(ostream &os, Polynomial &p)

{

        p.sortArray(); //정렬

        for (int i = 0; i < p.terms; i++)

        {

               if (p.termArray[i].coef && p.termArray[i].coef != 1 && p.termArray[i].coef != -1)

                       os << p.termArray[i].coef;

               if (p.termArray[i].exp != 0)

                       os << "x^";

               if (p.termArray[i].exp && p.termArray[i].exp != 1)

                       os << p.termArray[i].exp;

               if (i != p.terms - 1 && p.termArray[i + 1].coef >= 0)

                       os << " +";

               else if (p.termArray[i + 1].coef == -1)

                       os << " -";

               else if (p.termArray[i + 1].coef < -1)

                       os << " ";

        }

        os << endl;

        return os;

}

 

hw2b.cpp

#include <iostream>

#include "polyb.h"

using namespace std;

 

int main(void)

{

        Polynomial p1, p2;

 

        cin >> p1 >> p2; //2개의 다항식을 읽어들인다

        Polynomial p3 = p1*p2; //hw2a.cpp에서 + *로 바꾸었음

        cout << p1 << p2 << p3;

        return 0;

}



개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형