157 lines
4.2 KiB
Plaintext
157 lines
4.2 KiB
Plaintext
/* polyadd.c */
|
||
/* 多項式相加--使用降冪排列輸入兩個格式為 ax^b 的多項式相加 */
|
||
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
|
||
struct poly {
|
||
int coef; /* 多項式係數 */
|
||
int exp; /* 多項式指數 */
|
||
struct poly *next;
|
||
};
|
||
|
||
void input(struct poly *, struct poly *, struct poly *); /* 輸入函數 */
|
||
void poly_add(void); /* 多項式相加函數 */
|
||
void show_ans(void); /* 顯示多項式相加結果函數 */
|
||
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;
|
||
/* 取得輸入資料 */
|
||
printf("Coefficient: ");
|
||
scanf("%d", &ptr->coef);
|
||
flushBuffer();
|
||
printf("Exponential: ");
|
||
scanf("%d", &ptr->exp);
|
||
flushBuffer();
|
||
|
||
if (ptr->coef == -1 && ptr->exp == -1) {
|
||
break;
|
||
}
|
||
|
||
//插入資料
|
||
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;
|
||
/* 當兩個多項式皆相加完畢則結束 */
|
||
while (this_n1 != NULL || this_n2 != NULL) {
|
||
ptr = (struct poly *) malloc(sizeof(struct poly));
|
||
ptr->next = NULL;
|
||
/* 第一個多項式指數大於第二個多項式 */
|
||
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 {
|
||
/* 第一個多項式指數小於第二個多項式 */
|
||
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 { /* 兩個多項式指數相等,進行相加 */
|
||
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) { /* 當相加結果不等於0,則放入答案多項式中 */
|
||
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;
|
||
}
|
||
}
|