Wednesday, 28 August 2013

Dictionary Program for insert,display,modify,search,delete & sort

//====================================
// Name        : dictionary.cpp
// Author      : your name
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//====================================

#include <iostream>
#include<string.h>
#include<stdlib.h>
#define SIZE 30

struct dict
{
    char keyword[20];
    char meaning[20];
}D[SIZE];

using namespace std;

int main()
{
    int choice,n=0;
    char ans;
    void init();
    int insert(int n);
    void display(int n);
    void modify(int n);
    void search(int n);
    void del(int n);
    void sort(int n);

    init();

    do
    {
        cout<<"Prpgram for dictionary";
        cout<<"\n\nDictionary Menu\n";
        cout<<"1.insert"<<endl<<"2.display"<<endl<<"3.Modify"<<endl<<"4.Search"<<endl<<"5.Delete"<<endl<<"6.sort";
        cout<<"\nEnter your choice\n";
        cin>>choice;
        switch(choice)
        {
        case 1:n=insert(n);
                break;
        case 2:display(n);
                break;
        case 3:modify(n);
                break;
        case 4:search(n);
                break;
        case 5:del(n);
                break;
        case 6:sort(n);
               display(n);
                break;
        default:cout<<"Wrong Entry !!!";
                break;
        }

        cout<<"Want to Enter more ??\n";
        cin>>ans;
    }while(ans=='y'||ans=='Y');

    return 0;
}

void init()
{
    int i;
    for(i=0;i<SIZE;i++)
        {
            strcpy(D[i].keyword,"");
            strcpy(D[i].meaning,"");

        }
}

int insert(int i)
{
    if(i<SIZE)
    {
        cout<<"\nEnter the keyword : ";
        cin>>D[i].keyword;
        cout<<"\nEnter the meaning : ";
        cin>>D[i].meaning;
        i++;
    }
    else
        cout<<"Database full";
    return i;
}

void display(int n)
{
    int i;
    for(i=0;i<SIZE;i++)
    {
        if(strcmp(D[i].keyword,"")!=0)
        {
            {
                cout<<D[i].keyword<<"\t\t"<<D[i].meaning<<endl;
            }
        }
    }
}

void search(int n)
{
    int i,flag=0;
    int count=0;
    char key[20];

    cout<<endl<<"Enter the keyword to be searched";
    cin>>key;

    for(i=0;i<n;i++)
    {
        count++;
        if(strcmp(D[i].keyword,key)==0)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
    {
        cout<<endl<<"Meaning : "<<D[i].meaning;
        cout<<endl<<"Totle comparisons are "<<count<<endl;
    }
    else
        cout<<endl<<"The keyword is not present";
}


void modify(int n)
{
    int i;
    char newkey[20],newmean[20];
    char key[20];

    cout<<endl<<"Enter the keyword for the entry to be modified";
    cin>>key;

    for(i=0;i<n;i++)
    {
        if((strcmp(D[i].keyword,key))==0)
        {
            cout<<"Enter the new keyword : "<<endl;
            cin>>newkey;
            cout<<"Enter the new meaning : "<<endl;
            cin>>newmean;
            strcpy(D[i].keyword,newkey);
            strcpy(D[i].meaning,newmean);
            cout<<endl<<"The entry is modified !!\n";
        }
    }
}


void del(int n)
{
    int i,flag=0;
    char key[20];
    cout<<endl<<"Enter the keyword to be deleted ";
    cin>>key;

    for(i=0;i<n;i++)
    {
        if(strcmp(D[i].keyword,key)==0)
        {
            strcpy(D[i].keyword,"");
            strcpy(D[i].meaning,"");
            flag=1;
        }
    }
    if(flag==0)
    {
        cout<<endl<<"The word is not in dictionary";
    }
}

void sort(int n)
{
    int i,j;
    char tempkey[20];
    char tempmean[20];

    for(i=0;i<n-1;i++)
        for(j=i+1;j<n;j++)
        {
            if(strcmp(D[i].keyword,D[j].keyword)>0)
            {
            strcpy(tempkey,D[i].keyword);
            strcpy(tempmean,D[i].meaning);

            strcpy(D[i].keyword,D[j].keyword);
            strcpy(D[i].meaning,D[j].meaning);

            strcpy(D[j].keyword,tempkey);
            strcpy(D[j].meaning,tempmean);

            }
        }
}


                                          Output of Program--

Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
1

Enter the keyword : bird

Enter the meaning : bird
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
1

Enter the keyword : cat

Enter the meaning : animal
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
1

Enter the keyword : dog

Enter the meaning : animal
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
1

Enter the keyword : ant

Enter the meaning : ant
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
2
bird        bird
cat        animal
dog        animal
ant        ant
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
6
ant        ant
bird        bird
cat        animal
dog        animal
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
3

Enter the keyword for the entry to be modifiedant
Enter the new keyword :
ant
Enter the new meaning :
animal

The entry is modified !!
Want to Enter more ??
y
Prpgram for dictionary

Dictionary Menu
1.insert
2.display
3.Modify
4.Search
5.Delete
6.sort
Enter your choice
4

Enter the keyword to be searchedcat

Meaning : animal
Totle comparisons are 3
Want to Enter more ??
n

No comments:

Post a Comment