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

📄 polylist.cpp

📁 关于多项式相加的小程序
💻 CPP
字号:
// PolyList.cpp: implementation of the CPolyList class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PolyAdd.h"
#include "PolyList.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPolyList::CPolyList()
{
	head = NULL;
	p = head;
}

CPolyList::~CPolyList()
{

}

void CPolyList::InsertItem(int coef, int exp)
{
	p = 0;
	q = head;
	if( head != 0 )
		p = head->next;

	//创建项节点
	pnode *s=new pnode;
	if( coef == 0)
		return;
	s->node.coef=coef;
	s->node.exp=exp;
	s->next = NULL;

	//按指数排序插入
	if(q!=NULL)
	{
		while(p!= NULL && (p->node.exp < s->node.exp))
		{
			q = p;
			p = p->next;	
		}
		if( p == 0 )
		{
			if(q == head)
			{
				if(q->node.exp < s->node.exp)
				{
					p = q;
					q = s;
					q->next = p;
					head = q;
				}
				else if(q->node.exp < s->node.exp)
				{
					p = s;
					q->next = p;
				}
				else 
				{
					q->node.coef += s->node.coef;
					delete(s);
				}	
			}
			else
			{
				p = s;
				q->next = p;	
			}
		}
		else if(q->node.exp < s->node.exp)
		{
			head = s;
			s->next = q;
		}
		else if(q->node.exp == s->node.exp)
		{
			q->node.coef += s->node.coef;
				delete(s);
		}
		else
		{
			if(p->node.exp < s->node.exp)
			{
				s->next = p;
				q->next = s;
			}
			else if(p->node.exp == s->node.exp)
			{
				p->node.coef += s->node.coef;
				delete(s);
			}			
		}
	}	
	else
	{
		p = s;
		head = p;
	}
}

CString CPolyList::ShowPoly()
{ 
	CString strPoly;
	p = head;
	CString str = "";
	while( p!=NULL )
	{
		char str1[20],str2[20];
		CString a=itoa(p->node.coef , str1, 10 );
		CString b=itoa(p->node.exp , str2, 10 );
		str=a+"*x^"+b+"+";
		strPoly += str;
		p = p->next;
	}
	int len = strPoly.GetLength();
	return strPoly.Mid(0, len-1);
	 
}

⌨️ 快捷键说明

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