Wednesday, May 30, 2018

Project on Library Management System (C++)

=>The project titled Library Management system is Library management software for monitoring and controlling the transactions in a library. The project “Library Management System” is developed in C++, which mainly focuses on basic operations in a library like adding new member, new books, and updating new information and facility to borrow and return books.

The user will find it easy in this automated system rather than using the manual writing system. The system contains a database where all the information will be stored safely. The system is user-friendly and error free.
(For Investigatory Project)



//****************************************************************
// HEADER FILE USED IN PROJECT
//****************************************************************

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip.h>

//****************************************************************
// CLASS USED IN PROJECT
//****************************************************************

class book
{
 char bno[6];
 char bname[50];
 char aname[20];
 public:
 void create_book()
 {
  cout<<"\nNEW BOOK ENTRY...\n";
  cout<<"\nEnter The book no. : ";
  cin>>bno;
  cout<<"\n\nEnter The Name of The Book : ";
  gets(bname);
  cout<<"\n\nEnter The Author's Name : ";
  gets(aname);
  cout<<"\n\n\nBook Created...";
 }

 void show_book()
 {
  cout<<"\nBook No. : "<<bno;
  cout<<"\nBook Name : ";
  puts(bname);
  cout<<"Author Name : ";
  puts(aname);
 }

 void modify_book()
 {
  cout<<"\nBook No. : "<<bno;
  cout<<"\nModify Book Name : ";
  gets(bname);
  cout<<"\nModify Author's Name of Book : ";
  gets(aname);
 }

 char* retbno()
 {
  return bno;
 }

 void report()
 {
  cout<<bno<<setw(30)<<bname<<setw(30)<<aname<<endl;}
 }; //class ends here

class student
{
 char admno[6];
 char name[20];
 char stbno[6];
 int token;

 public:
 void create_student()
 {
  clrscr();
  cout<<"\nNEW STUDENT ENTRY...\n";
  cout<<"\nEnter The admission no. : ";
  cin>>admno;
  cout<<"\n\nEnter The Name of The Student : ";
  gets(name);
  token=0;
  stbno[0]='/0';
  cout<<"\n\nStudent Record Created...";
 }

 void show_student()
 {
  cout<<"\nAdmission no. : "<<admno;
  cout<<"\nStudent Name : ";
  puts(name);
  cout<<"\nNo of Book issued : "<<token;
  if(token==1)
  cout<<"\nBook No "<<stbno;
 }

 void modify_student()
 {
  cout<<"\nAdmission no. : "<<admno;
  cout<<"\nModify Student Name : ";
  gets(name);
 }

 char* retadmno()
 {
  return admno;
 }

 char* retstbno()
 {
  return stbno;
 }

 int rettoken()
 {
  return token;
 }

 void addtoken()
 {
  token=1;
 }

 void resettoken()
 {
  token=0;
 }

 void getstbno(char t[])
 {
  strcpy(stbno,t);
 }

 void report()
 {
  cout<<"\t"<<admno<<setw(20)<<name<<setw(10)<<token<<endl;}
 }; //class ends here

//****************************************************************
// global declaration for stream object, object
//****************************************************************

 fstream fp,fp1;
 book bk;
 student st;

//****************************************************************
// function to write in file
//****************************************************************

 void write_book()
 {
  char ch;
  fp.open("book.dat",ios::out|ios::app);
  do{
     clrscr();
     bk.create_book();
     fp.write((char*)&bk,sizeof(book));
     cout<<"\n\nDo you want to add more record...(y/n?)";
     cin>>ch;
    }while(ch=='y'||ch=='Y');
  fp.close();
 }

 void write_student()
 {
  char ch;
  fp.open("student.dat",ios::out|ios::app);
  do{
     st.create_student();
     fp.write((char*)&st,sizeof(student));
     cout<<"\n\ndo you want to add more record...(y/n?)";
     cin>>ch;
    }while(ch=='y'||ch=='Y');
  fp.close();
 }

//****************************************************************
// function to read specific record from file
//****************************************************************

 void display_spb(char n[])
 {
  cout<<"\nBOOK DETAILS\n";
  int flag=0;
  fp.open("book.dat",ios::in);
  while(fp.read((char*)&bk,sizeof(book)))
  {
   if(strcmpi(bk.retbno(),n)==0)
   {
    bk.show_book();
    flag=1;
   }
  }
  fp.close();
  if(flag==0)
  cout<<"\n\nBook does not exist";
  getch();
 }

 void display_sps(char n[])
 {
  cout<<"\nSTUDENT DETAILS\n";
  int flag=0;
  fp.open("student.dat",ios::in);
  while(fp.read((char*)&st,sizeof(student)))
  {
   if((strcmpi(st.retadmno(),n)==0))
   {
    st.show_student();
    flag=1;
   }
  }
  fp.close();
  if(flag==0)
  cout<<"\n\nStudent does not exist";
  getch();
 }

//****************************************************************
// function to modify record of file
//****************************************************************

 void modify_book()
 {
  char n[6];
  int found=0;
  clrscr();
  cout<<"\n\n\tMODIFY BOOK REOCORD... ";
  cout<<"\n\n\tEnter The book no. of The book : ";
  cin>>n;
  fp.open("book.dat",ios::in|ios::out);
  while(fp.read((char*)&bk,sizeof(book)) && found==0)
  {
   if(strcmpi(bk.retbno(),n)==0)
   {
    bk.show_book();
    cout<<"\nEnter The New Details of book : "<<endl;
    bk.modify_book();
    int pos=-1*sizeof(bk);
    fp.seekp(pos,ios::cur);
    fp.write((char*)&bk,sizeof(book));
    cout<<"\n\n\t Record Updated";
    found=1;
   }
  }
  fp.close();
  if(found==0)
  cout<<"\n\n Record Not Found ";
  getch();
 }

 void modify_student()
 {
  char n[6];
  int found=0;
  clrscr();
  cout<<"\n\n\tMODIFY STUDENT RECORD... ";
  cout<<"\n\n\tEnter The admission no. of The student : ";
  cin>>n;
  fp.open("student.dat",ios::in|ios::out);
  while(fp.read((char*)&st,sizeof(student)) && found==0)
  {
   if(strcmpi(st.retadmno(),n)==0)
   {
    st.show_student();
    cout<<"\nEnter The New Details of student : "<<endl;
    st.modify_student();
    int pos=-1*sizeof(st);
    fp.seekp(pos,ios::cur);
    fp.write((char*)&st,sizeof(student));
    cout<<"\n\n\t Record Updated";
    found=1;
   }
  }
  fp.close();
  if(found==0)
  cout<<"\n\n Record Not Found ";
  getch();
 }

//****************************************************************
// function to delete record of file
//****************************************************************

 void delete_student()
 {
  char n[6];
  int flag=0;
  clrscr();
  cout<<"\n\n\n\tDELETE STUDENT...";
  cout<<"\n\nEnter The admission no. of the Student You Want To Delete : ";
  cin>>n;
  fp.open("student.dat",ios::in|ios::out);
  fstream fp2;
  fp2.open("Temp.dat",ios::out);
  fp.seekg(0,ios::beg);
  while(fp.read((char*)&st,sizeof(student)))
  {
   if(strcmpi(st.retadmno(),n)!=0)
   fp2.write((char*)&st,sizeof(student));
   else
   flag=1;
  }
  fp2.close();
  fp.close();
  remove("student.dat");
  rename("Temp.dat","student.dat");
  if(flag==1)
  cout<<"\n\n\tRecord Deleted...";
  else
  cout<<"\n\nRecord not found";
  getch();
 }

 void delete_book()
 {
  char n[6];
  clrscr();
  cout<<"\n\n\n\tDELETE BOOK...";
  cout<<"\n\nEnter The Book no. of the Book You Want To Delete : ";
  cin>>n;
  fp.open("book.dat",ios::in|ios::out);
  fstream fp2;
  fp2.open("Temp.dat",ios::out);
  fp.seekg(0,ios::beg);
  while(fp.read((char*)&bk,sizeof(book)))
  {
   if(strcmpi(bk.retbno(),n)!=0) //change later
   {
    fp2.write((char*)&bk,sizeof(book));
   }
  }
  fp2.close();
  fp.close();
  remove("book.dat");
  rename("Temp.dat","book.dat");
  cout<<"\n\n\tRecord Deleted...";
  getch();
 }

//****************************************************************
// function to display all students list
//****************************************************************

 void display_alls()
 {
  clrscr();
  fp.open("student.dat",ios::in);
  if(!fp)
  {
   cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
   getch();
   return;
  }
  gotoxy(35,2);
  cout<<"STUDENT LIST\n\n";
  cout<<"==================================================================\n";
  cout<<"\tAdmission No."<<setw(10)<<"Name"<<setw(20)<<"Book Issued\n";
  cout<<"==================================================================\n";

  while(fp.read((char*)&st,sizeof(student)))
  {
   st.report();
  }
  fp.close();
  getch();
 }

//****************************************************************
// function to display Books list
//****************************************************************

 void display_allb()
 {
  clrscr();
  fp.open("book.dat",ios::in);
  if(!fp)
  {
   cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
   getch();
   return;
  }
  cout<<"\n\n\tBook LIST\n\n";
  cout<<"=========================================================================\n";
  cout<<"Book Number"<<setw(20)<<"Book Name"<<setw(25)<<"Author\n";
  cout<<"=========================================================================\n";

  while(fp.read((char*)&bk,sizeof(book)))
  {
   bk.report();
  }
  fp.close();
  getch();
 }

//****************************************************************
// function to issue book
//****************************************************************

 void book_issue()
 {
  char sn[6],bn[6];
  int found=0,flag=0;
  clrscr();
  cout<<"\n\nBOOK ISSUE...";
  cout<<"\n\n\tEnter The student's admission no. : ";
  cin>>sn;
  fp.open("student.dat",ios::in|ios::out);
  fp1.open("book.dat",ios::in|ios::out);
  while(fp.read((char*)&st,sizeof(student)) && found==0)
  {
   if(strcmpi(st.retadmno(),sn)==0)
   {
    found=1;
    if(st.rettoken()==0)
    {
     cout<<"\n\n\tEnter the book no. : ";
     cin>>bn;
     while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
     {
      if(strcmpi(bk.retbno(),bn)==0)
      {
       bk.show_book();
       flag=1;
       st.addtoken();
       st.getstbno(bk.retbno());
       int pos=-1*sizeof(st);
       fp.seekp(pos,ios::cur);
       fp.write((char*)&st,sizeof(student));
       cout<<"\n\n\t Book issued successfully\n\nPlease Note: Write the current date in backside of your book and submit within 15 days, fine Rs. 1 for each day after 15 days period";
      }
     }
      if(flag==0)
      cout<<"Book no. does not exist";
    }
   else
   cout<<"You have not returned the last book ";
   }
  }
  if(found==0)
  cout<<"Student record not exist...";
  getch();
  fp.close();
  fp1.close();
 }

//****************************************************************
// function to deposit book
//****************************************************************

 void book_deposit()
 {
  char sn[6],bn[6];
  int found=0,flag=0,day,fine;
  clrscr();
  cout<<"\n\nBOOK DEPOSIT ...";
  cout<<"\n\n\tEnter The student's admission no. : ";
  cin>>sn;
  fp.open("student.dat",ios::in|ios::out);
  fp1.open("book.dat",ios::in|ios::out);
  while(fp.read((char*)&st,sizeof(student)) && found==0)
  {
   if(strcmpi(st.retadmno(),sn)==0)
   {
    found=1;
    if(st.rettoken()==1)
    {
     while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
     {
      if(strcmpi(bk.retbno(),st.retstbno())==0)
      {
       bk.show_book();
       flag=1;
       cout<<"\n\nBook deposited in no. of days : ";
       cin>>day;
       if(day>15)
       {
fine=(day-15)*1;
cout<<"\n\nFine has to deposited Rs."<<fine;
       }
       st.resettoken();
       int pos=-1*sizeof(st);
       fp.seekp(pos,ios::cur);
       fp.write((char*)&st,sizeof(student));
       cout<<"\n\n\t Book deposited successfully";
      }
     }
    if(flag==0)
    cout<<"Book no does not exist";
    }
    else
    cout<<"No book is issued..please check!!";
   }
  }
  if(found==0)
  cout<<"Student record not exist...";
  getch();
  fp.close();
  fp1.close();
 }

//****************************************************************
// INTRODUCTION FUNCTION
//****************************************************************

 void intro()
 {
  clrscr();
  gotoxy(25,2);
  cout<<"LIBRARY MANAGEMENT SYSTEM";
  gotoxy(25,3);
  cout<<"=========================";
  cout<<"\n\nMADE BY : XYZ";
  cout<<"\n\nSCHOOL : ABC";
  cout<<"\n\t New Delhi";
  getch();
 }

//****************************************************************
// ADMINISTRATOR MENU FUNCTION
//****************************************************************

 void admin_menu()
 {
  clrscr();
  int ch2;
  cout<<"\n\n\n\tADMINISTRATOR MENU";
  cout<<"\n\n\t1.CREATE STUDENT RECORD";
  cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORD";
  cout<<"\n\n\t3.DISPLAY SPECIFIC STUDENT RECORD ";
  cout<<"\n\n\t4.MODIFY STUDENT RECORD";
  cout<<"\n\n\t5.DELETE STUDENT RECORD";
  cout<<"\n\n\t6.CREATE BOOK ";
  cout<<"\n\n\t7.DISPLAY ALL BOOKS ";
  cout<<"\n\n\t8.DISPLAY SPECIFIC BOOK ";
  cout<<"\n\n\t9.MODIFY BOOK ";
  cout<<"\n\n\t10.DELETE BOOK ";
  cout<<"\n\n\t11.BACK TO MAIN MENU";
  cout<<"\n\n\tPlease Enter Your Choice (1-11) ";
  cin>>ch2;
  switch(ch2)
  {
   case 1: clrscr();
   write_student();break;
   case 2: display_alls();break;
   case 3: char num[6];
   clrscr();
   cout<<"\n\n\tPlease Enter The Admission No. : ";
   cin>>num;
   display_sps(num);
   break;
   case 4: modify_student();break;
   case 5: delete_student();break;
   case 6: clrscr();
   write_book();break;
   case 7: display_allb();break;
   case 8: {
    char num[6];
    clrscr();
    cout<<"\n\n\tPlease Enter The book No. : ";
    cin>>num;
    display_spb(num);
    break;
   }
   case 9: modify_book();break;
   case 10: delete_book();break;
   case 11: return;
   default: cout<<"\a";
  }
  admin_menu();
 }

//****************************************************************
// THE MAIN FUNCTION OF PROGRAM
//****************************************************************

void main()
{
 char ch;
 intro();
 do
 {
  clrscr();
  cout<<"\n\n\n\tMAIN MENU";
  cout<<"\n\n\t01. BOOK ISSUE";
  cout<<"\n\n\t02. BOOK DEPOSIT";
  cout<<"\n\n\t03. ADMINISTRATOR MENU";
  cout<<"\n\n\t04. EXIT";
  cout<<"\n\n\tPlease Select Your Option (1-4) ";
  ch=getche();
  switch(ch)
  {
   case '1': clrscr();
     book_issue();
     break;
   case '2': book_deposit();
     break;
   case '3': admin_menu();
     break;
   case '4': exit(0);
     default :cout<<"\a";
  }
 }while(ch!='4');
}

//****************************************************************
// END OF PROJECT
//****************************************************************

Tuesday, May 22, 2018

Program to implement first-fit, best-fit and worst-fit allocation strategies

/* Program to implement first-fit, best-fit and worst-fit allocation strategies.
*/

#include<stdio.h>
#include<stdlib.h>
void display(int ,int [10]);
void firstfit(int ,int [10],int [10],int);
void bestfit(int ,int [10],int [10],int);
void worstfit(int,int [10],int [10],int);
int main()
{
 int npr,nh=0,i,j,k,pr[10],hol[10],ch,temp[10];

 printf("\n Enter the number of processes:");
 scanf("%d",&npr);
 printf("\nNumber of holes:");
 scanf("%d",&nh);
 for(i=0;i<npr;i++)
 {
  printf("\nProcess %d:",i+1);
  scanf("%d",&pr[i]);
 }
 for(i=0;i<nh;i++)
 {
  printf("\nHole %d:",i+1);
  scanf("%d",&hol[i]);
  temp[i]=hol[i];
 }
 do
 {
  printf("\n----------MENU------------"
"\n1.FIRST-FIT \n2.BEST FIT"
"\n3.WORST FIT\n4.EXIT\nENTER YOUR CHOICE:");
  scanf("%d",&ch);
  for(i=0;i<nh;i++)
  temp[i]=hol[i];
  switch(ch)
  {
   case 1: firstfit(npr,temp,pr,nh);
   break;
   case 2: bestfit(npr,temp,pr,nh);
   break;
   case 3:worstfit(npr,temp,pr,nh);
  break;
   case 4:exit (0);
  break;
   default:printf("\nWrong choice......!");
  }
 }
 while(ch!=4);
 return 0;
}
//First Fit
void firstfit(int npr,int hol[10],int pr[10],int nh)
{
 int i,j,k,flag=0;
 for(i=0;i<npr;i++)
 {
  flag=0;
  for(j=0;j<nh;j++)
  {
   if(pr[i]<hol[j])
   {
    hol[j]=hol[j]-pr[i];
    printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
    flag=1;
    break;
   }
   else if(pr[i]==hol[j])
   {
    flag=1;
    printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
    for(k=j;k<nh-1;k++)
    {
     hol[k]=hol[k+1];
    }
    nh--;
    break;
   }
  }
  if(flag==0)
  {
   printf("\n Hole is not Available.....");
   break;
  }
  display(nh,hol);
 }
}
//Best fit
void bestfit(int npr,int hol[10],int pr[10],int nh)
{
 int i,j,k,min,flag;
 for(i=0;i<npr;i++)
 {
  flag=0;
  for(j=0;j<nh;j++)
  {
   if(pr[i]<=hol[j])
   {
    flag=1;
    min=j;
    for(k=j;k<nh;k++)
    {
     if((hol[min]>hol[k])&&(pr[i]<=hol[k]))
     min=k;
    }
    if(pr[i]<hol[min])
    {
     printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
     hol[min]=hol[min]-pr[i];
     break;
    }
    else if(pr[i]==hol[min])
    {
     printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
     for(k=min;k<nh-1;k++)
     hol[k]=hol[k+1];
     nh--;
     break;
    }
   }
  }
  if(flag==0)
  {
   printf("\n Hole is not Available.....");
   break;
  }
  display(nh,hol);
 }
}
//worst fit
void worstfit(int npr,int hol[10],int pr[10],int nh)
{
 int i,j,k,max,flag;
 for(i=0;i<npr;i++)
 {
  flag=0;
  for(j=0;j<nh;j++)
  {
   if(pr[i]<=hol[j])
   {
    printf("\nMemory is Allocated to Process %d :(%d)",i+1,pr[i]);
    flag=1;
    max=j;
    for(k=j;k<nh;k++)
    {
     if((hol[max]<hol[k])&&(pr[i]<=hol[k]))
     max=k;
    }
    if(pr[i]<hol[max])
    {
     printf("\nCPU is Allocated for process %d",i+1);
     hol[max]=hol[max]-pr[i];
     break;
    }
    else if(pr[i]==hol[max])
    {
     printf("\nCPU is Allocated for process %d",i+1);
     for(k=max;k<nh-1;k++)
     hol[k]=hol[k+1];
     nh--;
     break;
    }
   }
  }
  if(flag==0)
  {
   printf("\n Hole is not Available.....");
   break;
  }
  display(nh,hol);
 }
}
void display(int nh,int hol[10])
{
 int i=0;
 printf("\nHoles:");
 for(i=0;i<nh;i++)
 {
  printf("%d\t",hol[i]);
 }
}




OUTPUT SCREEN:



Program to implement the following scheduling algorithms

/* Program to implement the following scheduling algorithms
a. FCFS
b. Shortest Job First
c. Non-preemptive priority based
d. Round Robin
*/

#include<stdio.h>
#include<stdlib.h>

typedef struct process
{
int pid,st,ft,prior;
int wt,tat,at,bt;
int flag;
}pr;

int n;
pr p[10];
void accept(int);
void fcfs();
void disp_gantt();
void sjf_sort();
void priority_sort();
void round_robin(int);

void main()
{
int ch,tq;
do
{
printf("\n\t******Scheduling Algorithms********");
printf("\n\t1. FCFS\n\t2. SJF\n\t3. Priority Based\n\t4. Round Robin\n\t5. Exit");
printf("\n\t Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: accept(ch);
fcfs();
break;

case 2: accept(ch);
sjf_sort();
fcfs();
break;

case 3: accept(ch);
priority_sort();
fcfs();
break;

case 4: printf("\n\tEnter the Time Quantum: ");
scanf("%d",&tq);
accept(ch);
round_robin(tq);
break;

case 5: break;
}
printf("\n\tPress any key to continue:");
}while(ch!=5);
}

void accept(int ch)
{
int i;
printf("\n\tHow many processes: ");
scanf("%d",&n);
printf("\tEnter the values\n");
if(ch==1 || ch==2)
{
printf("\n\tArrival Time and Burst Time\n");
for(i=0;i<n;i++)
{
printf("\tEnter for Process %d :",i);
scanf("%d%d",&p[i].at,&p[i].bt);
p[i].pid=i;
}
}
if(ch==3)
{
printf("\n\tArrival Time , Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\tEnter for Process %d :",i);
scanf("%d%d%d",&p[i].at,&p[i].bt,&p[i].prior);
p[i].pid=i;
}
}
if(ch==4)
{
printf("\n\tArrival Time and Burst Time\n");
for(i=0;i<n;i++)
{
printf("\tEnter for Process %d :",i);
scanf("%d%d",&p[i].at,&p[i].bt);
p[i].pid=i;
}
}
}

void fcfs()
{
int i;
float avgtat=0,avgwt=0;
for(i=0;i<n;i++)
{
if(i==0)
{
p[i].st=i;
p[i].wt=p[i].st-p[i].at;
p[i].tat=p[i].wt+p[i].bt;
p[i].ft=p[i].st+p[i].bt;
}
else
{
p[i].st=p[i-1].ft;
p[i].wt=p[i].st-p[i].at;
p[i].tat=p[i].wt+p[i].bt;
p[i].ft=p[i].st+p[i].bt;
}
avgtat=avgtat+p[i].tat;
avgwt=avgwt+p[i].wt;
}
avgtat=avgtat/n;
avgwt=avgwt/n;
disp_gantt(n);
printf("\n\t** Average Turn Around Time:%f **",avgtat);
printf("\n\t** Average waiting time:%f **",avgwt);
}

void disp_gantt()
{
int i;
printf("\n\tGANTT CHART\n ");
for(i=0;i<n;i++)
{
printf("\t P%d",p[i].pid);
}
printf("\n");
for(i=0;i<n;i++)
{
printf("\t %d",p[i].st);
}
printf("\t %d",p[i-1].ft);
printf("\n");
}

void sjf_sort()
{
int i,j;
pr temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].bt>p[j+1].bt)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;

}
}
}
}

void priority_sort()
{
int i,j;
pr temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].prior>p[j+1].prior)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;

}
}
}
}

void round_robin(int tq)
{
int allterminate=0,cnt=0,i;
int gt[30],ps[30],temp=0,pcnt=0;
float avgtat=0;
printf("\n\tGANTT CHART\n");
while(allterminate<n)
{
for(i=0;i<n;i++)
{
if(p[i].flag==0) //process incomplete
{
p[i].st=temp;
cnt++;
if(p[i].bt>=tq)//burst time > time slice
{

p[i].bt=p[i].bt-tq;
temp=temp+tq;
gt[cnt]=temp;
ps[pcnt]=p[i].pid;
pcnt++;
}
else
{ //burst time < time slice
temp=temp+p[i].bt;
gt[cnt]=temp;
p[i].bt=0;
ps[pcnt]=p[i].pid;
pcnt++;
}

if(p[i].bt==0)
{
allterminate++;
p[i].flag=1;
p[i].ft=temp;
}

if(p[i].flag==1)
{
p[i].tat=p[i].ft;
avgtat=avgtat+p[i].tat;
}
}//if

}//for

}//while
for(i=0;i<pcnt;i++)
{
printf("\t P%d",ps[i]);
}
printf("\n\t0 ");
for(i=1;i<=cnt;i++)
{
printf("\t %d ",gt[i]);
}
avgtat=avgtat/n;
printf("\n\t** Average Turn Around Time:%f **",avgtat);
}




OUTPUT SCREEN:



Program to understand working of pthread library

/* Program to understand working of pthread library.
*/

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
int sum;
void *runner(void *param);
int main(int argc,char *argv[])
{
 pthread_t tid;
 pthread_attr_t attr;
if(argc!=2)
{
 fprintf(stderr,"%d must be>=0\n",atoi(argv[1]));
 return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
printf("Sum=%d\n",sum);
return 0;
}
void *runner(void *param)
{
int i,upper=atoi(param);
sum=0;
for(i=1;i<=upper;i++)
sum+=i;
pthread_exit(0);
}




OUTPUT SCREEN:


Program to demonstrate Inter-Process Communication (IPC) between parent and child

/* Program to demonstrate Inter-Process Communication (IPC) between parent and child using pipe system call.
*/

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/uio.h>
#include<sys/types.h>
main()
{
int pid,pfd[2],n,a,b,c;
if(pipe(pfd)==-1)
{
printf("\nError in pipe connection\n");
exit(1);
}
pid=fork();
if(pid>0)
{
printf("\nParent Process");
printf("\nFibonacci Series");
printf("\nEnter the limit for the series:");
scanf("%d",&n);
close(pfd[0]);
write(pfd[1],&n,sizeof(n));
close(pfd[1]);
exit(0);
}
else
{
close(pfd[1]);
read(pfd[0],&n,sizeof(n));
printf("\nChild Process");
a=0;
b=1;
close(pfd[0]);
printf("\nFibonacci Series is:");
printf("\n\n%d\n%d",a,b);
while(n>1)
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
n--;
}
}
}



OUTPUT SCREEN:


Program to demonstrate Producer-Consumer Problem

/* Program to demonstrate producer-consumer problem using shared memory.
*/

#include<stdio.h>
int n;
void main()
{

int Q[30];
int front,rear;
int value;
char rr;
int ch;
printf("Enter the size of the Buffer=");
scanf("%d",&n);
printf("\n");
n++;

front=rear=0;
do
{
printf("\nMENU");
printf("\n1.Producer");
printf("\n2.Consume");
printf("\n3.Display Inventory Status");
printf("\nEnter Your Choice=");
scanf("%d",&ch);
if(ch==1)
{

int value=1;
 if ((rear+1)%n==front)
{
printf("The Buffer is full\n");
printf("Unable to Produce\n");
}
 else
 {
rear=(rear+1)%n;
Q[rear]=value;
printf("\nItem Produced");
 }

}
if(ch==2)
{

 int val;
 if(front!=rear)
 {
front=(front+1)%n;
val=Q[front];
printf("\nItem Consumed");
//printf("The Consumed value is: "<<val;
 }
 else
 {
 printf("The Inventory is empty");
 printf("\nUnable to Consume Item");
 }

}
if(ch==3)
{

 int i;
 i=front;
 while(i!=rear)
 {
 i=(i+1)%n;
 printf("\n");
 printf("%d",Q[i]);
 printf("  ");
 }
}

printf("\nWANT TO RUN IT AGAIN=");
scanf("%s",&rr);
}while(rr=='y'||rr=='Y');

}
/*
void add(int Q[],int front,int &rear,int value)
 {
 if ((rear+1)%n==front)
{
printf("The Buffer is full\n");
printf("Unable to Produce\n");
}
 else
 {
rear=(rear+1)%n;
Q[rear]=value;
printf("\nItem Produced");
 }
}


 void del(int Q[],int &front,int rear)
 {
 int val;
 if(front!=rear)
 {
front=(front+1)%n;
val=Q[front];
printf("\nItem Consumed");
//printf("The Consumed value is: "<<val;
 }
 else
 {
 printf("The Inventory is empty");
 printf("\nUnable to Consume Item");
 }
 }


 void display(int Q[],int front,int rear)
 {
 int i;
 i=front;
 while(i!=rear)
 {
 i=(i+1)%n;
 cout<<endl;
 printf("%d",Q[i]);
 printf("  ");
 }
 }




OUTPUT SCREEN:


Program (using fork() and/or exec() commands)

/* Program (using fork() and/or exec() commands) where parent and child execute:
d. before terminating, the parent waits for the child to finish its task
*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
a=fork();
if(a<0)
{
 printf("Child process could not be created");
 exit(-1);
}
else if(a==0)
{
 printf("in child process id=%d\n",getpid());
}
else
{
 wait(NULL);
 printf("in parent process id=%d\n",getppid());
}
return 0;
}




OUTPUT SCREEN:


Program (using fork() and/or exec() commands)

/* Program (using fork() and/or exec() commands) where parent and child execute:
c. different programs
*/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int a;
a=fork();
if(a<0)
{
 printf("Child process could not be created");
 exit(-1);
}
else if(a==0)
{
 execl("/bin/ls","ls\n",NULL);
}
else
{
 printf("\nParent process=%d\n",getppid());
}
return 0;
}




OUTPUT SCREEN:


Program (using fork() and/or exec() commands)

/* Program (using fork() and/or exec() commands) where parent and child execute:
b. same program, different code
*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
a=fork();
if(a<0)
{
 printf("Child process could not be created");
 exit(-1);
}
else if(a==0)
{
 printf("in child process=%d",getpid());
}
else
{
 printf("in parent process=%d\n",getppid());
}
return 0;
}




OUTPUT SCREEN:


Program (using fork() and/or exec() commands)

/* Program (using fork() and/or exec() commands) where parent and child execute:
a. same program, same code
*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
a=fork();
if(a<0)
{
 printf("Child process could not be created");
 exit(-1);
}
else
{
 printf("My process id=%d, My parent process id=%d\n",getpid(),getppid());
}
return 0;
}





OUTPUT SCREEN:


Program to copy a Source File into the Target File

/* Program to copy a source file into the target file and display the target file using system calls.
*/

#include<stdio.h>
#include<stdlib.h>
main(int argc, char *argv[3])
{
int fold,fnew;
if(argc!=3)
{
  printf("Read two arguments");
  exit(1);
}
fold=open(argv[1],0);
if(fold==1)
{
  printf("Unable to open file %s\n",argv[1]);
  exit(1);
}
fnew=creat(argv[2],0666);
if(fnew==-1)
{
  printf("Unable to create file %s\n",argv[2]);
  exit(1);
}
copy(fold,fnew);
exit(0);
}
copy(int old,int new)
{
int count;
char buffer[512];
while(read(old,&buffer,1)>0)
{
  write(new,&buffer,1);
}
}




OUTPUT SCREEN:


Program to print details including Owner Access Permissions and File Access Time

/* Program to print details including owner access permissions, file access time, where file name is given as a command line argument.
*/

#include<stdio.h>

#include<sys/stat.h>


int main(int argc, char *argv[3])

{

int i,statchmod;
struct stat buffer;

printf("Give file name:\n");

for(i=1;i<argc;i++)

{
 
printf("file=%s:-\n",argv[1]);
   
if(stat(argv[i], &buffer)<0)
   
printf("Error in file stated");
else
   
statchmod=buffer.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
   
printf("Access permission(in octal): %o\n",statchmod);
   
        printf("Owenered=%d,gid=%d\n",buffer.st_uid,buffer.st_gid);
   
printf("Access time=%d\n", (time(&(buffer.st_atime))));

}
}




OUTPUT SCREEN:


Program to display Kernel Version, CPU Type & Model

/* WAP to display the following:
a. Kernel Version
b. CPU type and model
c. Information in configured memory, amount of free and used memory
*/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int a;
printf("\nKernel Version:\n");
system("cat /proc/sys/kernel/osrelease");
printf("\nCPU TYPE AND MODEL:\n");
system("cat /proc/cpuinfo | awk 'NR==4,NR==5 {print}'");
        printf("\nInformation on Configured amount of free and Used Memory:\n");
system("cat /proc/meminfo | awk 'NR==1,NR==2 {print}'");
return 0;
}





OUTPUT SCREEN:



Sunday, May 20, 2018

Hash Function Encryption - Decryption Program

/* Program to implement Hash Function encryption and decryption technique.
*/

#include<string.h>
#include<iostream>
using namespace std;

int main()
{
  string m,k;
  cout<<"\nEnter the Message to Encrypt: ";
  getline(cin,m);
  cout<<"Enter Key: ";
  getline(cin,k);
  char enc[m.size()];
  for(int i=0;i<m.size();i++)
  {
    enc[i]=m[i]^k[i%k.size()];
    cout<<m[i]<<" XOR "<<k[i%k.size()]<<" ";
    cout<<enc[i]<<endl;
  }
  cout<<"\nEncrypted message: ";
  for(int i=0;i<m.size();i++)
  {
  cout<<enc[i]<<" ";
  }
  cout<<"\nDecrypted message: ";
  string dec;
  for(int i=0;i<m.size();i++)
  {
    dec[i]=enc[i]^k[i%k.size()];
    cout<<dec[i];
  }
cout<<endl<<endl;
  return 0;
}




OUTPUT SCREEN:



Columnar Transposition Cipher Encryption - Decryption Program

/* Program to implement columnar transposition cipher encryption decryption technique.
*/

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
  string m,k;
  cout<<"\nEnter the Message to Encrypt: ";
  getline(cin,m);
  int mlen=m.size(),row;
  if(mlen%5==0)
      row=mlen/5;
  else
    row = (mlen/5)+1;

  char enc[row][5];
  int mi=0;     
  for(int i=0;i<row;i++)
      for(int j=0;j<5;j++,mi++)
          enc[i][j]=(char)m[mi];
  cout<<endl;
  for(int i=0;i<row;i++)
{
      for(int j=0;j<5;j++)
{
        if(!isalpha(enc[i][j]))
            cout<<'x';
        else 
            cout<<enc[i][j]; 
      }
    cout<<endl;
    }
    char out[row*5];
    int ei=0;
    for(int i=0;i<5;i++)
      for(int j=0;j<row;j++,ei++)
{
        if(!isalpha(enc[j][i]))
            out[ei]='x';
        else 
            out[ei]=enc[j][i]; 
      }
    cout<<"\nEncrypted Message is: ";
    for(int i=0;i<row*5;i++)
    cout<<out[i];
cout<<"\n"<<"\n";     
  return 0;
}




OUTPUT SCREEN:



Vigenere Cipher Encryption - Decryption Program

/* Program to implement Vigenere Cipher encryption and decryption technique.
*/

#include<iostream>
using namespace std;

string generateKey(string str, string key)
{
int x=str.size();
    for(int i=0; ;i++)
    {
        if(x==i)
        i=0;
        if(key.size()==str.size())
        break;
        key.push_back(key[i]);
    }
    return key;
}

string cipherText(string str, string key)
{
    string cipher_text;
    for(int i=0;i<str.size();i++)
    {
        int x=(str[i]+key[i])%26;
        x+='A';
        cipher_text.push_back(x);
    }
    return cipher_text;
}

string originalText(string cipher_text,string key)
{
    string orig_text;
    for(int i=0;i<cipher_text.size();i++)
    {
        int x=(cipher_text[i]-key[i]+26)%26;
        x+='A';
        orig_text.push_back(x);
    }
    return orig_text;
}

int main()
{
char str[100],keyword[100];
cout<<"\nEnter the string: ";
cin>>str;
cout<<"Enter the keyword: ";
cin>>keyword;

    string key=generateKey(str,keyword);
    string cipher_text=cipherText(str,key);

    cout<<"\nEncrypted text is: "<<cipher_text<<"\n";
    cout<<"Decrypted text is: "<<originalText(cipher_text, key);
cout<<"\n"<<"\n";
    return 0;
}




OUTPUT SCREEN:



Vernam Cipher Encryption - Decryption Program

/* Program to implement Vernam Cipher encryption and decryption technique.
*/

#include<iostream>
using namespace std;

class vernam
{
       public:
              string s,k;
              char enc[1000],dec[1000];
              vernam(string a, string b)
              {
                     s=a;
                     k=b;
              }
              void encrypt()
              {
                     int i,j=0;
                     for(i=0;i<s.size();i++)
                     {
                           enc[i]=s[i]^k[j];
                           j++;
                           if(j>=k.size())
                           {
                                  j=0;
                           }
                     }
              }
              void decrypt()
              {
                     int i,j=0;
                     for(i=0;i<s.size();i++)
                     {
                           dec[i]=enc[i]^k[j];
                           j++;
                           if(j>=k.size())
                           {
                                  j=0;
                           }
                     }
              }
              void printenc()
              {
                     int i;
                     char c;
                     for(i=0;i<s.size();i++)
                     {
                           c=enc[i]%74+48;
                           cout<<c;
                     }
                     cout<<endl;
              }
              void printdec()
              {
                     int i;
                     for(i=0;i<s.size();i++)
                     {
                           cout<<dec[i];
                     }
                     cout<<endl;
              }
};

int main()
{
        string s,k;
        cout<<"\nEnter the Message for encryption: ";
        getline(cin,s);
        cout<<"Enter the Key: ";
        getline(cin,k);
   
        vernam v(s,k);
   
        v.encrypt();
        cout<<"\nEncrypted Message: ";
        v.printenc();
   
        v.decrypt();
        cout<<"Decrypted Message: ";
        v.printdec();
cout<<"\n";
       return 0;
}




OUTPUT SCREEN:



Affine Cipher Encryption - Decryption Program

/* Program to implement Affine Cipher encryption and decryption technique.
*/

#include<iostream>
#include<string.h>
using namespace std;
int inverse(int);
int main()
{
char str[100];
int key1,key2;
cout<<"\nEnter the string for encryption: ";
cin>>str;
cout<<"Enter the first cipher key: ";
cin>>key1;
cout<<"Enter the second cipher key: ";
cin>>key2;
int h=strlen(str);
for(int i=0;i<h;i++)
{
char T=(((toascii(str[i])-97)*key1)%26)+97;
char E=(((toascii(T)-97)+key2)%26)+97;
cout<<str[i]<<"\t"<<"-->"<<"\t"<<E<<"\n";
}

char str1[100];
int key3,key4;
cout<<"\nEnter the string for decryption: ";
cin>>str1;
cout<<"Enter the first decipher key: ";
cin>>key3;
cout<<"Enter the second decipher key: ";
cin>>key4;
int h1=strlen(str1);
int inv=inverse(key3);
for(int i=0;i<h1;i++)
{
int dkey=((toascii(str1[i])-97)-key4)%26;
if(dkey<0)
dkey+=26;
char T1=dkey+97;
int dkey1=(((toascii(T1)-97)*inv))%26;
char P=dkey1+97;
cout<<str1[i]<<"\t"<<"-->"<<"\t"<<P<<"\n";
}
cout<<"\n";
return 0;
}

int inverse(int key3)
{
int r1,r2,r,q,t1,t2,t;
r1=26; r2=key3;
t1=0; t2=1;
while(r2>0)
{
q=r1/r2;
r=r1-q*r2;
r1=r2; r2=r;
t=t1-q*t2;
t1=t2; t2=t;
}
if(r1==1)
{
if(t1<0)
t1+=26;
return t1;

}
}




OUTPUT SCREEN:



Multiplicative Cipher Encryption - Decryption Program

/* Program to implement Multiplicative Cipher encryption and decryption technique.
*/

#include<iostream>
#include<string.h>
using namespace std;
int inverse(int);
int main()
{
char str[100];
int key;
cout<<"\nEnter the string for encryption: ";
cin>>str;
cout<<"Enter the multiplicative cipher key: ";
cin>>key;
int h=strlen(str);
for(int i=0;i<h;i++)
{
char c=(((toascii(str[i])-97)*key)%26)+97;
cout<<str[i]<<"\t"<<((toascii(str[i])-97)*key)%26<<"\t"<<c<<endl;
}

char str1[100];
int key1;
cout<<"\nEnter the string for decryption: ";
cin>>str1;
cout<<"Enter the multiplicative decipher key: ";
cin>>key1;
int h1=strlen(str1);
int inv=inverse(key1);
for(int i=0;i<h1;i++)
{
int dkey=(((toascii(str1[i])-97)*inv))%26;
char ch=dkey+97;
cout<<str1[i]<<"\t"<<dkey<<"\t"<<ch<<"\n";
}
return 0;
}

int inverse(int key1)
{
int r1,r2,r,q,t1,t2,t;
r1=26; r2=key1;
t1=0; t2=1;
while(r2>0)
{
q=r1/r2;
r=r1-q*r2;
r1=r2; r2=r;
t=t1-q*t2;
t1=t2; t2=t;
}
if(r1==1)
{
if(t1<0)
t1+=26;
return t1;
}
}




OUTPUT SCREEN:



Additive Cipher Encryption - Decryption Program

/* Program to implement Additive Cipher encryption and decryption technique.
*/

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[100];
int key;

cout<<"\nEnter the message for encryption: ";
cin>>str;
cout<<"Enter the additive cipher key: ";
cin>>key;
int h=strlen(str);
for(int i=0;i<h;i++)
{
char c=(((toascii(str[i])-97)+key)%26)+97;
cout<<str[i]<<"\t"<<((toascii(str[i])-97)+key)%26<<"\t"<<c<<"\n";
}

char str1[100];
int key1;
cout<<"\nEnter the message for decryption: ";
cin>>str1;
cout<<"Enter the additive decipher key: ";
cin>>key1;
int h1=strlen(str1);
for(int i=0;i<h1;i++)
{
int dkey=((toascii(str1[i])-97)-key1)%26;
if(dkey<0)
dkey+=26;
char ch=dkey+97;
cout<<str1[i]<<"\t"<<dkey<<"\t"<<ch<<"\n";
}
cout<<"\n";
}




OUTPUT SCREEN:



Caesar Cipher Encryption - Decryption Program

/* Program to implement Caesar Cipher encryption and decryption technique.
*/

#include<iostream>
using namespace std;
int main()
{
int i;
char message[100], ch;
    cout<<"\nEnter the message for encryption: ";
    cin.getline(message,100);
    for(i=0;message[i]!='\0';++i)
{
        ch=message[i];
        if(ch>='a'&&ch<='z')
{
            ch=ch+3;
            if(ch>'z')
{
                ch=ch-'z'+'a'-1;
            }
            message[i]=ch;
        }
        else if(ch>='A'&&ch<='Z')
{
            ch=ch+3;
            if(ch>'Z')
{
                ch=ch-'Z'+'A'-1;
            }
            message[i]=ch;
        }
    }
    cout<<"Encrypted message: "<<message<<'\n'<<'\n';

char message1[100], ch1;
    cout<<"Enter the message for decryption: ";
    cin.getline(message1,100);
    for(i=0;message1[i]!='\0';++i)
{
        ch1=message1[i];
        if(ch1>='a'&&ch1<='z')
{
            ch1=ch1-3;
            if(ch1<'a')
{
                ch1=ch1+'z'-'a'+1;
            }
            message1[i]=ch1;
        }
        else if(ch1>='A'&&ch1<='Z')
{
            ch1=ch1-3;
            if(ch1>'a')
{
                ch1=ch1+'Z'-'A'+1;
            }
            message1[i]=ch1;
        }
    }
    cout<<"Decrypted message: "<<message1<<'\n'<<'\n';
    return 0;
}



OUTPUT SCREEN:



Project on Library Management System (C++)

=> The project titled Library Management system is Library management software for monitoring and controlling the transactions in a lib...