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

📄 infotable.cpp

📁 某个实验事编写粗糙集智能信息处理的程序
💻 CPP
字号:
// InfoTable.cpp: implementation of the InfoTable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "InfoTable.h"

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

InfoTable::InfoTable()
{
	InfoMax=InfoMin=0;
	nCount=0;//记录该链表中的元素个数
	head=tail=NULL;
}

InfoTable::~InfoTable()
{
	InfoCont* p;
	p=head;
	while(p)
	{
		head=head->next;
		delete p;
		p=head;
	}
}

double InfoTable::GetMaxGap()			//获得最大差值
{
	return InfoMax-InfoMin;
}

double InfoTable::GetInfo(int index)
	//获得值为index的相应的信息容量K(D|Ck=c} ,若找不到,则返回0
{							
	InfoCont* p=head;
	while(p)
	{
		if(p->nIndex==index)
			return p->fInfo;
		p=p->next;
	}
	return 0;
}
void InfoTable::AddItem(int index,double info)//加入元素不排序
{
	nCount++;
	if(nCount==1)
	{	InfoMax=InfoMin=info; indexMax=indexMin=index;}
	else//nCount大于1时
		if(info>InfoMax) 
		{ InfoMax=info; indexMax=index;}
		else
			if(info<InfoMin) 
			{	InfoMin=info; indexMin=index;}
	InfoCont* p;
	try
	{
		p=new InfoCont;
	}
	catch(CMemoryException * e)
	{
    	::MessageBeep(MB_ICONHAND);
       	AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
		e->Delete();
    	return ;
	} 
	p->fInfo=info;
	p->next=NULL;
	p->nIndex=index;

	if(head==NULL)
		head=p;
	else //直接插到末尾
		tail->next=p;
	tail=p;
}

void InfoTable::output()		//依次输出各值;
{
	InfoCont* p=head;
	while(p)
	{
		TRACE("%f ",p->fInfo);
		p=p->next;
	}
	TRACE(" max: %f min: %f\n",InfoMax,InfoMin);
}

void InfoTable::AddInOrder(int index,double info)
{//加入属性值为index,信息量为info节点,并按info从小到大的顺序排序
	nCount++;
	if(nCount==1)
	{	
		InfoMax=InfoMin=info;
		indexMax=indexMin=index;
	}
	else
		if(info>InfoMax) 
		{
			InfoMax=info;
			indexMax=indexMin=index;//添加
		}
		else if(info<InfoMin)
		{
			InfoMin=info;
			indexMin=index;//添加
		}
	InfoCont *p;
	try
	{	
		p=new InfoCont;
	}
	catch(CMemoryException * e)
	{
		::MessageBeep(MB_ICONHAND);
		AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
		e->Delete();
		return ;
	} 

	p->fInfo=info;
	p->nIndex=index;
	p->next=NULL;
	if(head==NULL)
	{
		head=tail=p; return;
	}

	if(head->fInfo>info)
	{//头部节点值大于要添加节点
		p->next=head;
		head=p;
		return ;
	}
	if(tail->fInfo<info)
	{//尾部节点值小于要添加节点
		tail->next=p;
		tail=p;
		return;
	}
	InfoCont* temp=head;
	while(temp->next && temp->next->fInfo<info)
	{
		temp=temp->next;
	}
	p->next=temp->next;
	temp->next=p;//插入节点
}

⌨️ 快捷键说明

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