......................

Database Management System (Using Tree)

Posted by Unknown On Wednesday, August 4, 2010 0 comments

/*******************************************************
 APPLICATION : Database Management System (Using Tree)
 CODED BY    : Ankit Pokhrel
 COMPILED ON : Borland C++ Ver 5.02
 DATE     : 2010 - August - 01
********************************************************/

#include "fstream.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "process.h"
#include "iomanip.h"

/*************** Class Student Section *****************/
class student
{
 private:
  int roll;
   char name[20],faculty[15];
   float marks;

 public:
  student() //initialize data with default constructor
   {
    roll = 0;
    strcpy(name,"");
    strcpy(faculty,"");
    marks = 0.0;
   }

  student(int rn,char nme[],char fact[],float mks)
   {
    roll = rn;
    strcpy(name,nme);
    strcpy(faculty,fact);
    marks = mks;
   }

  friend istream & operator >> (istream &,student &); //Read
  friend ostream & operator << (ostream &,student &); //Print
  int getroll()
  { return roll; }

  void editRoll(int r)
  { roll = r; }
  ~student()
  {}
};

istream & operator >> (istream &in,student &s)
{
 cout << setw(35) << "Roll NO : ";
 in >> s.roll;
 cin.ignore();
 cout << endl << setw(32) << "Name : ";
 in.getline(s.name,20);
 cout << endl << setw(35) << "Faculty : ";
 in >> s.faculty;
 cout << endl << setw(33) << "Marks : ";
 in >> s.marks;
 return in;
}

ostream & operator << (ostream &out,student &s)
{
 out << setw(15) << s.roll;
 out << setw(20) << s.name;
 out << setw(20) << s.faculty;
 out << setw(15) << s.marks;
 return out;
}

void errorMsg()
{
 cout << endl << setw(55) << "Error while Accessing the Database";
 getch();
 exit(0);
}

/*********** Tree Section *************/
struct TREE //structure to represent a tree
{
 int key,address;
 struct TREE *right,*left,*root;
};

typedef TREE tree;

tree *New;
char flenme[15];
void create() //function to create a node
{
 New = new tree;
 New -> left = NULL;
 New -> right = NULL;
}

void insert(int Item,int adr,tree *&t)
{
 if(t == NULL) //if tree doesn't exists
  {
   t = new tree; //create a tree
   t -> key = Item; //insert data
   t -> address = adr;
   t -> left = NULL;
   t -> right = NULL;
   t -> root = t; //initialize root
   return;
  }

 tree *trav = t;
 if(Item < trav -> key)
  {
   if(trav -> left == NULL)
    {
     create();
     New -> key = Item;
     New -> address = adr;
     trav -> left = New;
    }
   else
    insert(Item,adr,trav -> left); //go to left
  }

 else
  {
   if(trav -> right == NULL)
    {
     create();
     New -> key = Item;
     New -> address = adr;
     trav -> right = New;
    }
   else
    insert(Item,adr,trav -> right); //go to right
  }
}

void inorder(tree *t)
{
 student s;
 ifstream infile(flenme);
 if(infile.fail())
  errorMsg();
 if(t != NULL)
  {
   inorder(t -> left);
   infile.seekg(t -> address);
   infile.read((char *) &s,sizeof(s));
   if(s.getroll() != -1)
    {
     cout << s;
     cout << endl << endl << endl;
    }
   inorder(t -> right);
  }
}

/*********** Class Database Section ************/
class Database
{
 private:
  char *filename;
 public:
  Database()
  {
  filename = new char[15];
  strcpy(filename,"");
  }

  int isDBloaded();
  int createDB();
  int CheckPrimaryKey(int,tree *);
  void Add(student,tree *&);
  void Remove(int,tree *&);
  void Search(int,tree *&);
  void Modify(int,tree *&);
  void Clear();
  void Help();
  ~Database()
  { delete filename; }
};

int Database :: isDBloaded()
{
 if(strcmp(filename,"") != 0)
  return 1;
 else
  return 0;
}

int Database :: createDB() //Function to create a Database
{
 char fname[15];
 cout << endl << setw(47) << "Enter Database Name : ";
 cin >> fname;
 student s;
 ifstream infile(fname);
 ofstream outfile;
 if(infile.fail())
  {
   cout << endl << endl << setw(50) << "Database Doesn't Exist...";
   cout << endl << setw(50) << "Press Enter to create...";
   if(getch() == 13)
   {
    strcpy(filename,fname);
    outfile.open(fname);
    if(outfile.fail())
     errorMsg();
    else
     {
      strcpy(flenme,filename);
      cout << "\n\n" << setw(46) << "Database Created";
      cout << endl <<  setw(53) << "Press any key to continue...";
     }

    outfile.close();
    return -1; //Database just created
   }

  else
   return 0; //Database was not created
  }

 strcpy(filename,fname);
 strcpy(flenme,filename);
 int add = -1;
 while(infile.read((char *) &s,sizeof(s)))
  add++;

 if(add == -1)
  return -1; //Database is empty
 else
  return 1; //Database contains Records
}

int getAddress()
{
 ifstream infile(flenme);
 if(infile.fail())
  errorMsg();

 int addr = 0;
 student s;
 while(infile.read((char *) &s,sizeof(s)))
    addr += sizeof(s);

 infile.close();
 return addr;
}

int Database :: CheckPrimaryKey(int key,tree *t) //Function to Check the Repetition of Primary Key
{
 if(key <= 0)
  return 1; //Error

 if(key == t -> key)
  return 1; //Error, Duplicate Primary Key

 else if(key < t -> key)
   t = t -> left;

 else
   t = t -> right;

 while(t != NULL)
  {
   if(key == t -> key)
     return 1;

   else if(key < t -> key)
    t = t -> left;

   else
    t = t -> right;
  }

 return 0;
}

void Database :: Add(student s,tree *&t)
{
 if(t != NULL)
 {
  int status = CheckPrimaryKey(s.getroll(),t);
  if(status)
   {
    cout << endl << setw(44) << "Invalid Roll Number";
    cout << endl << setw(43) << "Please, try again";
    return;
   }
 }

 ofstream outfile;
 outfile.open(filename,ios::app);
 if(outfile.fail())
  errorMsg();

 outfile.write((char *) &s,sizeof(s));
 if(outfile.good())
  {
   if(s.getroll() > 0)
   {
    insert(s.getroll(),getAddress(),t),
    cout << "\n\n" << setw(42) << "Record Added";
    cout << endl << setw(52) << "Press any key to continue...";
   }

   else
    {
     cout << endl << setw(44) << "Invalid Roll Number";
     cout << endl << setw(43) << "Please, try again";
     return;
    }

  }
 else
  errorMsg();
 outfile.close();
}

void Database :: Search(int roll,tree *&t)
{
 int addr = -1;
 tree *temp = t;
 if(roll == temp -> key)
  addr = temp -> address;

 else if(roll < temp -> key)
  temp = temp -> left;

 else
  temp = temp -> right;

 while(temp != NULL)
  {
   if(roll == temp -> key)
    {
     addr = temp -> address;
     break;
    }

   else if(roll < temp -> key)
     temp = temp -> left;
   else
     temp = temp -> right;
  }

 if(addr == -1)
   cout << endl << setw(43) << "Record Not Found";

 else
  {
   ifstream infile(flenme);
   if(infile.fail())
    errorMsg();
   student s;
   infile.seekg(addr);
   infile.read((char *) &s,sizeof(s));
   infile.close();
   cout << "\n\n" << setw(47) << "Record Found";
   cout << endl << setw(74);
   cout << "---------------------------------------------------------------\n";
   cout << setw(15) << "Roll" << setw(20) << "Name";
   cout << setw(20) << "Faculty" << setw(15) << "Marks" << endl;
   cout << setw(74) << "---------------------------------------------------------------\n";
   cout << setw(45) << s;
   cout << endl << setw(74);
   cout << "---------------------------------------------------------------\n";
   cout << endl << setw(55) << "Press any key to continue...";
  }
}

void Database :: Modify(int roll,tree *&t)
{
 int addr = -1;
 tree *temp = t;
 if(roll == temp -> key)
  addr = temp -> address;

 else if(roll < temp -> key)
  temp = temp -> left;

 else
  temp = temp -> right;

 while(temp != NULL)
  {
   if(roll == temp -> key)
    {
     addr = temp -> address;
     break;
    }

   else if(roll < temp -> key)
     temp = temp -> left;
   else
     temp = temp -> right;
  }

 if(addr == -1)
   cout << endl << setw(43) << "Record Not Found";

 else
  {
   fstream file(flenme,ios::in | ios::out);
   if(file.fail())
    errorMsg();
   student s;
   file.seekg(addr);
   file.read((char *) &s,sizeof(s));
   cout << "\n\n" << setw(47) << "Current Record";
   cout << endl << setw(74);
   cout << "---------------------------------------------------------------\n";
   cout << setw(15) << "Roll" << setw(20) << "Name";
   cout << setw(20) << "Faculty" << setw(15) << "Marks" << endl;
   cout << setw(74) << "---------------------------------------------------------------\n";
   cout << setw(45) << s;
   cout << endl << setw(74);
   cout << "---------------------------------------------------------------\n";

   cout << "\n\n" << setw(43) << "Enter New Record\n\n";
   file.seekp(0,ios::beg);
   int rn = s.getroll();
   char nme[20],fact[15];
   float mks;
   cin.ignore();
   cout << endl << setw(32) << "Name : ";
   cin.getline(nme,20);
   cout << endl << setw(35) << "Faculty : ";
   cin >> fact;
   cout << endl << setw(33) << "Marks : ";
   cin >> mks;
   student st(rn,nme,fact,mks);
   temp -> key = s.getroll();
   file.seekp(addr);
   file.write((char *) &st,sizeof(st));
   if(file.good())
    cout << "\n\n" << setw(50) << "Record Modified Successfully";
   else
    cout << "\n\n" << setw(42) << "Error : Please try again...";

   file.close();
   cout << endl << setw(52) << "Press any key to continue...";
  }
}

void Database :: Remove(int roll,tree *&t)
{
 int addr = -1;
 tree *temp = t;
 if(roll == temp -> key)
  addr = temp -> address;              

 else if(roll < temp -> key)
  temp = temp -> left;

 else
  temp = temp -> right;

 while(temp != NULL)
  {
   if(roll == temp -> key)
    {
     addr = temp -> address;
     break;
    }

   else if(roll < temp -> key)
     temp = temp -> left;
   else
     temp = temp -> right;
  }

 if(addr == -1)
   cout << endl << setw(43) << "Record Not Found";

 else
  {
   temp -> key = -1;
   fstream file(flenme,ios::in | ios::out);
   student s,temp;
   file.seekg(addr);
   file.read((char *) &s,sizeof(s));
   temp = s;
   temp.editRoll(-1);
   file.seekp(addr);
   file.write((char *) &temp,sizeof(temp));
   if(file.good())
    cout << "\n\n" << setw(50) << "Record Deleted Successfully";
   else
    cout << "\n\n" << setw(42) << "Error : Please try again...";

   file.close();
   cout << endl << setw(52) << "Press any key to continue...";
  }
}

void Database :: Clear()
{
 fstream file(flenme,ios::in | ios::out);
 ofstream temp("temp.txt");
 if(temp.fail())
  {
   clrscr();
   cout << endl << setw(45) << "Internal Error";
   cout << endl << setw(45) << "Settings were not saved";
   cout << "\n\n" << setw(42) << "Please try again...";
   exit(0);
  }

 student s;
 while(file.read((char *) &s,sizeof(s)))
 {
   if(s.getroll() != -1)
     temp.write((char *) &s,sizeof(s));
 }

 file.close();
 temp.close();
 remove(flenme);
 rename("temp.txt",flenme);
}

int loadDB(Database &d,tree *&t)
{
 d.Clear();
 int success = d.createDB();
 student s;
 t = NULL;
 if(success == -1)
   return 1;

 if(success == 0)
   return 0;

 int add = 0;
 ifstream infile(flenme);
 while(infile.read((char *) &s,sizeof(s)))
 {
  insert(s.getroll(),add,t);
  add += sizeof(s);
 }

 infile.close();
 return 1;
}

void DBinfo()
{
 cout << endl << setw(42);
 cout << "Database Name : " << flenme;
 student s;
 int total = 0;
 ifstream infile(flenme);
 while(infile.read((char *) &s,sizeof(s)))
  {
   if(s.getroll() != -1)
    total++;
  }
 infile.close();
 cout << endl << setw(44);
 cout << "Total Record(s) : " << total;
}

void Database :: Help()
{
 clrscr();
 cout << setw(45) << "************\n";
 cout << setw(41) << "HELP\n";
 cout << setw(46) << "************\n\n";
 cout << "\t-->";
 cout << "This program is developed, coded and completed as a class Project ";
 cout << "on 16th Sharawan 2067 (August 01, 2010).\n";
 cout << "\tThis Program is developed using the concept of Data Structure in ";
 cout << "C and\nC++ Languages. The concept of Tree is used to increase the ";
 cout << "performance of the\nprogram. The Binary Search Tree is created ";
 cout << "according to the roll no. of the\nstudents stored in file. The ";
 cout << "Database is processed from the Tree.";

 cout << "\n\n1. The user can work on multiple number of Database by loading ";
 cout << "different \n   database using option 1.";
 cout << "\n2. Different operations on database can be carried out from option ";
 cout << "2 to 7.";
 cout << "\n3. You can view the database informations using options 7.";

 cout << "\n\n\n\n\n\n\n" << setw(48) << "Press any key to continue...";
}

void Msg()
{
 cout << endl << setw(45) << "Load Database First";
 cout << endl << setw(50) << "Press any key to continue...";
}

void Menu()
{
 clrscr();
 cout << "\n\t\t      **********************************" << endl;
 cout << "\t\t      *     ----------------------     *" << endl;
 cout << "\t\t      *   DATABASE MANAGEMENT SYSTEM   *" << endl;
 cout << "\t\t      *     ----------------------     *" << endl;
 cout << "\t\t      **********************************" << endl << endl;
 cout << "\t        ----------------------------------\n\n";
 cout << "\t\t       1. Load Database\n" << endl;
 cout << "\t\t       2. Add New Data\n" << endl;
 cout << "\t\t       3. View All Data\n" << endl;
 cout << "\t\t       4. Modify Existing Data\n" << endl;
 cout << "\t\t       5. Delete Existing Data\n" << endl;
 cout << "\t\t       6. Search\n" << endl;
 cout << "\t\t       7. Current Database Information\n" << endl;
 cout << "\t\t       8. Help\n" << endl;
 cout << "\t\t       0. Exit From Program\n" << endl;
 cout << "\t\t      ----------------------------------\n\n";
 cout << "\t\t\t   Enter Choice : ";
}

int main()
{
 Database d;
 student s;
 tree *tr;
 int choice,success,rln;
 do
 {
  Menu();
  cin >> choice;
  switch(choice)
   {
    case 0:
     clrscr();
     d.Clear();
     cout << "\n\n\n\n\n\n\n\n\n";
     cout << setw(52) << "DATABASE MANAGEMENT SYSTEM\n\n";
     cout << setw(53) << "A Project on Data Structure\n\n";
     cout << setw(45) << "By Ankit Pokhrel";
     cout << "\n\n\n\n\n" << setw(50) << "Press any key to halt...";
     getch();
     return 0;

    case 1:
     success = loadDB(d,tr);
     if(success)
      {
       cout << endl << endl << setw(53) << "Database Loaded Successfully";
       cout << endl << setw(53) << "Press any key to continue...";
      }

     else
      cout << endl << endl << setw(53) << "Press any key to continue...";
     break;

    case 2:
     if(d.isDBloaded())
     {
      cout << endl << setw(45) << "Enter Informations\n\n";
      cin >> s;
      d.Add(s,tr);
     }

     else
      Msg();

     break;

    case 3:
     if(d.isDBloaded())
      {
       cout << endl << setw(74);
       cout << "---------------------------------------------------------------\n";
       cout << setw(15) << "Roll" << setw(20) << "Name";
       cout << setw(20) << "Faculty" << setw(15) << "Marks" << endl;
       cout << setw(74) << "---------------------------------------------------------------\n";
       inorder(tr);
       cout << endl << setw(53) << "Press any key to continue...";
      }

     else
      Msg();

     break;

    case 4:
     if(d.isDBloaded())
      {
       cout << endl << setw(43) << "Enter Roll No : ";
       cin >> rln;
       d.Modify(rln,tr);
      }

     else
      Msg();

     break;

    case 5:
     if(d.isDBloaded())
      {
       cout << endl << setw(43) << "Enter Roll No : ";
       cin >> rln;
       d.Remove(rln,tr);
      }

     else
      Msg();

     break;

    case 6:
     if(d.isDBloaded())
      {
       cout << endl << setw(43) << "Enter Roll No : ";
       cin >> rln;
       d.Search(rln,tr);
      }

     else
      Msg();

     break;

    case 8:
     d.Help();
     break;

    case 7:
     if(d.isDBloaded())
      DBinfo();
     else
      Msg();

     break;

    default:
     cout << endl << setw(53) << "Please Select Appropriate Option";
  }
  getch();
 }while(1);
}

No comments:

Post a Comment

Leave Feedback about this BLOG