我为霍夫曼写这个程序 [英] i write this program for huffman

查看:65
本文介绍了我为霍夫曼写这个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "StdAfx.h"
#include<string>
#include<iostream>
using namespace std ;
#define ARRAY_SIZE 20


int main()
{
	float hh [100] ;
	int i = 0 ;
	int n = 0 ;
	string sentence = " " ;
	float count[256] = { 0 } ;

	 cout << "Enter your sentences ( End the sentence with . ): " << endl ;
	 getline(cin,sentence);

  for( int n = 0 ; n < sentence.length() ; n++ )
  {
    count[sentence[n]]++ ;
  }

  float mm = sentence.length() - 1 ;

  for ( n = 0 ; n < 256 ; n++ )
  {
    if ( isalpha ( ( char ) n ) )
    {
      cout << ( char ) n << " has " <<  count[n] << " occurrences " << " and has   " << count[n] / mm << endl ;
	
		hh[ n ] = ( count[ n ] / mm ) ;
		i++ ;
			 cout << "yay" << hh[n] << endl ;
	}
	int counter = 0 ;
	if( hh[n] != 0 )
	{
		counter++ ;
	}

  //int iarray[ARRAY_SIZE];
  int x, y, holder;

  // Bubble sort method.
  for(x = 0; x < counter ; x++)
    for(y = 0; y < x-1 ; y++)
      if(hh[n] > hh[n+1])
	  {
        holder = hh[n+1];
        hh[n+1] = hh[n];
        hh[n] = holder;
		cout << hh[n] << "yay" ;
      } 
  }
  
   return 0;
}

推荐答案

如何不排序?您的算法被彻底破坏了吗?还是因为浮点值不精确而使排序不正确吗?
How does it not sort ? Is your algorithm completely broken ? Or is it just not sorting quite right because float values are not precise ?


我认为您需要查看排序算法:Google可以为您提供帮助,但您可能需要查看一下,其中包含一个非常粗糙的冒泡排序,其效果会更好: http://www.metalshell.com/source_code/105/Bubble_Sort.html [ ^ ]
I think you need to look at your sort algorithm: Google can help you there, but you might want to look at this, which contains a very crude bubble sort that will work better than that: http://www.metalshell.com/source_code/105/Bubble_Sort.html[^]


int x, y, holder;

更改为

int x, y;
float holder;


这篇关于我为霍夫曼写这个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆