0x00数据结构——C语言实现(多项式)
#ifndef POLYNOMIAL
#define POLYNOMIAL
struct term;
typedef struct term term;
typedef term *polyn;
typedef enum {
false = 0,
true
} BOOL;
polyn init_polynomial(void);
polyn insert_term(int c, int e, polyn p);
BOOL destroy_polyn(polyn p);
void print_polyn(polyn p);
int polyn_length(polyn p);
polyn add_polyn(polyn p1, polyn p2);
polyn sub_polyn(polyn p1, polyn p2);
polyn mutiply_polyn(polyn p1, polyn p2);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "polynomial.h"
struct term {
float coef;
int expn;
struct term *next;
};
polyn init_polynomial(void)
{
term *head = NULL;
head = (term*)malloc(sizeof(term));
if(head!=NULL){
head->coef = 0;
head->expn = -1;
head->next = NULL;
}
return head;
}
polyn insert_term(int c, int e, polyn p)
{
if(c==0) {
return p;
} else {
term *tmp = p->next, *pre = p;
while(tmp != NULL && tmp->expn != e && tmp->expn < e) {
pre = tmp;
tmp = tmp->next;
}
if(tmp == NULL) {
pre->next = (term*)malloc(sizeof(term));
if(pre->next != NULL) {
pre->next->coef = c;
pre->next->expn = e;
pre->next->next = NULL;
}
} else if(tmp->expn == e) {
tmp->coef += c;
if(tmp->coef == 0) {
pre->next = tmp->next;
free(tmp);
}
} else if(tmp->expn > e) {
pre->next = (term*)malloc(sizeof(term));
if(pre->next != NULL) {
pre->next->coef = c;
pre->next->expn = e;
pre->next->next = tmp;
}
}
}
return p;
}
BOOL destroy_polyn(polyn p)
{
term *tmp;
p = p->next;
while(p != NULL){
tmp = p;
p = p->next;
free(tmp);
tmp = NULL;
}
return true;
}
void print_polyn(polyn p)
{
p = p->next;
if(p!=NULL) {
if(p->expn == 0){
printf("%g", p->coef);
p = p->next;
if(p!=NULL){
if(p->expn == 1){
p->coef>0? printf("+%gX", p->coef):printf("%gX", p->coef);
} else {
p->coef>0?printf("+%gX^%d", p->coef, p->expn):printf("%gX^%d", p->coef, p->expn);
}
p = p->next;
}
} else if(p->expn == 1){
if(p->coef != 1) {
printf("%gX", p->coef);
} else {
printf("X");
}
p = p->next;
} else if(p->expn > 1){
if(p->coef != 1) {
printf("%gX^%d", p->coef, p->expn);
} else {
printf("X^%d", p->expn);
}
p = p->next;
}
while(p!=NULL){
p->coef>0?printf("+%gX^%d", p->coef, p->expn):printf("%gX^%d", p->coef, p->expn);
p = p->next;
}
}
printf("
");
}
int polyn_length(polyn p)
{
int n = 0;
while(p->next != NULL){
n++;
p = p->next;
}
return n;
}
polyn add_polyn(polyn p1, polyn p2)
{
term *tmp = p1, *tmp1 = p1->next, *tmp2 = p2->next;
while(tmp1!=NULL && tmp2!=NULL){
if(tmp1->expn == tmp2->expn){
tmp1->coef += tmp2->coef;
if(tmp1->coef == 0){
p1->next = tmp1->next;
p2->next = tmp2->next;
free(tmp1);
free(tmp2);
} else {
p1 = p1->next;
p2 = p2->next;
}
tmp1 = p1->next;
tmp2 = p2->next;
} else if(tmp1->expn < tmp2->expn){
p1 = p1->next;
tmp1 = p1->next;
} else if(tmp2->expn < tmp1->expn){
p2->next = tmp2->next;
tmp2->next = tmp1;
p1->next = tmp2;
p1 = p1->next;
tmp2 = p2->next;
}
}
if(tmp1 == NULL){
p1->next = tmp2;
}
free(p2);
p2 = NULL;
return tmp;
}
polyn sub_polyn(polyn p1, polyn p2)
{
term *tmp = p1, *tmp1 = p1->next, *tmp2 = p2->next;
while(tmp1!=NULL && tmp2!=NULL){
if(tmp1->expn == tmp2->expn){
tmp1->coef -= tmp2->coef;
if(tmp1->coef == 0){
p1->next = tmp1->next;
p2->next = tmp2->next;
free(tmp1);
free(tmp2);
} else {
p1 = p1->next;
p2 = p2->next;
}
tmp1 = p1->next;
tmp2 = p2->next;
} else if(tmp1->expn < tmp2->expn){
p1 = p1->next;
tmp1 = p1->next;
} else if(tmp2->expn < tmp1->expn){
tmp2->coef *= -1;
p2->next = tmp2->next;
tmp2->next = tmp1;
p1->next = tmp2;
p1 = p1->next;
tmp2 = p2->next;
}
}
if(tmp1 == NULL){
p1->next = tmp2;
}
free(p2);
p2 = NULL;
return tmp;
}
polyn mutiply_polyn(polyn p1, polyn p2)
{
polyn tmp_res = (polyn)malloc(sizeof(term));
tmp_res->next = NULL;
term *cur1 = p1->next, *cur2 = p2->next;
while(cur1!=NULL){
polyn tmp_p2 = (polyn)malloc(sizeof(term));
term *cur_p2 = tmp_p2;
cur2 = p2->next;
while(cur2!=NULL){
cur_p2->next = (term *)malloc(sizeof(term));
cur_p2 = cur_p2->next;
cur_p2->coef = cur1->coef * cur2->coef;
cur_p2->expn = cur1->expn + cur2->expn;
cur2 = cur2->next;
}
cur_p2->next = NULL;
if(tmp_res->next == NULL){
free(tmp_res);
tmp_res = tmp_p2;
} else{
add_polyn(tmp_res, tmp_p2);
}
cur1 = cur1->next;
}
destroy_polyn(p1);
destroy_polyn(p2);
p1->next = tmp_res->next;
return tmp_res;
}