此次写该程序的时候费了不少劲,主要是在调试代码上,但是也花费了不少时间在构思代码的方法上,我做该程序时的思路如下:
1.首先构出两个多项式
head1=3 + 4x + 7x^2 +7x^9
head2=1 + 4x^2 + 8x^3 + 4x^6
- 人为计算的时候是head1中的每一项依次和head2中的每一项进行计算的,所以我用结构体数组存储该新形成的链表
p[0]=3*(1 + 4x^2 + 8x^3 + 4x^6)//用单链表存储
p[1]=4x * (1 + 4x^2 + 8x^3 + 4x^6)
p[2]=7x^2 * (1 + 4x^2 + 8x^3 + 4x^6)
p[3]=7x^9 * (1 + 4x^2 + 8x^3 + 4x^6)
- 将p[0]->p[3]连接成一个总链表
函数:void combineAll(NODE arr[], int n)
p=3 + 12x^2 +24x^3 +12x^6 +4x + 16 x^3 +32x^4 +16 x^7 +7x^2 +28x^4 +56x^5 +28x^8 +7x^9 +28x^11 + 56x^12 +28x^15
并将所有的指数存在num[lenth]
数组里面
- 当然不能这样输出要整理下,所以我想了个函数用来处理指数相同的项,并将系数相加
函数:void SD(int num[],int n)
void SD(int num[],int n)//找到有几种不同的指数
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if ((num[j] == num[i]) && (j != i)&&(num[j]!=-1))
{
num[j] = -1;
}
}
}
for (int i = 0; i < n; i++)
{
if (num[i] != -1)
{
Arr[cnt1] = num[i];
cnt1++;//cnt1是全局变量因为要计算有几种不同的指数
}
}
}
- 处理指数的排序问题
函数:void swapData(NODE head)
只交换数据域不交换链表的指针 - 完整代码如下
#include<stdio.h>
#include <stdlib.h>
#define lenth 16
typedef struct node
{
int index;
int coef;
struct node*next;
}node, *NODE;
int Arr[lenth] = { -1 };//全局变量,用来存储指数
int cnt1 = 0;
void initNode(NODE *head)//初始化头结点
{
(*head) = (NODE)malloc(sizeof(node));
(*head)->next = NULL;
}
NODE typeData(NODE head, int index[], int coef[], int len)//给相乘的两个单链表输入数据
{
//head必须在主函数里面申请过空间设置对象
NODE p;
p = head;
for (int i = 0; i < len; i++)
{
NODE temp = (NODE)malloc(sizeof(node));
temp->index = index[i];
temp->coef = coef[i];
p->next= temp;
p = temp;
temp->next = NULL;
}
return head;
}
void mutipleFunc(NODE add[],NODE head1, NODE head2, int len1, int len2)//功能函数
{
NODE p, begin;
p = head1;
p = p->next;
for (int i = 0; i < len1; i++)
{
NODE q = head2->next;
begin = add[i];
for (int j = 0; j < len2; j++)
{
NODE temp = (NODE)malloc(sizeof(node));
temp->index = p->index + q->index;//指数相加
temp->coef = p->coef * q->coef;//系数相乘
begin->next = temp;
begin = temp;
temp->next = NULL;
q = q->next;
}
p = p->next;
}
}
void showNode(NODE *head)
{
NODE p;
p = (*head);
p = p->next;
while (p!= NULL)
{
printf("系数是%d,指数是%d\n", p->coef, p->index);
p = p->next;
}
}
void SD(int num[],int n)//找到有几种不同的指数
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if ((num[j] == num[i]) && (j != i)&&(num[j]!=-1))
{
num[j] = -1;
}
}
}
for (int i = 0; i < n; i++)
{
if (num[i] != -1)
{
Arr[cnt1] = num[i];
cnt1++;
}
}
}
void swap(int *data1, int *data2)
{
int temp;
temp = (*data1);
(*data1) = (*data2);
(*data2) = temp;
}
void swapData(NODE head)//只交换数据域
{
NODE p,q;
p = head;
p = p->next;
int i = 0;
while (p->next != NULL)
{
q = head;
for (int j = 0; j <= i; j++)
{
q = q->next;
}
while (q->next != NULL)
{
q = q->next;
if (p->index > q->index)
{
swap(&p->index, &q->index);
swap(&p->coef, &q->coef);
}
}
p = p->next;
i++;
}
}
NODE combineAll(NODE arr[], int n)//多个多项式相加首先这些多项式都是顺序的
{
NODE p, q, head;
head = (NODE)malloc(sizeof(node));
p = head;//p指针指向head
for (int i = 0; i < n; i++)//将所有的多项式合并为一个式子
{
q = arr[i]->next;
while (q != NULL)
{
p->next = q;
p = q;
q = q->next;
}
}
}
//判断有几个不同的指数
void function(NODE head)
{
NODE SchIndex;
SchIndex = head;
SchIndex = SchIndex->next;
int cnt = 0;
int num[lenth] = { 0 };
while (SchIndex != NULL)//将所有的指数存在数组num[]中
{
num[cnt] = SchIndex->index;
cnt++;
SchIndex = SchIndex->next;
}
SD(num, cnt);
NODE New, P;
New = (NODE)malloc(sizeof(node));
New->next = NULL;
New->index = -1;
New->coef = -1;
P = New;
int j = 0;
while (j < cnt1)
{
NODE m;
m = head;
m = m->next;
int sumCoef = 0;
NODE temp = (NODE)malloc(sizeof(node));
while (m != NULL)
{
if (m->index == Arr[j])
{
sumCoef = sumCoef + m->coef;
}
m = m->next;
}
temp->coef = sumCoef;
temp->index = Arr[j];
temp->next = NULL;
P->next = temp;
P = temp;
j++;
}
swapData(New);
showNode(&New);
}
int main()
{
NODE head1, head2,head;
NODE add[lenth];
for (int i = 0; i < lenth; i++)
{
initNode(&add[i]);
}
//初始化多项式
initNode(&head1);
initNode(&head2);
initNode(&head);
//系数和指数用数组赋值
int arr_index[4] = { 0,1,2,9};
int arr_coef[4] = { 3,4,7,7};
int Arr_index[4] = { 0,2,3,6};
int Arr_coef[4] = { 1,4,8,4};
//输入多项式数据
typeData(head1, arr_index, arr_coef, 4);
typeData(head2, Arr_index, Arr_coef, 4);
//功能函数
mutipleFunc(add,head1, head2, 4, 4);
//多项式合并成一个式子
head=combineAll(add, 4);
function(head);
system("pause");
return 0;
}