一、实验内容及要求
1.任务描述:
实验内容:
设有一元多项式Am(x)和Bn(X),编程实现多项式Am(x)和Bn(x)的加法、减法和乘法运算。其中多项式描述为:
Am(x)=A0+A1x1+A2x2+A3x3+….+Amxm;
Bn(x)=B0+B1x1+B2x2+B3x3+….+Bnxn。
输入和输出:
(1)输入:
- 从键盘输入运算指令(相加、相减、相乘),根据运算指令进行相应运算;
- 从键盘输入两个多项式的系数和指数;
- 系数和指数采用int类型,运算结果不超出int取值范围。
(2)输出: - 每种运算结果以多项式形式输出,要输出升幂和降幂两种情况。
- 结果多项式中无重复阶项、无零系数项,输出多项式时请采用如下易读形式(一元多项式,总变元为x): x^4 - 3 x^2 + 5
实验要求: - 实现一个简单的交互式界面,包括系统菜单、输入提示等。
- 多项式运算前首先判定多项式特点,根据多项式是否稀疏来选用合适的存储结构;
- 根据多项式不同的运算要求选择合适的存储结构;
- 上机编辑、调试出完整正确的程序,包括相加、相减、相乘运算。
2.主要数据类型与变量
struct PolyNode {
int coef;
int expon;
PolyNode* link;
};
typedef PolyNode* Polynomial;
3.算法或程序模块
读多项式模块:
Polynomial ReadPoly()
多项式乘法模块:
Polynomial Mult(Polynomial p1, Polynomial p2)
多项式加法模块:
Polynomial Add(Polynomial p1, Polynomial p2)
多项式减法模块:
Polynomial Reduce(Polynomial p1, Polynomial p2)
输出多项式模块:
void PrintPoly(Polynomial p)
二、测试
1、 方案
输入两个多项式分别为:
3 4 -5 2 6 1 -2 0
5 20 -7 4 3 1
2、 结果
三、代码
#include<cstdio>
#include<cstdlib>
using namespace std;struct PolyNode {int coef;int expon;PolyNode* link;
};
typedef PolyNode* Polynomial;void Attach(int c, int e, Polynomial* prear)
{PolyNode* node = new PolyNode();node->coef = c;node->expon = e;(*prear)->link = node;*prear = node;
}Polynomial ReadPoly()
{int c, e;Polynomial p, rear, temp;p = new PolyNode();rear = p;while(scanf("%d%d", &c, &e)){Attach(c,e,&rear);char ch = getchar(); if(ch == '\n')break;}temp = p; p = p->link; free(temp);return p;
}Polynomial Mult(Polynomial p1, Polynomial p2)
{if (!p1||!p2) return NULL;Polynomial p, rear, t1, t2, temp;p = new PolyNode();rear = p;t1 = p1; t2 = p2;int c, e;while (t2){c = t1->coef * t2->coef;e = t1->expon + t2->expon;Attach(c, e, &rear);t2 = t2->link;} t1 = t1->link;while (t1){t2 = p2; rear = p;while (t2){c = t1->coef * t2->coef;e = t1->expon + t2->expon;while (rear->link && rear->link->expon > e){rear = rear->link;}if (rear->link && rear->link->expon == e){if (rear->link->coef + c){rear->link->coef += c;}else{Polynomial temp = rear->link;rear->link = temp->link;free(temp);}}else{PolyNode* node = new PolyNode();node->coef = c;node->expon = e;node->link = rear->link;rear->link = node;rear = rear->link;}t2 = t2->link;}t1 = t1->link; }temp = p; p = p->link; free(temp);return p;
}Polynomial Add(Polynomial p1, Polynomial p2)
{Polynomial p, rear, t1, t2, temp;p = new PolyNode();rear = p;t1 = p1; t2 = p2;while (t1 && t2){if (t1->expon == t2->expon){if (t1->coef + t2->coef != 0) Attach(t1->coef + t2->coef, t1->expon, &rear);t1 = t1->link;t2 = t2->link;}else if(t1->expon > t2->expon){Attach(t1->coef, t1->expon, &rear);t1 = t1->link;}else{Attach(t2->coef, t2->expon, &rear);t2 = t2->link;}}for (; t1; t1 = t1->link) Attach(t1->coef, t1->expon, &rear);for (; t2; t2 = t2->link) Attach(t2->coef, t2->expon, &rear);temp = p; p = p->link; free(temp);return p;
}
Polynomial Reduce(Polynomial p1, Polynomial p2)
{Polynomial p, rear, t1, t2, temp;p = new PolyNode();rear = p;t1 = p1; t2 = p2;while (t1 && t2){if (t1->expon == t2->expon){if (t1->coef - t2->coef != 0) Attach(t1->coef - t2->coef, t1->expon, &rear);t1 = t1->link;t2 = t2->link;}else if(t1->expon > t2->expon){Attach(t1->coef, t1->expon, &rear);t1 = t1->link;}else{Attach(-t2->coef, t2->expon, &rear);t2 = t2->link;}}for (; t1; t1 = t1->link) Attach(t1->coef, t1->expon, &rear);for (; t2; t2 = t2->link) Attach(-t2->coef, t2->expon, &rear);temp = p; p = p->link; free(temp);return p;
}
void PrintPoly(Polynomial p)
{if (!p){printf("0\n");return;}int flag = 0;while (p){int a=p->coef;int b=p->expon;if (!flag) {flag = 1;if(b==0)printf("%d",a);else printf("%dx^%d",a,b);}else{if(a>0){if(b==0) printf("+%d",a);else if(b==1) printf("+%dx",a);else printf("+%dx^%d", a, b);} else {if(b==0) printf("%d",a);else if(b==1) printf("%dx",a);else printf("%dx^%d", a, b);}}p = p->link;}printf("\n");
}int main()
{Polynomial p1, p2, pm, ps,pr;p1 = ReadPoly();p2 = ReadPoly();pm = Mult(p1, p2);PrintPoly(pm);ps = Add(p1, p2);PrintPoly(ps);pr=Reduce(p1,p2);PrintPoly(pr);
}