Problem Description
要求采用鏈表形式,求兩個(gè)一元多項(xiàng)式的乘積:h3 = h1*h2署尤。函數(shù)原型為:void multiplication( NODE * h1, NODE * h2, NODE * h3 )。
輸入:
輸入數(shù)據(jù)為兩行彬伦,分別表示兩個(gè)一元多項(xiàng)式嫂用。每個(gè)一元多項(xiàng)式以指數(shù)遞增的順序輸入多項(xiàng)式各項(xiàng)的系數(shù)(整數(shù))、指數(shù)(整數(shù))沼沈。
例如:1+2x+x2表示為:<1,0>,<2,1>,<1,2>,
輸出:
以指數(shù)遞增的順序輸出乘積: <系數(shù),指數(shù)>,<系數(shù),指數(shù)>,<系數(shù),指數(shù)>,
零多項(xiàng)式的輸出格式為:<0,0>,
測(cè)試輸入
<1,0>,<2,1>,<1,2>,
<1,0>,<1,1>,
測(cè)試輸出
<1,0>,<3,1>,<3,2>,<1,3>,
預(yù)置代碼
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#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 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;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
AcCode
//
// main.cpp
// 一元多項(xiàng)式相乘
//
// Created by jetviper on 2017/3/26.
// Copyright ? 2017年 jetviper. All rights reserved.
//
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#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 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;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
void multiplication(NODE * h1, NODE * h2, NODE * h3)
{
NODE *p1 = h2, *p2 = h1, *p3 = h3;
NODE *temp,*t1;
int cl = 0;
while (p1->next != NULL)
{
p2 = h1;
p1 = p1->next;
if (p1->coef == 0&&cl==1)continue;
p3 = h3;
while (p2->next != NULL)
{
p2 = p2->next;
int zs = p1->exp +p2->exp ;
int xs = p1->coef * p2->coef;
if (xs == 0 && cl == 1)continue;
NODE *tp;
while (p3!=NULL) {
tp = p3;
p3 = p3->next;
if (p3 == NULL) {
temp = (NODE*)malloc(sizeof(NODE));
if (xs == 0&&cl==0) {
temp->exp = 0;
cl = 1;
}
else temp->exp = zs;
temp->coef = xs;
temp->next = p3;
tp->next = temp;
p3 = tp;
break;
}
if (p3->exp < zs)continue;
if (p3->exp == zs) {
p3->coef += xs;
if (p3->coef == 0 && zs != 0) {
tp->next = p3->next;
}
p3 = tp;
break;
}
if (p3->exp > zs) {
temp = (NODE*)malloc(sizeof(NODE));
if (xs == 0&&cl==0) {
temp->exp = 0;
cl = 1;
}
else temp->exp = zs;
temp->coef = p1->coef * p2->coef;
temp->next = p3;
tp->next = temp;
p3 = tp;
break;
}
}
}
}
}