Saturday 7 April 2012

-54

 char sB = 0xCA;
 
  printf("sB: %X  %d\n", sB,sB);
  C = sB >> 1;
  printf("sB>>1: %X\n", C);



I forgot how come the sB equals -54?

Monday 2 April 2012

walkthrough 1

#include <fstream>
#include <iostream>
using namespace std;
int main(){
  int i,j;
  cout<<sizeof(i)<<endl;   // the output of this line is 4
  fstream f("f.bin",ios::out|ios::in|ios::binary|ios::trunc);
  for(i=10;i<100;i+=10){
    f.write((const char*)&i, sizeof(int));
  }
  f.seekg((ios::pos_type)20);
  f.read((char*)&i, sizeof(int));
  cout<<i<<endl; // 1 mark
  f.seekg(-(ios::off_type)sizeof(int), ios::end);
  f.read((char*)&i, sizeof(int));
  cout<<i<<endl; // 1 mark
  f.seekg(-(ios::off_type)sizeof(int)*3, ios::cur);
  f.read((char*)&i, sizeof(int));
  cout<<i<<endl; // 1 mark
  f.seekp((ios::off_type)sizeof(int)*2, ios::beg);
  f.write((char*)&i, sizeof(int));
  f.seekp((ios::off_type)0, ios::end);
  cout<<(j=f.tellp())<<endl;  // 1 mark
  f.seekg(0);
  while(f.tellg() < j){  // 1 mark
    f.read((char*)&i, sizeof(int));
    cout<<i<<", ";
  }
  cout<<endl;   
  return 0;
}



output:  4
            60
            90
            70
            36
            10 ~ 90

fstream

#include <iostream>
#include <fstream>
using namespace std;

int main(){
  fstream file("datafile.bin",ios::in|ios::binary);
  int i;
  while(file.good()){
    file.read((char*)&i, sizeof(i));
    if(file.good())
      cout<<i<<", ";
  }
  file.close();// no need
  return 0;
}




Why the output is end of 9999,begin with 6012?

Tuesday 20 March 2012

Template stack

#include <iostream>
using namespace std;

template <class T>
class Stack;

template <class T>
class SNode{
  T _data;
  SNode<T>* _next;
  SNode(T data, SNode<T>* next){
    _data = data;
    _next = next;
  }
  friend class Stack<T>;
};

template <class T>
class Stack{
  SNode<T>* _top;
public:
  Stack();
  void push(T data);
  T pop();
  bool isEmpty()const;
  virtual ~Stack();
};

template <class T>
Stack<T>::Stack(){
 _top = (SNode<T>*)0;
}

template <class T>
void Stack<T>::push(T data){
  SNode<T>* temp = new SNode<T>(data,_top);
  _top = temp;
}



template <class T>
T Stack<T>::pop(){
  T val = _top->_data;
  SNode<T>* toDel = _top;
  _top = _top->_next;
  delete toDel;
  return val;
}

template <class T>
bool Stack<T>::isEmpty()const{
  return !_top;
}


template <class T>
Stack<T>::~Stack(){
  SNode<T>* toDel;
  while(_top){
    toDel = _top;
    _top = _top->_next;
    delete toDel;
  }
}

int main(){
  Stack<int> S;
  int i;
  for(i=100;i<2000;i+=10){
    S.push(i);
  }
  while(!S.isEmpty()){
    cout<<S.pop()<<" ";
  }
  cout<<endl<<"*******************************************"<<endl;
  for(i=100;i<2000;i+=10){
    S.push(i);
  }
  for(i=0;i<10;i++){
    cout<<S.pop()<<" ";
  }
  cout<<endl<<"*******************************************"<<endl;
  return 0;
}

Monday 19 March 2012

Template

template<class type>
class Tqnode{
 type _date;
 Tqnode<type>* _next;
 Tqnode<type>* _prev;
 Tqnode(type data,
      Tqnode<type>* next = (Tqnode<type> *) 0,  Tqnode<type>* prev = (Tqnode<type>*)0){
                  _data = data;
                  _next = next;
                  _prev = prev;
}
};

bit print...

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++;
  }
}


important : set bit pattern

Thursday 15 March 2012

the output question

#include <iostream>
using namespace std;
void hanoi(int n, char p1, char p2, char p3){
  static int row = 0;
  if(n==1){
    cout<<(++row)<<" "<<p1<<"--------->"<<p3<<endl;
  }
  else{
    hanoi(n-1, p1, p3, p2);
    cout<<(++row)<<" "<<p1<<"--------->"<<p3<<endl;
    hanoi(n-1, p2, p1, p3);
  }
}

int main(){
  hanoi(4, 'A', 'B', 'C');
  return 0;
}


I cannot figure out the second output........why the follow output is 2 A----->c....

my answer is C----->B