思路:最刚开始的看到这个题想的是用map然后遍历的方式来做,考虑到老师不让用stl,就想到另一种方法,做成哈希表的方式。

做完之后老师说让用链表做,这里采用的尾插法做的。

代码:

哈希表方法:


#include<stdio.h> 
int a[100],b[100],jia[100],jian[100];
int main()
{
	int n,m;
	int s,z;
	printf("请输入多项式A的项数:\n");
	scanf("%d",&n);
	printf("每组输入两个数系数和指数:\n");
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&z,&s);
		a[s]+=z;
	}
	printf("请输入多项式B的项数:\n");
	scanf("%d",&m);
	printf("每组输入两个数系数和指数:\n");
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&z,&s);
		b[s]+=z;
	}
	
	for(int i=0;i<100;i++)
	{
		jia[i] = a[i] + b[i];
	}
	for(int i=0;i<100;i++)
	{
		jian[i] = a[i] - b[i];
	}
	
	printf("加的结果:Pn(x)=");
	int sign;
	int first = 0;
	for(int i = 100-1;i>=0;i--)
	{
		if(jia[i]!=0)
		{
			sign = i;
			break;
		}
	}
	for(int i = 0;i<100;i++)
	{
		if(jia[i]!=0)
		{	
			if(first==0)
			{
				printf("%dx(%d)",jia[i],i);
				first = 1;
			}else
			{
				if(jia[i]<0)
					printf("%dx(%d)",jia[i],i);
				else
					printf("+%dx(%d)",jia[i],i);
			}
		}
	}
	printf("\n");
	
	printf("减的结果:Pn(x)="); 
	for(int i = 100-1;i>=0;i--)
	{
		if(jian[i]!=0)
		{
			sign = i;
			break;
		}
	}
	first = 0;
	for(int i = 0;i<100;i++)
	{
		if(jian[i]!=0)
		{	
			if(first==0)
			{
				printf("%dx(%d)",jian[i],i);
				first = 1;
			}else
			{
				if(jian[i]<0)
					printf("%dx(%d)",jian[i],i);
				else
					printf("+%dx(%d)",jian[i],i);
			}
		}
	}
	printf("\n");
} 


尾插法方法:


#include<bits/stdc++.h>
using namespace std;
struct element{
	int zhishu;
	int xishu;
	struct element *next;
};
//用来指向头部 
struct element *a = new element;
struct element *b = new element;
struct element *jia = new element;
struct element *jian = new element;
//用来指向尾部,插入数据采用尾插法 
struct element *atail = new element;
struct element *btail = new element;
struct element *jiatail = new element;
struct element *jiantail = new element;
//选择排序 
void select_sort(element *l)
{
	for(element *i = l;i!=NULL;i = i->next)
	{
		for(element *j = i->next;j!=NULL;j = j->next)
		{
			if(i->zhishu>j->zhishu)
			{
				int tz = i->zhishu;
				int tx = i->xishu;
				i->zhishu = j->zhishu;
				i->xishu = j->xishu;
				j->zhishu = tz;
				j->xishu = tx;
			}
		}
	}
}
int main()
{
	atail = a,btail = b,jiatail = jia,jiantail = jian;
	
	a->next = NULL;
	b->next = NULL;
	
	int n,m,x,z,sign=0;
	cout<<"请输入多项式A的项数"<<endl;
	cin>>n;
	cout<<"每行输入两个数(系数和指数)"<<endl;
	for(int i=1;i<=n;i++)
	{
		cin>>x>>z;
		element *temp = new element;
		temp->xishu =  x;
		temp->zhishu = z;
		temp->next = NULL;
		atail->next = temp;
		atail = atail->next; 
		
		element *temp1 = new element;
		temp1->xishu =  x;
		temp1->zhishu = z;
		temp1->next = NULL;
		jiatail->next = temp1; 
		jiatail = jiatail->next; 
		
		element *temp2 = new element;
		temp2->xishu =  x;
		temp2->zhishu = z;
		temp2->next = NULL;
		jiantail->next = temp2; 
		jiantail = jiantail->next; 
	}
	cout<<"请输入多项式B的项数"<<endl;
	cin>>m; 
	cout<<"每行输入两个数(系数和指数)"<<endl;
	for(int i=1;i<=m;i++)
	{
		cin>>x>>z;
		element *temp = new element;
		temp->xishu =  x;
		temp->zhishu = z;
		temp->next = NULL;
		btail->next = temp; 
		btail = btail->next;
	}
	//相加运算 
	element *tb = b->next;
	while(tb!=NULL)
	{
		int tx = tb->xishu;
		int tz = tb->zhishu;
		element *tjia = jia;
		int flag = 0; 
		while(tjia->next!=NULL)
		{
			tjia = tjia->next;
			if(tjia->zhishu == tz)
			{
				tjia->xishu += tx;
				flag = 1;
				break;
			}
		}
		
		if(flag==0)
		{
			element *newjia = new element;
			newjia->xishu = tx;
			newjia->zhishu = tz;
			newjia->next = NULL;
			tjia->next = newjia;
		}
		tb = tb->next;
	}
	select_sort(jia->next);
	cout<<"多项式A+B: Pn(x)=";
	element *l = jia->next;
	int first = 0;
	while(l!=NULL)
	{
		if(first==0&&l->xishu!=0)
		{
			cout<<l->xishu<<'x'<<'('<<l->zhishu<<')'; 
			first = 1;
		}else
		{
			if(l->xishu>0&&l->xishu!=0)
				cout<<'+'<<l->xishu<<'x'<<'('<<l->zhishu<<')'; 
			else if(l->xishu!=0)
				cout<<l->xishu<<'x'<<'('<<l->zhishu<<')'; 
		}
		l = l->next;
	} 
	cout<<endl;
	//相减运算 
	tb = b->next;
	while(tb!=NULL)
	{
		int tx = tb->xishu;
		int tz = tb->zhishu;
		element *tjian = jian;
		int flag = 0; 
		while(tjian->next!=NULL)
		{
			tjian = tjian->next;
			if(tjian->zhishu == tz)
			{
				tjian->xishu -= tx;
				flag = 1;
				break;
			}
		}
		if(flag==0)
		{
			element *newjia = new element;
			newjia->xishu = -tx;
			newjia->zhishu = tz;
			newjia->next = NULL;
			tjian->next = newjia;
		}
		tb = tb->next;
	}
	select_sort(jian->next);
	cout<<"多项式A-B: Pn(x)=";
	l = jian->next;
	first = 0;
	while(l!=NULL)
	{
		if(first==0&&l->xishu!=0)
		{
			cout<<l->xishu<<'x'<<'('<<l->zhishu<<')'; 
			first = 1;
		}else
		{
			if(l->xishu>0&&l->xishu!=0)
				cout<<'+'<<l->xishu<<'x'<<'('<<l->zhishu<<')'; 
			else if(l->xishu!=0)
				cout<<l->xishu<<'x'<<'('<<l->zhishu<<')'; 
		}
		l = l->next;
	} 
	cout<<endl;
}