当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » C/C++  » 编程基础
分享到: 
收藏 +0
2
VS 2013基础版  
标签: <无>

代码片段(3) [全屏查看所有代码]

1. [文件] huffman.cpp ~ 4KB     下载(12)     跳至 [1] [2] [3] [全屏预览]

#include"iomanip"
#include"iostream"
using namespace std;
#include"huffman.h"
 hftree Tree;
 codelist codes;
void menu_main(){
	int select = 1000;
	while (1){
		menu_select();
		cin >> select; fflush(stdin);
		while (1){
			switch (select){
			case 1:creat_huffman_tree(); break;
			case 2:huffman_encode(codes, Tree); break;
			case 3:show_huffman_code(codes); break;
			case 4:huffman_decode(codes, Tree); break;
			case 5:show_huffman_tree(Tree); break;
			case 6:quit_system(); break;
			default:cout << "ÊäÈë´íÎó£¡ÇëÖØÐÂÊäÈë\n      ¡¾ 1 2 3 4 5 6 ¡¿  \n"; break;

			}break;           //Ìø³öÑ­»·  

		}
	}

}
void menu_select(){
	cout << "       " << "      *-----------------------------------------*" << endl;
	cout << "       " << "            »¶Ó­½øÈë¹þ·òÂü±àÂë¼°ÒëÂëϵͳ" << endl;
	cout << "       " << "      *-----------------------------------------*" << endl << endl;
	cout << "               " << "  1  Éú³ÉµÄ·òÂüÊ÷£¡" << endl << endl;
	cout << "               " << "  2  ÀûÓùþ·òÂüȨֵ±àÂ룡" << endl << endl;
	cout << "               " << "  3  ÏÔʾ±àÂë½á¹û£¡" << endl << endl;
	cout << "               " << "  4  ÀûÓùþ·òÂüÊ÷ÒëÂ룡" << endl << endl;
	cout << "               " << "  5  ÏÔʾÉú³ÉµÄ¹þ·òÂüÊ÷£¡" << endl << endl;
	cout << "               " << "  6  Í˳öϵͳ£¡" << endl;
	cout << "ÇëÊäÈëÄãµÄÑ¡Ôñ" << endl;
}
void creat_huffman_tree(){//´´½¨¹þ·òÂüÊ÷
	int i, j, k1 = -1, k2 = -1;
	float temp1, temp2;
	initial_huffman_tree();//³õʼ»¯
	input_huffman_weight();//ÊäËùÓÐÒ¶×ÓµÄÈëȨֵ
	for (i = n; i < m; i++){
		temp1 = temp2 = 1211313;//ȨֵËùÔÊÐíµÄ×î´óÖµ
		for (j = 0; j <= i - 1; j++){
			if (Tree[j].parent != -1)continue;
			if (Tree[j].weight < temp1){
				temp2 = temp1;
				temp1 = Tree[j].weight;
				k2 = k1;
				k1 = j;
			}
			else if (Tree[j].weight < temp2){
				temp2 = Tree[j].weight;
				k2 = j;
			}
		}

		Tree[k1].parent = Tree[k2].parent = i;
		Tree[i].parent = -1;
		Tree[i].left_child = k1;
		Tree[i].right_chlid = k2;
		Tree[i].weight = temp1 + temp2;
	}
	cout << "¹þ·òÂüÊ÷ÒѾ­Éú³É£¡\n";
}
void show_huffman_tree(hftree t){
	//creat_huffman_tree();
	for (int i = 0; i < m; i++)
		cout << "  weight=" << t[i].weight << "  parent=" << t[i].parent << "  left_child=" << t[i].left_child << "    right_child=" << t[i].right_chlid << endl;
}
void initial_huffman_tree(){//³õʼ»¯¹þ·òÂüÊ÷
	for (int i = 0; i < n; i++){    //³õʼ»¯¹þ·òÂüÊ÷
		Tree[i].parent = -1;
		Tree[i].left_child = Tree[i].right_chlid = -1;
	}
	cout << "\n";
}
void input_huffman_weight(){//ÊäÈën¸öÒ¶×ÓµÄȨֵ
	cout << "ÇëÊäÈë" << n << "¸öÒ¶×Ó½ÚµãµÄȨֵ\n";
	for (int i = 0; i < n; i++){

		cin >> Tree[i].weight; fflush(stdin);
	}
}                     //ÊäÈën¸ö½áµãµÄȨֵ

void huffman_encode(codelist codes, hftree Tree){//¹þ·òÂü±àÂë
	int i, c, p, start=0;
	cout << "ÇëÊäÈë" << n << "¸öÒ¶×Ó½ÚµãµÄ×Ö·û\n";
	for (i = 0; i < n; i++){
		cin.get(codes[i].ch);// fflush(stdin);
		cin.ignore();
		start = n;
		c = i;
		p = Tree[i].parent;
		while (p != -1){
			start--; 
			if (Tree[p].left_child == c)
				codes[i].bit[start] = '0';
			else
				codes[i].bit[start] ='1';
			c = p;
			p = Tree[p].parent; 
		  codes[i].bit[n] = '\0';
		}
		codes[i].start = start;
		
			
	}
	cout << "±àÂë½áÊø£¡\n";
}
void show_huffman_code(codelist codes){
	for (int i = 0; i<n; i++){
		cout <<"×Ö·û    "<<(codes[i].ch)<<"    µÄ±àÂëÊÇ" /*<<codes[i].bit <<endl*/;
		for (int j = 0; j<n+1; j++){
			cout << codes[i].bit[j];
		}
		cout << "\n";
	}
}
void huffman_decode(codelist codes, hftree Tree){//¹þ·òÂüÒëÂë
	int i, b = -10, endflag;
	endflag = -1;
	i = m - 1;
	cout << "½áÊøÒëÂ룬ÇëÊäÈë  -1  ";
	while (cin >> b,fflush(stdin), b != endflag){
		if (b == 0)i = Tree[i].left_child;
		else       i = Tree[i].right_chlid;
		if (Tree[i].left_child == -1){//Tree[i]ΪҶ×Ó
			cout << codes[i].ch;
			i = m - 1;

		}
	}
	if (i != m - 1)
		cout << "±àÂëÓÐÎó£¡\n";
}
void quit_system(){
	cout << "ÊÇ·ñÈ·¶¨Í˳öϵͳ£¿" << endl;
	char selection;
	if (cin >> selection, fflush(stdin), selection == 'y')
		delete []codes;
	    delete []Tree;
		exit(0);
}

2. [文件] huffman.h ~ 1KB     下载(11)     跳至 [1] [2] [3] [全屏预览]

#ifndef huffman_h
#define huffman_h 
const int n = 5;
const int m = 2 * n - 1;
typedef struct huffman_tree hftree[m];        //�����������ͣ������0�ŵ�Ԫ��ʼʹ��
struct huffman_tree{//���������
	float weight;
	int parent;
	int left_child, right_chlid;
};   //�������
struct huffman_coding{//������
	char bit[n+1];
	char ch;
	int start;
};
typedef huffman_coding codelist[n];
void creat_huffman_tree();                      //������������
void show_huffman_tree(hftree t);                ///�����������
void show_huffman_code(codelist codes);        //����������ı���
void initial_huffman_tree();                //��ʼ����������
void input_huffman_weight();                     //����n������Ȩֵ
void huffman_encode(codelist codes, hftree Tree);//����������
void huffman_decode(codelist codes, hftree Tree);//����������
void quit_system();                            //�˳�ϵͳ
void menu_select();                               //��ʾ���˵�
void menu_main();                                //
#endif

3. [文件] 课程设计 哈夫曼树.cpp ~ 184B     下载(11)     跳至 [1] [2] [3] [全屏预览]

#include<iostream>
#include<stdlib.h>
//#include"huffman.cpp"
using namespace std;
#include"huffman.h"
extern hftree Tree;
extern codelist codes;
int main(){
	menu_main();
}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(3)

  • 1楼:winips 发表于 2014-12-17 00:22 回复此评论
    乱码
  • 2楼:阿信sxq 发表于 2014-12-17 10:50 回复此评论
    为什么是乱码
  • 3楼:句龙胤 发表于 2014-12-17 10:56 回复此评论
    肯定是保存的GBK,而网页基本都是UTF8,所以乱码了
开源从代码分享开始 分享代码