⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 多项式相加.cpp

📁 采用堆栈的原理
💻 CPP
字号:
//多项式相加.cpp
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>

void jiemian(void);
void input(struct poly*,struct poly*,struct poly*,int );
void poly_add(void);
void show_answer(void);
void display_func(struct poly*,struct poly*,int);

struct poly
{
	int coef;
	int exp;
	struct poly *next;
};

struct poly *ptr,*head1,*head2,*this_n1,*this_n2,*prev1,*prev2,*eq_h1,*eq_h2,*ans_h;
static int a=1;
static int b=2;

void main()
{
	head1=(struct poly*)malloc(sizeof(struct poly));
	head1->next=NULL;
	head2=(struct poly*)malloc(sizeof(struct poly));
	head2->next=NULL;

	jiemian();

	cout<<"\n\n\t请输入第一个多项式:"<<endl;
	input(prev1,head1,this_n1,a);

	cout<<"\n\n\t请输入第二个多项式:"<<endl;
	input(prev2,head2,this_n2,b);


	poly_add();
	show_answer();

}

void input(struct poly *prev,struct poly *head,struct poly *this_n,int judge1)
{
	int count=0;
	char temp_coef[4],temp_exp[4];
	do
	{
		ptr=(struct poly*)malloc(sizeof(struct poly));
		ptr->next=NULL;

		cout<<"\n\ncoef: ";
	//	gets(temp_coef);
		cin>>temp_coef;
		cout<<"\nexp: ";
	//	gets(temp_exp);
		cin>>temp_exp;

		if(atoi(temp_coef)!=-1&&atoi(temp_exp)!=-1)
		{
			ptr->coef=atoi(temp_coef);
			ptr->exp=atoi(temp_exp);
		}
		else
		{
			break;
		}
		//插入数据
		prev=head;
		this_n=head->next;

		while((this_n!=NULL)&&(this_n->exp>ptr->exp))
		{
			prev=this_n;
			this_n=this_n->next;
		}
		ptr->next=this_n;
		prev->next=ptr;
	}while(atoi(temp_exp) != -1);

	display_func(this_n,head,judge1);
}

void poly_add(void)
{
	struct poly *prev;
	prev=NULL;
	this_n1=head1->next;
	this_n2=head2->next;

	while(this_n1!=NULL || this_n2!=NULL)
	{
		ptr=(struct poly*)malloc(sizeof(struct poly));
		ptr->next=NULL;

		if(this_n1!=NULL && (this_n2==NULL || this_n1->exp > this_n2->exp))
		{
			ptr->coef=this_n1->coef;
			ptr->exp=this_n1->exp;
			this_n1=this_n1->next;
		}
		else
		{
			//第一个多项式指数小于第二个多项式
			if(this_n1 == NULL || this_n1->exp < this_n2->exp)
			{
				ptr->coef=this_n2->coef;
				ptr->exp=this_n2->exp;
				this_n2=this_n2->next;
			}
			else        //两个多项式指数相等
			{
				ptr->coef=this_n1->coef + this_n2->coef;
				ptr->exp = this_n1->exp;
				if(this_n1 !=NULL)
					this_n1 = this_n1->next;
				if(this_n2 !=NULL)
					this_n2 = this_n2->next;
			}
			if(ptr->coef != 0)  //相加结果不等于0,则放入答案多项式中
			{
				if(ans_h == NULL)
					ans_h=ptr;
				else
					prev->next=ptr;
				prev=ptr;
			}
			else
			{
				free(ptr);
			}
		}
	}
}

void show_answer(void)
{
	struct poly *this_n;
	this_n=ans_h;
	cout<<"\n\n\t答案多项式: "<<endl;
	while(this_n!= NULL)
	{
		cout<<this_n->coef<<"x^"<<this_n->exp;
		
		if(this_n->next != NULL && this_n->next->coef >=0)
			cout<<"+";
		this_n=this_n->next;
	}
	cout<<endl;
}

void display_func(struct poly *this_n,struct poly *head,int judge2)
{
	int count=0;
	if(head->next == NULL)
	{
		cout<<"没有多项式记录"<<endl;
	}
	else
	{
		system("cls");
		jiemian();
		if(judge2==1)
		{
			cout<<"\n\t第一个多项式: "<<endl;
		}
		else
		{
			cout<<"\n\t第二个多项式: "<<endl;
		}
//		cout<<"\n\n\t     coef                  exp"<<endl;
//		cout<<"\n\t───────────────────"<<endl;
		this_n=head->next;
		while(this_n != NULL)
		{
		/*	cout<<"\n\n\t     "<<this_n->coef<<"                "<<this_n->exp<<endl;
			count++;
			this_n=this_n->next;
			if(count%20 == 0)
				getch();*/
			cout<<this_n->coef<<"x^"<<this_n->exp;		
			if(this_n->next != NULL && this_n->next->coef >=0)
				cout<<"+";
			this_n=this_n->next;
		}
		cout<<"\n\t───────────────────"<<endl;
//		printf("Total %d record(s) found\n",count);

	}
	
}

void jiemian(void)
{
	cout<<endl<<endl<<endl;
	cout<<"\t        ┌───────────────────────┐"<<endl;
	cout<<"\t        │        ******多  项  式  相  加******        │"<<endl;
	cout<<"\t        └───────────────────────┘"<<endl;
	cout<<endl;
	cout<<"\t\t             ┌────────┐"<<endl;
	cout<<"\t\t             │ 使用格式: ax^b │"<<endl;
	cout<<"\t\t             └────────┘"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -