实验目的
1、 了解线性表的特性,以及它在实际问题中的应用。
2、掌握线性表的顺序存储的实现方法以及它的基本操作,学会运用线性表来解决问题。
实验内容
用顺序存储结构实现一元多项式的加法、减法和乘法。具体要求为:用五个函数分别实现一元多项式的创建、输出、加法、减法和乘法;然后在主函数中调用这些函数实现这些功能的展示。
代码内容
#include<iostream>
using namespace std;
#define maxdegree 100 //多项式最大项数
//定义多项式结构体
typedef struct polynomial {
int coefarray[maxdegree+1]; //多项式系数
int highpower; //最高项指数
}polynomial;
//创建多项式
void creatlist(polynomial* L) {
int n, cofe, index; //项数,系数,指数
L->highpower = 0; //多项式指数初始化
for (int i = 0; i <= maxdegree; i++)
L->coefarray[i] = 0; //多项式系数初始化
cout << "输入多项式项数" << endl;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << "请输入第" <<i<<"项的系数和指数"<< endl;
cin >> cofe >> index;
if (L->highpower < index)
L->highpower = index; //更新最高项指数
L->coefarray[index] = cofe; //把对应的多项式系数存入
}
}
//初始化多项式
void initialize(polynomial* L) {
for (int i = 0; i <= maxdegree + 1; i++)
L->coefarray[i] = 0;
L->highpower = 0;
}
//输出多项式
void printout(polynomial *L) {
if (L->coefarray[0] != 0)
cout << L->coefarray[0] << "+"; //判断常数项是否为零
for (int i = 1; i < L->highpower; i++) {
if (L->coefarray[i] == 0)
; //判断该项是否存在
else {
if (L->coefarray[i] < 0)
cout << "\b"; //如果是负数删掉+
cout << L->coefarray[i] << "x^" << i << "+";
}
}
cout << L->coefarray[L->highpower] << "x^" << L->highpower ; //单独输出最后一项
}
//多项式加法
void add_polynomial(polynomial *L1,polynomial *L2,polynomial *L3) {
if (L1->highpower > L2->highpower) //判断最高项指数
L3->highpower = L1->highpower;
else
L3->highpower = L2->highpower;
for (int i = 0; i <= L3->highpower; i++)
L3->coefarray[i] = L1->coefarray[i] + L2->coefarray[i];
}
//多项式减法
void subtruct_polynomial(polynomial* L1, polynomial* L2, polynomial* L3) {
if (L1->highpower > L2->highpower) //判断最高项指数
L3->highpower = L1->highpower;
else
L3->highpower = L2->highpower;
for (int i = 0; i <= L3->highpower; i++)
L3->coefarray[i] = L1->coefarray[i] - L2->coefarray[i];
}
//多项式乘法
void multiply_polynomial(polynomial* L1, polynomial* L2, polynomial* L3) {
L3->highpower = L1->highpower + L2->highpower; //同底数相乘等于底数不变幂数相加
for (int i = 0; i <= L1->highpower; i++)
for (int j = 0; j <= L2->highpower; j++)
L3->coefarray[i + j] += L1->coefarray[i] * L2->coefarray[j];
}
//菜单函数
void display() {
cout << "========================"<<endl;
cout << "1.创建多项式" << endl;
cout << "2.多项式加法" << endl;
cout << "3.多项式减法" << endl;
cout << "4.多项式乘法" << endl;
cout << "0.退出" << endl;
cout << "========================" << endl;
}
int main() {
//为L1,L2,L3动态分布空间
polynomial* L1 = new polynomial;
polynomial* L2 = new polynomial;
polynomial* L3 = new polynomial;
int n;
display(); //调用菜单函数
while (true){
cout << "请输入要执行的操作:" ;
cin >> n;
switch (n){
case 1:
creatlist(L1); //调用创建多项式函数
cout << "第一个多项式为:" ;
printout(L1);
cout << endl;
creatlist(L2);
cout << "第二个多项式为:";
printout(L2);
cout << endl;
break;
case 2:
initialize(L3); //调用初始化函数
add_polynomial(L1, L2, L3); //调用加法函数
cout << "相加后结果为:" << endl;
printout(L3);
cout << endl;
break;
case 3:
initialize(L3);
subtruct_polynomial(L1, L2, L3); //调用减法函数
cout << "相减后结果为:" << endl;
printout(L3);
cout << endl;
break;
case 4:
initialize(L3);
multiply_polynomial(L1, L2, L3); //调用乘法函数
cout << "相乘后结果为:" << endl;
printout(L3);
cout << endl;
break;
case 0:
exit(1); //退出程序
default:
cout << "输入不合法,请重新输入!" << endl;
cout << endl;
break;
}
}
delete L1, L2, L3; //释放L1,L2,L3动态内存空间
return 0;
}
运行结果
测试数据:
总结
代码中的算法很容易实现,只需要把两个数组对应的分量项进行加减乘运算即可,但顺序存储只适合稠密多项式,如果是系数多项式的话就很浪费空间和时间。