다항식 링크리스트써서 덧셈곱셈나눗셈하는건데
type of operation // +:add *:multiply -:subtract !:stop execution
# of terms in the 1st polynomial // 4 for four term polynomial
coef - exponent pair for each term // 4 6 for 4x6
# of terms in the 2nd polynomial
coef - exponent pair for each term
<Example>
+
4
2 0 // poly #1: 2 + 3x2 + x5 + 4x6
4 6
3 2
1 5
5
3 0
2 2 // poly #2: 3 + 4x + 2x2 + 3x5 + x6
3 5
4 1
1 6
!
<output>
input polynomial 1 : 2 + 3x[2] + x[5] + 4x[6]
input polynomial 2 : 3 + 4x + 2x[2] + 3x[5] + x[6] : 3 + 4x + 2x2 + 3x5 + x6
Result of polynomial addition: 5 + 4x + 5x[2] + ...
요건데 뺄셈 곱셈하다가 포기네요 좀 도와주세요.ㅜㅜ
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define COMPARE(x,y) (((x)<(y)) ? -1 :(((x)==(y)) ? 0 : 1))
#define IS_FULL(ptr) (!(ptr))
#define IS_EMPTY(ptr) (!(ptr))
typedef struct polynode *poly_ptr;
struct polynode
{
int coef;
int expon;
poly_ptr link;
};
poly_ptr create_node(void);
poly_ptr padd(poly_ptr , poly_ptr );
poly_ptr psub(poly_ptr, poly_ptr);
poly_ptr pmul(poly_ptr, poly_ptr);
poly_ptr attach(int coef, int expon, poly_ptr ptr);
void print_poly(poly_ptr front);
void erase_poly(poly_ptr front);
void main(void)
{
poly_ptr poly1, poly2, poly;
char oper;
scanf("%c",&oper);
//printf(" polynomial 1's ");
poly1 = create_node();
//printf("\n polynomial 2's ");
poly2 = create_node();
if(oper=='+')
{
printf("STRAT POLYNOMIAL ADDITION!!\n");
poly = padd(poly1, poly2);
}
else if(oper=='-')
{
printf("STRAT POLYNOMIAL SUBTRACTING!!\n");
poly=psub(poly1,poly2);
}
else if(oper=='*')
{
printf("STRAT POLYNOMIAL MULTIPLYING!!\n");
poly=pmul(poly1,poly2);
}
printf("\n\n input polynomial1 = ");
print_poly(poly1);
printf("\n input polynomial2 = ");
print_poly(poly2);
printf("\n result of polynomial = ");
print_poly(poly);
getch();
}
poly_ptr create_node()
{
poly_ptr front,cur_poly,prev_poly,temp_poly;
int i, n, key;
int coef, expon;
front = NULL;
//printf(" term number? ");
scanf("%d", &n);
if(n<=0) exit(0);
for(i=1; i<=n; i++) {
do {
scanf("%d %d", &coef, &expon);
}while (!coef);
prev_poly = NULL;
cur_poly = front;
key = 1;
while(cur_poly && key) {
switch(COMPARE(expon, cur_poly->expon))
{
case -1 :
temp_poly = attach(coef, expon, prev_poly);
if(prev_poly==NULL)
{
temp_poly->link = front;
front = temp_poly;
}
temp_poly->link = cur_poly;
key = 0;
break;
case 0:
cur_poly->coef += coef;
if(0==cur_poly->coef){
if(cur_poly==front){
front = cur_poly->link;
}else{
prev_poly->link = cur_poly->link;
}
free(cur_poly);
}
key = 0;
break;
case 1:
prev_poly = cur_poly;
cur_poly = cur_poly->link;
}
}
if(key){
if(front==NULL) front = attach(coef, expon, NULL);
else attach(coef, expon, prev_poly);
}
}
return front;
}
poly_ptr padd(poly_ptr a, poly_ptr b)
{
poly_ptr front, rear, temp;
int sum;
front=NULL;
rear=NULL;
temp=NULL;
while(a && b){
switch (COMPARE(b -> expon, a -> expon)) {
case -1:
temp = attach(b -> coef, b ->expon, rear);
if(front==NULL) front=temp;
rear = temp;
b = b -> link;
break;
case 0:
sum = a -> coef + b -> coef;
if(sum){
temp = attach(sum, a -> expon, rear);
if(front==NULL) front=temp;
rear = temp;
}
a = a -> link;
b = b -> link;
break;
case 1:
temp=attach(a -> coef, a -> expon, rear);
if(front==NULL) front=temp;
rear = temp;
a = a -> link;
}
}
for(;a;a = a -> link){
temp=attach(a -> coef, a -> expon, rear);
if(front==NULL) front = temp;
}
for(;b;b = b -> link){
temp=attach(b -> coef, b -> expon, rear);
if(front==NULL) front = temp;
}
return front;
}
poly_ptr psub(poly_ptr a, poly_ptr b)
{
poly_ptr front, rear, temp;
int sum=0;
front=NULL;
rear=NULL;
temp=NULL;
while(a && b){
switch (COMPARE(b -> expon, a -> expon)) {
case -1:
temp = attach(b -> coef, b ->expon, rear);
if(front==NULL) front=temp;
rear = temp;
b = b -> link;
break;
case 0:
sum =a->coef - b-> coef;
if(sum)
{
temp = attach(sum, a -> expon, rear);
if(front==NULL) front=temp;
rear = temp;
}
a = a -> link;
b = b -> link;
break;
case 1:
temp=attach(a -> coef, a -> expon, rear);
if(front==NULL) front=temp;
rear = temp;
a = a -> link;
}
}
for(;a;a = a -> link){
temp=attach(a -> coef, a -> expon, rear);
if(front==NULL) front = temp;
}
for(;b;b = b -> link){
temp=attach(b -> coef, b -> expon, rear);
if(front==NULL) front = temp;
}
return front;
}
poly_ptr pmul(poly_ptr a, poly_ptr b)
{
poly_ptr front, rear, temp;
int sum;
front=NULL;
rear=NULL;
temp=NULL;
while(a && b){
switch (COMPARE(a -> expon, b -> expon)) {
case -1:
temp = attach(b -> coef, b ->expon, rear);
if(front==NULL) front=temp;
rear = temp;
b = b -> link;
break;
case 0:
sum = a -> coef * b -> coef;
if(sum){
temp = attach(sum, a -> expon, rear);
if(front==NULL) front=temp;
rear = temp;
}
a = a -> link;
b = b -> link;
break;
case 1:
temp=attach(a -> coef, a -> expon, rear);
if(front==NULL) front=temp;
rear = temp;
a = a -> link;
}
}
for(;a;a = a -> link){
temp=attach(a -> coef, a -> expon, rear);
if(front==NULL) front = temp;
}
for(;b;b = b -> link){
temp=attach(b -> coef, b -> expon, rear);
if(front==NULL) front = temp;
}
return front;
}
poly_ptr attach(int coef, int expon, poly_ptr ptr)
{
poly_ptr new_node;
new_node = (poly_ptr)malloc(sizeof(polynode));
if (IS_FULL(new_node)) {
fprintf(stderr, "The memory is full\n");
exit(1);
}
new_node -> coef = coef;
new_node -> expon = expon;
new_node -> link = NULL;
if(ptr!=NULL) ptr -> link = new_node;
return new_node;
}
void print_poly(poly_ptr front)
{
poly_ptr cur_poly = front;
while (cur_poly) {
switch (cur_poly -> coef) {
case 1 :
if(cur_poly != front) printf(" +");
break;
case -1 :
printf(" -");
break;
default :
if (cur_poly != front && cur_poly -> coef > 0) printf(" +");
printf(" %d", cur_poly -> coef);
}
if(cur_poly -> expon) {
printf("x");
if (cur_poly -> expon != 1) printf("^%d", cur_poly -> expon);
}
if(abs(cur_poly -> coef) == 1 && cur_poly -> expon == 0) printf(" %d", 1);
cur_poly = cur_poly -> link;
}
//printf("\n");
}
|