主题:一道求多项式相加题
1、编程求两多项式的和多项式
A (x) = 1+2x8+3x14
B (x) = 10x6-3x10+8x14
我下了下面的代码``但不知道哪里有问题``弄了半天了```
高手帮我看下
#include<stdio.h>
#include<stdlib.h>
typedef struct Term {
double coef;
int exp;
}ElemType;
typedef struct Node {
ElemType data;
struct Node* next;
}Node;
typedef struct List {
Node* head;
}*List;
void InitList(List L);
void InsertList(List L,ElemType item);
void OutputList(List L);
void ClearList(List L);
List PolyAdd(List p1, List p2);
int main(void)
{
List a,b;
InitList(a),InitList(b);
ElemType ra[3]={{1,0}, {2,8}, {3,14}};
ElemType rb[3]={{10,6}, {-3,10}, {8,14}};
int i;
for(i=2;i>=0;i--){
InsertList(a,ra[i]);
InsertList(b,rb[i]);
}
printf("线性表a:\n");
OutputList(a);
printf("线性表b:\n");
OutputList(b);
List c=PolyAdd(a,b);
printf("线性表c:\n");
OutputList(c);
ClearList(a),ClearList(b),ClearList(c);
getchar();
return (0);
}
void InitList(List L)
{
L->head=NULL;
}
void InsertList(List L,ElemType item)
{
Node* newp;
newp=(Node*)malloc(sizeof(Node));
if(!newp)
{
printf("内存不足!!\n");
exit(1);
}
newp->data=item;
newp->next=L->head;
L->head=newp;
}
void OutputList(List L)
{
Node* p=L->head;
p=p->next;
while(p!=NULL)
{
printf("%f %d ,",p->data.coef,p->data.exp);
}
printf("\n");
}
void ClearList(List L)
{
Node* p=L->head,*q;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
}
List PolyAdd(List p1, List p2)
{
List p3;
p3->head=(Node*)malloc(sizeof(Node));
Node* t1=p1->head,*t2=p2->head,*t3=p3->head;
while(t1&&t2)
{
if(t1->data.exp<t2->data.exp)
{
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=t>data;
t1=t1->next;
}
else if(t1->data.exp>t2->data.exp)
{
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=t2->data;
t2=t2->next;
}
else
{
double a=t1->data.coef+t2->data.coef;
if(a!=0.0)
{
ElemType item={a,t1->data.exp};
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=item;
}
t1=t1->next;
t2=t2->next;
}
}
while(t1)
{
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=t1->data;
t1=t1->next;
}
while(t2)
{
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=t2->data;
t2=t2->next;
}
t3->next=NULL;
t3=p3->head;
p3->head=t3->next;
free(t3);
return p3;
}
A (x) = 1+2x8+3x14
B (x) = 10x6-3x10+8x14
我下了下面的代码``但不知道哪里有问题``弄了半天了```
高手帮我看下
#include<stdio.h>
#include<stdlib.h>
typedef struct Term {
double coef;
int exp;
}ElemType;
typedef struct Node {
ElemType data;
struct Node* next;
}Node;
typedef struct List {
Node* head;
}*List;
void InitList(List L);
void InsertList(List L,ElemType item);
void OutputList(List L);
void ClearList(List L);
List PolyAdd(List p1, List p2);
int main(void)
{
List a,b;
InitList(a),InitList(b);
ElemType ra[3]={{1,0}, {2,8}, {3,14}};
ElemType rb[3]={{10,6}, {-3,10}, {8,14}};
int i;
for(i=2;i>=0;i--){
InsertList(a,ra[i]);
InsertList(b,rb[i]);
}
printf("线性表a:\n");
OutputList(a);
printf("线性表b:\n");
OutputList(b);
List c=PolyAdd(a,b);
printf("线性表c:\n");
OutputList(c);
ClearList(a),ClearList(b),ClearList(c);
getchar();
return (0);
}
void InitList(List L)
{
L->head=NULL;
}
void InsertList(List L,ElemType item)
{
Node* newp;
newp=(Node*)malloc(sizeof(Node));
if(!newp)
{
printf("内存不足!!\n");
exit(1);
}
newp->data=item;
newp->next=L->head;
L->head=newp;
}
void OutputList(List L)
{
Node* p=L->head;
p=p->next;
while(p!=NULL)
{
printf("%f %d ,",p->data.coef,p->data.exp);
}
printf("\n");
}
void ClearList(List L)
{
Node* p=L->head,*q;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
}
List PolyAdd(List p1, List p2)
{
List p3;
p3->head=(Node*)malloc(sizeof(Node));
Node* t1=p1->head,*t2=p2->head,*t3=p3->head;
while(t1&&t2)
{
if(t1->data.exp<t2->data.exp)
{
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=t>data;
t1=t1->next;
}
else if(t1->data.exp>t2->data.exp)
{
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=t2->data;
t2=t2->next;
}
else
{
double a=t1->data.coef+t2->data.coef;
if(a!=0.0)
{
ElemType item={a,t1->data.exp};
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=item;
}
t1=t1->next;
t2=t2->next;
}
}
while(t1)
{
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=t1->data;
t1=t1->next;
}
while(t2)
{
t3=t3->next=(Node*)malloc(sizeof(Node));
t3->data=t2->data;
t2=t2->next;
}
t3->next=NULL;
t3=p3->head;
p3->head=t3->next;
free(t3);
return p3;
}