Problem Description
要求采用链表形式,求两个一元多项式的乘积:h3 = h1*h2。函数原型为:void multiplication( NODE * h1, NODE * h2, NODE * h3 )。
输入:
输入数据为两行,分别表示两个一元多项式。每个一元多项式以指数递增的顺序输入多项式各项的系数(整数)、指数(整数)。
例如:1+2x+x2表示为:<1,0>,<2,1>,<1,2>,
输出:
以指数递增的顺序输出乘积: <系数,指数>,<系数,指数>,<系数,指数>,
零多项式的输出格式为:<0,0>,
测试输入
<1,0>,<2,1>,<1,2>,
<1,0>,<1,1>,
测试输出
<1,0>,<3,1>,<3,2>,<1,3>,
预置代码
/* 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
// 一元多项式相乘
//
// 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;
}
}
}
}
}