这里只写了一种加法运算,其他的运算方法大同小异就没有介绍,用数组存储的思想就是系数所在的数组位置的下标就是X的次数, 例如int coeffArray = {6,6,8,0,6,4} 转换成多项式就是6+6x+8x^2+6x^4+4x^5,根据此思想便可进行运算,代码里的注释也算是完善,后面会写一篇用链表来进行操作一元多项式,因为用数组存储当一元多项式的次数不连续时,会造成空间的浪费,过多的空间用0来存储。、
#include <stdio.h>
#include <stdlib.h>
#define MaxDegree 20 //定义多项式最高次数
typedef struct Component
{
int coeffArray[MaxDegree + 1]; //系数数组,存储单项式系数, +1是为了防止常数没有存储空间
int highPower; //存储单项式最高次数
}Component, *Polynomial;
//对系数数组进行初始化
void ZeroPolynomial(Polynomial *poly)
{
(*poly) = (Polynomial)malloc(sizeof(Component));
for (int i = 0; i <= MaxDegree; ++i)
{
(*poly) -> coeffArray[i] = 0;
}
(*poly) -> highPower = 0;
}
//创建多项式,提供多项式的存储结构和创建数据(第一存储系数,第二个存储次数),数组长度
void CreatePoly(Polynomial poly, int multiArray[][2], int len)
{
int power = 0;
for(int i = 0; i < len; i++)
{
poly -> coeffArray[multiArray[i][1]] = multiArray[i][0];
if(multiArray[i][1] > power)