Created
December 25, 2010 00:16
-
-
Save johnhmj/754580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @eyny@Force[TW]@ | |
#include "forcetw20101224.h" | |
// | |
CBitset::CBitset(void) | |
{ | |
this->m_dec = 0; | |
this->m_bitlen = 0; | |
this->m_bitset.clear(); | |
} | |
CBitset::CBitset(unsigned int nBitLen, unsigned int nVal) | |
{ | |
if ( nVal >= this->pwr(nBitLen) ) | |
{ | |
return; | |
} | |
this->m_dec = nVal; | |
this->m_bitlen = nBitLen; | |
this->ntob(); | |
} | |
CBitset::CBitset(const CBitset& cbitset) | |
{ | |
if ( this != &cbitset ) | |
{ | |
this->m_dec = cbitset.m_dec; | |
this->m_bitlen = cbitset.m_dec; | |
this->m_bitset.clear(); | |
this->m_bitset = cbitset.m_bitset; | |
} | |
} | |
CBitset& CBitset::operator =(const CBitset& cbitset) | |
{ | |
if ( this != &cbitset ) | |
{ | |
this->m_dec = cbitset.m_dec; | |
this->m_bitlen = cbitset.m_dec; | |
this->m_bitset.clear(); | |
this->m_bitset = cbitset.m_bitset; | |
} | |
return *this; | |
} | |
CBitset::~CBitset(void) | |
{ | |
this->m_dec = 0; | |
this->m_bitlen = 0; | |
this->m_bitset.clear(); | |
} | |
void CBitset::set(unsigned int nBitLen, unsigned int nVal) | |
{ | |
if ( nVal >= this->pwr(nBitLen) ) | |
{ | |
return; | |
} | |
if ( ! this->m_bitset.empty() ) | |
{ | |
this->m_bitset.clear(); | |
} | |
this->m_dec = nVal; | |
this->m_bitlen = nBitLen; | |
this->ntob(); | |
} | |
void CBitset::ntob(void) | |
{ | |
unsigned int val = this->m_dec; | |
while ( val != 0 ) | |
{ | |
this->m_bitset.push_back(((val % 2) == 1)? true: false); | |
val /= 2; | |
} | |
if ( this->m_bitset.size() < this->m_bitlen ) | |
{ | |
unsigned int i = 0; | |
for (; this->m_bitset.size() < this->m_bitlen; i ++) | |
{ | |
this->m_bitset.push_back(false); | |
} | |
} | |
reverse(this->m_bitset.begin(), this->m_bitset.end()); | |
} | |
void CBitset::bton(void) | |
{ | |
unsigned int nRet = 0; | |
vBitset vbs = this->m_bitset; | |
reverse(vbs.begin(), vbs.end()); | |
unsigned int i = 0; | |
for (; i < vbs.size(); i ++) | |
{ | |
nRet += (vbs[i]? 1: 0) * pwr(i); | |
} | |
this->m_dec = nRet; | |
} | |
unsigned int CBitset::pwr(unsigned int n) | |
{ | |
unsigned int i = 0, val = 1; | |
for (; i < n; i ++) | |
{ | |
val *= 2; | |
} | |
return val; | |
} | |
void CBitset::Print(void) | |
{ | |
for (vBitset::iterator it = this->m_bitset.begin(); it != this->m_bitset.end(); it ++) | |
{ | |
cout << dec << ((*it)? 1: 0); | |
} | |
cout << " " << dec << this->m_dec; | |
cout << " " << "0x" << hex << uppercase << this->m_dec; | |
} | |
// | |
CNumset::CNumset(void) | |
{ | |
this->m_bits = 0; | |
this->m_set.clear(); | |
} | |
CNumset::CNumset(unsigned int nBit) | |
{ | |
this->m_bits = nBit; | |
unsigned int i = 0, nMax = this->pwr(this->m_bits); | |
CBitset bs; | |
for (; i < nMax; i ++) | |
{ | |
bs.set(this->m_bits, i); | |
this->m_set.push_back(bs); | |
} | |
} | |
CNumset::~CNumset(void) | |
{ | |
this->m_bits = 0; | |
this->m_set.clear(); | |
} | |
unsigned int CNumset::pwr(unsigned int n) | |
{ | |
unsigned int i = 0, val = 1; | |
for (; i < n; i ++) | |
{ | |
val *= 2; | |
} | |
return val; | |
} | |
void CNumset::Print(void) | |
{ | |
for (vSet::iterator it = this->m_set.begin(); it != this->m_set.end(); it ++) | |
{ | |
it->Print(); | |
cout << endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @eyny@Force[TW]@ | |
#ifndef _FORCETW20101224_H_ | |
#define _FORCETW20101224_H_ | |
// | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
// | |
typedef vector<bool> vBitset; | |
// | |
class CBitset | |
{ | |
public: | |
unsigned int m_dec; | |
unsigned int m_bitlen; | |
vBitset m_bitset; | |
CBitset(void); | |
CBitset(unsigned int nBitLen, unsigned int nVal); | |
CBitset(const CBitset& cbitset); | |
CBitset& operator =(const CBitset& cbitset); | |
~CBitset(void); | |
void set(unsigned int nBitLen, unsigned int nVal); | |
void ntob(void); | |
void bton(void); | |
unsigned int pwr(unsigned int n); | |
void Print(void); | |
}; | |
// | |
typedef vector<CBitset> vSet; | |
// | |
class CNumset | |
{ | |
public: | |
unsigned int m_bits; | |
vSet m_set; | |
CNumset(void); | |
CNumset(unsigned int nBit); | |
~CNumset(void); | |
unsigned int pwr(unsigned int n); | |
void Print(void); | |
}; | |
// | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @eyny@Force[TW]@ | |
// Integrated Development Environment | |
// Visual C++ | |
#include <iostream> | |
#include "forcetw20101224.h" | |
using namespace std; | |
// | |
int main(int argc, char** argv) | |
{ | |
unsigned int n; | |
while ( cin >> n ) | |
{ | |
CNumset ns(n); | |
ns.Print(); | |
} | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment