157 lines
4.2 KiB
Plaintext
157 lines
4.2 KiB
Plaintext
|
/* polyadd.c */
|
|||
|
/* <20>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD>ۥ[--<2D>ϥέ<CFA5><CEAD><EFBFBD><EFBFBD>ƦC<C6A6><43><EFBFBD>J<EFBFBD><4A><EFBFBD>Ӯ榡<D3AE><E6A6A1> ax^b <20><><EFBFBD>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD>ۥ[ */
|
|||
|
|
|||
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
|
|||
|
struct poly {
|
|||
|
int coef; /* <20>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD>Y<EFBFBD><59> */
|
|||
|
int exp; /* <20>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
struct poly *next;
|
|||
|
};
|
|||
|
|
|||
|
void input(struct poly *, struct poly *, struct poly *); /* <20><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD> */
|
|||
|
void poly_add(void); /* <20>h<EFBFBD><68><EFBFBD><EFBFBD><EFBFBD>ۥ[<5B><><EFBFBD><EFBFBD> */
|
|||
|
void show_ans(void); /* <20><><EFBFBD>ܦh<DCA6><68><EFBFBD><EFBFBD><EFBFBD>ۥ[<5B><><EFBFBD>G<EFBFBD><47><EFBFBD><EFBFBD> */
|
|||
|
void display_func(struct poly *, struct poly *);
|
|||
|
void flushBuffer(void);
|
|||
|
|
|||
|
struct poly *ptr, *ans_h;
|
|||
|
struct poly *head1, *this_n1, *prev1, *eq_h1;
|
|||
|
struct poly *head2, *this_n2, *prev2, *eq_h2;
|
|||
|
|
|||
|
int main()
|
|||
|
{
|
|||
|
head1=(struct poly *) malloc(sizeof(struct poly));
|
|||
|
head1->next = NULL;
|
|||
|
head2=(struct poly *) malloc(sizeof(struct poly));
|
|||
|
head2->next = NULL;
|
|||
|
|
|||
|
printf("****************************************\n");
|
|||
|
printf(" -- Polynomial format is ax^b --\n");
|
|||
|
printf("****************************************\n");
|
|||
|
printf("Please enter the first equation, terminate at -1\n");
|
|||
|
input(prev1, head1, this_n1);
|
|||
|
printf("Please enter the second equation, terminate at -1\n");
|
|||
|
input(prev2, head2, this_n2);
|
|||
|
poly_add();
|
|||
|
show_ans();
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
void input(struct poly *prev, struct poly *head, struct poly *this_n)
|
|||
|
{
|
|||
|
do {
|
|||
|
ptr = (struct poly *) malloc(sizeof(struct poly));
|
|||
|
ptr->next = NULL;
|
|||
|
/* <20><><EFBFBD>o<EFBFBD><6F><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD> */
|
|||
|
printf("Coefficient: ");
|
|||
|
scanf("%d", &ptr->coef);
|
|||
|
flushBuffer();
|
|||
|
printf("Exponential: ");
|
|||
|
scanf("%d", &ptr->exp);
|
|||
|
flushBuffer();
|
|||
|
|
|||
|
if (ptr->coef == -1 && ptr->exp == -1) {
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD>J<EFBFBD><4A><EFBFBD><EFBFBD>
|
|||
|
prev = head;
|
|||
|
this_n = head->next;
|
|||
|
while ((this_n != NULL) && (this_n->exp > ptr->exp)) {
|
|||
|
prev = this_n;
|
|||
|
this_n = this_n->next;
|
|||
|
}
|
|||
|
ptr->next = this_n;
|
|||
|
prev->next = ptr;
|
|||
|
} while(ptr->exp != -1);
|
|||
|
display_func(this_n, head);
|
|||
|
}
|
|||
|
|
|||
|
void poly_add(void)
|
|||
|
{
|
|||
|
struct poly *prev;
|
|||
|
prev = NULL;
|
|||
|
this_n1 = head1->next;
|
|||
|
this_n2 = head2->next;
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD>Ӧh<D3A6><68><EFBFBD><EFBFBD><EFBFBD>Ҭۥ[<5B><><EFBFBD><EFBFBD><EFBFBD>h<EFBFBD><68><EFBFBD><EFBFBD> */
|
|||
|
while (this_n1 != NULL || this_n2 != NULL) {
|
|||
|
ptr = (struct poly *) malloc(sizeof(struct poly));
|
|||
|
ptr->next = NULL;
|
|||
|
/* <20>Ĥ@<40>Ӧh<D3A6><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƥj<C6A4><6A><EFBFBD>ĤG<C4A4>Ӧh<D3A6><68><EFBFBD><EFBFBD> */
|
|||
|
if (this_n1 != NULL && (this_n2 == NULL
|
|||
|
|| this_n1->exp > this_n2->exp)) {
|
|||
|
ptr->coef = this_n1->coef;
|
|||
|
ptr->exp = this_n1->exp;
|
|||
|
this_n1 = this_n1->next;
|
|||
|
}
|
|||
|
else {
|
|||
|
/* <20>Ĥ@<40>Ӧh<D3A6><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƥp<C6A4><70><EFBFBD>ĤG<C4A4>Ӧh<D3A6><68><EFBFBD><EFBFBD> */
|
|||
|
if (this_n1 == NULL || this_n1->exp < this_n2->exp) {
|
|||
|
ptr->coef = this_n2->coef;
|
|||
|
ptr->exp = this_n2->exp;
|
|||
|
this_n2 = this_n2->next;
|
|||
|
}
|
|||
|
else { /* <20><><EFBFBD>Ӧh<D3A6><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƭ۵<C6AC><DBB5>A<EFBFBD>i<EFBFBD><69><EFBFBD>ۥ[ */
|
|||
|
ptr->coef = this_n1->coef + this_n2->coef;
|
|||
|
ptr->exp = this_n1->exp;
|
|||
|
if(this_n1 != NULL)
|
|||
|
this_n1 = this_n1->next;
|
|||
|
if(this_n2 != NULL)
|
|||
|
this_n2 = this_n2->next;
|
|||
|
}
|
|||
|
}
|
|||
|
if(ptr->coef != 0) { /* <20><><EFBFBD>ۥ[<5B><><EFBFBD>G<EFBFBD><47><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD>A<EFBFBD>h<EFBFBD><68><EFBFBD>J<EFBFBD><4A><EFBFBD>צh<D7A6><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|||
|
if(ans_h == NULL)
|
|||
|
ans_h = ptr;
|
|||
|
else
|
|||
|
prev->next = ptr;
|
|||
|
prev = ptr;
|
|||
|
}
|
|||
|
else free(ptr);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void show_ans(void)
|
|||
|
{
|
|||
|
struct poly *this_n;
|
|||
|
this_n = ans_h;
|
|||
|
printf("The equation: ");
|
|||
|
while(this_n != NULL) {
|
|||
|
printf("%dx^%d", this_n->coef, this_n->exp);
|
|||
|
if(this_n->next != NULL && this_n->next->coef >= 0)
|
|||
|
printf("+");
|
|||
|
this_n = this_n->next;
|
|||
|
}
|
|||
|
printf("\n");
|
|||
|
}
|
|||
|
|
|||
|
void display_func(struct poly *this_n, struct poly *head)
|
|||
|
{
|
|||
|
int count=0;
|
|||
|
if (head->next == NULL) {
|
|||
|
printf(" No item in polynominal\n");
|
|||
|
}
|
|||
|
else {
|
|||
|
printf("\n coef exp\n");
|
|||
|
printf(" -----------------\n");
|
|||
|
this_n=head->next;
|
|||
|
while (this_n != NULL) {
|
|||
|
printf(" %-7d %3d\n", this_n->coef, this_n->exp);
|
|||
|
count++;
|
|||
|
this_n=this_n->next;
|
|||
|
}
|
|||
|
printf(" -----------------\n");
|
|||
|
printf(" Total %d item(s) found\n\n", count);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void flushBuffer()
|
|||
|
{
|
|||
|
while(getchar() != '\n') {
|
|||
|
continue;
|
|||
|
}
|
|||
|
}
|