Sunday, 18 November 2012

FS lab program for secondary indexing


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class secondary_index
{
  public:
      string Name_list[100];
      int Address_list[100];
      int count;

      void create_index();
      void insert();
      void remove(string);
      void delete_from_file(int);
      void search(string);
      int search_index(string);
      void read_from_file(int);
      string extract_Name(string);
      void sort_index();
     
};

void secondary_index::create_index()
{
      fstream file;
      int pos;
      string buffer,Name;
      count=-1;
      file.open("1.txt",ios::in);
      while (!file.eof())
      {
            pos=file.tellg();      
            buffer.erase();        
            getline(file,buffer);
            if (buffer.empty()) break; //imp since it reads the last \n and goes to new line
            Name=extract_Name(buffer);
            Name_list[++count]=Name;
            Address_list[count]=pos;     
      }
      file.close();    
      sort_index();          
}

string secondary_index::extract_Name(string buffer)
{
      string USN,Name;
      int i=0;
      USN.erase();     
      while (buffer[i]!='|')
        USN+=buffer[i++];

      Name.erase();
      i++;
      while (buffer[i]!='|')
        Name+=buffer[i++];

      return Name;
}
void secondary_index::sort_index()
{
      int i,j,temp_Address;
      string temp_Name;
      for (int i=0;i<=count;i++)
      {
        for (int j=i+1;j<=count;j++)
        {
            if (Name_list[i]>Name_list[j])
            {
              temp_Name=Name_list[i];
              Name_list[i]=Name_list[j];
              Name_list[j]=temp_Name;
              
              temp_Address=Address_list[i];
              Address_list[i]=Address_list[j];
              Address_list[j]=temp_Address;
      }}}
     
}
void secondary_index::insert()
{
      string USN, Name, Branch, sem, buffer;
      int Semester,pos;
      fstream file;    
      cout<<"\nUsn:";
      cin>>USN;
      cout<<"\nName:";
      cin>>Name;
      cout<<"\nBranch:";
      cin>>Branch;
      cout<<"\nSemster:";
      cin>>Semester;
      stringstream out;
      out << Semester;
      sem = out.str(); 
      buffer.erase();
      buffer=USN+'|'+Name+'|'+Branch+'|'+sem+'$'+'\n';
      file.open("1.txt",ios::out|ios::app);
      pos=file.tellp();
      file<<buffer;
      file.close();
      Name_list[++count]=Name;
      Address_list[count]=pos;
      sort_index();          
}

int secondary_index::search_index(string key)
{
      int low=0,high=count,mid=0,flag=0,pos;
      while (low<=high)
      {
            mid=(low+high)/2;
            if (Name_list[mid]==key) {flag=1; break;}
            if (Name_list[mid]>key) high=mid-1;
            if (Name_list[mid]<key) low=mid+1;
      }
      if (flag) return mid;
      else return 0;
}

void secondary_index::search(string key)
{
      int pos=0,t;
      string buffer;
     
      buffer.erase();  
      pos=search_index(key);
      if (pos){
            read_from_file(pos);
            t=pos;
            while (Name_list[++t]==key) read_from_file(t);
            t=pos;
            while (Name_list[--t]==key) read_from_file(t); 
            }
      else cout<<"\nNot Found";
}

void secondary_index::read_from_file(int pos)
{
      int address;     
      string buffer;
      fstream file;
      file.open("1.txt");
      address=Address_list[pos];
      file.seekp(address,ios::beg);
      getline(file,buffer);
      cout<<"\nFound the record:  "<<buffer;   
      file.close();
}

void secondary_index::remove(string key)
{
     
      int pos=0,t,choice;
      string buffer;
      buffer.erase();  
      pos=search_index(key);
      if (pos){
            read_from_file(pos);
            cout<<"\n Delete?";
            cin>>choice;
            if (choice) delete_from_file(pos);
            t=pos;
            while (Name_list[++t]==key){
                  read_from_file(t);
                  cout<<"\n Delete?";
                  cin>>choice;
                  if (choice) delete_from_file(t);
                  }
            t=pos;
            while (Name_list[--t]==key){
                  read_from_file(t);
                  cout<<"\n Delete?";
                  cin>>choice;
                  if (choice) delete_from_file(t);               
                  }
            }
      else cout<<"\n Not Found";
}

void secondary_index::delete_from_file(int pos)
{
      char del_ch='*';
      int i, address;
      fstream file;
      file.open("1.txt");
      address=Address_list[pos];
      file.seekp(address,ios::beg);
      file.put(del_ch);
      cout<<"\nRecord Deleted:  ";
     
      for (i=pos;i<count;i++)
            {
                  Name_list[i]=Name_list[i+1];
                  Address_list[i]=Address_list[i+1];
            }
      count--;
}
     
int main()
{
      int choice;
      string key;
      secondary_index i1;
     
      i1.create_index();
     
      while(1){
      cout <<"\nMain Menu\n------------\n1.Add  \n2.Search \n3.Delete \n4.EXIT\n---------------\nEnter the choice:";
      cin>>choice;     
      switch (choice)
      {
            case 1: cout<<"Data\n";
                  i1.insert();
                  break;
            case 2: cout <<"\n\nEnter the name";
                  cin>>key;
                  i1.search(key);
                  break;
            case 3:cout <<"\n\nEnter the name";
                  cin>>key;
                  i1.remove(key);
                  break;

            case 4: return 0;
            default:cout<<"\n\nWrong Choice";
                  break;
      }}
}
     
     
           

Saturday, 17 November 2012

ip lab program to check and validate the usn and semester in javascript


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function match()
{
var usn,sem,s1,s2;
usn=document.getElementById("USN").value;
sem=document.getElementById("SEM").value;
s1=usn.search(/^[1-4][A-Z]{2}[0-9]{2}[A-Z]{2}[0-9]{3}$/);
if(s1>=0)
alert("This is a valid Usn number");
else
alert("This is not a valid Usn number");
s2=sem.search(/^[1-8]$/);
if(s2>=0)
alert("This is a valid semester");
else
alert("This is not a valid semester.It must be a number between 1 and 8");
}
</script>
</head>
<body>
<form name="getusn" action="">
<p>USN:<input type="text" id="USN" /> </p>
<p>semester:<input type="text" id="SEM" /> </p>
<p><input type="submit" value="click" onclick="match();" /> </p>
</form>
</body>
</html>


Fs lab program for primary indexing


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class primary_index
{
  public:
      string USN_list[100];
      int Address_list[100];
      int count;

      void create_primary_index();
      void insert();
      void remove(string);
      void search(string);
      int search_primary_index(string);
      string extract_USN(string);
      void sort_primary_index();
     
};

void primary_index::create_primary_index()
{
      fstream file;
      int pos;
      string buffer,USN;
      count=-1;
      file.open("1.txt",ios::in);
      while (!file.eof())
      {
            pos=file.tellg();      
            buffer.erase();        
            getline(file,buffer);
            if (buffer.empty()) break; //imp since it reads the last \n and goes to new line
            USN=extract_USN(buffer);
            USN_list[++count]=USN;
            Address_list[count]=pos;     
      }
      file.close();    
      sort_primary_index();        
}

string primary_index::extract_USN(string buffer)
{
      string USN;
      int i=0;
      USN.erase();     
      while (buffer[i]!='|')
        USN+=buffer[i++];
      return USN;
}
void primary_index::sort_primary_index()
{
      int i,j,temp_Address;
      string temp_USN;
      for (int i=0;i<=count;i++)
      {
        for (int j=i+1;j<=count;j++)
        {
            if (USN_list[i]>USN_list[j])
            {
              temp_USN=USN_list[i];
              USN_list[i]=USN_list[j];
              USN_list[j]=temp_USN;
              
              temp_Address=Address_list[i];
              Address_list[i]=Address_list[j];
              Address_list[j]=temp_Address;
      }}}
     
}
void primary_index::insert()
{
      string USN, Name, Branch, sem, buffer;
      int Semester,pos;
      fstream file;    
      cout<<"\nUsn:";
      cin>>USN;
      cout<<"\nName:";
      cin>>Name;
      cout<<"\nBranch:";
      cin>>Branch;
      cout<<"\nSemster:";
      cin>>Semester;
      stringstream out;
      out << Semester;
      sem = out.str(); 
      buffer.erase();
      buffer=USN+'|'+Name+'|'+Branch+'|'+sem+'$'+'\n';
      file.open("1.txt",ios::out|ios::app);
      pos=file.tellp();
      file<<buffer;
      file.close();
      USN_list[++count]=USN;
      Address_list[count]=pos;
      sort_primary_index();        
}

int primary_index::search_primary_index(string key)
{
      int low=0,high=count,mid=0,flag=0,pos;
      while (low<=high)
      {
            mid=(low+high)/2;
            if (USN_list[mid]==key) {flag=1; break;}
            if (USN_list[mid]>key) high=mid-1;
            if (USN_list[mid]<key) low=mid+1;
      }
      if (flag) return mid;
      else return 0;
}

void primary_index::search(string key)
{
      int pos=0,address;
      string buffer;
      fstream file;
      buffer.erase();  
      pos=search_primary_index(key);
      if (pos){
            file.open("1.txt");
            address=Address_list[pos];
            file.seekp(address,ios::beg);
            getline(file,buffer);
            cout<<"\nFound the record:  "<<buffer;   
            file.close();    
            }
      else cout<<"\nNot Found";
}

void primary_index::remove(string key)
{
      int pos=0,address,i;
      char del_ch='*';
      fstream file;
      pos=search_primary_index(key);
      if (pos){
            file.open("1.txt");
            address=Address_list[pos];
            file.seekp(address,ios::beg);
            file.put(del_ch);
            cout<<"\nRecord Deleted:  "; 
            file.close();    

            for (i=pos;i<count;i++)
            {
                  USN_list[i]=USN_list[i+1];
                  Address_list[i]=Address_list[i+1];
            }
            count--;
      }
      else cout<<"\nNot Found";
}
     
int main()
{
      int choice;
      string key;
      primary_index i1;
     
      i1.create_primary_index();
     
      while(1){
      cout <<"\nMain Menu\n------------\n1.Add  \n2.Search \n3.Delete \n4.EXIT\n---------------\nEnter the choice:";
      cin>>choice;     
      switch (choice)
      {
            case 1: cout<<"Data\n";
                  i1.insert();
                  break;
            case 2: cout <<"\n\nEnter the USN to search";
                  cin>>key;
                  i1.search(key);
                  break;
            case 3:cout <<"\n\nEnter the USN to delete";
                  cin>>key;
                  i1.remove(key);
                  break;
            case 4: return 0;
            default:cout<<"\n\nWrong Choice";
                  break;
      }}
}
     
     
           
 

IP javascript program to validate gmail id page


<?xml version ="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "_//w3c//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>GMail id page</title>
<script language="javascript">
function username()
{
var a,ser;
a=document.getElementById("user name");
ser=a.value.search(/^\w+@\w+\.com$/);
if(ser!=0)
{
alert("Enter valid email id");
a.focus();
a.select();
}
}
function fname()
{
var a;
a=document.getElementById("fcheck");
if(a.value.length == 0)
{
alert("Name cant be empty");
a.focus();
}
}
function lenpw()
{
var a;
a=document.getElementById("password");
if(a.value.length>7)
;
else
{
alert("Password length must be more than 8");
a.focus();
a.select();
}
}
function pw2()
{
var a,b;
a=document.getElementById("password");
b=document.getElementById("passwordre");
if(a.value!=b.value)
{
alert("Passwords dont match");
a.focus();
a.select();
}
}
function phone()
{
var a,ser;
a=document.getElementById("phoneno");
ser=a.value.search(/^\d+$/);
if(ser!=0)
{
alert("enter a valid phone number");
a.focus();
a.select();
}
}
function day1()
{
var a;
a=document.getElementById("Day");
if(a.value>0)
{if(a.value>31)
{
alert("enter a valid date");
a.focus();
a.select();
}
}
else
{
alert("enter a valid date");
a.focus();
a.select();
}
}
function year1()
{
var a,day=new Date();
a=document.getElementById("Year");
if(a.value>day.getFullYear())
{
alert("enter a year before "+day.getFullYear());
a.focus();
a.select();
}
}
function email()
{
var a,ser;
a=document.getElementById("e-mail");
ser=a.value.search(/^\w+@\w+\.com$/);
if(ser!=0)
{
alert("Enter valid email id");
a.focus();
a.select();
}
}
function loc()
{
var a;
a=document.getElementById("location");
if(a.value.length==0)
{
alert("Location cant be empty");
a.focus();
}
}
function tnc()
{
var a;
a=document.getElementById("terms");
if(!a.checked)
{
alert("You must agree to our terms and conditions to proceed");
a.focus();
}
}
</script>
</head>
<body>
<img src="google_logo.png" />
<h1 style="color:red">Create a new Google account </h1>
<form action=" " >
<p><label>Name <br /><input type="text" id="fcheck" size="30" onblur="fname();" /></label></p>
<p><label><input type="text" id="laname" size="30" onblur="lname();"/></label></p>
<p><label>Choose your username <br /><input type="text" id="user name" size="30" onblur="username();" /></label></p>
<p><label>Create a new password <br /><input type="password" id="password" size="30" onblur="lenpw();" /></label></p>
<p><label>Confirm your password <br /><input type="password" id="passwordre" size="30" onblur="pw2();" /></label></p>
<p>Birthday(MM/DD/YYYY)<br />
<select name="month">
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option></select>
<label><input type="text" id="Day" size="2" onblur="day1();" /></label>
<label><input type="text" id="Year" size="4" onblur="year1();"/></label></p>
<p>Gender<br />
<select name="Gender">
<option>Male</option>
<option>Female</option></select></p>
<p><label>Mobile Phone <br /><input type="text" id="phoneno" size="15" onblur="phone();"/></label></p>
<p><label>Current e-mail address <br /><input type="text" id="e-mail" size="20" onblur="email();"/></label></p>
<p><label>Location <br /><input type="text" id="location" size="20" onblur="loc();"/></label></p>
<p><label><br /><input type="checkbox" id="terms" />I agree to the terms and conditions</label></p>
<p><input type="button" value="Submit" onclick="tnc();"/></p>
</form>
</body>
</html>