MOUSE POINTER IN C(USING GRAPHICS)


#include<dos.h>
#include<graphics.h>
#include<alloc.h>
#include<conio.h>
#include<stdio.h>
char *menu[]={“samosa”,”sambharwada”,”dahiwada”,”exit”};
union REGS i,o;
main()
{
int gd=DETECT,gm,choice=1,bill=0,width=0,i,count;
char **buffer;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
if(initmouse()==0)
{
printf(“\n unable to initialise mouse…”);
exit();
}

count=sizeof(menu)/sizeof(char *);
settextstyle(TRIPLEX_FONT,0,3);
displaymenu(menu,count,100,100);

for(i=0;i<count;i++)
{
if(textwidth(menu[i])>width)
width=textwidth(menu[i]);
}
buffer=malloc(sizeof(menu));
savemenu(menu,buffer,width,count,100,100);

while(choice!=4)
{
choice=getresponse(menu,buffer,width,count,100,100);
gotoxy(50,15);
printf(“you selected%s”,menu[choice-1]);
}
displaymenu(char**menu,int count,int x1,int y1)
{
int i,h;
h=textheight(menu[0]);
for(i=0;i<count;i++)
{
outtexttxy(x1,y1+i*(h+5),menu[i]);
}
}

savemenu(char**menu,char**buffer,int width,int count,int x1,int y1)
{
int i,x2,yy1,yy2,area,h;
h=textheight(menu[0]);
for(i=0;i<count;i++)
{
x2=x1+width;
yy1=y1+i*(h+5);
yy2=y1+(i+1)*(h+5);
area=imagesize(x1,yy1,x2,yy2);
buffer[i]=malloc(area);
getimage(x1,yy1,x2,yy2,buffer[i]);
}
}

getresponse(char**menu,char**buffer,int width,int count,int x1,int y1)
{
int choice=1,prevchoice=0,x,y,x2,y2,button;
int in,i,h;
h=textheight(menu[0]);
y2=y1+count*(h+5);
x2=x1+width;
rectangle(x1-5,y1-5,x2+5,y2+5);

while(1)
{
getmousepos(&button,&x,&y);
if(x>=x1 &&x<=x2 && y>=y1 && y<=y2)
{
in=1;
for(i=1;i<=count;i++)
{
if(y<=y1+i*(h+5))
{
choice=i;
break;
}
}

if(prevchoice!=choice)
{
hidemouseptr();
highlight(buffer,choice,h,x1,y1);
if(prevchoice)
dehighlight(buffer,prevchoice,h,x1,y1);
prevchoice=choice;
showmouseptr();
}

if((button & 1)==1)
{

while((button & 1)==1)
getmousepos(&button,&x,&y);
if(x>=x1 && x<=x2 && y>=y1 && y<=y2)
return(choice);
}
}
else
{
if(in==1)
{
in=0;
prevchoice=0;
hidemouseptr();
dehighlight(buffer,choice,h,x1,y1);
showmouseptr();
}
}
}
}

highlight(char**menu,char**buffer,int width,int count,int x1,int y1)
{
putimage(x1,y1+(ch-1)*(h+5),buffer[ch-1],NOT_PUT);
}

dehighlight(char**menu,char**buffer,int width,int count,int x1,int y1)
{
putimage(x1,y1+(ch-1)*(h+5),buffer[ch-1],COPY_PUT);
}

initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}

showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}

hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
}

getmousepos(int*button,int*x,int*y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}
}
}

Cricket Record Keeping Project in C++


//CRICKET RECORD KEEPING
//By Aditya K R (adityakr6124@gmail.com)
//Downloaded From c4cpp.co.nr

#include<fstream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<iostream>
using namespace std;

class data
{char name[30];
 int playercode;
 int M1R;
 int M2R;
 int M3R;
 int total_run;
public:
data()
{playercode=0;
strcpy(name,””);
M1R=0;
M2R=0;
M3R=0;
total_run=0;
}

void enter_name_runs()       //TO TAKE ENTRY OF NEW RECORDS
{cout<<“\nEnter player code(Integral) : “;
 cin>>playercode;
 cout<<“\nEnter player name : “;
 gets(name);
 cout<<“\nFirst match run(s)    : “;
 cin>>M1R;
 cout<<“\nSecond match run(s)    : “;
 cin>>M2R;
 cout<<“\nThird match run(s)  : “;
 cin>>M3R;
total_runs();
}
void show_record()              //TO DISPLAY THE RECORDS EXISTING IN THE FILE
{cout<<“\nPlayer code          :”<<playercode;
 cout<<“\nPlayer name          : “<<name;
 cout<<“\nFirst match run(s)   : “<<M1R;
 cout<<“\nSecond match run(s)  : “<<M2R;
 cout<<“\nThird match run(s)   : “<<M3R;
 cout<<“\nTotal run(s)         : “<<total_run;
}
void total_runs()                //CALCULATE TOTAL RUNS
{total_run=M1R+M2R+M3R;
}
int getplayercode()
{return playercode;
}
int gettotalruns()
{return total_run;
}
char* get_name()
{return name;
}

void modify_data()                   //TAKE DATA TO MODIFIFY EXISTING RECORD
{cout<<“\nEnter new data for modification :::::::: “;
cout<<“\nEnter First match run(s) : “;
cin>>M1R;
cout<<“\nEnter Second match run(s) : “;
cin>>M2R;
cout<<“\nEnter Third match run(s) : “;
cin>>M3R;
total_runs();
} };
data d;
fstream file;
fstream file1;
//*  *  *  *  *  *  *  *  *  *  CALCULATE HIGHEST RUN(s)  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
void highest()
{ file.open(“record.dat”,ios::in|ios::binary);
int post=0,tr=0,plr_code=0,run=0;

file.read((char*)&d,sizeof(d));
while(file)
{run=d.gettotalruns();
 if(run>tr)
 {post=file.tellg();
 tr=run;
 }
 file.read((char*)&d,sizeof(d));
 }file.close();
file.open(“record.dat”,ios::in|ios::binary);
file.seekg(post-sizeof(d));
file.read((char*)&d,sizeof(d));
cout<<“\n::::::::::Highest run getter data::::::::::”;
cout<<“\nHighest run(s) getter code     : “<<d.getplayercode();
cout<<“\nHighest run getter player is   :  “<<d.get_name();
cout<<“\nPlayer total run(s) are        : “<<tr;
float avg;
avg=(tr)/3;
cout<<“\n    Player average is              : “<<avg;
file.close();
}
//*  *  *  *  *  *  *  *  *  *  *  *  INSERT NEW RECORD(s)  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
void insert()
{int i,no;
file.open(“record.dat”,ios::in|ios::app|ios::binary);
cout<<“\n           ::Entry of new record(s)::          “;
cout<<“\nHow many record(s) you want to enter : “;
cin>>no;
for(i=1;i<=no;i++)
{d.enter_name_runs();               //insert  records
file.write((char*)&d,sizeof(d));
}file.close();
}
//*  *  *  *  *  *  *  *  *  *  *  *  *  DISPLAY EXISTING RECORD(s)   *  *  *  *  *  *  *  *  *  *  *  *  *  *
void display()
{file.open(“record.dat”,ios::in|ios::binary);
cout<<“\n|||||||||||||||||||||| Entered record(s) ||||||||||||||||||||||||”;
file.read((char*)&d,sizeof(d));
while(file)
{d.show_record();
file.read((char*)&d,sizeof(d));
}file.close();
}
//*  *  *  *  *  *  *  *  *  *  *  *  * SEARCH RECORD *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
void search()
{int p,r,srch=0;
file.open(“record.dat”,ios::in|ios::binary);
cout<<“\nEnter the player code to see his records : “;
cin>>r;
file.seekg(0);
file.read((char*)&d,sizeof(d));
while(file)
{p=d.getplayercode();
 if(r==p)
 {d.show_record();
  srch=1;
  break;
 }
 else
 { file.read((char*)&d,sizeof(d));
 }
}file.close();
if(srch==0)
{cout<<“\nThere is no record which have this playercode .”;
}
}
//*  *  *  *  *  *  *  *  *  *  *  *  *  *  MODIFY DATA *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
void modify()
{int posi=0,got=0,dmd=0;
 cout<<“\nEnter the playercode whose record to be modified : “;
 cin>>dmd;
int ifdata=1;
 file.open(“record.dat”,ios::in|ios::out|ios::binary);
 posi=file.tellg();
 while(file)
{ file.read((char*)&d,sizeof(d));
 got=d.getplayercode();
if(dmd==got)
      {file.seekg(posi);
d.modify_data();
ifdata=2;
file.write((char*)&d,sizeof(d));
      break;
      }
      posi=file.tellg();
}
file.close();
if(ifdata==1)
{cout<<“\n           Data not available for modification         “;
}
}
//*  *  *  *  *  *  *  *  *  *  *  *  *  *  * DELETE RECORD*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
void delete_record()
{int pointer=0,remove_record=0,size=0,code_get=0;
// size=sizeof(file);
 cout<<“\n Enter the player code whose record to be deleated : “;
 cin>>remove_record;
 file.open(“record.dat”,ios::in|ios::out|ios::binary);
 file1.open(“tempo.dat”,ios::in|ios::out|ios::binary);
 file.seekg(0);
 while(file)
 {file.read((char*)&d,sizeof(d));
  code_get=d.getplayercode();
  if(remove_record==code_get)
  {cout<<” “;
  }
  else
  { file1.write((char*)&d,sizeof(d));
  }
}file.close();
file1.close();
remove(“record.dat”);
rename(“tempo.dat”,”record.dat”);
}
//*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
void main()
{clrscr();
int choice;
char ans=’n’;
do
{cout<<“\n=*=*=*=*=*=*=*=*=* PLAYER RECORD KEEPING SYSTEM -Animesh*=*=*=*=*=*=*=*=*=*”;
cout<<“\n 1.INSERT RECORD \n 2.DISPLAY RCEORD \n 3.SEARCH RECORD \n 4.MODIFY RECORD \n 5.HIGHEST RUNS \n 6.DELETE PREVIOUS ENTERED RECORD \n 7.EXIT  “;
cout<<“\n Enter any one of the options : “;
cin>>choice;

switch(choice)
{case 1: insert();
 break;
 case 2: display();
 break;
 case 3: search();
 break;
 case 4: modify();
 break;
 case 5: highest();
 break;
 case 6: delete_record();
 break;
 case 7: exit(0);
 break;
default: cout<<“\n Enter choice between 1 to 6 “;
}
cout<<“\n Do you want to choose any other option ? :(y/n): “;
cin>>ans;
clrscr();
}while(ans==’y’||ans==’Y’);
getch();
}

Search An Element in Linked List in C


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
                  {
                    int data;
                    struct node*next;
                  };
 void insert(struct node**p,int num)   /*Function for inserting an
element into a list */
 
 {
   if(*p==NULL)
     {
      (*p)=(struct node*)malloc(sizeof(struct node));
      (*p)->next=NULL;
      (*p)->data=num;
     }
   else
    {
     insert(&((*p)->next),num);
    }
 }
 
void display(struct node*p) /*Function for displaying the list*/
 {
   while(p!=NULL)
    {
     printf(“%d “,p->data);
     p=p->next;
    }
 }
 
 
void reverse(struct node**p) /*Function for reversing the list by
recursion */
 {
   struct node*q,*r,*x;
     int d;
     q=(*p);     /*stores the address of the first element */
     x=q;        /*also stores the element of the first element for
counter pourpose */
     d=q->data;  /*stores the data of the first element*/
     r=q->next;  /*stores the address of the second element in the list
*/
      free(q);   /*deletes the first element of the list*/
      if(x==NULL)
                return ;
                else
                  {
                    reverse(&(r));/*Recursive call*/
                    insert(p,d);  /*This function is put in the stack so the first
                                                     will be taken as last element for the new list */
                  }
 }
 
 void main()
 {
  clrscr();
  struct node*p=NULL;
  int n,d,i=0;
   printf(“How many…?”);
   scanf(“%d”,&n);
    while(i++!=n)
     {
      scanf(“%d”,&d);
      insert(&p,d);
     }
  display(p);
  reverse(&p);
  printf(“The reversed list is…”);
  display(p);
  getch();
 }

Heapsort algorithm Program in C


#include <stdlib.h>  
#include <stdio.h>
 
 
#define uint unsigned int
 
typedef int (*compare_func)(int, int);
 
 
void heap_sort(int This[], compare_func func_pointer, uint len)
{
  /* heap sort */
 
  uint half;
  uint parents;
 
  if (len <= 1)
    return;
 
  half = len >> 1;
  for (parents = half; parents >= 1; –parents)
  {
    int tmp;
    int level = 0;
    uint child;
 
    child = parents;
    /* bottom-up downheap */
 
    /* leaf-search for largest child path */
    while (child <= half)
    {
      ++level;
      child += child;
      if ((child < len) &&
          ((*func_pointer)(This[child], This[child – 1]) > 0))
        ++child;
    }
    /* bottom-up-search for rotation point */
    tmp = This[parents – 1];
    for (;;)
    {
      if (parents == child)
        break;
      if ((*func_pointer)(tmp, This[child – 1]) <= 0)
        break;
      child >>= 1;
      –level;
    }
    /* rotate nodes from parents to rotation point */
    for (;level > 0; –level)
    {
      This[(child >> level) – 1] =
        This[(child >> (level – 1)) – 1];
    }
    This[child – 1] = tmp;
  }
 
  –len;
  do
  {
    int tmp;
    int level = 0;
    uint child;
 
    /* move max element to back of array */
    tmp = This[len];
    This[len] = This[0];
    This[0] = tmp;
 
    child = parents = 1;
    half = len >> 1;
 
    /* bottom-up downheap */
 
    /* leaf-search for largest child path */
    while (child <= half)
    {
      ++level;
      child += child;
      if ((child < len) &&
          ((*func_pointer)(This[child], This[child – 1]) > 0))
        ++child;
    }
    /* bottom-up-search for rotation point */
    for (;;)
    {
      if (parents == child)
        break;
      if ((*func_pointer)(tmp, This[child – 1]) <= 0)
        break;
      child >>= 1;
      –level;
    }
    /* rotate nodes from parents to rotation point */
    for (;level > 0; –level)
    {
      This[(child >> level) – 1] =
        This[(child >> (level – 1)) – 1];
    }
    This[child – 1] = tmp;
  } while (–len >= 1);
}
 
 
#define ARRAY_SIZE 250000
 
int my_array[ARRAY_SIZE];
 
void init()
{
  int indx;
 
  for (indx=0; indx < ARRAY_SIZE; ++indx)
  {
    my_array[indx] = rand();
  }
}
 
int cmpfun(int a, int b)
{
  if (a > b)
    return 1;
  else if (a < b)
    return -1;
  else
    return 0;
}
 
int main()
{
  int indx;
 
  init();
 
  heap_sort(my_array, cmpfun, ARRAY_SIZE);
 
  for (indx=1; indx < ARRAY_SIZE; ++indx)
  {
    if (my_array[indx – 1] > my_array[indx])
    {
      printf(“bad sort\n”);
      return(1);
    }
  }
 
  return(0);
}

Prgram to insert & delete the elemnts from queue


#include<iostream.h>
 
#include<conio.h>
 
#include<stdlib.h>
 
 
 
class queue
 
{
 
    private:
 
        int i,r,q[25],front,rear;
 
    public:
 
        void enqueue(int);
 
        void dequeue();
 
        void getdata();
 
};
 
 
 
 
 
void queue::getdata()
 
{
 
    clrscr();
 
    cout<<endl<<“enter the=”” range=”” of=”” queue:”;=”” <br=””> cin>>r;
 
    cout<<endl<<“enter the=”” elments=”” of=”” queue:”;=”” <br=””>   for(i=0;i<r;i++) <br=””>    {
 
        cin>>q[i];
 
    }
 
    front=q[0];
 
    rear=q[2];
 
}
 
 
 
 
 
void queue::dequeue()
 
{
 
    int item;
 
    if(front==0)
 
    {
 
        cout<<endl<<“queue is=”” empty.”;=”” <br=””>     exit(0);
 
    }
 
    else
 
    {
 
        item=q[1];
 
    }
 
    if(front==rear)
 
    {
 
        front=0;
 
        rear=0;
 
    }
 
    else
 
    {
 
        front=front+1;
 
    }
 
 
 
    cout<<endl<<“after delection;”;=”” <br=””>
 
    for(i=0;i<r;i++) <br=””>    {
 
        cout<<“\t”<<q[i]; <br=””>  }
 
}
 
 
 
 
 
void queue::enqueue(int item)
 
{
 
    if(rear==r)
 
    {
 
        cout<<endl<<“queue is=”” full.”;=”” <br=””>      exit(0);
 
    }
 
    else
 
    {
 
        if(rear==0 && front==0)
 
        {
 
            front=1;
 
            rear=1;
 
        }
 
        r=r+1;
 
        q[r]=item;
 
    }
 
    cout<<endl<<“after insertion.”;=”” <br=””>   for(i=0;i<r+1;i++) <br=””>  {
 
        cout<<“\t”<<q[i]; <br=””>  }
 
}
 
 
 
 
 
void main()
 
{
 
    int ch,no;
 
    clrscr();
 
    char cho;
 
    queue q;
 
    q.getdata();
 
    cout<<endl<<endl<<“select the=”” operation=”” you=”” “;=”” <br=””>   cout<<“want to perform on queue:”;
 
    cout<<endl<<endl<<“1) insert”<<endl<<endl<<“2)=”” delete”;=”” <br=””>    cout<<endl<<endl<<“enter your=”” choice:”;=”” <br=””>    cin>>ch;
 
    do
 
    {
 
        switch(ch)
 
        {
 
            case 1:
 
                cout<<endl<<“enter the=”” number=”” which=”” you=”” want=”” to=”” insert:”;=”” <br=””>               cin>>no;
 
                q.enqueue(no);
 
                break;
 
            case 2:
 
                q.dequeue();
 
                break;
 
        }
 
    cout<<endl<<endl<<“do you=”” want=”” to=”” continue:”;=”” <br=””>    cin>>cho;
 
    }
 
    while(cho==’y’||cho==’Y’);
 
}