open file and read.
fstream file("emp.bin",ios::in|ios::binary);
file.read((char*)&E, sizeof(Employee));
file.seekg(-((ios::pos_type)(sizeof(Employee)*2)), ios::cur);
2011年4月18日星期一
stack template
template <class T>
class Stack;
template <class T>
class Node{
T _data;
Node<T>* _next;
Node(T data, Node<T>* next);
friend class Stack<T>;
};
template <class T>
class Stack{
Node<T>* _top;
public:
Stack(void);
virtual ~Stack(void);
void push(T data);
T pop(void);
bool isEmpty(void);
};
template <class T>
Node<T>::Node(T data, Node<T>* next){
_data = data; // T must be ok with operator =
_next = next;
}
template <class T>
Stack<T>::Stack(void){
_top = (Node<T>*)0;
}
template <class T>
Stack<T>::~Stack(void){
Node<T>* toDel;
while(_top){
toDel = _top;
_top = _top->_next;
delete toDel;
}
}
template <class T>
void Stack<T>::push(T data){
Node<T>* tempnode = new Node<T>(data, _top);
_top = tempnode;
}
template <class T>
T Stack<T>::pop(void){
T ret = _top->_data; // T needs should be ok with copying
Node<T>* toDel = _top;
_top = _top->_next;
delete toDel;
return ret;
}
template <class T>
bool Stack<T>::isEmpty(void){
return _top == (Node<T>*)0;
}
class Stack;
template <class T>
class Node{
T _data;
Node<T>* _next;
Node(T data, Node<T>* next);
friend class Stack<T>;
};
template <class T>
class Stack{
Node<T>* _top;
public:
Stack(void);
virtual ~Stack(void);
void push(T data);
T pop(void);
bool isEmpty(void);
};
template <class T>
Node<T>::Node(T data, Node<T>* next){
_data = data; // T must be ok with operator =
_next = next;
}
template <class T>
Stack<T>::Stack(void){
_top = (Node<T>*)0;
}
template <class T>
Stack<T>::~Stack(void){
Node<T>* toDel;
while(_top){
toDel = _top;
_top = _top->_next;
delete toDel;
}
}
template <class T>
void Stack<T>::push(T data){
Node<T>* tempnode = new Node<T>(data, _top);
_top = tempnode;
}
template <class T>
T Stack<T>::pop(void){
T ret = _top->_data; // T needs should be ok with copying
Node<T>* toDel = _top;
_top = _top->_next;
delete toDel;
return ret;
}
template <class T>
bool Stack<T>::isEmpty(void){
return _top == (Node<T>*)0;
}
very important ---- set bit pattern
void printBits(unsigned int V){
for(int i=sizeof(unsigned int)*8-1;i>=0;!((i+1)%4) && putchar(' '),putchar('0' + !!(V & (1<<i))), (!i) && putchar('\n'),i--);
}
void SetBitPattern(unsigned int& val, const char* bitPattern, int startBitIndex){
int i = -1;
while(bitPattern[++i]);
while(i--){
if(bitPattern[i]-'0'){
val = val | 1 << startBitIndex;
}
else{
val = val & ~(1 << startBitIndex);
}
startBitIndex++;
}
}
for(int i=sizeof(unsigned int)*8-1;i>=0;!((i+1)%4) && putchar(' '),putchar('0' + !!(V & (1<<i))), (!i) && putchar('\n'),i--);
}
void SetBitPattern(unsigned int& val, const char* bitPattern, int startBitIndex){
int i = -1;
while(bitPattern[++i]);
while(i--){
if(bitPattern[i]-'0'){
val = val | 1 << startBitIndex;
}
else{
val = val & ~(1 << startBitIndex);
}
startBitIndex++;
}
}
订阅:
博文 (Atom)