📄 d_13_1.cpp
字号:
#include "stdafx.h"
#include <string>
#include "List.h"
#include "Hash.h"
#include <iostream>
#include <fstream>
using namespace std;
template <class Type>
int hashTable <Type> ::hashFunction(const Type &k)
{ int len =k.length(), key;
if (len<=1)
key = k[0];
else
key = k[0] + k[len-1];
return (key% numBuckets);
}
class Element {
public:
string word;
int num;
Element ()
{}
Element (string w,int n=1)
:word(w),num(n)
{}
};
void main()
{ string word;
Element frequence[100];
int current=0;
hashTable <string> a(20,10);
fstream file;
file.open("sample.txt",ios::in);
if(!file)
{ cerr << "文件打开失败"<<endl;
exit(-1);
}
while (file >> word)
{ if (!a.find(word))
{ a.insert(word);
Element temp(word);
frequence[current++] = temp;
}
else
for (int i=0;i<current;++i)
if (frequence[i].word == word)
{ frequence[i].num+=1;
break;
}
}
for (int i=0;i<current;++i)
cout << frequence[i].word << ":" <<frequence[i].num << endl;
file.close();
cin.get(); //等待结束,以便调测程序,可以删除
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -