|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
遇到了一元多项式的题目,要求用链表实现,给定代码如下,现在我不知道怎么写multiplication这部分,请大佬指点!!!
#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 *, NODE *, NODE * )
{
}*/
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 );
return 0;
}
本帖最后由 Croper 于 2018-12-22 10:30 编辑 #include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int coef, exp;
struct node *next;
} NODE;
NODE * multiplication(NODE *, NODE *);
void input(NODE *);
void output(NODE *);
NODE* Link(NODE* p,int i) //输入头节点,返回第i个节点,i从0算起
{
for (int j = 0; j <= i; j++)
{
p = p->next;
if (p == NULL) return NULL;
}
return p;
}
int Length(NODE* p) //输入头节点,返回长度,
{
int j=0;
while (p->next != NULL)
{
j++;
p = p->next;
}
return j;
}
void Append(NODE*& p, int coef0, int exp0) //为一个节点添加后继并重新将指针指向后继,
{
p->next = (NODE*)malloc(sizeof(NODE));
p = p->next;
p->coef = coef0;
p->exp = exp0;
p->next=NULL;
}
void DeleteLinklist(NODE* head) //删除链表
{
NODE *pPre;
while (head != NULL)
{
pPre = head;
head = head->next;
delete pPre;
}
}
NODE * multiplication(NODE *p1, NODE *p2) //乘法运算
{
NODE* ans,*p3;
NODE* Temp1,* Temp2;
p3 = new NODE; //新建链表p3
ans = p3; //ans指向p3的头节点,用于返回
int len = Length(p1) + Length(p2)-1; //p3的最高次项=两链表最高次项相加
for (int i = 0; i < len; i++) //依次填充p3的每一项
{
Append(p3, 0, i);
for (int j = 0; j <= i; j++)
{
Temp1 = Link(p1,j);
Temp2 = Link(p2, i - j);
if ((Temp1!=NULL)&&(Temp2!=NULL)) p3->coef += (Temp1->coef*Temp2->coef);
}
}
return ans;
}
void input(NODE * p) //输入头节点,读取并储存于链表中
{
int a, b;
int i = 0;
rewind(stdin);
while(scanf_s("<%d,%d>,", &a, &b)) //读取一项,并将值暂时储存于a和b中
{
for (i; i < b; i++) Append(p, 0, i); //延长链表,直到指数=b
Append(p, a, b); //赋值
i++;
}
}
void output(NODE * head)
{
bool b = false; //储存是否已产生有效输出
while (head->next != NULL)
{
head = head->next;
if (head->coef != 0)
{
b = true; //如果任意一项系数不为0,即产生了有效输出
printf("<%d,%d>,", head->coef, head->exp);
}
}
if (!b) printf("<0,0>,"); //如果没有产生有效输出,说明这是个0多项式,输出<0,0>
printf("\n");
}
int main()
{
int i;
NODE * head1, *head2, *head3;
head1 = (NODE *)malloc(sizeof(NODE));
input(head1);
head2 = (NODE *)malloc(sizeof(NODE));
input(head2);
head3 = multiplication(head1, head2);
output(head3);
DeleteLinklist(head1);
DeleteLinklist(head2);
DeleteLinklist(head3);
return 0;
}
|
-
-
|