【数据结构 链表的应用】一元多项式相加及相乘 和对问题的分析

版权声明:转载请注明出处 http://blog.csdn.net/irean_lau。 https://blog.csdn.net/Irean_Lau/article/details/45584267

一元多项式相乘,需要将一个多项式的每一项的指数与另一个多项式的每一项的指数相乘。


代码实现:



头文件以及函数声明:


#ifndef _POLYN_H  
#define _POLYN_H  

#include<iostream>  
#include <malloc.h>
#include <stdio.h>

using namespace std;
#define _CRT_SECURE_NO_DEPRECATE
#define NULL 0

typedef struct NODE {
	float  coef;                  //系数
	int    expn;                  //指数
	struct NODE *next;
}NODE;


NODE *Creat(int n);
void print(NODE *head);
NODE *AddPolyn(NODE *head1, NODE *head2);
NODE *Delfirst(NODE *head, NODE *q);
void InsertBefore(NODE *p1, NODE *p2);
int  compare(int a, int b);
NODE *CreatLinkList(int n);
void menu();
int inputnum(int n);
//NODE *reverse(NODE *head);
NODE *MulPolyn(NODE *A, NODE *B);
bool SortDown(NODE *head);//排序
bool SortUp(NODE *head);//排序
void Judge(NODE *head,char x);

#endif 

函数定义:

#include "polyn.h"
#include<iostream> 

void menu()
{
	cout << "								   " << endl;
	cout << "**********************************" << endl;
	cout << "* [0] quit_system                *" << endl;
	cout << "* [1] polynadd [2] polynmul      *" << endl;
	cout << "**********************************" << endl;
	cout << "plese chose:>";
}

int inputnum(int n)
{
	while (n <= 0 )
	{
		cout<<"输入的数值有误,请确认输入的数值为大于0的整数!: ";
		cin>>n;
	}
	return n;
}

/*创建链表*/
NODE *Creat(int n)
{
	NODE *current, *previous, *head;
	int i;
	head = (NODE *)malloc(sizeof(NODE)); /*创建头结点*/
	previous = head;

	for (i = 0; i < n; i++)
	{
		current = (NODE *)malloc(sizeof(NODE));

		cout<<"请输入系数和指数 : ";
		cin >> current->coef;
		cin>>current->expn;

		previous->next = current;
		previous = current;
	}
	previous->next = NULL;
	return head;
}



/*排序*/
bool SortDown(NODE *head)
{
	NODE* p = NULL;
	NODE* q = NULL;
	for (p = head->next; p != NULL; p = p->next)
	{
		for (q = p->next; q != NULL; q = q->next)
		{
			if (p->expn < q->expn)
			{
				p->expn = p->expn   ^    q->expn;
				q->expn = p->expn   ^    q->expn;
				p->expn = p->expn   ^    q->expn;

				p->coef = p->coef   +    q->coef;
				q->coef = p->coef   -    q->coef;
				p->coef = p->coef   -    q->coef;
			}
		}
	}
	return true;
}

bool SortUp(NODE *head)
{
	NODE* p = NULL;
	NODE* q = NULL;
	for (p = head->next; p != NULL; p = p->next)
	{
		for (q = p->next; q != NULL; q = q->next)
		{
			if (p->expn > q->expn)
			{
				p->expn = p->expn   ^    q->expn;
				q->expn = p->expn   ^    q->expn;
				p->expn = p->expn   ^    q->expn;

				p->coef = p->coef + q->coef;
				q->coef = p->coef - q->coef;
				p->coef = p->coef - q->coef;
			}
		}
	}
	return true;
}


/*比较*/
int compare(int a, int b)
{
	if (a < b)
		return -1;
	else if (a > b)
		return 1;
	else
		return 0;
}

/*删除结点q*/
NODE *Delfirst(NODE *p1, NODE *q)
{
	p1->next = q->next;
	return (q);
}

/*插入结点,引入结点p,可以让p插入到p2和p1之间*/
void InsertBefore(NODE *p1, NODE *p2)
{
	NODE *p;
	p = p1->next;
	p1->next = p2;
	p2->next = p;
}

/*打印,为了美观程序分开打印*/
void print(NODE *head)
{
	SortDown(head);
	NODE *cur;

	cur = head->next;
	while (cur->next != NULL)
	{
		cout<<cur->coef<<"*x^"<<cur->expn<< " + ";
		cur = cur->next;
	}
	cout << cur->coef << "*x^" << cur->expn ;
	cout << endl;
}



/*一元多项式的相加,总体考虑,可分qa的指数比qb小,或等于pb(如果系数相加等于0和不等于0),或大于pb
里面由InsertBefore和Delfirst两个小模块组成一部分*/
NODE  *AddPolyn(NODE *head1, NODE *head2)
{


	NODE *ha, *hb, *qa, *qb;
	int a, b;
	float sum;
	ha = head1;                  /*ha和hb指向头结点*/
	hb = head2;
	if (ha != NULL)              //head1判断是否为空
	{
		if (hb == NULL)
		{
			return head1;
		}
		else
		{

			qa = ha->next;            /*qa和qb指向头结点的下一个结点*/
			qb = hb->next;
			while (qa && qb)             /*qa和qb均非空*/
			{
				a = qa->expn;
				b = qb->expn;
				switch (compare(a, b)) {
				case -1:              /*qa->expn < qb->expn*/
					ha = qa;
					qa = qa->next;
					break;
				case 0:
					sum = qa->coef + qb->coef;  /*系数的和*/
					if (sum != 0.0) {            /*如果不是0.0*/
						qa->coef = sum;         /*改变系数*/
						ha = qa;
					}
					else {
						free(Delfirst(ha, qa));
					}
					free(Delfirst(hb, qb));
					qa = ha->next;
					qb = hb->next;              /*qb释放后要重新赋值*/
					break;
				case 1:                        /*如果qa-> expn > qb -> expn*/
					Delfirst(hb, qb);
					InsertBefore(ha, qb);       /*把qb插入到ha下一个结点之前*/
					qb = hb->next;
					ha = ha->next;
					break;
				}
			}
		}
		if (qb)
			ha->next = qb;                  /*插入剩余的pb*/
		free(head2);
		return head1;
	}

	else
	{
		return head2;
	}

}


/*实现相加及创建多个一元多项式 */
NODE *CreatLinkList(int n)
{
	int i, a;
	char x;
	NODE *head[100], *headsum;  //长度为100的数组,足够长了吧应该
	headsum = NULL;
	for (i = 1; i <= n; i++)
	{

		cout << "\n请输入第" << i << "个多项式的数目: ";
		//scanf_s("%d", &a);
		cin >> a;

		a = inputnum(a);

		cout << "(请按指数大小依次输入系数和指数)" << endl;

		head[i] = Creat(a);//创建以head[i]为头结点的链表
		cout << "下式是你所输入的第" << i << "个多项式: " << endl;
		print(head[i]);

		cout << "请确认输入是否正确,若有误请输入n重新输入上式,正确请输入y确认(小写):"; //确认输入
		cin >> x;

		if (x == 'n')   //如果有误则更正
		{
			if (i == 1)   //如果第一项有误i不减,将第一个一元多项式清空
			{
				head[i] = NULL;
			}
			else{      //如果不是第一项,将已输入的多项式清空,并i减1        
				head[i] = NULL;
				//			i--;
			}

		}

		headsum = AddPolyn(headsum, head[i]);//将各链表相加

		if (x == 'n') //如果第一项有误i--
		{
			i--;
		}
	}
	return headsum;
}



/*
一元多项式的相乘,需要将一个多项式的每一项的指数与另一个多项式的每一项的指数相加,并将系数相乘
*/

NODE *MulPolyn(NODE *A, NODE *B)
{
	NODE *pa, *pb, *pc, *u, *head;
	int k = 0;
	int maxExp = 0;//maxExp 为两个链表指数和的最大值
	float coef = 0.0;
	head = (NODE *)malloc(sizeof(NODE));

	if (!head)
		return NULL;

	head->coef = 0.0;
	head->expn = 0;
	head->next = NULL;

	SortDown(A);
	SortDown(B);

	if (A->next != NULL && B->next != NULL)
		maxExp = (A->next->expn) + (B->next->expn);
	else
		return head;
	pc = head;
	SortUp(B);
	for (k = maxExp; k >= 0; k--)   /*确定多项式乘积的指数范围为0到maxExp*/
	{
		pa = A->next;
		while (pa != NULL && pa->expn > k)  /*找到Pa位置*/
			pa = pa->next;
		pb = B->next;

		while (pb != NULL && pa != NULL  && (pa->expn + pb->expn) < k)
			/*如果和小于K,pb移动到下一个节点*/
			pb = pb->next;
		coef = 0.0;

		while (pa != NULL && pb != NULL)
		{
			if ((pa->expn) + (pb->expn) == k)
			{
				coef += (pa->coef) * (pb->coef);
				pa = pa->next;
				pb = pb->next;
			}
			else if ((pa->expn) + (pb->expn) > k)  /*如果和大于K,pa移动到下一个节点*/
				pa = pa->next;
			else
				pb = pb->next;					/*如果和小于K,pb移动到下一个节点*/
		}

		if (coef != 0.0)
		{
			/*如果系数不为0,则生成新的节点,并将系数和指数分别赋值给新节点,并插入到链表中*/
			u = (NODE*)malloc(sizeof(NODE));
			u->coef = coef;
			u->expn = k;
			u->next = pc->next;
			pc->next = u;
			pc = u;
		}
	}

	SortDown(B);
		return head;
}

主函数:

#include "polyn.h"

void main(int n)
{
	char y;
	NODE *headsum;
	NODE *headmul;
	int select = 1;

	NODE *A;
	NODE *B;
	int NumA = 0;
	int NumB = 0;

	menu();
	cin >> select;
	switch (select)
	{

	case 1:
		/*相加*/
		cout << "****************************欢迎使用一元多项式相加程序******************" << endl;

		cout << "请输入多项式的数目(大于0的整数): ";
		cin >> n;
		n = inputnum(n);

		headsum = CreatLinkList(n);

		cout << endl;
		cout << "相加后的多项式的显示 : " << endl;

		print(headsum);
		cout << "****************************谢谢使用***********************************" << endl;
		cout << "继续?请输入(y/n): ";
		cin >> y;
		if (y == 'y')
		{
			main(1);
		}
		break;



	case 2:
		/*相乘*/
		cout << "****************************欢迎使用两个一元多项式相乘程序******************" << endl;
		
		cout << "请输入第1个多项式的数目:";
		cin >> NumA;
		cout << "(请按指数大小依次输入系数和指数)" << endl;
		A = Creat(NumA);
		cout << "下式是你所输入的第1个多项式: " << endl;
		print(A);


		cout << "请输入第2个多项式的数目:";
		cin >> NumB;
		cout << "(请按指数大小依次输入系数和指数)" << endl;
		B = Creat(NumB);
		cout << "下式是你所输入的第1个多项式: " << endl;
		print(B);

		headsum = MulPolyn(A, B);
		

		cout << endl;
		cout << "相乘后的多项式的显示 : " << endl;

		print(headsum);

		cout << "****************************谢谢使用***********************************" << endl;
		cout << "继续请输入(y/n): ";
		cin >> y;
		if (y == 'y')
		{
			main(1);
		}
		break;
		break;

	default:
		break;
		
	}
}

	

运行结果如下:



展开阅读全文

没有更多推荐了,返回首页