文章标题
来源:互联网 发布:excel数据保护 编辑:程序博客网 时间:2024/05/16 10:32
#include<iostream>#include<string.h>#include<stdio.h>#include<string>#include<sstream>#include<fstream>#include <cstdlib>using namespace std;//关于hw4中命令怎么写得问题struct ListNode;typedef unsigned int uint;int size1 = 0;uint string_to_int(string s){ uint k; k= atoi(s.c_str()); return k;}struct elemtype{ string str; int type; int fre;};struct ListNode{ elemtype element; struct ListNode *Next;};class HashTable{public: int Tablesize; ListNode **Thelists;public: HashTable Init(int Tablesize); HashTable insert(elemtype Key, HashTable H1); int Hash(elemtype Key, int Tablesize); ListNode *Find(elemtype Key, HashTable H1); HashTable Delete(elemtype Key, HashTable H1); HashTable Rebuild(HashTable H1, int size, elemtype Harry[], int le, int fac); HashTable Build(HashTable H1, int size, elemtype Harry[], int le, int fac);};HashTable HashTable::Init(int Tablesize){ HashTable H1; int i; H1.Tablesize = Tablesize;// H1.Thelists = (ListNode**)malloc(sizeof(ListNode) * H1.Tablesize); H1.Thelists = new ListNode*[H1.Tablesize]; if (H1.Thelists == NULL) { exit(0); } for (i = 0; i < H1.Tablesize; i++) { //H1.Thelists[i] = (ListNode *)malloc(sizeof(struct ListNode));//从这一行开始读取字符串字符时出错,为啥好好的ListNode*结构到这里成了ListNode //但是同在结构体里的int类型能够读取//是不是malloc本身的问题 H1.Thelists[i] = new ListNode; if (H1.Thelists[i] == NULL) { exit(0); } else { H1.Thelists[i]->Next = NULL;//位置冲突怎么避免 } } return H1;}int HashTable::Hash(elemtype Key, int Tablesize){ uint hashval = 0; uint hash_value = 0; if (Key.type == 1) { hashval = string_to_int(Key.str); hash_value = hashval % Tablesize; } if (Key.type == 0) { int lens = Key.str.length(); for (int i = 0; i <lens; i++) { uint va = uint(Key.str[i]); //va = abs(va); va = va % Tablesize; for (int j = 0; j < lens - 1 - i; j++) { va = va * 256; va = va % Tablesize; } hash_value += va % Tablesize; } hash_value = hash_value%Tablesize; } return hash_value;}ListNode* HashTable:: Find(elemtype Key, HashTable H1){ ListNode* P; ListNode* L; int ha = Hash(Key, H1.Tablesize); L = H1.Thelists[ha]; P = L->Next; while (P != NULL && P->element.str != Key.str) { P = P->Next; } return P;}HashTable HashTable::insert(elemtype Key, HashTable H1){ ListNode* pos; ListNode* Newcell; ListNode* L; pos = Find(Key, H1); if (pos == NULL) { Newcell = new ListNode; L = H1.Thelists[Hash(Key, H1.Tablesize)];//要能记录插入字符的信息 Newcell->Next = L->Next; Newcell->element = Key; L->Next = Newcell;//什么问题? L->Next->element.fre = 1; size1 += 1; } else { pos->element.fre += 1; } return H1;}HashTable HashTable:: Delete(elemtype Key, HashTable H1){ ListNode* pos; ListNode* L; pos = Find(Key, H1); L = H1.Thelists[Hash(Key, H1.Tablesize)]; if (pos != NULL) { while (L->Next != pos) { L = L->Next; } L->Next = pos->Next; free(pos); } return H1;}HashTable HashTable:: Build(HashTable H1, int size, elemtype Harry[], int le, int fac){ int i = 0; size1 = 0; delete[]H1.Thelists; H1 = Init(le); while (size1 * 100 <= le*fac && i < size) { H1 = insert(Harry[i], H1); i += 1; } return H1;}HashTable HashTable::Rebuild(HashTable H1, int size, elemtype Harry[], int le, int fac){ int i = 0; H1 = Init(le); while (size1 * 100 <= le*fac && i < size) { H1 = insert(Harry[i], H1); i += 1; } while (size1 * 100 > le*fac) { le = le * 2 + 1; H1 = Build(H1, size, Harry, le, fac); } return H1;}class HashTable H;//class HashTable H2;int main(int argc,char** argv){ int length1 = argc;//如果是stringcin读的是一个字符串,如果是char或者int类型那么cin就是一个一个一个读。都会跳过空格 int len; int len1; int lenD; int factor; elemtype Hashz[10000]; string h; string D; elemtype Dele[10000]; for (int i = 0; i < length1; i++) { if (argv[i][1] == 'l') { string num; len = string_to_int(argv[i+1]); } else if (argv[i][1] == 'f') { factor = string_to_int(argv[i+1]); } else if (argv[i][1] == 'w') { char txt[10]; strcpy_s(txt,argv[i+1]); // ifstream ifs(txt, ifstream::in); ifstream ifs; ifs.open(txt); int i = 0; while (ifs>>h)//记住cin的判断条件 { int length = h.length(); int count = 0; for (int y = 0; y < length; y++) { uint x = h[y]; if (x >= 48 && x <= 57) { count++; if (count == length) { Hashz[i].str = h; Hashz[i].type = 1; break; } } else { Hashz[i].str = h; Hashz[i].type = 0; break; } } i += 1; } len1 = i; } else if (argv[i][1] == 's') { char txt1[10]; strcpy_s(txt1,argv[i+1]); ifstream ifs; ifs.open(txt1); int j = 0; while (ifs>>D) { int lengthD = D.length(); int count1 = 0; for (int y1 = 0; y1 < lengthD; y1++) { uint xy = uint(D[y1]); if (xy >= 48 && xy <= 57) { count1++; if (count1 == lengthD) { Dele[j].str = D; Dele[j].type = 1; break; } } else { Dele[j].str = D; Dele[j].type = 0; break; } } j += 1; } lenD = j; } } H=H.Rebuild( H, len1, Hashz, len, factor);//返回值的函数与不返回值的函数不一样 for (int j = 0; j < lenD; j++) { H=H.Delete(Dele[j], H); } cout << "[" << endl; for (int i1 = 0; i1 < H.Tablesize-1; i1++)//i = 8,23时 { cout << "{"; while(H.Thelists[i1]->Next != NULL)//为什么无缘无故就出错,但是一步一步调试就没有问题 { cout <<"\""<< H.Thelists[i1]->Next->element.str<<"\"" << ":" <<H.Thelists[i1]->Next->element.fre; H.Thelists[i1] = H.Thelists[i1]->Next; if(H.Thelists[i1]->Next != NULL) { cout << ","; } } cout << "}," << endl; } cout << "{"; while(H.Thelists[H.Tablesize-1]->Next != NULL) { cout<<"\"" << H.Thelists[H.Tablesize - 1]->Next->element.str<<"\""<< ":" << H.Thelists[H.Tablesize - 1]->Next->element.fre << ","; H.Thelists[H.Tablesize - 1] = H.Thelists[H.Tablesize - 1]->Next; if (H.Thelists[H.Tablesize - 1]->Next != NULL) { cout << ","; } } cout << "}" << endl; cout << "]" << endl; return 0;}//在vs下怎么使用命令行
以上是哈希表的构造函数,有几个细节
尽量不使用void有时候不能把新的哈希表的内容保存下来。
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- Linux内核源代码解析之——小弟我与神童聊Linux内核
- oracle数据库操作方式
- git学习
- 由于SSH配置文件的不匹配,导致的Permission denied (publickey)及其解决方法
- 价值交换的5个层次
- 文章标题
- 初学python1
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- SDWebImage最新的使用
- PHP模板smarty简单入门教程
- ActionContext和ServletActionContext
- Git 常用命令详解
- [看书日记20151227]Layout的过程,draw的过程,自定义view
- long类型字段转换成varchar2类型