现在的位置: 首页 > 综合 > 正文

January 14th Wednesday 2009 (一月 十四日 水曜日)

2013年02月27日 ⁄ 综合 ⁄ 共 3850字 ⁄ 字号 评论关闭

   There is a list demo to express my mind on list.  The code is just used to express, so it is not good to write.

//listdemo.cpp
#include <stdlib.h>
#include <string.h>

#include <iostream>

using namespace std;

#define ATOM_MAX 256

#define ATOM 0
#define LIST 1
#define FUNCTION 2

typedef int (*Function)(void * arg);

struct _Node_ {
  int node_type;
  union {
     struct _Node_ *list;
     struct _Atom_ {
        int atom_size;
        void *atom_val;
     } atom;
     Function fun;
  } val;
  struct _Node_ *next;
  _Node_()  {
      node_type = ATOM;
      memset(&val, 0, sizeof(val));
      next = NULL;
  }
};

typedef _Node_ Node;

class MList {
private:
        mutable Node *head;
        mutable Node *tail;
public:
        MList() {
            head = NULL;
            tail = NULL;
        }
        ~MList() {
            Node *p = NULL;
            Node *q = NULL;
           
            for (p = head; p; ) {
                q = p;
                p = p->next;
                if (q->node_type == ATOM)
                    delete q->val.atom.atom_val;
                   
                delete q;
            } //for
        }
       
        Node * getHead() {
            return this->head;
        }
       
        void addStringNode(char *str, int len) {
            char *buf = new char[len + 1];
            memset(buf, 0, len + 1);
            strncpy(buf, str, len);

            Node *np = new Node;
            np->node_type = ATOM;
            np->val.atom.atom_size = len + 1;
            np->val.atom.atom_val = (void*)buf;
           
            if (!head && !tail) {
                // first node
                head = np;
                tail = np;
            }
            else{
                tail->next = np;
                tail = np;
            }
        }

        void addFunctionNode(Function f) {
            Node *np = new Node;
            np->node_type = FUNCTION;
            np->val.fun = f;
           
            if (!head && !tail) {
                // first node
                head = np;
                tail = np;
            }
            else {
                tail->next = np;
                tail = np;
            }
        }
       
        const MList& operator+(MList& mlist) {
            Node *np = new Node();
           
            np->node_type = LIST;
            np->val.list = mlist.getHead();
           
            if (!head && !tail) {
                head = np;
                tail = np;
            }
            else {
                tail->next = np;
                tail = np;
            }
           
            return (*this);
        }
};

char demo_str[] = "just to test.";
int demo(void * arg) {
    cout<<"demo()"<<(char*)arg;
    return 0;
}

void displayList(Node* head) {
  Node *p = NULL;
  char buf[257];
 
  p = head;
  while (p) {
    memset(buf, 0, 257);
    if (p->node_type == ATOM)
      strncpy(buf, (char*)p->val.atom.atom_val, p->val.atom.atom_size);
    else if (p->node_type == FUNCTION)
      p->val.fun(demo_str);
    else if (p->node_type == LIST) {
        cout<<"[";
        displayList(p->val.list);
        cout<<"]";
    }
    cout<<buf<<"->";
    p = p->next;
  }
  cout<<"(null)";
}

int main() {
  MList mlist1;
  MList mlist2;

  mlist1.addStringNode("abc", 3);
  mlist2.addStringNode("hello", 5);
  mlist2.addFunctionNode(demo);

  displayList(mlist1.getHead());
  cout<<endl;
  displayList(mlist2.getHead());
  cout<<endl;

  mlist1 + mlist2;
  displayList(mlist1.getHead());
  cout<<endl;

  return 0;
}

//mfp.cpp
#include <string.h>
#include <iostream>

using namespace std;

class Item {
public:
    char name[256];
    void action(void * arg) {
        cout<<(char*)arg<<endl;
    }

    Item(){ memset(name, 0, sizeof(name)); }
private:
    const Item& operator=(const Item& item);
    Item(const Item& item);
};

typedef void (Item::*Action)(void * arg);

int main() {
    char buf[256];
    Item items[3];

    for (int i = 0; i < 3; i++){
        memset(buf, 0, sizeof(buf));
        sprintf(buf, "item%d", i);
        strncpy(items[i].name, buf, strlen(buf));
    }

    //void (Item::*act)(void *) = &Item::action;
    Action act = &Item::action;
    for (int j = 0; j < 3; j++){
        (items[j].*act)(items[j].name);
    }

    Action acts[3];
    acts[0] = Item::action;
    acts[1] = Item::action;
    acts[2] = Item::action;
    for (int k = 0; k < 3; k++) {
        memset(buf, 0, sizeof(buf));
        sprintf(buf, "%d", k);
        (items[k].*acts[k])(buf);
    }

    return 0;
}

抱歉!评论已关闭.