기록장

[C++ 자료구조] ch2 프로그래밍 프로젝트 1번 : 다항식 클래스 테스트 프로그램 본문

코딩 공부/C++

[C++ 자료구조] ch2 프로그래밍 프로젝트 1번 : 다항식 클래스 테스트 프로그램

민j 2023. 3. 14. 11:29
#include <stdio.h>
#include <cstdio>
#define MAX_DEGREE 80

class Polynomial {
	int degree;
	float coef[MAX_DEGREE];

public:
	Polynomial() {
		degree = 0;
		coef[MAX_DEGREE] = 0;

	}

	// 다항식의 내용을 입력받는 멤버함수
	void read() {
		printf("다항식의 최고 차수를 입력하시오: ");
		scanf_s("%d", &degree);
		printf("각 항의 계수를 입력하시오 (총 %d개): ", degree + 1);
		for (int i = 0; i <= degree; i++)
			scanf_s("%f", coef + i);
	}

	// (4)번 문제
	// 다항식의 내용을 화면에 출력하는 함수
	void display(const char* str = " Poly =") {
		printf("\t%s", str);
		for (int i = 0; i < degree; i++)
			if (coef[i] != 0) {
				if (coef[i] != 1)
					printf("%5.1f x^%d + ", coef[i], degree - i);
				else
					printf("x^%d + ", degree - i);
			}
		if (coef[degree] != 0)
			printf("%4.1f\n", coef[degree]);
		else
			printf("\n");
	}

	// 다항식 a와 b를 더하는 함수. a와 b를 더해 자신의 다항식 설정.
	void add(Polynomial a, Polynomial b) {
		if (a.degree > b.degree) {
			*this = a;
			for (int i = 0; i <= b.degree; i++)
				coef[i + (degree - b.degree)] += b.coef[i];
		}
		else {
			*this = b;
			for (int i = 0; i <= a.degree; i++)
				coef[i + (degree - a.degree)] += a.coef[i];
		}
	}

	//(1)번 문제
	// 다항식 a와 b의 뺄셈을 구하는 함수
	void sub(Polynomial a, Polynomial b) {
		if (a.degree < b.degree) {
			*this = b;
			for (int i = 0; i <= a.degree; i++)
				coef[i + (degree - a.degree)] -= a.coef[i];
		}
		else {
			*this = a;
			for (int i = 0; i <= b.degree; i++)
				coef[i + (degree - b.degree)] -= b.coef[i];
		}
	}

	// (2)번 문제
	// 다항식 a와 b의 곱셈을 구하는 함수
	void mult(Polynomial a, Polynomial b) {
		if (a.degree < b.degree) {
			*this = b;
			for (int i = 0; i < a.degree; i++)
				coef[i + (degree - a.degree)] *= a.coef[i];
		}
		else {
			*this = a;
			for (int i = 0; i <= b.degree; i++)
				coef[i + (degree - b.degree)] *= b.coef[i];
		}
	}

	// (3)번 문제
	// 최고차항의 계수가 0이 아닌 값이 나오도록 하는 함수
	void trim() {
		for (int i = degree; i >= 0; i--) {
			if (coef[i] == 0)
				degree--;
			else
				break;
		}
	}

	bool isZero() { return degree == 0; }

	void negate() {
		for (int i = 0; i <= degree; i++)
			coef[i] = -coef[i];
	}

};

int main()
{
	Polynomial a, b, c, d, e;
	a.read();
	b.read();
	c.add(a, b);
	d.sub(a, b);
	e.mult(a, b);

	a.display("A = ");
	b.display("B = ");
	c.display("A+B= ");
	d.display("A-B= ");
	e.display("A*B= ");

	return 0;
}

 

(1)번. 두 다항식의 뺄셈을 구하는 함수.

a와 b중 차수가 큰 다항식을 자신으로 복사한 후 나머지 다항식의 계수를 차례대로 차수를 맞춰 뺄셈을 한다.

*this = b; 는 c = b; 를 의미함. c에 b를 복사한 후 c -= a; 를 연산한다. 

 

(2)번. 두 다항식의 곱셈을 구하는 함수.

마찬가지로 차수가 큰 다항식을 자신으로 복사한 후 나머지 다항식의 계수를 차래대로 차수를 맞춰 곰셈을 한다.

1번과 동일한 방식.

 

(3)번. 최고차항의 계수가 0이 아닌 값이 나오도록 하는 함수.

for문을 통해 i=최고차 부터 i=0까지 반복한다. coef[i]==0이라면 최고차항을 1씩 줄인다. (degree--;)

coef != 0 이라면 for문을 탈출한다.

 

(4)번. 계수가 0인 항은 출력되지 않도록, 계수가 1인 경우는 계수 1을 출력하지 않도록 다항식을 출력하는 함수.

for문을 통해 i=0부터 i<degree까지 반복하며 coef[i] != 0 이고 coef[i] != 1 이라면 그대로 출력하고, 중첩 if 문에서 coef[i] ==1 이 된다면 계수를 출력하지 않고 x와 차수만 출력한다. (printf("x^%d + ", degree-1);

차수가 0인 항에 도달하면 계수만 출력한다.

 


 

실행 결과 예시는 아래와 같다.

 

다항식의 최고 차수를 입력하시오: 5
각 항의 계수를 입력하시오 (총 6개): 3 6 0 0 0 10
다항식의 최고 차수를 입력하시오: 4
각 항의 계수를 입력하시오 (총 5개): 7 0 5 0 1
        A =   3.0 x^5 +   6.0 x^4 + 10.0
        B =   7.0 x^4 +   5.0 x^2 +  1.0
        A+B=   3.0 x^5 +  13.0 x^4 +   5.0 x^2 + 11.0
        A-B=   3.0 x^5 +  -1.0 x^4 +  -5.0 x^2 +  9.0
        A*B=   3.0 x^5 +  42.0 x^4 + 10.0
다항식의 최고 차수를 입력하시오: 2
각 항의 계수를 입력하시오 (총 3개): 2 1 0
다항식의 최고 차수를 입력하시오: 3
각 항의 계수를 입력하시오 (총 4개): 5 3 1 2
        A =   2.0 x^2 + x^1 +
        B =   5.0 x^3 +   3.0 x^2 + x^1 +  2.0
        A+B=   5.0 x^3 +   5.0 x^2 +   2.0 x^1 +  2.0
        A-B=   5.0 x^3 + x^2 +  2.0
        A*B=   5.0 x^3 +   6.0 x^2 + x^1 +  2.0