다항식의 덧셈에 대해서 질문이요

pentas의 이미지

아래에 /////ostream& operator<< (ostream& os, Polynomial& p) 부분을 어떻게 채워야 하나요?ㅠㅠ

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial;

class Term
{
friend class Polynomial;
friend ostream& operator<<(ostream&, Polynomial&);
friend istream& operator>>(istream&, Polynomial&);

private:
float coef; // coefficient
int exp; // exponent
};

class Polynomial
{
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&);
private:
Term *termArray;
int capacity; // 1로 초기화
int terms; // 저장된 항의 수로 0으로 초기화
};
#endif

==================================================================
#include
#include "polya.h"
using namespace std;

istream& operator>> (istream& is, Polynomial& p)
{
// #terms and (coefficoent, 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)
{
//////////////////
////////////////// 이 부분을 어떻게 해야 하나요?
//////////////////////

return os;
}

Polynomial::Polynomial():capacity(4), terms(0)
{
termArray = new Term[capacity];
}

void Polynomial::NewTerm(const float theCoeff, const int theExp)
{
if(terms == capacity)
{
capacity *=2;
Term *temp = new Term[capacity]; // 새로운 배열
copy(termArray, termArray + terms, temp);
delete[] termArray; // 그전 메모리 반환
termArray = temp;
}
termArray[terms].coef=theCoeff;
termArray[terms++].exp=theExp;
}

Polynomial Polynomial::operator+(Polynomial& b)
{// *this와 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++;
}
}
// *this 의 나머지 항들을 추가한다.
for( ; aPos < terms; aPos++)
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
//b(x)의 나머지 항들을 추가한다.
for(; bPos < b.terms; bPos++)
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);

return c;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.