Solved programming problem

Save data Using Hashing in C++.

Source code

#include <iostream>

#include<string>

#include<string.h>

using namespace std;

struct data

{

    string name,type;

};

struct data allData[50];

int hashFunction(string name)

{

    int hashValue=0,i=0;

    while(name[i]!='')

    {

        hashValue+= (int)name[i];

        i++;

    }

    return hashValue%50;

}

void menu()

{

    cout<<"aPress 1 to Insert
Press 2 to Search
Press 3 to Delete
Press 4 to Show
Press 5 to Update
Press 0 to Exit"<<endl;

}

void insertVal()

{

    string str,name,type;

    int i=0;

    cout<<"Enter Your String(str1,str2): ";

    cin>>str;

    while(str[i]!=',')

    {

        if(str[i]==' ')

        {

            cout<<"Wrong input Formate 1"<<endl;

            return;

        }

        name += str[i];

        i++;

    }

    int j=0;

    name +='';

    i++;

    while(str[i]!='')

    {

        if(str[i]==',')

        {

            cout<<"Wrong input Formate 2"<<endl;

            return;

        }

        type +=str[i];

        j++;i++;

    }

    type +='';

    int val=hashFunction(name);

    if(allData[val].name=="" || allData[val].name=="DEL")

    {

      allData[val].name=name;

      allData[val].type=type;

    }

    else

    {

       while(allData[val].name != "")

       {

           if(allData[val].name==name && allData[val].type==type)

           {

               cout<<"Data Already Exist"<<endl;

           }

           val++;

           if(val>=50)

           {

             val=0;

           }

       }

      allData[val].name=name;

      allData[val].type=type;

    }

}

void deleteVal()

{

   string str,name,type;

   cout<<"Enter Your Delete String(str1,str2): ";

   cin>>str;

   int i=0;

    while(str[i]!=',')

    {

        name +=str[i];

        i++;

    }

    int j=0;

    name +='';

    i++;

    while(str[i]!='')

    {

        type +=str[i];

        j++;i++;

    }

    type +='';

    int val=hashFunction(name);

    i=0;

    if(allData[val].name=="")

    {

        cout<<"No Data Found"<<endl;

    }

    else

    {

       i=val;

       while(allData[i].name!="")

       {

           if(allData[i].name==name &&allData[i].type==type)

           {

               allData[i].name = "DEL";

               allData[i].type= "DEL";

               cout<<"Delete Successful"<<endl;

                return;

           }

           i++;

       }

       cout<<"No Data Found"<<endl;

       return;

    }

}

void show()

{

   for(int i=0;i<50;i++)

   {

       if(allData[i].name!="")

       {

        cout<<"Index: "<<i<<" Name: "<<allData[i].name<<" Type: "<<allData[i].type<<endl;

       }

   }

}

void update()

{

  string str1,str2,name1,type1,name2,type2;

  cout<<"Enter Update String: "<<endl;

  cin>>str1;

  cout<<"Enter New Element: "<<endl;

  cin>>str2;

  int i=0;

    while(str1[i]!=',')

    {

        name1 += str1[i];

        i++;

    }

    int j=0;

    name1 +='';

    i++;

    while(str1[i]!='')

    {

        type1 +=str1[i];

        j++;i++;

    }

    type1 +='';

    int val1=hashFunction(name1);

    //2nd

     i=0;

    while(str2[i]!=',')

    {

        name2 += str2[i];

        i++;

    }

     j=0;

    name2 +='';

    i++;

    while(str2[i]!='')

    {

        type2 +=str2[i];

        j++;i++;

    }

    type2 +='';

    int val2=hashFunction(name2);

}

void serachVal()

{

   string str,name,type;

   cout<<"Enter Your Search String(str1,str2): ";

   cin>>str;

   int i=0;

    while(str[i]!=',')

    {

        name +=str[i];

        i++;

    }

    int j=0;

    name +='';

    i++;

    while(str[i]!='')

    {

        type +=str[i];

        j++;i++;

    }

    type +='';

    int val=hashFunction(name);

    i=0;

    if(allData[val].name=="")

    {

        cout<<"No Data Found"<<endl;

    }

    else

    {

       i=val;

       while(allData[i].name!="")

       {

           if(allData[i].name==name &&allData[i].type==type)

           {

               cout<<"Search Successful"<<endl;

               cout<<"Name: "<<allData[i].name <<endl;

               cout<<"Type: "<<allData[i].type<<endl;

                return;

           }

           i++;

       }

       cout<<"No Data Found"<<endl;

       return;

    }

}

int main()

 {

    int choice;

    while(1)

    {

      menu();

      cin>>choice;

      if(choice==0) return 0;

      else if(choice==1) insertVal();

      else if(choice==2) serachVal();

      else if(choice==3)  deleteVal();

      else if(choice==4) show();

      else if(choice==5) update();

    }

    return 0;

}

Output

Inputs
The input to your program will be a sequence of twotuplcs, where each clement in each tuple is a string. An example of input sequence is given below:
• int, INTEGER
• my Function, FUNCTION
• x, ID
• 5, NUM The first element of each tuple will be the name of the record to be stored in the symbol table. Hence, you have to apply the hash function on the first element of each tuple that is the name.
Tasks
Implement the following functionalities:
 1 Insert a new symbol/name along with its type into the symbol table
 2. Search a symbol/name along with its type from the symbol table
 3. Delete a symbol/name along with its type into the symbol table
 4. Show the contents of symbol table in the console
 5. Update an already existing entry in the symbol table

 

Join the discussion

Add your comment