1、一元多项式的运算:实现两个多项式加、减乘运算
设计内容:
用顺序存储结构实现一元多项式的加法、减法和乘法。具体要求为:用五个函数分别实现一元多项式的创建、输出、加法、减法和乘法;
设计思路:
将顺序表数组下标作为多项式的指数项,数组内的数据元素存放多项式的系数,通过访问数组内元素的同时获取下标并对二者进行不同的运算后,将运算结果依旧按原形式放入新的数组中,完成对两个多项式的加减乘运算。
测试数据:
多项式1: 10 + 5 x − 4 x2+ 3 x3+ 2 x4 10 + 5x -4x^2 + 3x^3 + 2x^410+5x−4x2+3x3+2x4(即输入10 0 5 1 -4 2 3 3 2 4)(输入-1 -1结束)
多项式2: − 3 + 8 x + 4 x2− 5 x4+ 7 x5− 2 x6 -3 + 8x + 4x^2 – 5x^4 + 7x^5 -2x^6−3+8x+4x2−5x4+7x5−2x6(即输入 -3 0 8 1 4 2 -5 4 7 5 -2 6)(输入 -1 -1结束)
代码实现:
1、构造一个空的线性表L
void InitList(SqList *L) {/**申请连续的MAXSIZE长度空间*/L->data = (ElemType *) malloc(sizeof(ElemType) * MAXSIZE);/**判断空间是否申请成功*/if (!L->data)exit(-1);for (int i = 0; i < MAXSIZE; i++)L->data[i] = 0;/**空表最高次项为0*/L->highPower = 0;}
2、销毁线性表L
- 释放data指向的空间
- 让L -> data指向null
- 让L- > highPower =0
void DestroyList(SqList *L) {/**释放data指向的空间*/free(L->data);L->data = NULL;L->highPower = 0;}
3、两多项式相加
/**两多项式相加*/void TwoPolynomialAdd(SqList L1, SqList L2, SqList *L3) {/**获取两多项式的最大项以确定相加后的多项式的最大项*/L3->highPower = L1.highPower > L2.highPower " />.highPower : L2.highPower;/**通过循环将两个多项式的同指数项的系数相加并保存到新的多项式中*/for (int i = 0; i <= L3->highPower; i++)L3->data[i] = L1.data[i] + L2.data[i];}
4、两多项式相减
/**两多项式相减*/void TwoPolynomialSub(SqList L1, SqList L2, SqList *L3) {/**获取两多项式的最大项以确定相加后的多项式的最大项*/L3->highPower = L1.highPower > L2.highPower ? L1.highPower : L2.highPower;/**通过循环将两个多项式的同指数项的系数相减并保存到新的多项式中*/for (int i = 0; i <= L3->highPower; i++)L3->data[i] = L1.data[i] - L2.data[i];}
5、两多项式相乘
/**两多项式相乘*/void TwoPolynomialMul(SqList L1, SqList L2, SqList *L3) {/**两多项式的最高项相加为新多项式的最高项*/L3->highPower = L1.highPower + L2.highPower;for (int i = 0; i <= L1.highPower; i++)/**通过双层循环将两个多项式的每一项两两相乘与并保存到新的多项式中*/for (int j = 0; j <= L2.highPower; j++)/**相乘后的指数项为两项指数项相加*/L3->data[i + j] += L1.data[i] * L2.data[j];}
6、完整代码实现
#include #include /**表可能达到的最大长度,存储空间初始分配量*/#define MAXSIZE 20/**表的数据类型,根据实际情况而定*/typedef int ElemType;typedef struct {/**数组存储数据元素,最大值为MAXSIZE*/ElemType *data;/**最高次项*/int highPower;} SqList;/**构造一个空的线性表L*/void InitList(SqList *L) {/**申请连续的MAXSIZE长度空间*/L->data = (ElemType *) malloc(sizeof(ElemType) * MAXSIZE);/**判断空间是否申请成功*/if (!L->data)exit(-1);for (int i = 0; i < MAXSIZE; i++)L->data[i] = 0;/**空表最高次项为0*/L->highPower = 0;}/**销毁线性表L*/void DestroyList(SqList *L) {/**释放data指向的空间*/free(L->data);L->data = NULL;L->highPower = 0;}/**创建多项式:由用户输入多项式的每项系数与指数*/void CreatePolynomial(SqList *L) {int coefficient, exponent;/**循环输入常数项的每一项*/for (int i = 0; i < 10; i++){printf("\n请输入第%d项的常数项和指数项,结束请输入-1 -1:", i + 1);scanf("%d%d", &coefficient, &exponent);/**指数为-1则结束输入*/if (exponent != -1) {/**数组下标为指数项,常数项存入下标对应的位置,若有相同指数项则常数项相加*/L->data[exponent] += coefficient;/**指数项最大项为多项式长度*/if (L->highPower < exponent) L->highPower = exponent;} elsebreak;}printf("输入完毕\n");}/**输出常数项:按多项式指数大小依次输出多项式每一项*/void PrintPolynomial(SqList L) {printf("多项式为:");/**若常数项为0则不输出,若不为0则只输出常数项*/if (L.data[0] != 0)printf("%d", L.data[0]);/**若常数项为0则不输出*/if (L.data[1] > 0)printf("+%dx", L.data[1]);/**不输出指数项为1*/else if (L.data[1] < 0)printf("%dx", L.data[1]);/**从第二项开始输出指数项*/for (int i = 2; i <= L.highPower; i++)/**若常数项为0则不输出该项*/if (L.data[i] > 0)printf("+%dx^%d", L.data[i], i);else if (L.data[i] < 0)printf("%dx^%d", L.data[i], i);printf("\n");}/**两多项式相加*/void TwoPolynomialAdd(SqList L1, SqList L2, SqList *L3) {/**获取两多项式的最大项以确定相加后的多项式的最大项*/L3->highPower = L1.highPower > L2.highPower ? L1.highPower : L2.highPower;/**通过循环将两个多项式的同指数项的系数相加并保存到新的多项式中*/for (int i = 0; i <= L3->highPower; i++)L3->data[i] = L1.data[i] + L2.data[i];}/**两多项式相减*/void TwoPolynomialSub(SqList L1, SqList L2, SqList *L3) {/**获取两多项式的最大项以确定相加后的多项式的最大项*/L3->highPower = L1.highPower > L2.highPower ? L1.highPower : L2.highPower;/**通过循环将两个多项式的同指数项的系数相减并保存到新的多项式中*/for (int i = 0; i <= L3->highPower; i++)L3->data[i] = L1.data[i] - L2.data[i];}/**两多项式相乘*/void TwoPolynomialMul(SqList L1, SqList L2, SqList *L3) {/**两多项式的最高项相加为新多项式的最高项*/L3->highPower = L1.highPower + L2.highPower;for (int i = 0; i <= L1.highPower; i++)/**通过双层循环将两个多项式的每一项两两相乘与并保存到新的多项式中*/for (int j = 0; j <= L2.highPower; j++)/**相乘后的指数项为两项指数项相加*/L3->data[i + j] += L1.data[i] * L2.data[j];}int Menu() {int a;printf("输入1创建两个多项式\t\t输入2输出两个多项式\n");printf("输入3将两个多项式相加 \t\t输入4将两个多项式相减\n");printf("输入5将两个多项式相乘 \t\t输入0退出\n");printf("请输入:");scanf("%d", &a);return a;}int main() {SqList L1, L2, L3;InitList(&L1);InitList(&L2);InitList(&L3);while (1) {switch (Menu()) {case 1:printf("\n请输入多项式 1 :");CreatePolynomial(&L1);printf("\n多项式 1 为:\n");PrintPolynomial(L1);printf("\n请输入多项式 2 :");CreatePolynomial(&L2);printf("\n多项式 2 为:\n");PrintPolynomial(L2);break;case 2:printf("\n多项式 1 为:\n");PrintPolynomial(L1);printf("\n多项式 2 为:\n");PrintPolynomial(L2);break;case 3:InitList(&L3);TwoPolynomialAdd(L1, L2, &L3);printf("两多项式相加后的结果");PrintPolynomial(L3);break;case 4:TwoPolynomialSub(L1, L2, &L3);printf("两多项式相减后的结果");PrintPolynomial(L3);break;case 5:DestroyList(&L3);InitList(&L3);TwoPolynomialMul(L1, L2, &L3);printf("两多项式相乘后的结果");PrintPolynomial(L3);break;case 0:exit(0);default:printf("暂无此功能\n");}}}
运行结果:
相加后的多项式为: 7 + 13 x + 3 x3− 3 x4+ 7 x5− 2 x6 7+13x+3x^3-3x^4+7x^5-2x^67+13x+3x3−3x4+7x5−2x6
相减后的多项式为: 13 − 3 x − 8 x2+ 3 x3+ 7 x4− 7 x5+ 2 x6 13-3x-8x^2+3x^3+7x^4-7x^5+2x^613−3x−8x2+3x3+7x4−7x5+2x6
相乘后的多项式为:- 30 + 65 x + 92 x2− 21 x3− 48 x4+ 73 x5+ 43 x6− 53 x7+ 19 x8+ 8 x9− 4 x1030+65x+92x^2-21x^3-48x^4+73x^5+43x^6-53x^7+19x^8+8x^9-4x^1030+65x+92x2−21x3−48x4+73x5+43x6−53x7+19x8+8x9−4x10