一元多项式计算(C语言)
题目:
- 一元多项式计算
功能要求:
(1)能够按照指数降序排列建立并输出多项式;
(2)能够完成两个多项式的相加、相减,并将结果输出;
程序:
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
typedef struct term {
float base; //底数
int expn; //指数
struct term *next;
}term, *LinkList;//term为一个新类型(是一个结构体),LinkList为指向这样的结构体的指针
typedef LinkList polynomial;
//若有序链表L中存在与项t的指数相等的元素,则指针q指向L中第一个指数为t->expn的节点的位置,
//否则q指向第一个指数满足与t->expn相比>0的节点的前驱位置
bool locateElem(LinkList L, LinkList t, LinkList &q) {
LinkList p1 = L->next;
LinkList p2 = L;//p2总指向p1的前驱
while (p1) {
if (t->expn < p1->expn) {
p1 = p1->next;
p2 = p2->next;
}
else if (t->expn == p1->expn) {
q = p1;
return true;
}
else {//p1->expn > t->expn,因为L是有序表,所以如果程序走到了这一步,说明没找到与项t的指数相等的节点元素
//则返回q的前驱结点
q = p2;
return false;
}
}
if (!p1) {//t->expn比当前列表所有元素的指数都大,则上面的while循环会因为p2到达了尾节点,p1=NULL而跳出
q = p2;
return false;
}
}
// 1. 直接选择排序 ------直接交换数据
void ListSort_1(LinkList head)
{
LinkList p = NULL;
LinkList q = NULL;
LinkList t = NULL;
if (head == NULL || (head)->next == NULL)
{
return;
}
for (p = head; p != NULL; p = p->next)
{
t = p;
for (q = p->next; q != NULL; q = q->next)
{
if (q->expn > t->expn)
{
t = q;
}
}
if (t != p)
{
LinkList tmp = NULL;
tmp = p;
p = t;
t = tmp;
}
}
return;
}
//输入m项的系数和指数,建立表示一元多项式的带有头节点的有序链表P
//利用尾插法
void createPolyn(polynomial &P, int m) {
//先建立一个带有头节点的空链表,即初始化
P = (polynomial)malloc(sizeof(term));
P->next = NULL;
LinkList r = P;//r指针总是指向当前线性表的最后一个元素,即尾元素
printf("输入系数,指数,如项2x^5则输入(2,5):\n");//不必按升幂输入
for (int i = 0; i < m; i++) {//依次输入m个非零项
LinkList t = (LinkList)malloc(sizeof(term));
t->next = NULL;
LinkList q;
scanf("%f,%d", &t->base, &t->expn);
char c = getchar();
if (!locateElem(P, t, q)) {//当前链表中不存在该指数项,则插入,此时q为链表中第一个指数>t->expn的节点的前驱结点
t->next = q->next;
q->next = t;
}
else {//当前列表中已经存在有节点元素的指数与本次输入的项的指数相同,所以本次输入无效,应重新输入
i--;
}
}
ListSort_1(P);
}
//打印多项式链表
void printPolynomial(polynomial P) {
LinkList q = P->next;
printf("打印多项式的线性表:[");
while (q) {
printf("(%.2f,%d) ", q->base, q->expn);
q = q->next;
}
printf("]\n\n");
}
void display(polynomial L) //打印多项式
{
polynomial p;
p = L->next;
printf("%.0fx^(%d)", p->base, p->expn);
p = p->next;
while (p != NULL)
{
if (p->base > 0)
{
printf("+%.0fx^(%d)", p->base, p->expn);
}
else
{
printf("%.0fx^(%d)", p->base, p->expn);
}
p = p->next;
}
printf("\n");
}
int cmp(LinkList qa, LinkList qb) {//比较项的指数大小
if (qa->expn > qb->expn)
return 1;
else if (qa->expn == qb->expn)
return 0;
else
return -1;
}
void addPolyn(LinkList La, LinkList Lb, LinkList &Lc)
{
LinkList pa, pb, pc;
pc = (LinkList)malloc(sizeof(term));
Lc = pc;
pa = La->next;
float x;
pb = Lb->next;
while (pb) {
if (!pa) {
break;
}
if (pa->expn == pb->expn) //指数相同
{
//EXPN=pa->expn;
x = pa->base + pb->base;
if (x)
{
pc->next = (LinkList)malloc(sizeof(term));
pc = pc->next;
pc->base = x;
pc->expn = pa->expn;
}
pa = pa->next;
pb = pb->next;
continue;
}
else if (pa->expn > pb->expn)
{
pc->next = (LinkList)malloc(sizeof(term));
pc = pc->next;
pc->base = pa->base;
pc->expn = pa->expn;
pa = pa->next;
continue;
}
else if (pa->expn < pb->expn)
{
pc->next = (LinkList)malloc(sizeof(term));
pc = pc->next;
pc->base = pb->base;
pc->expn = pb->expn;
pb = pb->next;
continue;
}
}
pc->next = NULL;
}
void subPolyn(LinkList La, LinkList Lb, LinkList &Lc)
{
LinkList pa, pb, pc;
pc = (LinkList)malloc(sizeof(term));
Lc = pc;
pa = La->next;
float x;
pb = Lb->next;
while (pb) {
if (!pa) {
break;
}
if (pa->expn == pb->expn) //指数相同
{
//EXPN=pa->expn;
x = pa->base - pb->base;
if (x)
{
pc->next = (LinkList)malloc(sizeof(term));
pc = pc->next;
pc->base = x;
pc->expn = pa->expn;
}
pa = pa->next;
pb = pb->next;
continue;
}
else if (pa->expn > pb->expn)
{
pc->next = (LinkList)malloc(sizeof(term));
pc = pc->next;
pc->base = pa->base;
pc->expn = pa->expn;
pa = pa->next;
continue;
}
else if (pa->expn < pb->expn)
{
pc->next = (LinkList)malloc(sizeof(term));
pc = pc->next;
pc->base = pb->base;
pc->expn = pb->expn;
pb = pb->next;
continue;
}
}
pc->next = NULL;
}
int main() {
polynomial Pa,Pb,Pc,Pd;
createPolyn(Pa, 5);//初始化并创建多项式链表Pa
createPolyn(Pb, 3);//初始化并创建多项式链表Pb
display(Pa);
display(Pb);
printf("执行多项式相加以后\n");
addPolyn(Pa, Pb, Pc);
display(Pc);
printf("执行多项式相减以后\n");
subPolyn(Pa, Pb, Pd);
display(Pd);
return 0;
}
参考资源:
https://blog.csdn.net/qq_37623612/article/details/80312121