tinyxml的使用例子

今天使用dev c++编程 下载了tinyxml试试,感觉不错

就留下了一段代码如下:

#include #include #include #include #include class RandGen { public: RandGen(); RandGen(unsigned long seedVal); public: void seed(unsigned long seedVal); unsigned long genRand(unsigned long max); int genRand(int min, int max); double genRand(double min, double max); double genRandSign(); double genRand01(); private: unsigned long genrand_int32(); private: // constants static const unsigned long N = 624; static const unsigned long M = 397; static const unsigned long MATRIX_A = 0x9908b0dfUL; static const unsigned long UPPER_MASK = 0x80000000UL; static const unsigned long LOWER_MASK = 0x7fffffffUL; private: //state data unsigned long stateVector_[N]; unsigned long stateVectorIndex_; }; const unsigned long RandGen::N; const unsigned long RandGen::M; const unsigned long RandGen::MATRIX_A; const unsigned long RandGen::UPPER_MASK; const unsigned long RandGen::LOWER_MASK; RandGen::RandGen() { seed(static_cast(std::time(0))); } RandGen::RandGen(unsigned long seedVal) { seed(seedVal); } void RandGen::seed(unsigned long seedVal) { stateVector_[0]= seedVal & 0xffffffffUL; for(stateVectorIndex_=1; stateVectorIndex_> 30)) + stateVectorIndex_); stateVector_[stateVectorIndex_] &= 0xffffffffUL; } } unsigned long RandGen::genRand(unsigned long max) { return genrand_int32()%max; } int RandGen::genRand(int min, int max) { return min + genrand_int32()%(max-min+1); } //mostly adapted from mt19937ar : double genrand_real1(void) double RandGen::genRand(double min, double max) { return min + ((genrand_int32()*(1.0/4294967295.0))*(max-min)); } double RandGen::genRandSign() { return genrand_int32()%2 == 0 ? -1.0 : 1.0; } //adapted directly from mt19937ar : double genrand_real2(void) double RandGen::genRand01() { return genrand_int32()*(1.0/4294967296.0); } //adapted directly from mt19937ar : unsigned long genrand_int32(void) unsigned long RandGen::genrand_int32() { unsigned long y; static unsigned long mag01[2]={0x0UL, MATRIX_A}; /* mag01[x] = x * MATRIX_A for x=0,1 */ if (stateVectorIndex_ >= N) { /* generate N words at one time */ unsigned int kk; //Removed uninitialized check, class initializes in constructor. -James for (kk=0;kk> 1) ^ mag01[y & 0x1UL]; } for (;kk> 1) ^ mag01[y & 0x1UL]; } y = (stateVector_[N-1]&UPPER_MASK)|(stateVector_[0]&LOWER_MASK); stateVector_[N-1] = stateVector_[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; stateVectorIndex_ = 0; } y = stateVector_[stateVectorIndex_++]; /* Tempering */ y ^= (y >> 11); y ^= (y << 7) & 0x9d2c5680UL; y ^= (y << 15) & 0xefc60000UL; y ^= (y >> 18); return y; } using namespace boost; using namespace std; int main() { TiXmlDocument *doc = new TiXmlDocument(); TiXmlElement *RootElement = new TiXmlElement("person"); doc->LinkEndChild(RootElement); TiXmlElement *PersonElement = new TiXmlElement("person"); doc->LinkEndChild(PersonElement); int index = 0; RandGen rand; while(index<200) { string id = lexical_cast(index); PersonElement->SetAttribute("id", id.c_str()); TiXmlElement *power = new TiXmlElement("power"); TiXmlElement *fangyu = new TiXmlElement("fangyu"); TiXmlElement *age = new TiXmlElement("age"); PersonElement->LinkEndChild(power); PersonElement->LinkEndChild(fangyu); PersonElement->LinkEndChild(age); int con = rand.genRand(19,99); string att = lexical_cast(con); TiXmlText *powerconent = new TiXmlText(att.c_str()); con = rand.genRand(19,99); att = lexical_cast(con); TiXmlText *agecontent = new TiXmlText(att.c_str()); con = rand.genRand(19,99); att = lexical_cast(con); TiXmlText *fangyucontent = new TiXmlText(att.c_str()); power->LinkEndChild(powerconent); fangyu->LinkEndChild(fangyucontent); age->LinkEndChild(agecontent); index ++; } doc->SaveFile("xml.xml"); getchar(); return 0; }

 

上面的类是随机生成数字

你可能感兴趣的:(c++,开源软件,boost,游戏引擎,matrix,constants,string,class,编程,c)