实验题目:多项式乘法问题
实验目的:设计一个一元稀疏多项式简单计算器。实验内容与要求 一元稀疏多项式简单计算器的基本功能是: (1)输入并建立多项式; (2)输出多项式,输出形式为整数序列:n,c1,e1,c2,e2,...,cn,en,其中n是多项式的项数,ci和ei分别是第i项的系数和指数,序列按指数降序排列。 (3)多项式a与多项式b相乘,建立多项式。
实验内容和实验步骤:先确定用链表处理和储存数据,用函数compare排序链表,函数creatpoly创建节点,mul用来将两个链表相乘返回结果指针,nreturn返回链表长度,print函数输入多项式内容,主函数来调用函数
代码如下
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct poly
{
int c;
int e;
poly* next;
};
poly *creatpoly(int c,int e)
{
poly *node = NULL;
node = (poly*)malloc(sizeof(poly));
if (node == NULL) {
printf("malloc fair!\n");
return NULL;
}
memset(node, 0, sizeof(struct poly));
node->c = c;
node->e = e;
node->next = NULL;
return node;
}
void print(poly *head)
{
poly* p = head;
while (1)
{
if (p->next == NULL)
{
if (p->c != 0)
{
if (p->e == 0)
printf("%d", p->c);
else
{
if(p->c==1)
{
if (p->e == 1)
printf("X");
else
printf("X^%d", p->e);
}
else
{
if (p->e == 1)
printf("%dX", p->c);
else
printf("%dX^%d", p->c, p->e);
}
}
}
break;
}
if (p->c != 0)
{
if (p->e == 0)
printf("%d", p->c);
else
{
if (p->c == 1)
{
if (p->e == 1)
printf("X");
else
printf("X^%d", p->e);
}
else {
if (p->e == 1)
printf("%dX", p->c);
else
printf("%dX^%d", p->c, p->e);
}
}
if(p->next->c>0)
printf("+");
}
if (p->next->next != NULL)
{
p = p->next;
}
else
{
if (p->next->c!=0)
{
if (p->next->e == 0)
printf("%d", p->next->c);
else
{
if (p->next->c == 1)
{
if (p->next->e == 1)
printf("X");
else
printf("X^%d", p->next->e);
}
else {
if (p->next->e == 1)
printf("%dX", p->next->c);
else
printf("%dX^%d", p->next->c, p->next->e);
}
}
}
break;
}
}
printf("\n");
}
void compare(poly* head, int n)
{
poly* p2 = head;
poly* p1 = NULL;
p1 = head->next;
poly* temp = creatpoly(0, 0);
poly* q1 = p1;
poly* q2 = p2;
for (int i = 0; i < n - 1; i++)
{
p1 = q1;
p2 = q2;
for (int j = 0; j < n - i - 1; j++)
{
if (p2->e < p1->e)
{
temp->e = p1->e;
temp->c = p1->c;
p1->e = p2->e;
p1->c = p2->c;
p2->e = temp->e;
p2->c = temp->c;
}
p1 = p1->next;
p2 = p2->next;
}
q1 = q1->next;
q2 = q2->next;
}
}
int nreturn(poly* head)
{
poly* note = head;
poly* p = note;
int i = 0;
while (p->next != NULL) {
i++;
p = p->next;
}
return i + 1;
}
poly* mul(poly *poly1, poly* poly2)
{
int c, e;
poly* creatpoly(int c, int e);
poly* node3 = NULL;
poly* p1 = poly1;
poly* p2 = poly2;
node3 = creatpoly(0, 0);
poly* p3 = node3;
while (1)
{
p2 = poly2;
while (1)
{
c = p1->c * p2->c;
e = p1->e + p2->e;
p3->next = creatpoly(c, e);
p3 = p3->next;
if (p2->next == NULL)
break;
p2 = p2->next;
}
if (p1->next == NULL)
break;
p1 = p1->next;
}
node3 = node3->next;
p3 = node3;
int sum = 0;
e = 0;
poly* temp=NULL;
poly* p = NULL;
while (1)
{
sum = 0;
temp = p3;
e = p3->e;
while (1)
{
if (p3->e == e)
{
sum = p3->c + sum;
if (p3 != temp)
{
if (p3->next == NULL)
{
p->next = NULL;
free(p3);
}
else {
p->next = p3->next;
free(p3);
p3 = p->next;
}
}
}
if (p3->next == NULL)
break;
p = p3;
p3 = p3->next;
}
temp->c = sum;
p3 = temp;
p = p3;
p3 = p3->next;
if (p3->next == NULL)
break;
}
return node3;
}
int main()
{
int n1,n2,e,c;
int i = 0;
int n=0;
poly* creatpoly(int c, int e);
void compare(poly * head, int n);
poly* mul(poly * poly1, poly * poly2);
int nreturn(poly * head);
void print(poly * head);
poly* head1 = NULL;
struct poly* p = head1;
poly* head2 = NULL;
poly* head3 = NULL;
printf("请输入第一个多项式的项数:");
scanf_s("%d", &n1);
printf("请输入第1项的项和系数:x=");
scanf_s("%d,%d", &c, &e);
head1 = creatpoly(c, e);
p = head1;
while(i+1<n1) {
printf("请输入第%d项的项和系数:",i+2);
scanf_s("%d,%d", &c ,& e);
p->next = creatpoly(c, e);
p = p->next;
i++;
}
i = 0;
printf("请输入第二个多项式的项数:");
scanf_s("%d", &n2);
printf("请输入第1项的项和系数:x=");
scanf_s("%d,%d", &c, &e);
head2 = creatpoly(c, e);
p = head2;
while (i+1 < n2) {
printf("请输入第%d项的项和系数:x=",i+2);
scanf_s("%d,%d", &c, &e);
p->next = creatpoly(c, e);
p = p->next;
i++;
}
n = nreturn(head1);//计算第一链表的长度
compare(head1, n);
printf("第一个多项式:");
print(head1);
n = nreturn(head2);//计算第二链表的长度
compare(head2, n);
printf("第二个多项式:");
print(head2);
head3 = mul(head1, head2);
n = nreturn(head3);//计算结果链表的长度
compare(head3,n);
printf("第三个多项式:");
print(head3);
return 0;
}