티스토리 뷰

사용언어 : C++

 

옛날에 만들었던 내용인데, 두개의 다항식을 입력하면 두 다항식의 합, 차, eval 등을 해주는 프로그램입니다. 다항식의 끝은 마지막 계수 입력이 0이면 하나의 다항식이 입력되는 걸로 취급합니다.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct node {
	int data; // 계수
	int exp; // 지수
	struct node *next; // 링크 연결 부분.
}node;
typedef node* point;

point avail;

void retNode(point node) {
	node->next = avail;
	avail = node;
}

void cerase(point ptr) {
	avail = NULL;
	point temp;
	if (ptr) {
		temp = ptr->next;
		ptr->next = avail;
		avail = temp;
		ptr = NULL;
	}
}

void setting(point head) {
	head->data = 0;
	head->exp = -1;
	head->next = NULL;
}
void pread(point head,int b, int c) {
	point n = malloc(sizeof(node));
	n->data = b;
	n->exp = c;
	if (head->next == NULL) {
		n->next = head;
		head->next = n;
	}
	else {
		point tmp = head;
		while (tmp->next != head) {	
			if (tmp->next->exp == n->exp) {
				tmp->next->data = tmp->next->data + n->data;
				return;
			}
			if (tmp->next->exp < c)
				break;
			tmp = tmp->next;
		}
		n->next = tmp->next;
		tmp->next = n;
		
	}
}
void Pwrite(point head) {
	point tmp = head;
	if (tmp->next == head)
		printf("텅 비어있습니다.\n");
	else {
		while (tmp->next != head) {
			tmp = tmp->next;
			if (head->next == tmp && tmp->data != 0)
				printf("%dx^%d ", tmp->data, tmp->exp);
			else if (tmp->data > 0)
				printf("+ %dx^%d ", tmp->data, tmp->exp);
			else if (tmp->data < 0)
				printf("- %dx^%d ", -tmp->data, tmp->exp);
		}
		printf("\n");
	}
}

void Padd(point x, point y, point z) {
	point a = x->next;
	point b = y->next;
	while ((a->exp != -1) || (b->exp != -1)) {
		if (a->exp == b->exp && a->exp != -1) {
			point c = z;
			int xy = a->data + b->data;
			int ex = a->exp;
			pread(c, xy, ex);
			a = a->next;
			b = b->next;
			
		}
		
		else if (a->exp > b->exp) {
			point c = z;
			int xy = a->data;
			int ex = a->exp;
			pread(c, xy, ex);
			a = a->next;
		}

		else if (a->exp < b->exp) {
			point c = z;
			int xy = b->data;
			int ex = b->exp;
			pread(c, xy, ex);
			b = b->next;
		}
	}
}

void Psub(point x, point y, point z) {
	point a = x->next;
	point b = y->next;
	while ((a->exp != -1) || (b->exp != -1)) {
		if (a->exp == b->exp && a->exp != -1) {
			point c = z;
			int xy = a->data - b->data;
			int ex = a->exp;
			pread(c, xy, ex);

			a = a->next;
			b = b->next;

		}

		else if (a->exp > b->exp) {
			point c = z;
			int xy = a->data;
			int ex = a->exp;
			pread(c, xy, ex);
			a = a->next;
		}

		else if (a->exp < b->exp) {
			point c = z;
			int xy = -b->data;
			int ex = b->exp;
			pread(c, xy, ex);
			b = b->next;
		}
	}
}
double eval(double x, point a) {
	point b = a->next;
	double total = 0;
	while (b->exp != -1) {
		total += pow(x, b->exp) * b->data;
		b = b->next;
	}
	return total;
}
int main() {
	int d = 1;
	int e = 1;
	double x = 2.5;
	point a = malloc(sizeof(node));
	point b = malloc(sizeof(node));
	point c = malloc(sizeof(node));
	setting(a);
	setting(b);
	setting(c);
	printf("첫번째 다항식  입력 \n");
	while (e != 0) {
		printf("지수 입력 : ");
		scanf_s("%d", &d);
		printf("계수 입력 : ");
		scanf_s("%d", &e);
		pread(a, d, e);
		
	}
	e = 1;
	printf("두번째 다항식  입력 \n");
	while (e != 0) {
		printf("지수 입력 : ");
		scanf_s("%d", &d);
		printf("계수 입력 : ");
		scanf_s("%d", &e);
		pread(b, d, e);
	}
	printf("a : ");
	Pwrite(a);
	printf("b : ");
	Pwrite(b);
	printf("플러스 실행\n");
	Padd(a, b, c);
	Pwrite(c);
	cerase(c);
	printf("마이너스 실행\n");
	Psub(a, b, c);
	Pwrite(c);
	cerase(c);
	printf("a에 대한 Eval 실행\n");
	printf("answer : %lf\n",eval(x, a));
	printf("b에 대한 Eval 실행\n");
	printf("answer : %lf\n", eval(x, b));
	printf("끝");
	return 0;
}

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함