//bcd.h
#ifndef BCD_H_INCLUDED
#define BCD_H_INCLUDED
#include <iostream>
using namespace std;
class BCD
{
protected:
unsigned int decimal, binary;
public:
BCD(unsigned int x);
BCD();
void setCode(unsigned int y);
static int staticMethod();
static int ninstence;
unsigned int getBinary()
{
return binary;
}
void outputBCD();
friend ostream& operator << (ostream & o, BCD& bcd);
};
ostream& operator << (ostream & o, BCD& bcd);
class BCD8421:public BCD
{
public :
BCD8421() ;
BCD8421(unsigned int x ) ;
void setCode(unsigned int y) ;
};
class BCD5421:public BCD
{
public :
BCD5421() ;
BCD5421(unsigned int x ) ;
void setCode(unsigned int y) ;
};
class BCD2421:public BCD
{
public :
BCD2421(){}
BCD2421(unsigned int x ) ;
void setCode(unsigned int y) ;
};
#endif // BCD_H_INCLUDED
//bcd.cpp
#include "bcd.h"
#include <cstring>
#include <cmath>
#include <iostream>
#include "stackLink.h"
using namespace std;
int BCD::ninstence=0;
BCD::BCD(unsigned int x)
{
decimal=x;
binary=-1;
ninstence++;
cout<< "BCD(int x) constructing" << endl ;
}
int BCD::staticMethod()
{
return ninstence;
}
void BCD::setCode(unsigned int x)
{
binary=x;
decimal=-1;
cout<< "BCD() constructing" << endl ;
}
BCD::BCD()
{
cout<< "BCD8421 constructing" << endl ;
decimal=-1;
binary=-1;
}
void BCD::outputBCD()
{
unsigned int a=binary;
char x, y;
StackLink<char> stack;
while(a!=0)
{
x=(a&15);
a>>=4;
stack.push(x);
}
cout <<"decimal:" << dec << decimal << " BCD in hex:" << hex << binary<<endl<<"BCD in Binary:" ;
while(!stack.isEmpty())
{
y=stack.pop();
cout<<((y>>3)&1)<<((y>>2)&1)<<((y>>1)&1)<<(y&1)<<", ";
}
cout<<endl;
}
ostream& operator << (ostream & o, BCD &bcd)
{
o<<dec<<"decimal="<<bcd.decimal<<", "<<"binary="<<hex<<bcd.binary;
return o;
}
BCD8421::BCD8421()
{
cout<< "BCD8421() constructing" << endl ;
}
BCD8421::BCD8421(unsigned int x ):BCD(x)
{
cout<< "BCD8421(int x) constructing" << endl ;
decimal=x;
unsigned int y, n=0, b=0;
while(x!=0)
{
y=x%10;
x=x/10;
b=b+(y<<n*4);
n++;
}
binary=b;
}
void BCD8421::setCode( unsigned int x )
{
binary=x;
unsigned int y, n=0, d=0;
unsigned int a[10]={1,10,100,1000,10000,100000,100000,1000000,100000000,1000000000};
while(x!=0)
{
y=x%16;
x=x/16;
d=d+(y*a[n]);
n++;
}
decimal=d;
}
BCD5421::BCD5421()
{
cout<< "BCD5421() constructing" << endl ;
}
BCD5421::BCD5421(unsigned int x ):BCD(x)
{
cout<< "BCD5421(int x) constructing" << endl ;
decimal=x;
unsigned int y, n=0, b=0;
while(x!=0)
{
y=x%10;
if( y >= 5 ) {
y = ((y-5) | (1<<3));
}
x=x/10;
b=b+(y<<n*4);
n++;
}
binary=b;
}
void BCD5421::setCode( unsigned int x )
{
binary=x;
unsigned int y, n=0, d=0;
unsigned int a[10]={1,10,100,1000,10000,100000,100000,1000000,100000000,1000000000};
while(x!=0)
{
y=x%16;
if( y >= 8 ) {
y = (y&7)+5;
}
x=x/16;
d=d+(y*a[n]);
n++;
}
decimal=d;
}
BCD2421::BCD2421(unsigned int x ):BCD(x)
{
cout<< "BCD5421(int x) constructing" << endl ;
decimal = x;
unsigned int y, n=0, b=0;
while(x!=0)
{
y=x%10;
if( y >= 5 ) {
y = 0xB+(y-5) ;
}
x=x/10;
b=b+(y<<n*4);
n++;
}
binary=b;
}
void BCD2421::setCode( unsigned int x )
{
binary=x;
unsigned int y, n=0, d=0;
unsigned int a[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
while(x!=0)
{
y=x%16;
if( y >= 0xB ) {
y = (y-0xB)+5;
}
x=x/16;
d=d+(y*a[n]);
n++;
}
decimal=d;
}
//main.cpp
#include <iostream>
#include "bcd.h"
#include "StackLink.h"
using namespace std;
int main()
{
int a = 6666642 ;
BCD obj1(a);
//cout << a << endl ;
obj1.outputBCD();
//cout<<"hex: " << obj1 << endl ;
a = 60872 ;
BCD8421 obj2(a) ;
obj2.outputBCD() ;
BCD5421 obj3(5174) ;
obj3.outputBCD() ;
BCD5421 obj4;
obj4.setCode(0x81a4) ;
obj4.outputBCD() ;
BCD2421 obj5(719835) ;
obj5.outputBCD() ;
BCD2421 obj6 ;
obj6.setCode( 0xF2B4CDE ) ;//9254678
obj6.outputBCD() ;
return 0;
}