2011年4月18日星期一

fstream

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);

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

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

2011年3月30日星期三

connect php with mysql and configuration

After installing MYSQL, we need to uncomment the following lines
extension=php_mysql.dll and extension=php_mysqli.dll
in php.ini file and restart the apache2 web server.
But when I finish it, it doesn't work.
It shows "Fatal error: Call to undefined function mysql_connect() in……" when I run my php file.
We also need to add something in the path.
computer->properties->advanced system settings->advanced->environment variables->system variables->path->edit->add "C:\php;C:\php\ext" at the end and save it. At last, restart the computer.
It will work!

2011年2月22日星期二

pointer

#include <stdio.h>
int add(int , int );
int prnch(int , int );
int mul(int , int);
int main(){
  int (*fptr[3])(int, int) = {add, mul, prnch};
  int x = 10;
  int y = 20;
  int z=0;
  int i;
  for(i=0;i<3;i++){
    z += (*fptr[i])(x, y);
  }
  printf("z: %d\n", z);
  return 0;
}
int add(int a, int b){
  return printf("sum: %d\n", a+b);
}
int mul(int a, int b){
  return printf("mul: %d\n", a*b);
}
int prnch(int m, int n){
  while(n--){
    putchar('-');
  }
  putchar('\n');
  while(m--){
    putchar('-');
  }
  putchar('\n');
  return m - n;
}


the output is:
sum: 30
mul: 200
--------------------
----------
z: 17

why Z is equal to 17?

about the pointer

after reviewing Fardad's lecture, i have a question about the address.

#include <stdio.h>
int main(){
  int a[3][5]={
                {10,20,30,40,50},
                {100,200,300,400,500},
                {1000,2000,3000,4000,5000}
              };
  printf("%u\n", a);
  printf("%u\n", a+1);
  printf("%u\n", a[0]);
  printf("%u\n", a[0]+1);
  printf("%d, %d\n", *(*(a+2) + 3), a[2][3]);
  return 0;
}

in my opinion, the first one "a" is the same as the third one "a[0]". so they have the same address.
but is it the second one "a+1" = "a[0]+1"????

the output is:
2030780
2030800
2030780
2030784
4000, 4000

i don't know why???

2011年2月1日星期二

int2str

const char* Int2Str(int num) {
 static char string[50];
 int count=1;
 int temp=num;
 int i=0;
 while(num=num/10){
  count++;
 }
 while(count)
 {
  string[count-1]= 48+temp%10;
 temp=temp/10;
 count--;
 }
 return string;
}