Monday, May 21, 2007

A mini Project source code----"HASHING"

/* Hashing ----- By ashesh deep "a beautiful mind" */
#include #include #include #include #include #include #include #include #include #include #include #include
int hash(char *key){ int sum=0; int len=strlen(key); if(len%2==1) len++; for(int j=0;jint makeaddress(char *key,int dept){ int retval=0; int hashval=hash(key); //cout<<"\n Hash value"<>1; } return retval;}
/********************************************************************** CLASS student**********************************************************************/int count;char stuff[100][100],stuff1[100][100];class student{ public: char *URN,*Name,*Addr,*Sem,*Bran; void Clear(); int Unpack(fstream&); int Pack(fstream&); void Input(int); void Display(); void Assign(student&); void Displayall(); //void format_text(char *);};
/********************************************************************** CLASS iobuf:PARENT CLASS FOR varlen AND delmfld BUFFER CLASS.**********************************************************************/class iobuf //AN ABSTRACT CLASS{ protected: char *Buffer; int BufSiz; public: void Clear(); virtual int Read(fstream&)=0; virtual int Write(fstream&)=0; virtual int Pack(student&,fstream&)=0; virtual int Unpack(student&,fstream&)=0;};
/********************************************************************** varlen: DEFINES READ AND WRITE OPERATION ON FILES. (Data is stored in buffer before writing or after reading)**********************************************************************/class varlen:public iobuf{ public: int Read(fstream&); int Write(fstream&);};
/********************************************************************* delmfld:DEFINES PACK AND UNPACK OPERATION FOR THE BUFFER*********************************************************************/class delmfld:public varlen{ public: int Pack(student&,fstream&); int Unpack(student&,fstream&);};void update(char *key){ student stds[100],std; int f=0,found=0,g; char upd='n'; fstream file("student.dat",ios::in); file.seekg(ios::beg); while(!file.fail()) if(std.Unpack(file)) if(strcmpi(std.URN,key)==0) //strmcpi { found=1; clrscr(); cout<<"\n\tRecord:"; std.Display(); cout<<"\n\n Confirm permanent updation:[Y/N]"; cin>>upd; if(!(upd=='Y'upd=='y')) { stds[f].Clear(); stds[f++].Assign(std);
} else { cout<<"\n\t Enter the new record:\n"; strcpy(std.URN,key); std.Input(1); stds[f].Clear(); stds[f++].Assign(std); //OVERWRITE } } else { stds[f].Clear(); stds[f++].Assign(std); } file.clear(); if(found==0) cout<<"\n\t Record not found."; else { file.close(); file.open("student.dat",ios::out); file.seekp(0,ios::beg); for(g=0;g/********************************************************************** DISPLAY FILE CONTENTS (i.e.ALL RECORDS SEQUENTIALLY.)**********************************************************************/void dispsq(){ student std; int i=0; fstream file("student.dat",ios::in); file.seekg(0,ios::beg); while(!file.fail()) if(std.Unpack(file)) { count++; std.Displayall(); strcpy(stuff[i],std.URN); i++; } else break; file.clear(); file.close();}
/******************************************************************** SEARCHING FOR A "USN" KEY********************************************************************/int search(char *key){ student std; int found=0; fstream file("student.dat",ios::in); file.seekg(ios::beg); while(!file.fail()) if(std.Unpack(file)) if(strcmpi(std.URN,key)==0) //COMPARE WITHOUT CASE SENSITIVITY { found=1; } //file.clear(); file.close(); return found;}
/********************************************************************** ADD A NEW RECORD TO THE DATA FILE.**********************************************************************/void append(){ student std; char key[30]; int flag=1,pos; std.Input(0); strcpy(key,std.URN); int result=search(key); if(result==1) { cout<<"\nRecord already present"; return; } //pos=file.tellp(); //flag=bt.insert(std.URN,pos); fstream file("student.dat",ios::app); file.seekp(0,ios::end); if(flag && std.Pack(file)) cout<<"\n\t Done."; else cout<<"\n\t Failure."; file.clear(); //db file.close();}
/********************************************************************** SEARCHING FOR GIVEN RECORD WITH URN AS KEY;**********************************************************************/void srch(char *key){ student std; int found=0; fstream file("student.dat",ios::in); file.seekg(ios::beg); while(!file.fail()) if(std.Unpack(file)) if(strcmpi(std.URN,key)==0) //COMPARE WITHOUT CASE SENSITIVITY { clrscr(); cout<<"\n\t Record found:"; cout<<"\n\n\t---------------------------------------------"; std.Display(); cout<<"\n\n\t---------------------------------------------"; found=1; } if (found==0) { cout<<"\n\t Record not found."; } file.clear(); file.close();}
/******************************************************************* DELETE RECORDS FROM FILE.*******************************************************************/void delrec(char *key){ int r=0, found=0,s; char del='N'; student stds[100],std; fstream file("student.dat",ios::in); file.seekg(0,ios::beg); while(!file.fail()) if(std.Unpack(file)) if(strcmpi(std.URN,key)==0) { found=1; clrscr(); cout<<"\n\t Record:"; cout<<"\n--------------------------------------------"; std.Display(); cout<<"\n--------------------------------------------"; cout<<"\n\t Confirm permanent deletion:\n Are you sure?(Y/N)"; cin>>del; if(!(del=='Y'del=='y')) { cout<<"\n\t Not deleted."; stds[r].Clear(); stds[r++].Assign(std); } else cout<<"\n\t Deleted."; } else { stds[r].Clear(); stds[r++].Assign(std); } file.clear(); if(found==0) cout<<"\n\t Record not found."; else { file.close(); file.open("student.dat",ios::out); file.seekp(0,ios::beg); for(s=0;s //bt.root=new node; //bt.create(); } void iobuf::Clear() { Buffer=new char[100]; BufSiz=0; }
/********************************************************************** READ RECORD INTO BUFFER AND FIND BUFFER LENGTH***********************************************************************/
int varlen :: Read(fstream &file){ if(file.fail()) return 0; Clear(); file.getline (Buffer,100,'\n'); BufSiz=strlen(Buffer); return 1;}
/********************************************************************* WRITE RECORDS IN FILE & APPEND '\n', THE RECORD DELIMETER*********************************************************************/int varlen :: Write (fstream &file ){ if(file.write(Buffer,BufSiz)) { file.write("\n",1); return 1; } return 0;}
/******************************************************************** PACK THE STUDENTS WITH DELIMETERS AND WRITE INTO FILE********************************************************************/int delmfld :: Pack (student &std,fstream &file){ Clear(); strcpy(Buffer,std.URN); //copy the value of the fields strcat(Buffer,""); //and add the record delimeter strcat(Buffer,std.Name); //after each field strcat(Buffer,""); strcat(Buffer,std.Addr); strcat(Buffer,""); strcat(Buffer,std.Sem); strcat(Buffer,""); strcat(Buffer,std.Bran); strcat(Buffer,'\0'); //add terminating character BufSiz = strlen (Buffer); return(Write (file)); //write into file}
/****************************************************************** READ RECORD FROM FILE INTO BUFFER AND UNPACK INTO STUDENT******************************************************************/int delmfld :: Unpack (student &std,fstream &file){ if (!Read (file)) return 0; std.Clear(); if(BufSiz>0) { int p=0,q; for(q=0;Buffer[q]!='';q++) std.URN[p++]=Buffer[q]; //unpack urn std.URN[p]='\0'; p=0;q++; for(;Buffer[q]!='';q++) std.Name[p++]=Buffer[q];//unpack name std.Name[p]='\0';p=0;q++; for(;Buffer[q]!='';q++) std.Addr[p++]=Buffer[q]; //unpack address std.Addr[p]='\0'; p=0;q++; for(;Buffer[q]!='';q++) std.Sem[p++]=Buffer[q]; //unpack Semester std.Sem[p]='\0'; p=0;q++; for(;q/******************************************************************** CLEAR STUDENT OBJECTS********************************************************************/void student :: Clear(){ URN=new char[100]; Name=new char[100]; Addr=new char[100]; Sem=new char[100]; Bran=new char[100];}
/******************************************************************** READ DATA********************************************************************/void student :: Input (int flUpd){ if(!flUpd) { Clear(); cout<<"\t URN (max 10 characters):"; cin>>URN; } cout<<"\t Name :";cin>> Name; // Input values for each field cout<<"\t Address :";cin>> Addr; cout<<"\t Semester[1-8] :";cin>>Sem; cout<<"\t Branch :";cin>>Bran;}

/******************************************************************* DISPLAY RECORD OF ONLY ONE STUDENT********************************************************************/void student:: Displayall(){ cout.setf(ios::left,ios::adjustfield); cout<<"\n"<
/******************************************************************** ASSIGN GIVEN STUDENT TO THIS STUDENT********************************************************************/void student :: Assign(student &std){ /*Assign student to this student*/ strcpy(URN,std.URN); strcpy(Name,std.Name); strcpy(Addr,std.Addr); strcpy(Sem,std.Sem); strcpy(Bran,std.Bran);}

/******************************************************************* PACK STUDENT INTO NEW BUFFER*******************************************************************/int student::Pack(fstream &file){ delmfld buffer; return (buffer.Pack(*this,file));
}

/****************************************************************** UNPACK BEFFER INTO STUDENT******************************************************************/int student::Unpack(fstream &file){ delmfld buffer ; return (buffer.Unpack(*this,file));}

/**************************THE main() PROGRAM*********************/void main(){ clrscr(); int ch=0,pos,depth,found=0; student stud; //bt.creat(); char key[100]; do { window(1,1,80,25); textbackground(BLACK); clrscr(); textcolor(RED); //clrscr(); cout<<"\n\t\t Internal Data File :student.dat:" <<"\n\t*********************************************************\n" <<"\n\t 1: Displays all records ." <<"\n\t 2: Add record into the file ." <<"\n\t 3: Search sequentially." <<"\n\t 4: Delete record." <<"\n\t 5: Update record." <<"\n\t 6: Hash Table." <<"\n\t 7: Search Hash able." <<"\n\t 0: Quit Program." <<"\n\t*********************************************************\n" <<"\n\t\t Plz enter your choice [0-7]:"; cin>>ch; switch(ch) { case 1: clrscr(); count=0; cout<<"\n\n\n\t\t\t\t STUDENT.DAT"; cout<<"\n********************************************************************************\n"; cout.setf(ios::left,ios::adjustfield); cout<<" "<<>>key; srch(key); break; case 4: cout<<"\n\t Enter the URN to delete:"; cin>>key; delrec(key); break; case 5: cout<<"\n\t Enter the URN to Update:"; cin>>key; update(key); break; case 6: int k=0,i,addr; cout<<"\n Enter depth"; cin>>depth; k=pow(2,depth); k=k; for(i=0;i clrscr(); cout<<"\n\t\t******** HASH TABLE **********"<\t KEY"<"<KEY"<KEY"<"<KEY"<>key; addr=makeaddress(key,depth); for(i=0;i0 && ch<6) { gotoxy(25,23); cout<<"\n\t Press any key to return to the Main Menu..."; } getch(); } while(ch!=0); //fstream f1("driver.dat",ios::out); //f1.close();}

1 comment:

Anonymous said...

Hi ashesh!

wonderfull codes on hashing technique.
i guess u will publish some codes on b+ tree n indexing...
keep going.
bye tc