#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct poly_node * polynomial;
struct poly_node{
int coef;
int expon;
polynomial link;
};
polynomial read_poly();
void attack(int c, int e, polynomial * rear);
void print_poly(polynomial p);
polynomial add_poly(polynomial p1, polynomial p2);
polynomial mult_poly(polynomial p1, polynomial p2);
int main(int argc, char *argv[]) {
polynomial p1, p2, pp, ps;
p1 = read_poly();
p2 = read_poly();
ps = add_poly(p1, p2);
pp = mult_poly(p1, p2);
print_poly(p1);
print_poly(p2);
print_poly(ps);
print_poly(pp);
return 0;
}
polynomial read_poly()
{
int n, c, e;
scanf("%d", &n);
polynomial p, temp, rear;
p = (polynomial)malloc(sizeof(struct poly_node));
rear = p;
p->link = NULL;
while(n--)
{
scanf("%d%d", &c, &e);
attack(c, e, &rear);
}
temp = p;
p = p->link;
free(temp);
return p;
}
void attack(int c, int e, polynomial * rear)
{
polynomial p;
p = (polynomial)malloc(sizeof(struct poly_node));
p->coef = c;
p->expon = e;
p->link = NULL; ///!!!忘记
(*rear)->link = p;精髓
(*rear) = p;精髓
}
void print_poly(polynomial p)
{
int flag = 0;
if(!p)
{
printf("0 0\n");
return;
}
while(p)
{
if(!flag)
flag = 1;
else
printf(" ");
printf("%d %d", p->coef, p->expon);
p = p->link;
}
printf("\n");
}
polynomial add_poly(polynomial p1, polynomial p2)
{
polynomial t1, t2, ps, rear, temp;
t1 = p1;
t2 = p2;
int sum;
ps = (polynomial)malloc(sizeof(struct poly_node));
ps->link = NULL;
rear = ps;
while(t1 && t2)
{
if(t1->expon == t2->expon)
{
sum = t1->coef + t2->coef;
if(sum)
attack(sum, t1->expon, &rear);
t1 = t1->link;
t2 = t2->link;
}
else if(t1->expon > t2->expon)
{
attack(t1->coef, t1->expon, &rear);
t1 = t1->link;
}
else if(t1->expon < t2->expon)
{
attack(t2->coef, t2->expon, &rear);
t2 = t2->link;
}
}
while(t1)
{
attack(t1->coef, t2->expon, &rear);
t1 = t1->link;
}
while(t2)
{
attack(t2->coef, t2->expon, &rear);
t2 = t2->link;
}
temp = ps;
ps = ps->link;
free(temp);
return ps;
}
polynomial mult_poly(polynomial p1, polynomial p2)
{
if(!p1 || !p2)
return NULL;
polynomial t1, t2, pp, rear, temp;
int c, e;
t1= p1;
t2 = p2;
pp = (polynomial)malloc(sizeof(struct poly_node));
pp ->link =NULL;
rear = pp;
while(t2)
{ ///首先用t1 的第一项 乘以 t2的所有项,得到一个表达式
attack(t1->coef*t2->coef, t1->expon+t2->expon, &rear);
t2 = t2->link;
}
t1 = t1->link; ///t1 指向第二项
while(t1)
{
t2 = p2; ///每当t1 移向下一个, t2重头开始循环
rear = pp; ///rear 指向表达式的前一个空位置
while(t2)
{
c = t1->coef * t2->coef;
e = t1->expon + t2->expon;
while(rear->link && rear->link->expon > e) ///当rear 后还有项时,且指数大于e
{
rear = rear->link;
}
if(rear->link && rear->link->expon == e) ///指数相等时
{
if(rear->link->coef + c) ///检查系数是否为 0
rear->link->coef += c;
else
{
temp = rear->link;
rear->link = temp->link;
free(temp); ///系数为0,释放
}
}
else
{ ///rear下一项 的指数小于e, 开辟空间添加进表达式中
temp = (polynomial)malloc(sizeof(struct poly_node));
temp->coef = c;
temp->expon = e;
temp->link = rear->link;
rear->link = temp;
}
t2 = t2->link;
}
t1 = t1->link;
}
temp = pp;
pp = pp->link;
free(temp);
return pp;
}
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct poly_node * polynomial;
struct poly_node{
int coef;
int expon;
polynomial link;
};
polynomial read_poly();
void attack(int c, int e, polynomial * rear);
void print_poly(polynomial p);
polynomial add_poly(polynomial p1, polynomial p2);
polynomial mult_poly(polynomial p1, polynomial p2);
int main(int argc, char *argv[]) {
polynomial p1, p2, pp, ps;
p1 = read_poly();
p2 = read_poly();
ps = add_poly(p1, p2);
pp = mult_poly(p1, p2);
print_poly(p1);
print_poly(p2);
print_poly(ps);
print_poly(pp);
return 0;
}
polynomial read_poly()
{
int n, c, e;
scanf("%d", &n);
polynomial p, temp, rear;
p = (polynomial)malloc(sizeof(struct poly_node));
rear = p;
p->link = NULL;
while(n--)
{
scanf("%d%d", &c, &e);
attack(c, e, &rear);
}
temp = p;
p = p->link;
free(temp);
return p;
}
void attack(int c, int e, polynomial * rear)
{
polynomial p;
p = (polynomial)malloc(sizeof(struct poly_node));
p->coef = c;
p->expon = e;
p->link = NULL; ///!!!忘记
(*rear)->link = p;精髓
(*rear) = p;精髓
}
void print_poly(polynomial p)
{
int flag = 0;
if(!p)
{
printf("0 0\n");
return;
}
while(p)
{
if(!flag)
flag = 1;
else
printf(" ");
printf("%d %d", p->coef, p->expon);
p = p->link;
}
printf("\n");
}
polynomial add_poly(polynomial p1, polynomial p2)
{
polynomial t1, t2, ps, rear, temp;
t1 = p1;
t2 = p2;
int sum;
ps = (polynomial)malloc(sizeof(struct poly_node));
ps->link = NULL;
rear = ps;
while(t1 && t2)
{
if(t1->expon == t2->expon)
{
sum = t1->coef + t2->coef;
if(sum)
attack(sum, t1->expon, &rear);
t1 = t1->link;
t2 = t2->link;
}
else if(t1->expon > t2->expon)
{
attack(t1->coef, t1->expon, &rear);
t1 = t1->link;
}
else if(t1->expon < t2->expon)
{
attack(t2->coef, t2->expon, &rear);
t2 = t2->link;
}
}
while(t1)
{
attack(t1->coef, t2->expon, &rear);
t1 = t1->link;
}
while(t2)
{
attack(t2->coef, t2->expon, &rear);
t2 = t2->link;
}
temp = ps;
ps = ps->link;
free(temp);
return ps;
}
polynomial mult_poly(polynomial p1, polynomial p2)
{
if(!p1 || !p2)
return NULL;
polynomial t1, t2, pp, rear, temp;
int c, e;
t1= p1;
t2 = p2;
pp = (polynomial)malloc(sizeof(struct poly_node));
pp ->link =NULL;
rear = pp;
while(t2)
{ ///首先用t1 的第一项 乘以 t2的所有项,得到一个表达式
attack(t1->coef*t2->coef, t1->expon+t2->expon, &rear);
t2 = t2->link;
}
t1 = t1->link; ///t1 指向第二项
while(t1)
{
t2 = p2; ///每当t1 移向下一个, t2重头开始循环
rear = pp; ///rear 指向表达式的前一个空位置
while(t2)
{
c = t1->coef * t2->coef;
e = t1->expon + t2->expon;
while(rear->link && rear->link->expon > e) ///当rear 后还有项时,且指数大于e
{
rear = rear->link;
}
if(rear->link && rear->link->expon == e) ///指数相等时
{
if(rear->link->coef + c) ///检查系数是否为 0
rear->link->coef += c;
else
{
temp = rear->link;
rear->link = temp->link;
free(temp); ///系数为0,释放
}
}
else
{ ///rear下一项 的指数小于e, 开辟空间添加进表达式中
temp = (polynomial)malloc(sizeof(struct poly_node));
temp->coef = c;
temp->expon = e;
temp->link = rear->link;
rear->link = temp;
}
t2 = t2->link;
}
t1 = t1->link;
}
temp = pp;
pp = pp->link;
free(temp);
return pp;
}