马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 智能 于 2013-10-8 19:46 编辑 #include <stdio.h>
#include <stdlib.h>
typedef struct node
{ int coef, exp;
struct node *next;
} NODE;
void multiplication( NODE *, NODE * , NODE * );
void input( NODE * );
void output( NODE * );
void multiplication(NODE*a,NODE*b,NODE*c)
{
NODE *a0=a;
NODE *b0=b;
NODE *c0=c;
NODE *c1=NULL;
NODE *lin=NULL;
NODE *zhong=NULL;
int ji,p=8,k;
while(b0->next!=NULL)
{
b0=b0->next;
a0=a;
while(a0->next!=NULL)
{
a0=a0->next;
ji=a0->coef*b0->coef;
k=a0->exp+b0->exp;
c0=c;
while(c0->next!=NULL)
{
p=0;
lin=c0;//临时存储c0
c0=c0->next;
if(c0->exp>=k)
{p=1;break;}
}
if(c0->next==NULL&&p!=1)
{
if(c0->exp!=k)
{
zhong=(NODE*)malloc(sizeof(NODE));
c0->next=zhong;
zhong->next=NULL;
zhong->coef=ji;
zhong->exp=k;
}
else
c0->coef=c0->coef+ji;
}
if(c0->exp==k)
{
c0->coef=c0->coef+ji;
}
if(c0->exp>k)
{
c1=(NODE*)malloc(sizeof(NODE));//新申请c1
lin->next=c1;
c1->next=c0;
c1->exp=k;
c1->coef=ji;
}
}
}
}
void input( NODE * head )
{ int flag, sign, sum, x;
char c;
NODE * p = head;
while ( (c=getchar()) !='\n' )
{
if ( c == '<' )
{ sum = 0;
sign = 1;
flag = 1;
}
else if ( c =='-' )
sign = -1;
else if( c >='0'&& c <='9' )
{ sum = sum*10 + c - '0';
}
else if ( c == ',' )
{ if ( flag == 1 )
{ x = sign * sum;
sum = 0;
flag = 2;
sign = 1;
}
}
else if ( c == '>' )
{ p->next = ( NODE * ) malloc( sizeof(NODE) );
p->next->coef = x;
p->next->exp = sign * sum;
p = p->next;
p->next = NULL;
flag = 0;
}
}
}
void output( NODE * head )
{
while ( head->next != NULL )
{ head = head->next;
printf("<%d,%d>,", head->coef, head->exp );
}
printf("\n");
}
int main()
{ NODE * head1, * head2, * head3;
head1 = ( NODE * ) malloc( sizeof(NODE) );
input( head1 );
head2 = ( NODE * ) malloc( sizeof(NODE) );
input( head2 );
head3 = ( NODE * ) malloc( sizeof(NODE) );
head3->next = NULL;
multiplication( head1, head2, head3 );
output( head3 );
system("pause");
return 0;
}
用dev和vs都可以编译并输出正确结果,但用gcc会提示“无效内存引用。”其中multiplication函数是自己编的,其它是预制代码 |