⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model.cpp

📁 有关几个数字编码的C++ 程序
💻 CPP
字号:
// Model.cpp: implementation of the CModel class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Model.h"

#include <stdio.h>
#include <stdlib.h>

//--------------------------------------------------------------------
//-------------------Construction/Destruction-------------------------
CModel::CModel()
{
    start();
}//model
CModel::~CModel()
{

}
//====================================================================


//--------------------------------------------------------------------
//-------------------initialize the model-----------------------------
void CModel::start()
{
	int i;
	for(i=0; i<nof_chars; i++)
	{
		char_to_index[i] = i+1;
		index_to_char[i+1] = i;
	}
	for(i=0; i<=nof_symbols; i++)
	{
		freq[i] = 1;
		cum_freq[i] = nof_symbols-i;
	}
	freq[0] = 0; 
}//start
//====================================================================

//--------------------------------------------------------------------
//-----------update the model to account for a new symbol-------------
void CModel::update(int symbol)
{
    
	int i;
	if ( cum_freq[0] == max_frequency )
	{
		int cum;
		cum = 0;
		for(i=nof_symbols; i>=0; i--)
		{
			freq[i] = (freq[i]+1)/2;
			cum_freq[i] = cum;
			cum += freq[i];
		}
	}
	for(i=symbol; freq[i]==freq[i-1]; i--) {};// find symbol's new index		
	if ( i<symbol )
	{
		int ch_i, ch_symbol;
		ch_i = index_to_char[i];
		ch_symbol = index_to_char[symbol];
		index_to_char[i] = ch_symbol;
		index_to_char[symbol] = ch_i;
		char_to_index[ch_i] = symbol;
		char_to_index[ch_symbol] = i;
		
	}		
	freq[i] += 1;
	while(i>0)
	{
		i -= 1;
		cum_freq[i] += 1;
	}
	
}
//==================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -