当前位置: 首页>>代码示例>>C++>>正文


C++ HuffmanTree::Encoding方法代码示例

本文整理汇总了C++中HuffmanTree::Encoding方法的典型用法代码示例。如果您正苦于以下问题:C++ HuffmanTree::Encoding方法的具体用法?C++ HuffmanTree::Encoding怎么用?C++ HuffmanTree::Encoding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HuffmanTree的用法示例。


在下文中一共展示了HuffmanTree::Encoding方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main()
{
	string text;
	bool BeingInit = 0, BeingEncode = 0;
	HuffmanTree *tree;
	char *c;
	int *value;
	int n;
	int select;
	while (true)
	{
		cout << "1.initialization" << endl;
		cout << "2.write text to file" << endl;
		cout << "3.Encoding" << endl;
		cout << "4.Decoding" << endl;
		cout << "5.Print CodeFile" << endl;
		cout << "6.TreePrinting" << endl;
		cout << "7.show The Huffman code" << endl;
		cout << "8.exit" << endl;
		cin >> select;
		switch (select)
		{
		case 1:
			int t; 
			cout << "1.build a new HuffmanTree" << endl;
			cout << "2.load from the file" << endl; 
			cin >> t;
			while (true)
			{
				if (t == 1)
				{
					cout << "输入字符个数:";
					cin >> n;
					c = new char[n];
					value = new int[n];
					for (int i = 0; i<n; i++)
					{
						cout << "第" << i + 1 << "个字符:";
						cin >> c[i];
						cout << "第" << i + 1 << "个权值:";
						cin >> value[i];
					}
					tree = new HuffmanTree(value, n, c);
					break;
				}
				if (t == 2)
				{
					tree = new HuffmanTree;
					break;
				}
			}
			cout << "initialization success!" << endl;
			BeingInit = 1;
			tree->save();
			break;
		case 2:cout << "input your text: "; cin >> text;
			tree->writefile(text);
			cout << "OK!" << endl;
			break;
		case 3:if (!BeingInit) { cout << "the tree is not exist" << endl; break; }
			   tree->Encoding();
			   cout << "succeed in encoding!" << endl;
			   BeingEncode = 1;
			   break;
		case 4:if (!BeingInit) { cout << "the tree is not exist" << endl; break; }
			   if (!BeingEncode) { cout << "the tree haven't been encode, you must encoding before!" << endl; break; }
			   tree->Decoding();
			   cout << "succeed in decoding!" << endl;
			   break;
		case 5:if (!BeingInit) { cout << "the tree is not exist" << endl; break; }
			   if (!BeingEncode) { cout << "the tree haven't been encode, you must encoding before!" << endl; break; }
			   tree->Print(); cout << endl; break;
		case 6:if (!BeingInit) { cout << "the tree is not exist" << endl; break; }
			   tree->printing();
			   break;
		case 7:tree->Huffmancode(); cout << endl; break;
		case 8:exit(0);
		default:break;
		}
	}
开发者ID:Diunax,项目名称:C-CS-CPP,代码行数:80,代码来源:hfmtree.cpp


注:本文中的HuffmanTree::Encoding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。