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

📄 usexml.cpp

📁 在任务级并行平台P2HP上开发的demo应用
💻 CPP
字号:
// UseXML.cpp: implementation of the UseXML class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Perm.h"
#include "UseXML.h"
#include "tinyxml.h"
#include <IOSTREAM>


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

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

UseXML::UseXML():isInited(false){
}

UseXML::~UseXML(){
}

int UseXML::readXML(char *location){	
	TiXmlDocument doc(location);
	bool loadOkay = doc.LoadFile();
	if ( !loadOkay ){
		cout<<"Cann't find the file"<<endl;
		exit( 1 );
	}
	TiXmlNode* node = NULL;
	TiXmlElement* rootElement = NULL;
	TiXmlElement* itemElement = NULL;
	node = doc.FirstChild( "simulation" );
	rootElement = node->ToElement();


	//begin read the content of the file
	node = rootElement->FirstChildElement();
	itemElement = node->ToElement();
	GlobePar::sequence = itemElement->Attribute( "value" );
	GlobePar::length = atoi(itemElement->Attribute( "length" ));
	GlobePar::bestEnergy = atoi(itemElement->Attribute( "bestEnergy" ));

	//input the paramter
	itemElement = itemElement->NextSiblingElement();
	GlobePar::c = atoi(itemElement->Attribute( "c" ));
	GlobePar::z0 = atoi(itemElement->Attribute( "z0" ));
	GlobePar::Exp = atoi(itemElement->Attribute( "exp" ));
	double tempValue;
	sscanf( itemElement->Attribute( "c0" ), "%lf", &tempValue );
	GlobePar::c0 = tempValue;
	GlobePar::step = atoi(itemElement->Attribute( "step" ));

	//get number of ruond which the simulation will do
	itemElement = itemElement->NextSiblingElement();
	GlobePar::round = atoi(itemElement->Attribute( "value" ));

	//get the status of the task
	itemElement = itemElement->NextSiblingElement();
	if( strcmp( itemElement->Attribute( "value" ), "initializtion" ) != 0 ){
		isInited = true;
		itemElement = itemElement->NextSiblingElement();
		int length = atoi(itemElement->Attribute( "size" ));
		//get the weight
		itemElement = itemElement->FirstChildElement();
		for(int i=0; i<length; i++){
			sscanf( itemElement->Attribute( "weight" ), "%lf", &tempValue );
			averageOfWei.push_back(tempValue);
			itemElement = itemElement->NextSiblingElement();
		}		
	}
	return 0;
}




int UseXML::outputXML(char *location, GlobePar &globePar, Simulation &simulation){
	char buffer[200];

	//initial
	TiXmlDocument doc( location );
	TiXmlDeclaration decl( "1.0", "GB2312" , "yes" );
	TiXmlElement root( "simulation" );
	root.SetAttribute( "lab" , "cgcl in hust" );
	root.SetAttribute( "platform" , "p2hp" );
	root.SetAttribute( "developer" , "liu yu" );

	//put the information into the sequence
	TiXmlElement sequence( "sequence" );
	sequence.SetAttribute( "value", GlobePar::sequence );
	sequence.SetAttribute("length", GlobePar::length);
	sequence.SetAttribute("bestEnergy", GlobePar::bestEnergy);
	root.InsertEndChild(sequence);

	//put the information into the parameter
	//<parameter c="1" z0="1" exp="18" c0="50000" step="20000" />
	TiXmlElement parameter( "parameter" );
	parameter.SetAttribute( "c" , GlobePar::c );
	parameter.SetAttribute( "z0" , GlobePar::z0 );
	parameter.SetAttribute( "exp" , GlobePar::Exp );
	sprintf( buffer, "%e" , GlobePar::c0 );
	parameter.SetAttribute( "c0", buffer );
	parameter.SetAttribute( "step", GlobePar::step );
	root.InsertEndChild(parameter);

	//put the number of the round into the parameter
	//<round value="1" /> 
	TiXmlElement round( "round" );
	round.SetAttribute( "value" , GlobePar::round );
	root.InsertEndChild(round);

	//status of the simulation
	//<status value="initializtion" /> 
	TiXmlElement status( "status" );
	status.SetAttribute( "value" , "running" );
	root.InsertEndChild(status);

	//input the weight
	TiXmlElement weight( "weight" );
	weight.SetAttribute( "size" , GlobePar::length );
	for(int i=0; i<GlobePar::length; i++){
		TiXmlElement node( "node" );
		node.SetAttribute( "index" , i+1 );
		sprintf( buffer, "%e", globePar.averageOfWei[i] );
		node.SetAttribute( "weight" , buffer);
		weight.InsertEndChild(node);
	}
	root.InsertEndChild(weight);

	//input the structure
	TiXmlElement structure( "structure" );
	structure.SetAttribute( "energy" , simulation.bestRecoder );
	for(i=0; i<GlobePar::length; i++){
		TiXmlElement node( "node" );
		node.SetAttribute( "x" , simulation.bestResult[i].x );
		node.SetAttribute( "y" , simulation.bestResult[i].y );
		node.SetAttribute( "z" , simulation.bestResult[i].z );
		structure.InsertEndChild(node);
	}
	root.InsertEndChild(structure);

	doc.InsertEndChild(decl);
	doc.InsertEndChild(root);
	doc.SaveFile();
	return 0;
}

⌨️ 快捷键说明

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