算法课第二次作业
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const string file_path = "/Users/sjy/Documents/data.csv";
const int table_length = 200;
const int p = 199;
template <class Data1, class Data2>
struct Node{
Data1 word;
Data2 paraphrase;
struct Node *next;
};
template <class Key, class Value>
class Hash{
public:
Hash();
~Hash();
bool load();
void insertByKey(Key key, Value value);
bool deleteByKey(Key key);
Value queryBykey(Key key);
void print();
private:
Node<Key,Value> *table[table_length];
int getKey(Key str);
};
template <class Key, class Value>
Hash<Key, Value>::Hash(){
for(int i = 0; i < table_length; i++){
this->table[i] = new Node<Key, Value>;
this->table[i]->next = NULL;
}
}
template <class Key, class Value>
Hash<Key, Value>::~Hash(){
for(int i = 0; i < table_length; i++){
while(this->table[i] != NULL){
Node<Key, Value> *temp = this->table[i];
this->table[i] = this->table[i]->next;
delete temp;
}
}
}
template <class Key, class Value>
bool Hash<Key, Value>::load(){
ifstream fin;
fin.open(file_path, ios::in);
if(!fin.is_open()){
return false;
}
string str;
getline(fin, str);
while(getline(fin, str)){
int pos = str.find(',');
Key key = str.substr(0,pos);
Value value = str.substr(pos+1);
// cout<<key<<" "<<value<<endl;
insertByKey(key, value);
}
return true;
}
template <class Key, class Value>
int Hash<Key, Value>::getKey(Key key){
if(is_same<Key, string>::value){
int ans = 0;
for(int i = 0; i < key.size(); i++) {
if(!(key[i] >= 'A' && key[i] <= 'Z' || key[i] >= 'a' && key[i] <= 'z')){
return -1;
}
if(key[i] >= 'A' && key[i] <= 'Z'){
key[i] = key[i] + 32;
}
ans += ((key[i] - 'a') + 1) % p;
}
return (ans % p);
}else {
if(key.size()==1){
return (key[0] % p);
}else{
int ans = 0;
for(int i = 0; i < key.size(); i++) {
ans += (key[i] - '0')*10 % p;
}
return ans;
}
}
}
template <class Key, class Value>
void Hash<Key, Value>::insertByKey(Key key,Value value){
int hash_key = getKey(key);
if(hash_key == -1){
return;
}
Node<Key, Value> *node = new Node<Key, Value>;
node->word = key;
node->paraphrase = value;
node->next = table[hash_key]->next;
table[hash_key]->next = node;
}
template <class Key, class Value>
bool Hash<Key, Value>::deleteByKey(Key key){
int hash_key = getKey(key);
bool flag = false;
Node<Key, Value> *temp = table[hash_key];
while(temp->next != NULL){
if(temp->next->word == key){
flag = true;
Node<Key, Value> *t = temp->next;
temp->next = temp->next->next;
delete t;
t = NULL;
}else{
temp = temp->next;
}
}
return flag;
}
template <class Key, class Value>
Value Hash<Key, Value>::queryBykey(Key key){
int hash_key = getKey(key), pos = 0, step = 1;
Node<Key, Value> *temp = table[hash_key];
Value value = "";
while(temp->next != NULL){
if(temp->next->word == key){
pos = step;
value = temp->next->paraphrase;
break;
}
temp = temp->next;
step++;
}
return value;
}
template <class Key, class Value>
void Hash<Key, Value>::print(){
for(int i = 0; i < table_length; i++){
if(table[i]->next != NULL){
cout<<i<<" : ";
Node<Key, Value> *temp = table[i];
while(temp->next){
cout<<temp->next->word<<" ";
temp = temp->next;
}
cout<<endl;
}
}
}
void test(Hash<string, string> *hash){
string key;
cin>>key;
string pos = hash->queryBykey(key);
if(pos!="") {
cout<<pos<<endl;
}else{
cout<<"暂未收录该词"<<endl;
}
// if(hash->deleteByKey(key)){
// cout<<"成功删除"<<endl;
// }else{
// cout<<"暂未收录该词"<<endl;
// }
// pos = hash->queryBykey(key);
// if(pos!="") {
// cout<<pos<<endl;
// }else{
// cout<<"暂未收录该词"<<endl;
// }
}
int main(){
cout<<"数据加载中..."<<endl;
Hash<string, string> hash;
if(hash.load()){
cout<<"数据加载完成!"<<endl;
// hash.print();
test(&hash);
// hash.print();
}else{
cout<<"文件打开失败,请检查路径是否正确!"<<endl;
}
return 0;
}