50% found this document useful (2 votes)
896 views53 pages

C++ Project On Cricket Database System

The document discusses code for menu driven programs to perform file operations like creating, reading, editing and inserting records in a file. It includes function definitions and class declarations to read and write data to files. The main program uses a switch case to call different functions based on user input, like creating a file, reading contents, editing a specific record, inserting a new record at a given position. The functions use file streams to open, read, write and close data files.

Uploaded by

AbhistChauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
896 views53 pages

C++ Project On Cricket Database System

The document discusses code for menu driven programs to perform file operations like creating, reading, editing and inserting records in a file. It includes function definitions and class declarations to read and write data to files. The main program uses a switch case to call different functions based on user input, like creating a file, reading contents, editing a specific record, inserting a new record at a given position. The functions use file streams to open, read, write and close data files.

Uploaded by

AbhistChauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

/* Infix to postfix conversion using stack*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int size = 50;
char infix[size], postfix[size], Stack[size];
int top = -1;
int precedence(char ch);
char Pop();

//function to get precedence of operator

//function to pop an element from stack

char Topelement();

//function to return top element from stack without popping

void Push(char ch);

//function to push an elemnt in the stack

int braces(char *);

//function to match number of braces

int main()
{ clrscr();
char ele, elem, st[20];
int prep, pre, popped, j =0 ,chk = 0;
strcpy(postfix," ");
cout<<"ASSUMPTIoN: The infix expression contains single letter variables\n\t\t
and single digit expression only. \n";
cout<<"Enter Infix Expression :\t ";
gets(infix);
chk = braces(infix);
if(chk!=0)
{ cout<<"Unbalanced no. of braces.. \n\t Extra";
cout<<(chk == 1 ? "right braces":"left braces")<<endl;

system("pause");
exit(1);
} for(int i = 0; infix[i]!='\0';i++)
{ if(infix[i]!='('&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!
='-')
postfix[j++]=infix[i];
else if(infix[i]=='(')
{ elem = infix[i];
Push(elem);
} else if(infix[i]==')')
{ while((popped = Pop())!= '(')
{ postfix[j++]=popped;
} }
else
{elem = infix[i];
pre = precedence(elem);//pre stores precedence of operator coming
from infix expression
ele = Topelement();
prep = precedence(ele);//prep stores precedence of operator at the top
of stack
if(pre>prep) Push(elem);
else
{ while(prep>=pre)
{ if(ele=='#') break;
popped = Pop();
ele = Topelement();
postfix[j++] = popped;

prep = precedence(ele);
}
Push(elem);
}}

while((popped = Pop() )!= '#')


postfix[j++]= popped;
postfix[j++]= '\0';
cout<<"\nPostfix:"<<postfix<<endl;
getch();
return 0;
}
int precedence(char ch)
{ switch(ch)
{ case'^':return 5;
case'/':return 4;
case'*': return 4;
case'+':return 3;
case'-':return 3;
default : return 0;
}}
char Pop() //function to pop an element from Stack
{ char ret;
if(top!=-1)
{ ret = Stack[top];
top--;
return ret;

}
else
return '#'; }
char Topelement()//function to return top element from Stack withoutpopping
{ char ch;
if(top!=-1)
ch= Stack[top];
else
ch = '#';
return ch;
}
void Push(char ch)
{ if(top!=size-1)
{ top++;
Stack[top]= ch;
}}
int braces (char *s)
{ int leftbr,rightbr;
leftbr=rightbr=0;
for(int i = 0; s[i]; i++)
{ if(s[i]=='(')
leftbr++;
else
if(s[i]==')')
rightbr++;
} if(leftbr == rightbr)

return 0;
else if(leftbr<rightbr)
return 1;
else return -1;
}

OUTPUT

/* Wap to implement stack as a linked list */

#include<iostream.h>
#include<conio.h>
#include<process.h>

struct node {
int roll;
node* next;
}*top,*save,*ptr,*newptr,*np;
node *create(int a)
{ ptr=new node;
ptr->roll=a;
ptr->next=NULL;
return ptr;
}
void push(node *np)
{ if(top==NULL)
top=np;
else
{ save=top;
top=np;
np->next=save;
}}

void pop()
{ if(top==NULL)

cout<<"\n Underflow!!!!";
else
{ ptr=top;
top=top->next;
delete ptr;
}

void display(node *np)


{ while(np!=NULL)
{ cout<<np->roll<<" -> ";
np=np->next;
}

void main()
{ clrscr();
top=NULL;
int n,m;
char k,ch;
do { cout<<"\nChoose from the menu :\n"
<<"\n 1. Push."
<<"\n 2. Pop."
<<"\n 3. Display."
<<"\n 4. Quit."
<<"\n\nEnter your choice : ";
cin>>n;
switch(n)
{ case 1: k='y';

while(k=='y'||k=='Y')
{ cout<<"\n Enter element to be inserted .";
cin>>m;
newptr=create(m);
if(newptr==NULL)
cout<<"\n Cannot create !!!!";
push(newptr);
cout<<"\n The Stack formed is : ";
display(top);
cout<<"\n\n Want to enter again ?: ";
cin>>k;
}
break;
case 2: k='y';
while(k=='y'||k=='Y')
{ pop();
cout<<"\n The Stack formed is : \n\n";
display(top);
cout<<"\n\n Want to delete again ?: ";
cin>>k;
}
break;
case 3: cout<<"\n The Stack formed is : ";
display(top);
break;

case 4: exit(0);
break;
default: cout<<"\n Please enter desired keyword : ";
}
cout<<"\n Do you want to continue..? : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}

OUTPUT

/* Wap in c++ to implement Queue as a linked list */


#include<iostream.h>
#include<conio.h>

struct node {
int roll;
node* next;
}*front,*rear,*ptr,*newptr,*np;
node *create(int a)
{ ptr=new node;
ptr->roll=a;
ptr->next=NULL;
return ptr;
} void insert(node *np)
{ if(front==NULL)
front=rear=np;
else
{ rear->next=np;
rear=np;
}

void delet()
{

if(front==NULL)
cout<<"\n Underflow!!!!";

else
{ ptr=front;
front=front->next;
delete ptr;
}

void display(node *np)


{

while(np!=NULL)
{ cout<<np->roll<<"-> ";
np=np->next;
}

void main()
{ clrscr();
front=rear=NULL;
int n,m;
char ans,ch;
do
{ cout<<"\nChoose from the menu : "
<<"\n 1) Insert."
<<"\n 2) Delete"
<<"\n 3) Display."
<<"\n\n Enter your choice : ";
cin>>n;
switch(n)
{

case 1: ans='y';
while(ans=='y'||ans=='Y')
{ cout<<"\n Enter element to be inserted .";
cin>>m;
newptr=create(m);
if(newptr==NULL)
cout<<"\n Cannot create !!!!";
insert(newptr);
cout<<"\n The Queue formed is : ";

display(front);
cout<<"\n Want to enter more nodes ?: ";
cin>>ans;
}
break;
case 2: ans='y';
while(ans=='y'||ans=='Y')
{
delet();
cout<<"\n Queue : ";
display(front);
cout<<"\n Want to delete more ?: ";
cin>>ans;
}

break;
case 3: cout<<"\n Queue : ";
display(front);
break;
default: cout<<"\n You entered wrong choice...";
}
cout<<"\n Want to return to main menu ? : ";
cin>>ch;
}while(ch=='y'||ch=='Y');

getch();
}

OUTPUT

//Menu driven program to create a file,read a file,count the no. of records and search for a
records

//header files used


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

//class declaration
class floppy
{ public:
int floppy_id;
char name[20];
int size;
void getdata()
{ cout<<"Enter the floppy id ";
cin>>floppy_id;
cout<<"Enter the name of the floppy ";
gets(name);
cout<<"Enter the size of the floppy ";
cin>>size;
}
void display()
{

cout<<"Floppy Id ===>"<<floppy_id;
cout<<"\nFloppy Name ===>";
puts(name);
cout<<"Size ===>"<<size<<"\n";
}
}f1;
void create()

//function to create a file

{
char c;
fstream file;
file.open("floppy.dat",ios::out|ios::binary);
do
{
f1.getdata();
file.write((char*)&f1,sizeof(f1));
cout<<"\nDo you want to enter another record?(y/n)"<<"\n";
cin>>c;
}while(c=='y');
file.close();
}
void showfile()
{
fstream file;

//function to read a file

file.open("floppy.dat",ios::in|ios::binary);
while(file.read((char*)&f1,sizeof(f1)))
{
f1.display();
}
file.close();
}
void main()
{

fstream file;
char q;
int ch;
do
{clrscr();
cout<<"What do you want to do ?\n";
cout<<"1.Create a file\n";
cout<<"2.Read a file\n";
cout<<"3.Count the number of records\n";
cout<<"4.Search a floppy by name or id\n";
cout<<"Enter your choice ";
cin>>ch;

switch(ch)
{
case 1: create();
break;
case 2:
showfile();
break;
case 3: int r=0;
file.open("floppy.dat",ios::in);
while(file.read((char*)&f1,sizeof(f1)))

//counting the total no. of records

{
++r;
};
file.close();
cout<<"No. of records= "<<r<<"\n";
break;
case 4:

//case for searching a record by name or id

int w;
file.open("floppy.dat",ios::in|ios::binary);
cout<<"Search according to\n"<<"1.Name\n"<<"2.Id\n";
cin>>w;
if(w==1)
{

char n[20],e[20];
cout<<"Enter the name you want to search\n";
gets(n);
while(file.read((char*)&f1,sizeof(f1)))
{
strcpy(e,f1.name);
if(strcmp(n,f1.name)==0)
f1.display();
};
file.close();
break;
}

if(w==2)
{
int i;
cout<<"Enter the Id of the floppy you want to search\n";
cin>>i;
while(file.read((char*)&f1,sizeof(f1)))
{
if(f1.floppy_id==i)
f1.display();
};

file.close();
break;
}
}
cout<<"Do you want to continue?(y/n)";
cin>>q;
}while(q=='y');
getch();
}

OUTPUT

//menu driven program to create a file,read a file,edit a record or insert a new record
//header files used
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
int r=1;
class book
{ public:
int book_id;
char name[20];
int pages;
void getdata()
{ cout<<"Enter the book id ";
cin>>book_id;
cout<<"Enter the name of the book ";
gets(name);
cout<<"Enter the number of pages in the book ";
cin>>pages;
} void display()
{ cout<<"Book Id ===>"<<book_id;
cout<<"\nBook Name ===>";

puts(name);
cout<<"Pages===>"<<pages<<"\n"<<"\n";
} }f1,f2;
void create();

//function prototype

void showfile()

//function to read a file

{ fstream file;
file.open("book.dat",ios::in|ios::binary);
while(file.read((char*)&f1,sizeof(f1)))
{ f1.display();
};
file.close();
} void rewrite()

//function to rewrite a file

{ fstream file,temp;
file.open("book.dat",ios::out|ios::binary);
temp.open("temp.dat",ios::in|ios::binary);
while(temp.read((char*)&f1,sizeof(f1)))
{ file.write((char*)&f1,sizeof(f1));
} file.close();
temp.close();
remove("temp");
} //main program
void main()
{ clrscr();

fstream file,temp;
int ch,r=0;
char q;
do
{ clrscr();
cout<<"What do you want to do ?\n";
cout<<"1.Create a file\n";
cout<<"2.Read a file\n";
cout<<"3.Edit nth record\n";
cout<<"4.Insert a record after nth record\n";
cout<<"5.Insert a record after a specified book id\n";
cout<<"Enter your choice ";
cin>>ch;
switch(ch)
{ case 1: create();
break;
case 2: showfile();
break;
case 3:
int i;
cout<<"Enter the record no. you want to edit ";
cin>>i;
cout<<"Enter the altered record\n";

f2.getdata();
temp.open("temp.dat",ios::out|ios::binary);
file.open("book.dat",ios::in|ios::binary);
while(file.read((char*)&f1,sizeof(f1)))
{

//case for editing a specified record

r++;

if(r==i)
temp.write((char*)&f2,sizeof(f2));
else
temp.write((char*)&f1,sizeof(f1));
}; file.close();
temp.close();
rewrite();
showfile();
break;
case 4:
int y=0,u;
cout<<"Enter the new record\n";
f2.getdata();
cout<<"Enter the record after which you want to insert the record ";
cin>>u;
temp.open("temp.dat",ios::out|ios::binary);
file.open("book.dat",ios::in|ios::binary);
while(file.read((char*)&f1,sizeof(f1)))

y++;

if(y==u)
{ temp.write((char*)&f1,sizeof(f1));

//case for inserting a record at a

specified postion
temp.write((char*)&f2,sizeof(f2));
}

else

temp.write((char*)&f1,sizeof(f1));
}; temp.close();
file.close();
rewrite();
showfile();
break;
case 5:
int w;
cout<<"Enter the new record\n";
f2.getdata();
cout<<"enter the book id after which you want to insert the record ";
cin>>w;
file.open("book.dat",ios::in|ios::binary);
temp.open("temp.dat",ios::out|ios::binary);
while(file.read((char*)&f1,sizeof(f1)))
{

if(f1.book_id!=w)

temp.write((char*)&f1,sizeof(f1));

//case for inserting a record after

a specified book id
else
{

temp.write((char*)&f1,sizeof(f1));

temp.write((char*)&f2,sizeof(f2));
}

temp.close();
file.close();
rewrite();
showfile();
break;
} cout<<"Do you want to continue?(y/n) ";
cin>>q;
}while(q=='y');
getch();
} void create()

//function to create a file

{ char c;
fstream file;
file.open("book.dat",ios::out|ios::binary);
do
{ clrscr();
f1.getdata();
file.write((char*)&f1,sizeof(f1));

cout<<"Do you want to enter another record?(y/n)";


cin>>c;
} while(c=='y');
file.close();
}

OUTPUT

//Menu driven program to create a file,read a file,delete the last record or delete any record
//header files used
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

#include<fstream.h>
int r=0;
class employee
{ public:
int eno;
char ename[20];
double salary;
void getdata()
{ cout<<"Enter the employee no. ";
cin>>eno;
cout<<"Enter the name of the employee ";
gets(ename);
cout<<"Enter the employee's salary ";
cin>>salary;
} void display()
{ cout<<"Employee no. ===>"<<eno;
cout<<"\nEmployee's Name ===>";
puts(ename);
cout<<"Salary ===>"<<salary<<"\n";
} } f1;
void create();

//function prototype

void showfile()

//function to read a file

{ fstream file;

file.open("emp.dat",ios::in|ios::binary);
while(file.read((char*)&f1,sizeof(f1)))
{ f1.display();
}; file.close();
} void rewrite()

//function to rewrite a file

{ fstream file,temp;
file.open("emp.dat",ios::out|ios::binary);
temp.open("temp.dat",ios::in|ios::binary);
while(temp.read((char*)&f1,sizeof(f1)))
{ file.write((char*)&f1,sizeof(f1));
} file.close();
temp.close();
remove("temp");
} void dellst()

//function to delete the last record

{ fstream file,temp;
int e=1;
file.open("emp.dat",ios::in|ios::binary);
temp.open("temp.dat",ios::out|ios::binary);
file.seekg(0);
while(file.read((char*)&f1,sizeof(f1)))
{ if(e!=r)
{ temp.write((char*)&f1,sizeof(f1));
e++;

} file.close();
temp.close();
rewrite();
} showfile();
} void main()
{ clrscr();
fstream file,temp;
int ch,r=0;
char q;
do
{ clrscr();
cout<<"What do you want to do ?\n";
cout<<"1.Create a file\n";
cout<<"2.Read a file\n";
cout<<"3.Count the no. of records\n";
cout<<"4.Delete the last record\n";
cout<<"5.Delete any record\n";
cout<<"Enter your choice ";
cin>>ch;
switch(ch)
{ case 1: create();
break;
case 2: showfile();

break;
case 3:
file.open("emp.dat",ios::in);
while(file.read((char*)&f1,sizeof(f1)))
{

r++;

};
file.close();
cout<<"No. of records= "<<r;
break;
case 4: dellst();
break;
case 5:
int w,t=1;
cout<<"enter the record you want to delete ";
cin>>w;
file.open("emp.dat",ios::in|ios::binary);
temp.open("temp.dat",ios::out|ios::binary);
while(file.read((char*)&f1,sizeof(f1)))
{

if(t!=w)

temp.write((char*)&f1,sizeof(f1));
t++;
}

temp.close();

file.close();

//case to delete any record

rewrite();
showfile();
break;
} cout<<"Do you want to continue?(y/n)";
cin>>q;
}while(q=='y');
getch();
}void create()

//function to create a file

{ char c;
fstream file;
file.open("emp.dat",ios::out|ios::binary);
do
{ clrscr();
f1.getdata();
file.write((char*)&f1,sizeof(f1));
cout<<"Do you want to enter another record?(y/n)";
cin>>c;
}while(c=='y');
file.close();
}

OUTPUT

/*Program To Find The Largest Number


Using Pointers*/
#include<iostream.h>
#include<conio.h>
int *big(int &a, int &b, int &c)
{ if((a>b) && (a>c))
return (&a);
if((b>c) && (b>a))
return (&b);
else
return(&c);
}
void main()

{ clrscr();
int *big(int&, int&,int&);
int n1,n2,n3,*large;
cout<<" Enter the numbers: \n";
cin>>n1>>n2>>n3;
large=big(n1,n2,n3);
cout<<"The largest number is: "<<*large;
getch();
}

OUTPUT

/* Wap using pointers to swap two integers. */


#include<iostream.h>
#include<conio.h>

void swap_using_pointers(int *, int *);

void main()
{
clrscr();

int a,b;

cout<<"\n\nEnter first integer : "; cin>>a;


cout<<"\n\nEnter second integer : "; cin>>b;

swap_using_pointers(&a,&b);

cout<<"\n\nNow value of first integer = "<<a;


cout<<"\n\nNow value of second integer = "<<b;

getch();

void swap_using_pointers(int *a,int *b)


{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

OUTPUT

/* PROGRAM FOR INSERTION,DELETION & DISPLAY


OF QUEUE USING ARRAY*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
int del(int[]);
int insert(int[],int);
void display(int[] ,int,int);
const int size=50;
int queue[size],front=-1,rear=-1;
void main()
{ clrscr();
int item,res;
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"Enter Number for insertion : ";
cin>>item;
res=insert(queue,item);

if(res==-1)
{ cout<<"\nOVERFLOW ";
exit(1);}
cout<<"Now the Queue (Front to Rear) is :- \n";
display(queue,front,rear);
cout<<"Want to insert more elements (y/n) : ";
cin>>ch;
}
cout<<"\nQueue After Deletion:";
ch='y';
while(ch=='y'||ch=='Y')
{ res=del(queue);
if(res==-1)
{ cout<<"\nUNDERFLOW ";
exit(1);}
else
{ cout<<"\nElement Deleted is : "<<res<<endl;
cout<<"\nNow the Queue(Front to Rear) is :- \n";
display(queue,front,rear);

cout<<"\nWant to delete more elements (y/n) : ";


cin>>ch;
}

}
getch();
}
int insert(int queue[],int ele)
{

if(rear==size-1)
return -1;
else if(rear==-1)
{
front=rear=0;
queue[rear]=ele;
}
else
{
rear++;
queue[rear]=ele;
}
return 0;

}
int del(int queue[])
{
int ret;
if(front==-1)

return -1;
else
{
ret=queue[front];
if(front==rear)
front=rear=-1;
else
front++;
}
return ret;
}
void display(int queue[],int front,int rear)
{
if(front==-1)
return ;
for(int i=front;i<rear;i++)
cout<<queue[i]<<"\t<-\t";
cout<<queue[rear]<<endl;
}

OUTPUT

//This program illustrates how to insert a node at kth position using Linked List
#include<iostream.h>
#include<conio.h>
class NODE
{ public:
int bookno;

NODE *link;
};
class LINKLIST
{ private:
NODE *first, *last,*temp;
public:
void NCREATE(int);
void insertatk(int);
void display();
};
void LINKLIST::NCREATE(int n)
{ first=new NODE;
cout<<"\n Enter the book no";
cin>>first->bookno;
first->link=NULL;
temp=first;
for(int i=1;i<n;i++)
{ last=new NODE;
cout<<"\n Enter the book number";
cin>>last->bookno;
last->link=NULL;
temp->link=last;
temp=last;

}
}
void LINKLIST::insertatk(int j)
{ int i=0;
NODE *newnode,*back;
newnode= new NODE;
cout<<"\nEnter the data value";
cin>>newnode->bookno;
newnode->link=NULL;
temp=first;
while( i<(j-1))
{ back=temp;
temp=temp->link;
i++;
}
back->link=newnode;
newnode->link=temp;
}
void LINKLIST::display()
{ temp=first;
cout<<"\nThe Linked List is: \n";
while(temp!=NULL)
{ cout<<"\n"<<temp->bookno;

temp=temp->link;
}
getch();
}
void main()
{ int ch,n,k;
LINKLIST list;
clrscr();
cout<<"\nEnter how many nodes in the list: ";
cin>>n;
list.NCREATE(n);
do
{ clrscr();
cout<<"\n1. For insert ";
cout<<"\n2. For display ";
cout<<"\n3. For quit ";
cout<<"\nEnter your choice ";
cin>>ch;
switch(ch)
{ case 1: cout<<"\nEnter the position at which insertion is required ";
cin>>k;
list.insertatk(k);
break;

case 2: list.display();
break;
}
}while(ch!=3);
}

OUTPUT

//This program illustrates the basic operations on stacks using array


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#define MAX 100
char stack[MAX];
int top;
void push(char stack[],char vl,int &top);
char pop(char stack[], int &top);
void show_stack(char stack[], int top);
void main()
{ int choice;
char val, opt='Y';
top=-1;
clrscr();
do

{ cout<<"\n\t\tMain Menu"<<endl;
cout<<"\t1. Addition of stack"<<endl;
cout<<"\t2. Deletion from stack"<<endl;
cout<<"\t3. Traversal of stack"<<endl;
cout<<"\t4. Exit from Menu"<<endl;
cout<<"Enter your choice: "<<endl;
cin>>choice;
switch(choice)
{ case 1: do
{ cout<<"Enter the value to be added to be added in the stack: "<<endl;
cin>>val;
push(stack,val,top);
cout<<"Do you want to add more element(Y/N)?: "<<endl;
cin>>opt;
} while(toupper(opt)=='Y');
break;
case 2: opt='Y';
do
{ val=pop(stack,top);
if (val!=-1)
cout<<"Value deleted from the stack is: "<<val<<endl;
cout<<"Do you want to delete more element(Y/N): "<<endl;
cin>>opt;

} while(toupper(opt)=='Y');
break;
case 3: show_stack(stack,top);
break;
case 4: exit(0);
}
}while(choice!=4);
}
void push(char stack[],char val,int &top)
{ if (top==MAX)
{ cout<<"Stack Overflow!"<<endl;
}
else
{ top=top+1;
stack[top]=val;
}
}
char pop( char stack[],int &top)
{ char value;
if (top<0)
{ cout<<"Stack Underflow!"<<endl;
value=-1;
}

else
{ value=stack[top];
top=top-1;
}
return(value);
}
void show_stack(char stack[],int top)
{ int i;
i=top;
clrscr();
cout<<"The values are: ";
do
{ cout<<"\n"<<stack[i];
i=i-1;
}
while(i>=0);
}

OUTPUT

You might also like