Untitled

 avatar
unknown
plain_text
13 days ago
3.6 kB
4
Indexable
#include<iostream>
#include<fstream>
#include<string>
#include<cctype>

using namespace std;

const int TABLE_SIZE = 26;

struct Node
{
	string english;
	string vietnamese;
	Node* next;
};

Node* hashTable[TABLE_SIZE];

void InitHashTable()
{
	for (int i = 0; i < TABLE_SIZE; i++)
		hashTable[i] = NULL;
}

int hashFunction(string key)
{
	char c = tolower(key[0]);
	if (c < 'a' || c >'z')
		return 0;
	return c - 'a';
}

//Them mot tu moi vao tu dien
void insertWord(string eng, string vie)
{
	int index = hashFunction(eng);
	Node* p = new Node;
	p->english = eng;
	p->vietnamese = vie;
	p->next = hashTable[index];
	hashTable[index] = p;
}

//Doc du lieu tu file
void loadFromFile(string filename)
{
	ifstream fin(filename);
	if (!fin)
	{
		cout << "Khong mo duoc file!\n";
		return;
	}
	string line;
	while (getline(fin, line))
	{
		size_t pos = line.find(':');
		if (pos != string::npos)
		{
			string eng = line.substr(0, pos);
			string vie = line.substr(pos + 1);
			insertWord(eng, vie);
		}
	}
	fin.close();
	cout << "Doc file thanh cong.\n";
}

//Tra nghia 
void searchWord(string eng)
{
	int index = hashFunction(eng);
	Node* p = hashTable[index];
	while (p != NULL)
	{
		if (p->english == eng)
		{
			cout << "Nghia: " << p->vietnamese;
			return;
		}
		p = p->next;
	}
	cout << "Tu ban tim khong ton tai.";
}

//Xoa mot tu
void deleteWord(string eng)
{
	int index = hashFunction(eng);
	Node* p = hashTable[index];
	Node* prev = NULL;
	while (p != NULL)
	{
		if (p->english == eng)
		{
			if (prev == NULL)
				hashTable[index] = p->next;
			else
				prev->next = p->next;
			delete p;
			cout << "Da xoa!\n";
			return;
		}
		prev = p;
		p = p->next;
	}
	cout << "Khong tim thay tu!\n";
}

//Xoa toan bo tu dien
void clearDictionary()
{
	for (int i = 0; i < TABLE_SIZE; i++)
	{
		Node* p = hashTable[i];
		while (p != NULL)
		{
			Node* temp = p;
			p = p->next;
			delete temp;
		}
		hashTable[i] = NULL;
	}
	cout << "Da xoa toan bo tu dien.\n";
}

//Hien thi
void display()
{
	for (int i = 0; i < TABLE_SIZE; i++)
	{
		cout << char('A' + i) << ": ";
		Node* p = hashTable[i];
		while (p != NULL)
		{
			cout << "[ " << p->english << ": " << p->vietnamese << "] ->";
			p = p->next;
		}
		cout << "NULL\n";
	}
}

int main()
{
	InitHashTable();
	int choice;
	do
	{
		cout << "\n====TU DIEN ANH - VIET====\n";
		cout << "1. Doc du lieu tu file.\n";
		cout << "2.Them mot tu moi vao tu dien.\n";
		cout << "3. Tra nghia cua mot tu tieng anh.\n";
		cout << "4. Xoa mot tu trong tu dien.\n";
		cout << "5. Xoa toan bo tu dien.\n";
		cout << "6. Hien thi tu dien.\n";
		cout << "0. Thoat.\n";
		cout << "Chon: ";
		cin >> choice;
		cin.ignore();

		switch (choice)
		{
		case 1:
		{
			string filename;
			cout << "Nhap ten file:";
			getline(cin, filename);
			loadFromFile(filename);
			break;
		}
		case 2:
		{
			string eng, vie;
			cout << "Nhap tu tieng anh: ";
			getline(cin, eng);

			cout << "Nhap nghia tieng viet: ";
			getline(cin, vie);
			insertWord(eng, vie);
			break;
		}
		case 3:
		{
			string eng;
			cout << "Nhap tu can xoa:";
			getline(cin, eng);
			deleteWord(eng);
			break;
		}
		case 4:
		{
			string eng;
			cout << "Nhap tu can xoa: ";
			getline(cin, eng);
			deleteWord(eng);
			break;
		}
		case 5:
		{
			clearDictionary();
			break;
		}
		case 6:
		{
			display();
			break;
		}
		}
	}
	while (choice != 0);
	clearDictionary();
	return 0;
}
Editor is loading...
Leave a Comment