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

📄 hash_long.cpp

📁 哈希表设计:数组,结构体
💻 CPP
字号:
// hash_long.cpp : Definiert den Einsprungpunkt f黵 die Konsolenanwendung.
//

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

#define STL_USING_STRING
#include "stl.h"

#include "hash_table.h"


/* my hash table is of type <long> */
typedef CHashTable<long> CLongHashT;


int main(int argc, char* argv[])
{
  CLongHashT MyHashTable;
  int        entries = 1000;
  char       random_key[40];
  
  printf("\n");
  printf("\n");
  printf("/********************************************/\n");
  printf("/*              WRITE TO THE MAP            */\n");
  printf("/********************************************/\n");
  printf("\n");
  while(entries)
  {
    long key_end = (rand()*32)/RAND_MAX + 1;
    long key_sum = 0;
    memset(random_key, 0, sizeof(random_key));
    
    /* calculate random key (string) and check_sum as value (long) */
    for(int pos = 0; pos < key_end; pos++)
    {
      random_key[pos] = (char)((rand()*26)/RAND_MAX + 65);
      /* key_sum = check_sum(key) */
      key_sum += random_key[pos];
    }

    /* allocate <long>, set to random value and add long to hash table */
    long* pLong = new long;
    if(pLong)
    {
      *pLong = key_sum;
      MyHashTable.AddKey(random_key, pLong);
      printf("Set \"%-32s\"   to       %6d\n", random_key, *pLong);
    }
    
    entries--;
  }
  
  printf("\n");
  printf("\n");
  printf("/********************************************/\n");
  printf("/*              READ FROM THE MAP           */\n");
  printf("/********************************************/\n");
  printf("\n");
  CLongHashT::iterator iter = MyHashTable.begin();
  long errors = 0;
  while(iter != MyHashTable.end())
  {
    /* get long member */
    std::string key = *iter;
    long* pLong = MyHashTable.GetMember(key);

    if(pLong)
    {
      long key_sum       = *pLong;
      long key_sum_check = 0;
      /* recalculate check_sum for compare */
      for(unsigned int pos = 0; pos < key.length(); pos++) key_sum_check += (long)key.at(pos);
    
      /* print result */
      printf("Get \"%-32s\"   as       %6d/%6d (%6d)\n", key.data(), key_sum, key_sum_check, key_sum - key_sum_check);
      if(key_sum != key_sum_check) errors++;
    }
    
    iter++;
  }
  
  printf("\n");
  printf("\n");
  printf("/********************************************/\n");
  printf("/*         COMPLETED with %6d errors     */\n", errors);
  printf("/********************************************/\n");
  printf("\n");
  
  /* remove all keys and free alloctated memory */
  MyHashTable.RemoveAllKey(true);
    
  return 0;
}

⌨️ 快捷键说明

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