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

📄 calculate.cpp

📁 此程序实现了类似protel电路画图程序。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Calculate.cpp: implementation of the CCalculate class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "pointtest.h"
#include "Calculate.h"

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

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

CCalculate::CCalculate(CJointList *p,int datum )
{
	m_nDatumMark=datum;
	m_pJointList=p;
	m_bIsNewX=false;
	for( int i=0;i<100;i++ )
	{
		m_pArrayPower[i] = NULL;
		m_pCopyPower[i] = NULL;
	}
}

CCalculate::~CCalculate()
{
	m_listOnePower.RemoveAll();
	POSITION pos=m_capaList.GetHeadPosition();
	while(pos!=NULL)
	{
		CAElement *pCapa=m_capaList.GetNext(pos);
		delete pCapa;
	}
	for(int i=0;i<100;i++)
	{
		if(m_pArrayPower[i])
		{
			delete m_pArrayPower[i];
			m_pArrayPower[i] = NULL;
		}
		if(m_pCopyPower[i])
		{
			delete m_pCopyPower[i];
			m_pCopyPower[i] = NULL;
		}
	}
}
void CCalculate::EstablishEquation()
{
	if(m_listOnePower.IsEmpty())
	{

	}
	else
	{
		int i=0;
		CAJoint *pJoint;
		POSITION pos;
		pos=m_listOnePower.GetHeadPosition();
		while(pos!=NULL)
		{
			pJoint=m_listOnePower.GetAt(pos);
			m_pListElement.AddTail(&((pJoint->pElementList).m_pListElement));
			BuildMatrix(i,pJoint);
			m_listOnePower.GetNext(pos);
			i++;
			m_pListElement.RemoveAll();
		}
	}	
}
void CCalculate::BuildMatrix(int i,CAJoint *pJoint)
{

		CAElement *pElement;
		POSITION pos;
		pos=m_pListElement.GetHeadPosition();
//		pos=(pJoint->pElementList).m_pListElement.GetHeadPosition();
		while(pos!=NULL)
		{
			CAJoint *p1,*p2,*p;
			pElement=m_pListElement.GetNext(pos);
//			pElement=(pJoint->pElementList).m_pListElement.GetNext(pos);
			p1=(pElement->GetJointStruct())->pJoint1;
			p2=(pElement->GetJointStruct())->pJoint2;
			if(p1==pJoint)
			{
				p=p2;
			}
			else
			{
				p=p1;
			}
	//		p->SearchElecSource();
			if(p->IsHoldElecSource())
			{
				m_fPowerOfR[i]+=(p->GetVoltage())/(pElement->GetValue());
			}
			else
			{
				p->m_dReciprocalOfR[i]+=1/(pElement->GetValue());
			}
				pJoint->m_dReciprocalOfR[i]+=-1/(pElement->GetValue());
		}
}
void CCalculate::SetMatrixValue()
{
	if(m_pOnePowerJointList->m_pListJoint.IsEmpty())
	{

	}
	else
	{
		m_pOnePowerJointList->AdjustJoint(m_pOnePowerJointList->m_pOldList);
		m_listOnePower.RemoveAll();
		m_listOnePower.AddTail(&(m_pOnePowerJointList->m_pListJoint));
		EstablishEquation();
		int i;
		int count1=m_listOnePower.GetCount();//此处的链表不包括连有电源的节点
		for(i=0;i<count1;i++)
		{
			int j=0;
			POSITION pos;
			CAJoint *pJoint;
			pos=m_listOnePower.GetHeadPosition();
			while(pos!=NULL)
			{
				pJoint=m_listOnePower.GetAt(pos);
				m_dMatrix[i][j]=pJoint->m_dReciprocalOfR[i];
				m_listOnePower.GetNext(pos);
				j++;
			}
			m_dMatrix[i][j]=m_fPowerOfR[i];
		}
	}
}
void CCalculate::Calculate()
{
	int k,i,j;
	SetMatrixValue();
	int nCalJointNum=m_listOnePower.GetCount();
	if(nCalJointNum>0)
	{
		x=new float[nCalJointNum];
		m_bIsNewX=true;
		for(i=0;i<nCalJointNum;i++)
		{
			x[i]=0;
		}
		int m=0;
		float a,b;
		for(k=0;k<nCalJointNum;k++)
		{
			if(m_dMatrix[k][m]!=0.0)
			{
				a=m_dMatrix[k][m];
				for(i=0;i<(nCalJointNum+1);i++)
				{
					m_dMatrix[k][i]=m_dMatrix[k][i]/a;
				}
				for(i=k+1;i<nCalJointNum;i++)
				{
					if(m_dMatrix[i][m]!=0)
					{
						b=m_dMatrix[i][m];
						for(j=m;j<(nCalJointNum+1);j++)
						{
							m_dMatrix[i][j]=m_dMatrix[i][j]/b;
							m_dMatrix[i][j]=m_dMatrix[i][j]-m_dMatrix[k][j];
						}	
					}
				}
			}
			m++;
		}


		for(i=nCalJointNum-1;i>=0;i--)
		{
			if(m_dMatrix[i][i]==1)
			{
				k=i;
				break;
			}
		}


		for(i=k;i>=0;i--)
		{
			x[i]=-m_dMatrix[i][k+1];
			if(i<k)
			{
				for(j=k;j>i;j--)
				{
					x[i]=x[i]-m_dMatrix[i][j]*x[j];
				}
			}
		}
	}
}
void CCalculate::RealCalculate()
{
	m_pJointList->CreateCopyJointList(m_pJointList->m_pOldList,m_pJointList->m_pSaveList);
	SavePowerInArray(m_pJointList->m_pOldList);
	CreateCopyPowerArray(m_pArrayPower,m_pCopyPower);
	m_pJointList->CreateCopyJointList(m_pJointList->m_pSaveList,m_pJointList->m_pCopyList);
	m_pJointList->CreateCopyJointList(m_pJointList->m_pSaveList,m_pJointList->m_pOldList);
	int nPowerNum = m_nPowerNum;
//	AddPowerResistance(m_pJointList->m_pSaveList);
	for(int l = 0;l< m_nPowerNum;l++)
	{
		
//		AddPowerResistance(m_pJointList->m_pOldList);
		SavePowerInArray(m_pJointList->m_pOldList);
		RemovePowerFromJoint(l);
		m_pJointList->DealWithSwitch(m_pJointList->m_pOldList);
		m_pJointList->DealWithAmpermeter(m_pJointList->m_pOldList);
		m_pJointList->DealWithVoltmeter(m_pJointList->m_pOldList);
		float fAverageOfR = m_pJointList->GetAverageOfResistance(m_pJointList->m_pOldList);
		BOOL bIsDiode = m_pJointList->IsExistSpecEl(ID_DIODE,m_pJointList->m_pOldList,&(m_pJointList->m_pDiodeList));
		if(bIsDiode)
		{
			m_pJointList->AllElsToOthers(ID_DIODE,ID_RESISTANCE,fAverageOfR,m_pJointList->m_pOldList);
			OnePowerCalculate(l);
			m_pJointList->CreateCopyJointList(m_pJointList->m_pCopyList,m_pJointList->m_pOldList);
		//	AddPowerResistance(m_pJointList->m_pSaveList);
//			AddPowerResistance(m_pJointList->m_pOldList);
			m_pJointList->IsExistSpecEl(ID_DIODE,m_pJointList->m_pOldList,&(m_pJointList->m_pDiodeList));
			SavePowerInArray(m_pJointList->m_pOldList);
			RemovePowerFromJoint(l);
			m_pJointList->DealWithSwitch(m_pJointList->m_pOldList);
			m_pJointList->DealWithAmpermeter(m_pJointList->m_pOldList);
			m_pJointList->DealWithVoltmeter(m_pJointList->m_pOldList);
			m_pJointList->AdjustPloarityOfDiode(l);
			m_pJointList->SetZeroVoltage(l,m_pJointList->m_pSaveList);
			OnePowerCalculate(l);
		}
		else
		{
			OnePowerCalculate(l);
		}
		m_pJointList->CreateCopyJointList(m_pJointList->m_pCopyList,m_pJointList->m_pOldList);
	}
	POSITION pos2=m_pJointList->m_pSaveList->GetHeadPosition();
	while(pos2!=NULL)
	{
		CAJoint *p10=m_pJointList->m_pSaveList->GetNext(pos2);
		float v=0;
		for(int i=0;i<nPowerNum;i++)
		{
			v+=p10->m_fRealVoltage[i];
		}
		p10->SetVoltage(v);
	}
	m_pJointList->SetAllElCurrent(m_pJointList->m_pSaveList);
	m_pJointList->SetVoltmeterValue(m_pJointList->m_pSaveList);
	m_pJointList->SetVoltOutput(&(m_pJointList->m_VoltList),m_pJointList->m_pSaveList);
	m_pJointList->SetAmperOutput(&(m_pJointList->m_AmperList),m_pJointList->m_pSaveList);
}
void CCalculate::SavePowerInArray(CList<CAJoint*,CAJoint*>*pList)//save all powers in a array (m_pArrayPower)
{
	m_pJointList->SetAllIsAllPowerInList(false,pList);
	m_nPowerNum=0;
	if(m_pArrayPower[0])
	{
		for(int i=0;i<100;i++)
		{
			if(m_pArrayPower[i])
			{
				delete m_pArrayPower[i];
				m_pArrayPower[i]=NULL;
			}
		}
		if(pList->IsEmpty())
		{
		}
		else
		{
			POSITION pos=pList->GetHeadPosition();
			while(pos!=NULL)
			{
				CAJoint *p=pList->GetAt(pos);
	//			p->SearchElecSource();
				if(p->IsHoldElecSource())
				{
					POSITION pos1=p->pElementList.m_pListElement.GetHeadPosition();
					while(pos1!=NULL)
					{
						CAElement *pE=p->pElementList.m_pListElement.GetNext(pos1);
						if(pE->GetType()==ID_POWER&&!pE->IsAsPower())
						{
							BOOL bIsParalleCo=DealParallelConnectionPower(pE,m_pArrayPower,pList);
							if(!bIsParalleCo)
							{
								float value = pE->GetValue();
								int index = pE->GetIndex();
								int order1 = pE->GetJointStruct()->pJoint1->GetOrder();
								int order2 = pE->GetJointStruct()->pJoint2->GetOrder();
								CAElement* pNewE = m_pJointList->NewElement( ID_POWER,value );
								pNewE->SetIndex( index );
								pNewE->SetOrderStruct( order1,order2 );
								m_pArrayPower[m_nPowerNum]=pNewE;//save all powers in array;
								m_nPowerNum++;
							}
							else pos1=p->pElementList.m_pListElement.GetHeadPosition();
							pE->SetIsAsPower(true);
						}
					}
				}
				pList->GetNext(pos);
			}
		}
		for( i=0;i<100;i++)
		{
			if(m_pArrayPower[i])
			{
				delete m_pArrayPower[i];
				m_pArrayPower[i]=NULL;
			}
		}
		CreateCopyPowerArray(m_pCopyPower,m_pArrayPower);
	}
	else
	{
		if(pList->IsEmpty())
		{
		}
		else
		{
			POSITION pos=pList->GetHeadPosition();
			while(pos!=NULL)
			{
				CAJoint *p=pList->GetAt(pos);
	//			p->SearchElecSource();
				if(p->IsHoldElecSource())
				{
					POSITION pos1=p->pElementList.m_pListElement.GetHeadPosition();
					while(pos1!=NULL)
					{
						CAElement *pE=p->pElementList.m_pListElement.GetNext(pos1);
						if(pE->GetType()==ID_POWER&&!pE->IsAsPower())
						{
							BOOL bIsParalleCo=DealParallelConnectionPower(pE,m_pArrayPower,pList);
							if(!bIsParalleCo)
							{
								float value = pE->GetValue();
								int index = pE->GetIndex();
								CAJoint* p1 = pE->GetJointStruct()->pJoint1;
								CAJoint* p2 = pE->GetJointStruct()->pJoint2;
								CAElement* pNewE = m_pJointList->NewElement( ID_POWER,value );
								pNewE->SetIndex( index );
								pNewE->SetOrderStruct(p1->GetOrder(),p2->GetOrder() );
								m_pArrayPower[m_nPowerNum]=pNewE;//save all powers in array;
								m_nPowerNum++;
							}
							else pos1=p->pElementList.m_pListElement.GetHeadPosition();
							pE->SetIsAsPower(true);
						}
					}
				}
				pList->GetNext(pos);
			}
		}
	}
	
}
void CCalculate::RemovePowerFromJoint(int k)
{
	//SavePowerInArray();
	//P0SITION pos=p->pElementList->m_pListElement.GetHeadPosition();
	for(int i=0;i<m_nPowerNum;i++)
	{
		if(i!=k&&m_pArrayPower[i]->GetType()==ID_POWER)
		{
			CAElement* pPower = m_pArrayPower[i];
			CAElement* pPInList = m_pJointList->GetElement(pPower,m_pJointList->m_pOldList);
			CAElement *pNewE = m_pJointList->NewElement(ID_INDUCTANCE,1);
			CAElement *pEInSave = m_pJointList->GetElement(pPInList,m_pJointList->m_pSaveList);
			pEInSave->SetIsAsInductor(true);
			CAJoint* p1 = pPInList->GetJointStruct()->pJoint1;
			CAJoint* p2 = pPInList->GetJointStruct()->pJoint2;
			int index   = pPInList->GetIndex();
			pNewE->SetIndex( index );
			POSITION posDel1 = p1->pElementList.m_pListElement.Find(pPInList,NULL);
			POSITION posDel2 = p2->pElementList.m_pListElement.Find(pPInList,NULL);
			p2->pElementList.m_pListElement.RemoveAt(posDel2);
			p1->pElementList.m_pListElement.RemoveAt( posDel1 );
			delete pPInList;
			pNewE->SetStructJoint(p1,p2);
			pNewE->SetOrderStruct(p1->GetOrder(),p2->GetOrder() );
			p1->pElementList.m_pListElement.AddTail(pNewE);
			p2->pElementList.m_pListElement.AddTail(pNewE);
		}
	}
	/*		CAJoint *p1=pPInList->GetJointStruct()->pJoint1;//power[i]'s joint
			CAJoint *p2=pPInList->GetJointStruct()->pJoint2;
			POSITION pos1=p1->pElementList.m_pListElement.GetHeadPosition();
			int nNoPower1=0,nNoPower2=0;//the unpower numbers of p1 and p2;
			CAElement *arrayP1[100],*arrayP2[100];
			for(int n=0;n<100;n++)
			{
				arrayP1[n]=NULL;
				arrayP2[n]=NULL;
			}
			//the unpower of p1 and p2
			//which joint will be Gotten rid of?
			while(pos1!=NULL)//p1?
			{
				CAElement *pE=p1->pElementList.m_pListElement.GetAt(pos1);
				if(pE->GetIndex()==m_pArrayPower[k]->GetIndex()||pE->GetType()!=ID_POWER)
				{
					arrayP1[nNoPower1]=pE;
					nNoPower1++;
				}
				p1->pElementList.m_pListElement.GetNext(pos1);
			}
			pos1=p2->pElementList.m_pListElement.GetHeadPosition();
			while(pos1!=NULL)//p2?
			{
				CAElement *pE=p2->pElementList.m_pListElement.GetAt(pos1);
				if(pE->GetIndex()==m_pArrayPower[k]->GetIndex()||pE->GetType()!=ID_POWER)
				{
					arrayP2[nNoPower2]=pE;
					nNoPower2++;
				}
				p2->pElementList.m_pListElement.GetNext(pos1);
			}
			if(nNoPower1>=2)
			{
				if(nNoPower2>=2)
				{
//					m_pArrayPower[i]->pRemove=NULL;
					POSITION posDel1=p1->pElementList.m_pListElement.Find(pPInList,NULL);
					POSITION posDel2=p2->pElementList.m_pListElement.Find(pPInList,NULL);
					p1->pElementList.m_pListElement.RemoveAt(posDel1);
					p2->pElementList.m_pListElement.RemoveAt(posDel2);
					delete pPInList;
				}
				else//get rid of p2
				{
			//		m_pArrayPower[i]->pRemove=p2;
			//		m_pArrayPower[i]->pNoRemove=p1;
					pos1=m_pOnePowerJointList->m_pListJoint.Find(p2,NULL);
					m_pOnePowerJointList->m_pListJoint.RemoveAt(pos1);
				//	int count22=p2->pElementList.m_pListElement.GetCount()-1;
				//	for(int j=0;j<nNoPower2;j++)
					POSITION pos22=p2->pElementList.m_pListElement.GetHeadPosition();
					while(pos22!=NULL)
					//for(int j=0;j<count22;j++)
					{
						CAElement *pE22=p2->pElementList.m_pListElement.GetNext(pos22);
						if(pE22!=pPInList)
						{
							ChangeElement(p1,p2,pE22);
						}
					//		ChangeElement(p1,p2,arrayP2[j]);

⌨️ 快捷键说明

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