Untitled

 avatar
unknown
plain_text
3 years ago
3.4 kB
1
Indexable
//============================================================================
// Name        : mock.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
//dictionary
//all functions
#include <iostream>
#include<string>
#define Size 10
using namespace std;

class node
{
private:
	string word;
	string meaning;
	node*next;
public:
	node()
	{
		word=" ";
		meaning=" ";
		next=NULL;

	}
	node (string word,string meaning)
	{
		this->word=word;
		this->meaning=meaning;
		this->next=NULL;
	}
	friend class LL;
};

class LL
{

public:
	node * start;
	node*last;
	LL()
	{
		start=NULL;
	}
	void insert(string word,string meaning)
	{
		node * temp=new node(word,meaning);
		if (start==NULL)
		{
			start=temp;
			last=temp;
			return;
		}
		else
		{
			node*curr=start;
			while(curr->next!=NULL)
			{
				curr=curr->next;
			}
			curr->next=temp;
			curr=temp;
		}
		return;
	};
	void display()
	{
		node* curr=start;
		while(curr!=NULL)
		{
			cout<<curr->word<<" : "<<curr->meaning<<endl;
			curr=curr->next;
		}
		return;
	}

	bool is_present(string words)
	{
		for(node*temp=start;temp!=NULL;temp=temp->next)
		{
			if (temp->word==words)
			{
				return 1;
			}
		}
		return 0;
	}
	bool sarch(string words)
	{
		node*curr=start;
		while(curr!=NULL)
		{
			if(curr->word==words)
			{
				return 1;
			}
			else
			{
				curr=curr->next;
			}
		}
		return 0;
	}
	 void update(string words,string meanings)
	{
		node*curr=start;
		while(curr!=NULL)
		{
			if(curr->word==words)
			{
				curr->meaning=meanings;
				return;
			}
			else
			{
				curr=curr->next;
			}

		}
		return;
	}

	friend class Dict;
};

class Dict
{
public:
	LL dict[Size];
	int hashfnc(string word)
	{
		int sum=0;
		for(int i=0;i<word.length();i++)
		{
			sum+=word[i];
		}
		return sum%Size;
	}
	void create()
	{
		int hi;
		bool tru;
		string word,meaning;
		cout<<"Enter Word"<<endl;
		cin>>word;
		cout<<"Enter Meaning"<<endl;
		cin>>meaning;
		hi=hashfnc(word);
		tru=dict[hi].is_present(word);
		if(tru==1)
		{
			cout<<"Word already present";
			return;
		}
		else
		{
			dict[hi].insert(word,meaning);
		}

	}
	void disp()
	{
		for(int i=0;i<Size;i++)
		{

			dict[i].display();
			cout<<endl;
		}
		return;
	}
	void search()
	{
		int hi;
				bool tru;
				string word,meaning;
				cout<<"Enter Word"<<endl;
				cin>>word;
				hi=hashfnc(word);
	    tru=dict[hi].sarch(word);
	    if(tru==1)
	    {
	    	cout<<"Found"<<endl;
	    }
	    else
	    {
	    	cout<<"Not Found"<<endl;
	    }

	}
	void update()
	{
		int hi;
				bool tru;
				string word,meaning;
				cout<<"Enter Word"<<endl;
				cin>>word;
				cout<<"Enter Meaning"<<endl;
				cin>>meaning;
				hi=hashfnc(word);
				dict[hi].update(word, meaning);

	}

};


int main() {
	Dict a;
	int ch;
//	a.create();
//	a.create();
//	a.create();
//	//a.create();
//	//a.create();
//	a.disp(); // prints !!!Hello World!!!
//	a.search();
//	a.update();
//	a.disp();
	bool tru=true;

	while(tru)
	{
		cout<<"1.Insert"<<endl;
		cout<<"2.display"<<endl;
		cout<<"3.Search"<<endl;
		cout<<"4.Update"<<endl;
		cout<<"EXIT"<<endl;
		cout<<"Enter choice :" ;
		cin>>ch;
		switch(ch)
		{
		case 1:
			a.create();
			break;
		case 2:
			a.disp();
			break;
		case 3 :
			a.search();
		case 4:
			a.update();
		case 5:
			tru=false;
		}

	};

	return 0;
}
Editor is loading...