PTA 6-14 用单向链表完成多项式运算(35分)
输入两个多项式,计算它们的加、减及乘法, 将计算结果输出到屏幕上。
1) 输入:从键盘分两行分别输入两个多项式数据,每个多项式输入格式如下:
n a1 m1 a2 m2 a3 m3 . .. ai mi.... an mn
其中n为多项式系数为非零的项数,ai为系数,mi为指数, 它们都是整数,该输入数据所表达的多项式意义为(其中符号^表示幂次):
a1x^m1 + a2x^m2 + a3x^m3 + ...... + an x^mn
2)输出:先输出参与运算的两个多项式, 然后输出它们的运算结果,格式为:
要求:多项式输出时从幂次数高到低的顺序逐项输出,若任何一项的系数是0的话,请不要输出,除非所有的系数为0,则输出0。
函数接口定义:
void print_polynomial(polynomial *head);
本函数完成一个多项式的输出。题目要求的功能以及本函数具体涉及到多项式链表的创建以及多项式加、减和乘法运算所需的功能模块请在本函数实现之前一并自行完成,并将代码插入在合适的位置。
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int coef; /* 系数 */
int exp; /* 指数 */
struct node *next;
} polynomial;
/* 提示:递交的答案将会自动插入此处 */
int main(void)
{
polynomial *polyA,*polyB;
polyA=create_polynomial(); /* 其中函数create_polynomial()用于读入并创建多项式A的链表,请自行实现 */
polyB=create_polynomial(); /* 读入并创建多项式B的链表 */
printf("A=");
print_polynomial(polyA); /* 输出多项式A */
printf("B=");
print_polynomial(polyB); /* 输出多项式B */
printf("A+B=");
print_polynomial(add_polynomial(polyA,polyB) );
/* 输出多项式加A+B 的结果, 其中函数add_polynomial(polyA,polyB)将返回A+B的多项式,请自行实现*/
printf("A-B=");
print_polynomial(subtract_polynomial(polyA,polyB) );
/* 输出多项式减A-B 的结果,其中函数subtract_polynomial(polyA,polyB)返回A-B的多项式,请自行实现*/
printf("A*B=");
print_polynomial(multiply_polynomial(polyA,polyB));
/* 输出多项式乘A*B 的结果,其中函数multiply_polynomial(polyA,polyB)返回A*B的多项式,请自行实现 */
return 0;
/* 请在这里填写答案 */
}
输入样例:
在这里给出一组输入。例如:
3 1 2 3 4 5 6
3 1 2 3 4 5 6
输出样例:
在这里给出相应的输出。例如(其中 x 是小写字母):
A=5x^6 +3x^4 +x^2
B=5x^6 +3x^4 +x^2
A+B=10x^6 +6x^4 +2x^2
A-B=0
A*B=25x^12 +30x^10 +19x^8 +6x^6 +x^4
有点坑的题目 卡了半天样例(机考写完的时候老师问我是不是搞错题意,然而并不是...这题真就要把函数全写了,并不是只写个输出函数)
输入要排序!!!我这里直接每次输入一个插入一次链表,也可以输入后链表排序, x^1 要输出成x,ax^0 要写成a,格式挺麻烦.... 当时太懒了用数组偷懒被老师锤了..... ⑧说了,我也觉得输出函数写得丑,因为看不下去然后改得好看些然后wa了就不想改了..
polynomial *create_polynomial()
{
int n;
scanf("%d", &n);
polynomial *head, *p, *t;
head = (polynomial *)malloc(sizeof(polynomial));
head->next = NULL;
/*输入排序*/
for (int i = 0; i < n; i++)
{
int coef, exp;
polynomial *cah;
scanf("%d%d", &coef, &exp);
t = head;
for (; t->next != NULL; t = t->next)
{
if (t->next->exp < exp)
{
cah = (polynomial *)malloc(sizeof(polynomial));
cah->coef = coef;
cah->exp = exp;
cah->next = t->next;
t->next = cah;
break;
}
else if (t->next->exp == exp)
{
t->next->coef += coef;
break;
}
}
if (!t->next)
{
cah = (polynomial *)malloc(sizeof(polynomial));
cah->coef = coef;
cah->exp = exp;
t->next = cah;
cah->next = NULL;
}
}
return head;
}
void print_polynomial(polynomial *head)
{
polynomial *p;
int flag = 0;
for (p = head->next; p != NULL; p = p->next)
{
if (!flag && p->coef != 0)
{
flag = 1;
if (p->coef > 0)
{
if (p->exp == 0)
printf("%d", p->coef);
else
{
p->coef == 1 ? printf("x") : printf("%dx", p->coef);
p->exp == 1 ? printf("") : printf("^%d", p->exp);
}
}
else
{
if (p->exp == 0)
printf("%d", p->coef);
else
{
p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
p->exp == 1 ? printf("") : printf("^%d", p->exp);
}
}
}
else if (flag && p->coef != 0)
{
if (p->coef > 0)
{
if (p->exp == 0)
printf("+%d", p->coef);
else
{
p->coef == 1 ? printf("+x") : printf("+%dx", p->coef);
p->exp == 1 ? printf("") : printf("^%d", p->exp);
}
}
else
{
if (p->exp == 0)
printf("%d", p->coef);
else
{
p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
p->exp == 1 ? printf("") : printf("^%d", p->exp);
}
}
}
}
if (!flag)
printf("0");
printf("\n");
}
polynomial *add_polynomial(polynomial *a, polynomial *b)
{
polynomial *head, *p, *tail = NULL;
head = (polynomial *)malloc(sizeof(polynomial));
polynomial *a1 = a->next, *b1 = b->next;
head->next = NULL;
tail = head;
while (a1 && b1)
{
p = (polynomial *)malloc(sizeof(polynomial));
p->next = NULL;
if (a1->exp > b1->exp)
{
p->coef = a1->coef;
p->exp = a1->exp;
tail->next = p;
tail = p;
a1 = a1->next;
}
else if (a1->exp < b1->exp)
{
p->coef = b1->coef;
p->exp = b1->exp;
tail->next = p;
tail = p;
b1 = b1->next;
}
else
{
p->coef = a1->coef + b1->coef;
p->exp = b1->exp;
tail->next = p;
tail = p;
a1 = a1->next;
b1 = b1->next;
}
}
while (a1)
{
p = (polynomial *)malloc(sizeof(polynomial));
p->next = NULL;
p->coef = a1->coef;
p->exp = a1->exp;
tail->next = p;
tail = p;
a1 = a1->next;
}
while (b1)
{
p = (polynomial *)malloc(sizeof(polynomial));
p->next = NULL;
p->coef = b1->coef;
p->exp = b1->exp;
tail->next = p;
tail = p;
b1 = b1->next;
}
return head;
}
polynomial *subtract_polynomial(polynomial *a, polynomial *b)
{
polynomial *head, *p, *tail = NULL;
head = (polynomial *)malloc(sizeof(polynomial));
polynomial *a1 = a->next, *b1 = b->next;
head->next = NULL;
tail = head;
while (a1 && b1)
{
p = (polynomial *)malloc(sizeof(polynomial));
p->next = NULL;
if (a1->exp > b1->exp)
{
p->coef = a1->coef;
p->exp = a1->exp;
tail->next = p;
tail = p;
a1 = a1->next;
}
else if (a1->exp < b1->exp)
{
p->coef = -b1->coef;
p->exp = b1->exp;
tail->next = p;
tail = p;
b1 = b1->next;
}
else
{
p->coef = a1->coef - b1->coef;
p->exp = b1->exp;
tail->next = p;
tail = p;
a1 = a1->next;
b1 = b1->next;
}
}
while (a1)
{
p = (polynomial *)malloc(sizeof(polynomial));
p->next = NULL;
p->coef = a1->coef;
p->exp = a1->exp;
tail->next = p;
tail = p;
a1 = a1->next;
}
while (b1)
{
p = (polynomial *)malloc(sizeof(polynomial));
p->next = NULL;
p->coef = -b1->coef;
p->exp = b1->exp;
tail->next = p;
tail = p;
b1 = b1->next;
}
return head;
}
polynomial *multiply_polynomial(polynomial *a, polynomial *b)
{
polynomial *head, *p1, *tail;
head = (polynomial *)malloc(sizeof(polynomial));
head->next = NULL;
tail = head;
polynomial *p, *q, *t, *cah;
for (p = a->next; p != NULL; p = p->next)
for (q = b->next; q != NULL; q = q->next)
{
int coef = p->coef * q->coef;
int exp = p->exp + q->exp;
for (t = head; t->next != NULL; t = t->next)
{
if (t->next->exp < exp)
{
cah = (polynomial *)malloc(sizeof(polynomial));
cah->coef = coef;
cah->exp = exp;
cah->next = t->next;
t->next = cah;
break;
}
else if (t->next->exp == exp)
{
t->next->coef += coef;
break;
}
}
if (!t->next)
{
cah = (polynomial *)malloc(sizeof(polynomial));
cah->coef = coef;
cah->exp = exp;
t->next = cah;
cah->next = NULL;
}
}
return head;
}