You are on page 1of 20

Telephone directory which can be used to store, edit, search data

#include<iostream.h>
#include<fstream.h>
#include<graphics.h>
#include<process.h>
#include<io.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

class telephone
{
char name[25];
char address[40];
char phoneno[15];
public:
telephone(){ }
telephone(char nam[25], char add[40], char phone[15])
{
strcpy(name,nam);
strcpy(address,add);
strcpy(phoneno,phone);
}
void init();
void button(int x1,int y1,int x2,int y2,char str[]);
int press(int,int,int,int);
int unpress(int,int,int,int);
int mouseini();
int showmp();
int hidemp();
int getmp(int *button,int *x,int *y);
int setmp();
int click(int x1,int y1,int x2,int y2,char str[]);
int screen();
void login();
void search();
void delete1();
void insert();
void update();
void display()
{
cout<<"

Name : "<<name;
cout<<"
Address : "<<address;
cout<<"
Phone Number: "<<phoneno;
}
int compare(char nam1[25])
{
if(strcmp(nam1,name)==0)
return 1;
else
return 0;
}
};
void telephone::login()
{
setcolor(15);
line (320-150,320-13,320+150,320-13);
line (320-150,320+12,320+150,320+12);
line (320-150,320-13,320-150,320+12);
line (320+150,320+12,320+150,320-13);
int s=30,w;
gotoxy(20,23);
cout<<"LOADING . . .";
for (int x1=171,x2=171,y1=308,y2=331,y=1,S=0;x1<470;x1++,x2++,y++,S++)
{
setcolor(1);
line (x1,y1,x2,y2);
w=(x1-169)/3;
for (int i=34; i<=78; i++)
{
gotoxy(i,23) ;
cout <<" " ;
}
gotoxy(34,23); cout<<w<<"%";
if (x2>270) s=45; if (x2>370) s=10;
if (x2==320) delay(999); else
delay(s);
}
delay(800);
for (int i=27; i<=78; i++)
{
gotoxy(i,23) ;
cout <<" " ;
}
}

void telephone::insert()
{
telephone tel1;
fstream file;
file.open("Phone.txt",ios::app);
tel1.init();
file.write((char *) &tel1, sizeof(tel1));
setcolor(7);
outtextxy(250,250,"Inserting Record.....");
file.close();
delay(3000);
}
void telephone::init()
{
cout<<"
Enter Name : "; cin.getline(name,25);
cout<<"
Enter Address : "; cin.getline(address,40);
cout<<"
Enter Phone Number: "; cin.getline(phoneno,15);
}

void telephone::delete1()
{
telephone tel1;
fstream file ;
char nam[25],nam1[25];
strcpy(nam1,"");
cout<<"Enter the Name to be deleted: ";
cin>>nam;
file.open("Phone.txt", ios::in) ;
fstream temp ;
temp.open("temp.txt", ios::out) ;
file.seekg(0,ios::beg) ;
while (file.read((char *) &tel1, sizeof(telephone)))
{
if(!tel1.compare(nam))
temp.write((char *) &tel1, sizeof(telephone)) ;
else
strcpy(nam1,nam);
}
file.close() ;
temp.close() ;
setcolor(7);
if(strlen(nam1)!=0)
{
file.open("Phone.txt", ios::out) ;
temp.open("temp.txt", ios::in) ;
temp.seekg(0,ios::beg) ;
while (temp.read((char *) &tel1, sizeof(telephone)))
{
file.write((char *) &tel1, sizeof(telephone)) ;
}
file.close() ;
temp.close() ;
outtextxy(250,250,"Deleting Record.....");
}
else
outtextxy(250,250,"Record not found");
delay(3000);
}

void telephone::update()
{
telephone tel1;
fstream file ;
char nam[25],nam1[25];
strcpy(nam1,"");
cout<<"Enter the Name to be updated: ";
cin>>nam;
file.open("Phone.txt", ios::in) ;
fstream temp ;
temp.open("temp.txt", ios::out) ;
file.seekg(0,ios::beg) ;
while (file.read((char *) &tel1, sizeof(telephone)))
{
if(!tel1.compare(nam))
temp.write((char *) &tel1, sizeof(telephone)) ;
else
strcpy(nam1,nam);
}
file.close() ;
temp.close() ;
setcolor(7);
if(strlen(nam1)!=0)
{
file.open("Phone.txt", ios::out) ;
temp.open("temp.txt", ios::in) ;
temp.seekg(0,ios::beg) ;
while (temp.read((char *) &tel1, sizeof(telephone)))
{
file.write((char *) &tel1, sizeof(telephone)) ;
}
file.close() ;
temp.close() ;
char nam[25],add[40],ph[15];
file.open("Phone.txt",ios::app);
cout<<"
Enter Name : ";
cin.getline(nam,25);cin.getline(nam,25);
cout<<"
Enter Address : "; cin.getline(add,40);
cout<<"
Enter Phone Number: "; cin.getline(ph,15);
tel1=telephone(nam,add,ph);
file.write((char *) &tel1, sizeof(tel1));
file.close();
outtextxy(250,250,"Updating Record.....");
}
else
outtextxy(250,250,"Record not found");
delay(3000);
}

void telephone::search()
{
fstream file;
telephone tel1;
int i=1;
char nam[25];
file.open("Phone.txt",ios::in);
cout<<"Enter name to be Searched: ";
cin>>nam;
file.seekg(0,ios::beg);
while(file.read((char *) &tel1, sizeof(telephone)))
{
if(tel1.compare(nam))
{
tel1.display();
i=0;
break ;
}
}
file.close() ;
if(i)
{
setcolor(7);
outtextxy(250,250,"Record not found");
}
getch();
}

void telephone:: button(int x1,int y1,int x2,int y2,char str[])


{
int xc,yc,i=0,l=0;
while(i<strlen(str))
{
l+=4;
i++;
}
xc=(x2-x1)/2+x1-l;
yc=(y2-y1)/2+y1;
unpress(x1,y1,x2,y2);
settextstyle(0,0,0);
setcolor(11);
outtextxy(xc,yc,str);
}
int telephone:: unpress(int x1,int y1,int x2,int y2)
{
setlinestyle(0,1,1);
setfillstyle(1,1);
bar(x1,y1,x2,y2);
setcolor(WHITE);
line(x1,y1,x2,y1);
line(x1,y1,x1,y2);
setcolor(0);
line(x1,y2,x2,y2);
line(x2,y1,x2,y2);
return 0;
}

int telephone:: press(int x1,int y1,int x2,int y2)


{
setlinestyle(0,1,1);
setfillstyle(1,1);
bar(x1,y1,x2,y2);
setcolor(0);
line(x1,y1,x2,y1);
line(x1,y1,x1,y2);
setcolor(WHITE);
line(x1,y2,x2,y2);
line(x2,y1,x2,y2);
return 0;
}

int telephone:: mouseini()


{
union REGS i,o;
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
int telephone:: showmp()
{
union REGS i,o;
i.x.ax=1;
int86(0x33,&i,&o);
return 0;
}

int telephone:: hidemp()


{
union REGS i,o;
i.x.ax=2;
int86(0x33,&i,&o);
return 0;
}
int telephone:: getmp(int *button,int *x,int *y)
{
union REGS i,o;
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
return 0;
}

int telephone:: setmp()


{
union REGS i,o;
i.x.ax=4;
int mx=getmaxx(),my=getmaxy();
i.x.cx=(3*mx/4)+20;
i.x.dx=(3*my/4)+20;
int86(0x33,&i,&o);
return 0;
}

int telephone:: click(int x1,int y1,int x2,int y2,char str[])


{
int button,x,y;
int xc,yc,i=0,l=0;
while(i<strlen(str))
{
l+=4;
i++;
}
xc=(x2-x1)/2+x1-l;
yc=(y2-y1)/2+y1;
getmp(&button,&x,&y);
if( (x>x1 && x<x2) && (y>y1 && y<y2) && button==1)
{
hidemp();
press(x1,y1,x2,y2);
setcolor(11);
settextstyle(0,0,0);
outtextxy(xc,yc,str);
showmp();
while((button==1))
getmp(&button,&x,&y);
hidemp();
unpress(x1,y1,x2,y2);
showmp();
setcolor(11);
settextstyle(0,0,0);
outtextxy(xc,yc,str);
for(i=50;i<500;i=i+50)
{
delay(10);
sound(i+200);
}
showmp();
nosound();
setcolor(11);
settextstyle(0,0,0);
outtextxy(xc,yc,str);
return 0;
}
else return 1;
}

int telephone:: screen()


{
settextstyle(0,1,6);
setcolor(11);
outtextxy(100,30,"TELEPHONE");
outtextxy(600,30,"DIRECTORY");
setmp();
button(250,100,400,150,"Insert");
button(250,150,400,200,"Delete");
button(250,200,400,250,"Update");
button(250,250,400,300,"Search");
button(250,300,400,350,"Exit");
while(1)
{
if(click(250,100,400,150,"Insert")==0)
{
cleardevice();
insert();
return 0;
}
if(click(250,150,400,200,"Delete")==0)
{
cleardevice();
delete1();
return 0;
}
if(click(250,200,400,250,"Update")==0)
{
cleardevice();
update();
return 0;
}
if(click(250,250,400,300,"Search")==0)
{
cleardevice();
search();
return 0;
}
if(click(250,300,400,350,"Exit")==0)
exit(0);
}
}

void main()
{
char user[25]="User Name",*pass,*pass1="user";
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
while(1)
{
cleardevice();
settextstyle(0,0,1);
outtextxy(250,250,"User Name:");
outtextxy(250,265,"Password :");
outtextxy(335,250,user);
pass=getpass("");
if(strcmp(pass,pass1)==0)
{
cleardevice();
telephone tel;
char op[8],cp[8],np[8];
tel.login();
while(1)
{
cleardevice();
tel.mouseini();
tel.showmp();
tel.screen();
}
}
else
{
cleardevice();
settextstyle(0,0,2);
outtextxy(250,250,"Illegal User....");
delay(3000);
}
}
}
Moving ball screensaver, in which two balls are bounchback
from the wall of the console.

*/
#include"graphics.h"
#include"dos.h"
#include"conio.h"
#include"stdlib.h"
#define DELAY 1
#define SOUND 3500
void state(int x,int y,int mode);
int i;
void *ptr1[4],*ptr2[4];
/* 3d Ball */
char ball[20][20]=
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,12,12,12,12,12,12,12,12,0,0,0,0,0,0},
{0,0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,12,12,12,12,12,15,15,12,12,12,12,12,12,12,12,12,0,0},
{0,12,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0},
{0,0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0,0},
{0,0,0,0,0,0,12,12,12,12,12,12,12,12,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
/* Condition to check out of range */
int COND(int x,int y)
{
if (x>10 && x<getmaxx()-10 && y>10 && y<getmaxy()-10)
return 1;
else
return 0;
}
/* This sub-routine will check which path to follow on stiking the wall
of
the container */
void state(int x,int y,int mode)
{
while(COND(x,y)&&!kbhit())
{
putimage(x,y,ptr1[4],COPY_PUT);
putimage(getmaxx()-x,getmaxy()-y,ptr1[4],COPY_PUT);
switch(mode)
{
case 0:
x++;
y++;
break;
case 1:
x++;
y--;
break;
case 2:
x--;
y++;
break;
case 3:
x--;
y--;
break;
}
delay(DELAY);
nosound();
putimage(x,y,ptr2[4],COPY_PUT);
putimage(getmaxx()-x,getmaxy()-y,ptr2[4],COPY_PUT);
}
cleardevice();
if(x>=(getmaxx()-10)||x<=10)
{
sound(SOUND);
switch(mode)
{
case 0:
state(--x,--y,2);
break;
case 1:
state(--x,++y,3);
break;
case 2:
state(++x,--y,0);
break;
case 3:
state(++x,++y,1);
break;
}
}
else
if(y>=getmaxy()-10||y<=10)
{
sound(SOUND);
switch(mode)
{
case 0:
state(--x,--y,1);
break;
case 1:
state(--x,++y,0);
break;
case 2:
state(++x,--y,3);
break;
case 3:
state(++x,++y,2);
break;
}
}
else
exit(0);
}
void main()
{
int gm,gd=DETECT;
int i,j;
initgraph(&gd,&gm,"\tc\tc\bgi");
for(i=0;i<20;i++)
for(j=0;j<20;j++)
if(ball[i][j]!='0')
putpixel(10+i,10+j,ball[j][i]);
getimage(10,10,30,30,ptr1[4]);
cleardevice();
getimage(10,10,30,30,ptr2[4]);
/* start with (20,20) */
state(20,20,0);
getch();
}

Snake Game

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<string.h>
class Snake
{
int p1,p2,v1,v2,v3,e1,e2,prev,now,n,colr,dsp,cnt,dly,m;
int stp,egGen;
int xr,yr;
void caught();
public:
long scr;
int strtX,strtY,endX,endY;
int pos[100][2];
void show();
void init();
void egg();
void transpose();
void gnrtCond();
void gnrtUnCond();
void check();
void checkEgg();
void move();
void chngDir();
void sndEt();
void sndCgt();
int test();
void score();
Snake();
Snake(Snake*);
~Snake();
};
Snake::Snake()
{
}
Snake::~Snake()
{

}
void Snake::checkEgg()
{
if((e1 == p1) && (e2 == p2))
{sndEt();
egg();
dly--;
score();
n++;
}
}
void Snake::sndEt()
{nosound();
sound(2500);
delay(2);
nosound();
}
void Snake::sndCgt()
{nosound();
for(int x=1000;x>0;x--)
{sound(x);
delay(1);
}
nosound();
}
void Snake::score()
{ char *p;

ltoa(scr,p,10);
settextstyle(8,0,1);
setcolor(0);
outtextxy(585,40,p);
if(egGen != 1){
scr = scr + dly / 10;
}
ltoa(scr,p,10);
setcolor(10);
outtextxy(585,40,p);

}
void Snake::gnrtCond()
{if(n < 367)
{if(now == 8 && (prev != 8 && prev != 2))
{pos[0][0] = p1;
pos[0][1] = p2 - dsp;
prev = now;
}
if(now == 4 && (prev != 4 && prev != 1))
{pos[0][0] = p1 + dsp;
pos[0][1] = p2;
prev = now;
}
if(now == 2 && (prev != 8 && prev != 2))
{pos[0][0] = p1;
pos[0][1] = p2 + dsp;
prev = now;
}
if(now == 1 && (prev != 1 && prev != 4))
{pos[0][0] = p1 - dsp;
pos[0][1] = p2;
prev = now;
}
}
}
void Snake::gnrtUnCond()
{
if( prev == 8 )
{pos[0][0] = p1;
pos[0][1] = p2 - dsp;
}
if( prev == 4 )
{pos[0][0] = p1 + dsp;
pos[0][1] = p2;
}
if( prev == 2 )
{pos[0][0] = p1;
pos[0][1] = p2 + dsp;
}
if( prev == 1 )
{pos[0][0] = p1 - dsp;
pos[0][1] = p2;
}
p1 = pos[0][0];
p2 = pos[0][1];
}
void Snake::check()
{
if(p1 > endX)
{p1 = strtX;}
else if(p1 < strtX)
{p1 = endX;}

if(p2 > endY)


{p2 = strtY;}
else if(p2 < strtY)
{p2 = endY;}

pos[0][0] = p1;
pos[0][1] = p2;
for(int i = 1;i < n;i++)
{ if(p1 == pos[i][0] && p2 == pos[i][1])
{caught();
break;
}
}
}
void Snake::show()
{
int x = getcolor();
if(egGen != 1)
{
setcolor(getbkcolor());
setfillstyle(1,getbkcolor());
fillellipse(v1,v2,yr,yr);
}
else
egGen = 0;
if(egGen == 2)
egGen--;
setcolor(colr);
setfillstyle(1,9);
if(now == 8 || now == 2)
fillellipse(pos[0][0],pos[0][1],xr,yr);
else if(now == 4 || now == 1)
fillellipse(pos[0][0],pos[0][1],yr,xr);
setcolor(x);
}
void Snake::transpose()
{ int i,j,x,y;
p1 = pos[0][0];
p2 = pos[0][1];
if(!egGen){
v1 = pos[n-1][0];
v2 = pos[n-1][1];
}
else
egGen = 0;
for(i = n-1;i >= 1;i--)
{pos[i][0] = pos[i-1][0];
pos[i][1] = pos[i-1][1];
}
}
void Snake::move()
{ int st = 0;
do{
if(!kbhit())
{checkEgg();
if(!st)
show();
else
st = 0;
delay(dly/4);
transpose();
delay(dly/4);
gnrtUnCond();
delay(dly/4);
check();
delay(dly/4);
}
else if(stp){
chngDir();
gnrtCond();
check();
show();
st = 1;
}
}while(stp);
}
void Snake::init()
{time_t tm;
srand(time(&tm));
dsp = 20;
n = 5;
prev = 4;
for(int i = 4;i >= 0;i--)
{pos[i][0] = 201 + (n - i - 1) * dsp;
pos[i][1] = 301;
}
strtX = 21;
strtY = 21;
endX = 481;
endY = 361;
colr = 14;
now = prev;
dsp = 20;
stp = 111;
cnt = -1;
scr = 0;
dly = 150;
xr = 3;
yr = 9;
egg();
egGen = 1;
score();
int x = getcolor();
setlinestyle(0,1,3);
setcolor(15);
rectangle(strtX-15,strtY-15,endX+15,endY+15);
rectangle(endX+25,strtY-15,getmaxx()-15,endY+15);
rectangle(strtX-15,endY+25,getmaxx()-15,getmaxy()-5);
line(endX+25,strtY+75,getmaxx()-15,strtY+75);
line(endX+25,strtY+200,getmaxx()-15,strtY+200);
line(endX+25,strtY+275,getmaxx()-15,strtY+275);
setlinestyle(0,1,1);
settextstyle(8,0,1);
setcolor(11);
outtextxy(514,40,"SCORE");
setcolor(14);
settextstyle(11,0,5);
outtextxy(524,110," CONTROLS ");
outtextxy(522,135,"p = PAUSE");
outtextxy(522,155,"g = RESUME");
outtextxy(522,175,"e = EXIT");
outtextxy(513,195,"ARROWS");
outtextxy(512,205," -MOVEMENT");
setcolor(14);
settextstyle(4,0,9);
outtextxy(getmaxx()-500,getmaxy()-110,"SNAKE");
settextstyle(8,0,1);
setcolor(x);
}
void Snake::caught()
{
stp = 0;
sndCgt();
for(int i=0;i<=7;i++)
{if(i%2)
{setcolor(10);
outtextxy(512,250,"GAME OVER");
delay(900);
}
else
{setcolor(0);
outtextxy(512,250,"GAME OVER");
delay(500);
}
}
sleep(1);
}
void Snake::chngDir()
{int clr;
fillsettingstype *p;
char x = getch();
if(x == 72)
now = 8;
else if(x == 77)
now = 4;
else if(x == 80)
now = 2;
else if(x == 75)
now = 1;
else if(x == 'e')
caught();
else if(x == 'p')
{//int y = getcolor();
int twnkl = 1;
settextstyle(11,0,9);
while(1)
{if(kbhit())
{int c = getch();
if(c == 'g')
{clr = getcolor();
setcolor(0);
rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
outtextxy(endX+60,endY-29,"PAUSE");
break;
}
}
else
{if(twnkl%2)
{clr = getcolor();
setcolor(10);
rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
outtextxy(endX+60,endY-29,"PAUSE");
setcolor(clr);
delay(1000);
}
else
{
clr = getcolor();
setcolor(0);
rectangle(endX+40,endY-10,getmaxx()-35,getmaxy()-160);
outtextxy(endX+60,endY-29,"PAUSE");
delay(1000);
}
}
twnkl++;
}
settextstyle(8,0,1);
}
}
Snake::Snake(Snake *p)
{
*p=NULL;
}
void Snake::egg()
{ do
{e1 = (rand() % 100) * dsp + strtX;
e2 = (rand() % 100) * dsp + strtY;
}while(test());
int x = getcolor();
setcolor(7);
setfillstyle(1,random(15)+1);
fillellipse(e1,e2,xr+2,xr+2);
setcolor(x);
egGen = 2;
}
int Snake::test()
{ for(int i=0;i<n;i++)
{if(e1 == pos[i][0] && e2 == pos[i][1])
break;
if(v1 == e1 && v2 == e2)
break;
if((e1 >= endX+1) || (e2 >= endY+1))
break;
}
if(i != n)
return 1;
else
return 0;
}
void main()
{Snake snk;
int gd=DETECT,gm,i,j,k,x,y;
clrscr();
initgraph(&gd,&gm,"E:\Turboc3");
snk.init();
snk.move();

closegraph();
restorecrtmode();
}

You might also like