Dictionary CPP

#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
class dictionary
{
private:
    struct wordentry
    {
        char word[30],mean[100];
        struct wordentry *prev,*next;
    }*head;
public:
    void crt();
    void inst();
    void disp();
    void search();
    void modi();
    void del();
    dictionary()
    {
        head=NULL;
    }
};

void dictionary::crt()
{
    cout<<"\nCreating a Dictionary.\n";
    inst();
}

void dictionary::inst()
{
    int flag=0,ch;
    wordentry *temp,*new1,*down;
    do
    {
        new1=new wordentry;
        cout<<"\nEnter a word to add to The Dictionary:";
        cin>>new1->word;
        cout<<"\nEnter the meaning of the word: ";
        cin>>new1->mean;
        if(head==NULL)
        {
            head=new1;
            head->next=head;
            head->prev=head;
        }
        else if(strcmp(new1->word,head->word)<0)
        {
            temp=head->prev;
            new1->next=head;
            new1->prev=temp;
            head->prev=new1;
            temp->next=new1;
            head=head->prev;
        }
        else if(strcmp(new1->word,head->word)>0)
        {
            temp=head;
            do
            {

                if(strcmp(new1->word,temp->word)==0)
                {
                    cout<<"\nWord already is in dictionary.\n";
                    break;
                }
                else if(strcmp(new1->word,temp->word)<0)
                {
                    down=temp->prev;
                    new1->next=temp;
                    new1->prev=down;
                    down->next=new1;
                    temp->prev=new1;
                    break;
                }
                else
                {
                    temp=temp->next;
                }
            }while(temp!=head);
        }
        temp=head->prev;
        if(strcmp(new1->word,temp->word)>0)
        {
            new1->next=head;
            new1->prev=temp;
            head->prev=new1;
            temp->next=new1;
        }
        cout<<"\nPress 1 to if you want to continue adding words: ";
        cin>>ch;
    }while(ch==1);
}

void dictionary::disp()
{
    int ch;
    wordentry *temp;
    temp=head;
    cout<<"\nSorting Dictionary\n";
    cout<<"\n1.Ascending Order\n2.Descending Order\n3.Go back to Main Menu\nEnter Your choice:";
    cin>>ch;
    cout<<"WORD\t:\tMEANING\n";
    switch(ch)
    {
    case 1 : if(head==NULL)
             {
                 cout<<"\nDICTIONARY IS EMPTY.\n";
                 break;
             }
             else
             {
                 do
                  {
                     cout<<temp->word<<"\t:\t"<<temp->mean<<"\n";
                     temp=temp->next;
                 }while(temp!=head);
             }
             printf("\n");
             break;
    case 2 : if(head==NULL)
             {
                 cout<<"\nDICTIONARY IS EMPTY.\n";
                 break;
             }
             else
             {
                 do
                 {
                     temp=temp->prev;
                     cout<<temp->word<<"\t:\t"<<temp->mean<<"\n";
                 }while(temp!=head);
             }
             printf("\n");
             break;
    case 3 : exit(0);
    }
}

void dictionary::search()
{
    int flag=0,ch;
    char key[30];
    wordentry *temp;
    do
    {
        temp=head;
        cout<<"\nEnter the word to be searched: ";
        cin>>key;
        if(temp==NULL)
        {
            cout<<"\nDICTIONARY IS EMPTY.\n";
        }
        else if(temp!=NULL)
        {
            do
            {
                if(strcmp(key,temp->word)==0)
                {
                    cout<<"\nWord Found\nWORD     :      MEANING\n";
                    cout<<temp->word<<"\t:\t"<<temp->mean;
                    flag=1;
                    break;
                }
                temp=temp->next;
            }while(temp!=head);
        }
        if(flag==0)
        {
            cout<<"\nWORD NOT FOUND.\n";
        }
        cout<<"\nPress 1 to if you want to continue searching words: ";
        cin>>ch;
    }while(ch==1);
}

void dictionary::del()
{
    char key[30];
    int ch,flag=0;
    wordentry *temp,*up,*down;
    do
    {
        temp=head;
        cout<<"\nSearch and Delete.\n";
        cout<<"\nEnter the word to be deleted: ";
        cin>>key;
        if(head==NULL)
        {
            cout<<"\nDICTIONARY IS EMPTY.\n";
        }
        else if(head!=NULL)
        {
            do
            {
                if(strcmp(key,temp->word)==0)
                {
                    cout<<"\nWORD\t\t:\t\tMEANING\n";
                    cout<<temp->word<<"\t\t:\t\t"<<temp->mean;
                    flag=1;
                    cout<<"\nAre You sure You want to delete this word(1 for yes)? : ";
                    cin>>ch;
                    if(ch==1)
                    {
                        if(temp->next==temp)
                        {
                            head=NULL;
                        }
                        else if(temp==head)
                        {
                            up=temp->next;
                            down=temp->prev;
                            down->next=up;
                            up->prev=down;
                            head=up;
                        }
                        else
                        {
                            up=temp->next;
                            down=temp->prev;
                            down->next=up;
                            up->prev=down;
                        }
                        cout<<"\nTHE WORD HAS BEEN DELETED.\n";
                        break;
                    }
                }
                temp=temp->next;
            }while(temp!=head);
        }
        if(flag==0)
        {
            cout<<"\nWORD NOT IN THE DICTIONARY.\n";
        }
        cout<<"\nPress 1 to if you want to continue deleting words: ";
        cin>>ch;
    }while(ch==1);
}

void dictionary::modi()
{
    int flag=0,ch;
    char key[30];
    wordentry *temp;
    do
    {
        temp=head;
        cout<<"\nEnter the word to be modified: ";
        cin>>key;
        if(head==NULL)
        {
            cout<<"\nDICTIONARY IS EMPTY.\n";
            flag=1;
        }
        else if(head!=NULL)
        {
            do
            {
                if(strcmp(key,temp->word)==0)
                {
                    cout<<"Word Found:\t"<<temp->word;
                    cout<<"\nEnter its new Meaning: ";
                    cin>>temp->mean;
                    flag=1;
                    break;
                }
                else
                {
                    temp=temp->next;
                }
            }while(temp!=head);
        }
        if(flag==0)
        {
            cout<<"\nWORD NOT FOUND.\n";
        }
        cout<<"\nPress 1 to if you want to continue modifying words: ";
        cin>>ch;
    }while(ch==1);
}

int main()
{
    int ch;
    dictionary menu;
    do
    {
        cout<<"Menu\n1.Create\n2.display\n3.Insert\n4.Modify\n5.Search\n6.Search and Delete\n7.Exit\nEnter Your choice:";
        cin>>ch;
        switch (ch)
        {
        case 1 : menu.crt();
                break;
        case 2 : menu.disp();
                break;
        case 3 : menu.inst();
                break;
        case 4 : menu.modi();
                break;
        case 5 : menu.search();
                break;
        case 6 : menu.del();
                break;
        case 7 : exit(0);
        }
    }while(ch!=7);
    return 0;
}
SHARE
    Blogger Comment
    Facebook Comment