Computer Science/Data Structure, Algorithm

두근두근 자료구조 2장 프로그래밍 프로젝트 1번

TwinParadox 2018. 4. 12. 01:07
728x90
어떤 것들은 심심할 때마다 다시 공부하곤 하는데, 자료구조도 그 중 하나다. 개인적으로 공부하면서 정리한 것들이고 답이 틀렸을 수도 있기 때문에 지적은 언제나 환영한다.

 
#include <stdio.h>
#pragma warning(disable:4996)
#define MAX_DEGREE    101
typedef struct
{
	int degree;
	float coef[MAX_DEGREE];
} Polynomial;
Polynomial read_poly()
{
	int i;
	Polynomial p;

	printf("다항식의 최고 차수를 입력하시오: ");
	scanf("%d", &p.degree);
	printf("각 항의 계수를 입력하시오 (총 %d개): ", p.degree + 1);
	for (i = 0; i <= p.degree; i++)
		scanf("%f", p.coef + i);
	return p;
}
void print_poly(Polynomial p, const char str[])
{
	int i;
	printf("\t%s", str);
	for (i = 0; i < p.degree; i++)
	{
		// 계수가 0인 경우,
		if (p.coef[i] == 0)
			continue;
		// 계수가 1인 경우,
		else if (p.coef[i] == 1)
			printf("     x^%d + ", p.degree - i);
		// 나머지 경우
		else
			printf("%5.1f x^%d + ", p.coef[i], p.degree - i);
	}
	// 상수항이 0이 아닌 경우
	if(p.coef[p.degree]!=0)
		printf("%4.1f\n", p.coef[p.degree]);
}
Polynomial add_poly(Polynomial a, Polynomial b)
{
	int i;
	Polynomial p;
	if (a.degree > b.degree)
	{
		p = a;
		for (i = 0; i <= b.degree; i++)
			p.coef[i + (p.degree - b.degree)] += b.coef[i];
	}
	else
	{
		p = b;
		for (i = 0; i <= a.degree; i++)
			p.coef[i + (p.degree - a.degree)] += a.coef[i];
	}
	return p;
}
// 1-(1)
Polynomial sub_poly(Polynomial a, Polynomial b)
{
	int i;
	Polynomial result;
	// a의 최고차항이 b의 최고차항보다 큰 경우,
	if (a.degree > b.degree)
	{
		// 결과는 a의 최고차항으로 결정됨.
		result = a;
		for (i = 0; i <= b.degree; i++)
			// 두 다항식의 차수를 고려하여 계산.
			result.coef[i + (result.degree - b.degree)] -= b.coef[i];
	}
	// 그렇지 않은 경우
	else
	{
		// 결과는 b의 최고차항으로 결정됨.
		// 단, a-b를 구하기 때문에 b의 부호를 바꾸어야 함.
		result = b;
		for (i = 0; i <= result.degree; i++)
			result.coef[i] *= -1;
		// -b+a
		for (i = 0; i <= a.degree; i++)
			// 두 다항식의 차수를 고려하여 계산.
			result.coef[i + (result.degree - a.degree)] += a.coef[i];
	}
	return result;
}
// 1-(2)
Polynomial mult_poly(Polynomial a, Polynomial b)
{
	int i, j;
	Polynomial result;
	// 결과의 최고차항은 두 항의 최고차항의 합.
	result.degree = a.degree + b.degree;
	// 결과를 담기 위해 모든 항을 0으로 초기화.
	for (i = 0; i <= result.degree; i++)
		result.coef[i] = 0;
	for (i = 0; i <= a.degree; i++)
		for (j = 0; j <= b.degree; j++)
			result.coef[i + j] += a.coef[i] * b.coef[j];
	return result;
}
// 1-(3)
void trimPoly(Polynomial* p)
{
	print_poly(*p, "Poly= ");
	int i = 0, zero = 0;

	// 최고차항이 되는 항까지 확인.
	for (i = 0; i <= p->degree; i++)
		if (p->coef[i] != 0)
			break;
	zero = i;
	// 최고차항의 인덱스를 옮기는 작업 실시.
	for (i = zero; i <= p->degree; i++)
		p->coef[i - zero] = p->coef[i];
	// 최고차항 정보 수정.
	p->degree -= zero;

	printf("변경 후-->\n");
	print_poly(*p, "Poly =");
}
int main(void)
{
	Polynomial a, b, c, d, e, trim;
	a = read_poly();
	b = read_poly();
	c = add_poly(a, b);
	d = sub_poly(a, b);
	e = mult_poly(a, b);
	print_poly(a, " A = ");
	print_poly(b, " B = ");
	print_poly(c, "A+B= ");
	print_poly(d, "A-B= ");
	print_poly(e, "A*B= ");
	trim = read_poly();
	trimPoly(&trim);
	return 0;
}


728x90
728x90