2011年11月30日

C++LAMP

                一次偶然的机会,看到了C++博客。欣喜万分之于就立刻注册了一个账号。希望自己真的能在这里得到知识的滋润。对于喜欢搞Linux的我来说以后的路还很漫长,也需要很多的技术和学习上的支持,这应该是我注册的初衷。Linux是我的最爱。至今,我认为,这是我喜欢的。除此我也在搞数据库(mySQL,Oracle)。

我有个想法,那就是将开源与网络紧密结合,到底以什么形式结合,我希望在两年之后的今天能够得到答案。路漫漫其修远兮,吾将上下而求索!

                对,开源。我同时我也喜欢开源的东西,因为我也被UBUNTU的精神所鼓舞!

posted @ 2011-11-30 23:04 AlexanderNan 阅读(126) | 评论 (0)编辑 收藏

Huffman

/*************.h File**************/
#include <string>
#include <iostream>
#include <fstream>

#ifndef HUFFMAN
#define HUFFMAN

class Huffman
{
 private:
  /*** Node structure ***/
  class BinNode
  {
   public:
    char data;
    BinNode * left,
            * right;

    // BinNode constructor
    BinNode(char item)
    : data(item), left(0), right(0)
    { }
  };

  typedef BinNode * BinNodePointer;

 public:
  /*** Function members ***/
  Huffman();
 
 /******************************************************************/
void BuildDecodingTree(ifstream & CodeFile);
 
/ ******************************************************************
void Insert(char ch, string code);

 
/ ******************************************************************/
void Decode(ifstream & in);

 
 ******************************************************************/
void PrintTree(ostream & out, BinNodePointer root, int indent);

 
 /******************************************************************/
void DisplayDecodingTree(ostream & out);

/*** Data members ***/
private:
  BinNodePointer root;
};

//--- Definition of consructor
inline Huffman::Huffman()
{ root = new BinNode('*'); }

#endif
/****************.cpp File*********************/
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

#ifndef HUFFMAN
#define HUFFMAN

class Huffman
{
private:
/*** Node structure ***/
struct BinNode
{
  char data;
  BinNode * left,
          * right;

  // BinNode constructor
  BinNode(char item)
  {
    data = item;
    left = right = 0;
  }
};

typedef BinNode * BinNodePointer;

/*** Function members ***/
public:
/* Constructor
 *  Precondition:  A Huffman object has been declared.
 *  Postcondition: One-node binary tree with root node
 *                 pointed to by root has been created.
 ******************************************************************/
Huffman();

/* Build the Huffman decoding tree.
 *  Receive:       Huffman object containing this function (implicitly)
 *                 fstream in
 *  Input:         Characters and their codes via in.
 *                 Last line of code file must contain *.
 *  Postcondition: Huffman decoding tree has been created with root
 *                 node pointed to by root.
 ******************************************************************/
void BuildDecodingTree(ifstream & CodeFile);

/* Insert a node for a character in Huffman decoding tree.
 *  Receive:       char c and code, a bit string
 *  Postcondition: Node containing c has been inserted into
 *                 Huffman tree with root pointed to by root.
 ******************************************************************/
void Insert(char ch, string code);

/* Read a message (string of bits) from a file and decode it
 * using the huffman decoding tree.
 *  Receive:   Huffman object containing this function (implicitly)
 *             fstream in connected to message file
 *  Input:     Message via in
 *             Last line of message file must contain *.
 *  Output:    Decoded message
 ******************************************************************/
void Decode(ifstream & in);

/* --- A binary tree printer
 * Displays a binary tree recursively.  The tree is displayed
 * "on its side" with each level  indented by a specified value
 * Indent, but with  no arcs sketched in.
 *  Receive: Root of binary tree and integer indent
 *  Output:  Graphical representation of the binary tree
 ******************************************************************/
void PrintTree(ostream & out, BinNodePointer root, int indent);

/* Display the decoding tree
 *  Receive: Huffman object containing this function (implicitly)
 *           ostream out
 *  Output:  The decoding tree via PrintTree()
 ******************************************************************/
void DisplayDecodingTree(ostream & out);

/*** Data members ***/
private:
  BinNodePointer root;
};

//--- Definition of consructor
inline Huffman::Huffman()
{ root = new BinNode('*'); }

//--- Definition of BuildDecodingTree()
void Huffman::BuildDecodingTree(ifstream & in)
{
  char ch;          // a character
  string code;      // its code
  for (;;)
  {
    in >> ch >> code;
    if (ch == '*') return;
    Insert(ch, code);
  }
}

//--- Definition of Insert()
void Huffman::Insert(char ch, string code)
{
  Huffman::BinNodePointer p = root;   // pointer to move down the tree

  for(int i = 0; i < code.length(); i++)
  {
    switch (code[i])
    {
      case '0' :           // descend left
        if (p->left == 0)  // create node along path
          p->left = new Huffman::BinNode('*');
        p = p->left;
        break;

      case '1' :           // descend right
        if (p->right == 0) // create node along path
          p->right = new Huffman::BinNode('*');
        p = p->right;
        break;

      default:
        cout << "*** Illegal character in code ***\n";
        exit(-1);
    }
  }
  p->data = ch;
}

//--- Definition of Decode()
void Huffman::Decode(ifstream & in)
{
  char bit;                  // next message bit
  Huffman::BinNodePointer p; // pointer to trace path in decoding tree

  for(;;)
  {
    p = root;
    while (p->left != 0 || p->right != 0)
    {
      in >> bit;
      if (bit == '*') return;
      cout << bit;
      if (bit == '0')
        p = p->left;
      else if (bit == '1')
        p = p->right;
      else
        cout << "Illegal bit: " << bit << " -- ignored\n";
    }
    cout << "--" << p->data << endl;
  }
}

//--- Definition of PrintTree()
void Huffman::PrintTree(ostream & out, Huffman::BinNodePointer root,
                        int indent)
{
  if (root != 0)
  {
    PrintTree(out, root->right, indent + 8);
    out << setw(indent) << " " << root->data << endl;
    PrintTree(out, root->left, indent + 8);
  }
}

//--- Definition of DisplayDecodingTree()
inline void Huffman::DisplayDecodingTree(ostream & out)
{ PrintTree(out, root, 0); }

#endif
/*******************main.cpp***********************/
//--- Driver Program ---
#include "Huffman"
#include <iostream>
#include <fstream>

int main()
{
  char filename[32];
  cout << "Enter name of code file: ";  cin >> filename;
  ifstream codestream(filename);
  if (!codestream.is_open())
  {
    cout << "Cannot open code file.\n";
    exit(-1);
  }

  Huffman h;
  h.BuildDecodingTree(codestream);
  h.DisplayDecodingTree(cout);
  cout << endl << endl;
 
  cout << "Enter name of message file: ";  cin >> filename;
  ifstream message(filename);
  if (!message.is_open())
  {
    cout << "Cannot open message file.\n";
    exit(-1);
  }
  h.Decode(message);
}


posted @ 2011-11-30 22:55 AlexanderNan 阅读(249) | 评论 (0)编辑 收藏

仅列出标题  
<2019年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜