Showing posts with label Advance Programming In C Programming language. Show all posts
Showing posts with label Advance Programming In C Programming language. Show all posts

Wednesday, September 24, 2008

program of baal game catch circles coming down with use left right arrow key

program of baal game catch circles coming down with use left right arrow key


Program
#include
#include
#include
#include
#include
#include
int a,b,ch;
int c=2,d=3;
static char msg[1];
static int chance,pts,x1,x2,y1,y2;
over()
{
int i=0,j,x=185,y=240;
while(i<1000) { j=1+rand()%15; setcolor(j); settextstyle(0,0,4); outtextxy(x,y,"GAME OVER"); delay(100); i++; } exit(1); return; } move(int x1,int x2,int y1,int y2,int k) { int i=0; i=i+10; if(k==1) //left {i=i; bar1(x2-i,x2,y1,y2,2); } else { i=-i; bar1(x1,x1-i,y1,y2,3); } bar1(x1-i,x2-i,y1,y2,1); return; } bar1(int x1,int x2,int y1,int y2,int k) { if(k==1) {c=2; d=3;} else {c=d=0;} setfillstyle(2,d); bar(x1,479-y1,x2,479-y2); return; } keyin() { int i; ch=getch(); switch(ch) { case 75: if(x1>24)
{
i=10; //left
move(x1,x2,y1,y2,1);
x2=x2-i;
x1=x1-i;
}
break;
case 77: //right
if(x2<614) { i=10; move(x1,x2,y1,y2,2); x1=x1+i; x2=x2+i; } break; }//end switch if(ch==27) over(); return; } ball() { int a,b,t=0; a=639-rand()%639; b=350; while(b>100)
{
b=b-1;
setcolor(RED);
if((a-5)<=x2&&(a+5)>=x1&&(479-(b-5)>=(479-y1))&&(479-(b+5)<=(479-y2)))
{score();
t=1;
break;}
if(kbhit())
keyin();
circle(a,479-b,5);
delay(10);
setcolor(BLACK);
circle(a,479-b,5);
}
if(t==0)
chance++;
if(chance==2)
over();
return;
}
score()
{
setcolor(0);
settextstyle(0,0,5);
sprintf(msg,"%d",pts);
outtextxy(110,20,msg);
setcolor(4);
sound(1100);
delay(50);
nosound();
pts++;
settextstyle(0,0,5);
sprintf(msg,"%d",pts);
outtextxy(110,20,msg);
return;
}
play()
{
while(ch!=27)
ball();
return;
}

program of 2 player game in c language

2 player game









Program
#include
#include
#include
#include
#include
static float a,b,h1,h2,x1=20,x2=25,y1=250,y2=200,x3=20,x4=25,y3=190,y4=140;
static int ch=3,ch1=3,q=1,t,s=1,s1,s2,c=0,c1=0,i=0,k=1,t1,t2,p1=0,p2=0;
gameover()
{
int j=0;
while(j<100) p="="1)" t="getpixel(x1+2,479-(y1+y2)/2);" t1="getpixel(a-6,479-b);">(479-(y1+y2)/2)&&(t1==2))
c=1; //down
else if((479-b)<(479-(y1+y2)/2)&&(t1==2)) c=2; //up return c; } else if(p==2) { t=getpixel(639-(x3+2),479-(y3+y4)/2); t2=getpixel(a+6,479-b); if((479-b)>(479-(y3+y4)/2)&&(t2==2))
{c1=1; //down
//printf("%2.f %2.f",479-(y3+y4)/2,479-b);
//printf("yes ");
}
else if((479-b)<(479-(y3+y4)/2)&&(t2==2)) { //printf("%2.f %2.f",479-(y3+y4)/2,479-b); c1=2; //up //printf("no "); } return c1; } return 0; } ball() { ch1=getch(); while(ch1!=13) { move(); ch1=getch(); if(ch1==27) break; } //ch1=getch(); if(ch1==13) { q=2; sound(1100); delay(50); nosound(); while(ch1!=27) { setfillstyle(1,5); setcolor(5); circle(a,479-b,5); floodfill(a,479-b,5); c=check(s1); c1=check(s2); delay(5); setfillstyle(1,0); setcolor(0); circle(a,479-b,5); floodfill(a,479-b,0); if(kbhit()) move(); if(k==1) { t=getpixel(639-(x3+2),479-(y3+y4)/2); t2=getpixel(a+7,479-b); if(t==t2) { sound(1100); delay(50); nosound(); h2=a; c=0;c1=0;//printf("h2=%2.f,yes",h2); k=2; } a++; } else if(k==2) { t=getpixel(x1,479-(y1+y2)/2); t1=getpixel(a-7,479-(b)); if(t==t1) { sound(1100); delay(50); nosound(); h1=a; c=0;c1=0; //printf("yes %2.f =h1",h1); k=1; } a--; } if((479-b)==(479-50)) { //printf("s=%d",s); if(c==1) c=2; else if(c1==1) c1=2; } if(a==639||(a+25)==639) { ch1=3; p1++; printf("%d %d",p1,p2); s=1; q=1; c=0; c1=0; start(1,5); } if(a==0||(a-25)==0) { p2++; s=2; c=0;c1=0; q=1; ch1=3; printf("%d %d",p1,p2); start(2,5); } if((479-b)==(479-429)) { //printf("s=%d",s); if(c1==2) c1=1; else if(c==2) c=1; } if(c==1) //down frm 1st { a++; b--; } else if(c==2) //up frm 1st { a++; b++; } else if(c1==1) { a--; b--; } else if(c1==2) { a--; b++; } if(p1==5||p2==5) gameover(); }//end while } //end ch if return; } move() { fflush(stdin); ch1=getch(); if(ch1==119||ch1==115) move1(); else move2(); return; } move1() { //ch1=getch(); switch(ch1) { case 119://up if(y1<=420) { if(q==1&&s==1) { star(1,0); //delay(1); } setfillstyle(1,2); bar(x1,479-(y1+10),x2,479-(y2+10)); //delay(1); setfillstyle(1,1); bar(x1,479-(y2),x2,479-(y2+10)); y1=y1+10; y2=y2+10; setfillstyle(1,2); if(q==1&&s==1) star(1,5); } break; case 115://down if(y2>=50)
{
if(q==1&&s==1)
{
star(1,0);
//delay(1);
}
setfillstyle(1,2);
bar(x1,479-(y1-10),x2,479-(y2-10));
//delay(1);
setfillstyle(1,1);
bar(x1,479-(y1),x2,479-(y1-10));
y1=y1-10;
y2=y2-10;
setfillstyle(1,2);
if(q==1&&s==1)
star(1,5);
}
break;
}
return;
}
move2()
{
switch(ch1)
{
case 112://up
if(y3<=420) { if(q==1&&s==2) star(2,0); setfillstyle(1,2); bar(639-x3,479-(y3+10),639-x4,479-(y4+10)); setfillstyle(1,1); bar(639-x3,479-(y4),639-x4,479-(y4+10)); y3=y3+10; y4=y4+10; setfillstyle(1,2); if(q==1&&s==2) star(2,5); } break; case 108://down if(y4>=(50))
{
if(q==1&&s==2)
star(2,0);
setfillstyle(1,2);
bar(639-x3,479-(y3-10),639-x4,479-(y4-10));
setfillstyle(1,1);
bar(639-x3,479-(y3),639-x4,479-(y3-10));
y3=y3-10;
y4=y4-10;
setfillstyle(1,2);
if(q==1&&s==2)
star(2,5);
}
break;
}
return;
}
star(int o,int c)
{
s=o;
if(s==1)
{
a=x2+7;
b=(y2+y1)/2;
i=608;
k=1;
s1=1;
s2=2;
setfillstyle(1,c);
setcolor(c);
circle(a,479-b,5);
floodfill(a,479-b,c);
}
if(s==2)
{
a=620-(x3-7);
b=(y3+y4)/2;
i=608;
k=2;
s1=1;
s2=2;
setfillstyle(1,c);
setcolor(c);
circle(a,479-b,5);
floodfill(a,479-b,c);
}
return;
}
start(int o,int c)
{
s=o;
if(s==1)
{
a=x2+7;
b=(y2+y1)/2;
i=608;
k=1;
s1=1;
s2=2;
setfillstyle(1,c);
setcolor(c);
circle(a,479-b,5);
floodfill(a,479-b,c);
}
if(s==2)
{
a=620-(x3-7);
b=(y3+y4)/2;
i=608;
k=2;
s1=1;
s2=2;
setfillstyle(1,c);
setcolor(c);
circle(a,479-b,5);
floodfill(a,479-b,c);
}
ball();
return;
}
play()
{
setfillstyle(1,2); //solid color
bar(x1,479-y1,x2,479-y2);
bar(639-x3,479-y3,639-x4,479-y4);
line(0,479-435,639,479-435);
line(0,440,639,440);
start(s,5);
return;
}
main()
{
int k,a=DETECT,b,ch;
initgraph(&a,&b,"D:\\TC\\BGI");
play();
getch();
return 0;
}

Program a menu driven program showing several operations on string.



Program
#include
#include
#include
void stringlen(char s1);
void stringcopy(char *s1);
void stringcon(char *s1);
void stringrev(char *s1);
void stringcompare(char *s1);
void wordcount(char *s1);
void lettercount(char *s1);
void main()
{
char s[40],x;
clrscr();
printf("\n Enter a string");
gets(s1);
printf("\n Enter 'a' for string length");
printf("\n Enter 'b' for string copy");
printf("\n Enter 'c' for string concatanation");
printf("\n Enter 'd' for string rev");
printf("\n Enter 'e' for string compare");
printf("\n Enter 'f' for word count within a string");
printf("\n Enter 'g' for letter count within a string\n");
scanf("%2c",&x);
switch(x)
{
case 'a':
stringlen(s1);
break;
case 'b':
stringcopy(s1);
break;
case 'c':
stringcon(s1);
break;
case 'd':
stringrev(s1);
break;
case 'e':
stringcompare(s1);
break;
case 'f':
wordcount(s1);
break;
case 'g':
lettercount(s1);
break;
}
}
void stringlen(char *s1)
{
int n=0;i=0;
while(s[i]!=NULL)
{
n++;
i++;
}
printf("length of the string is:-\n%d",n);
getch();
}
void stringcopy(char *s1)
{
int i;
char s2[40];
for(i=0;s1[i]=!NULL;i++)
{
s2=s1[i];
}
s2[i]=NULL;
printf("NEW STRING IS;-\n%s",s2);
getch();
}
void stringconcatanation(char *s1)
{
int i,j;
char str[40];
printf("ENTER ANOTHER STRING:-\n");
fflush(stdin);
gets(str);
for(i=0;s1[i]!=NULL;i++);
for(j=0;s1[j]!=NULL;j++)
{
s1[i]=str[j];
i++;
s1[i]=NULL;
}
printf("NEW STRING IS;-\n%s",s1);
getch();
}
void stringrev(char *s1)
{
int i,c=0,l;
char s2[100];
l=strlen(s1);
for(i=l-1;i>=0;i--)
s2[c++]=s1[i];
printf("NEW STRING IS \n");
for(i=0;i
printf("%c",s2[i]);
getch();
}
void stringcompare(char *s1)
{
int i,flag=1;
char s2[40];
printf("ENTER ANOTHER STRING");
fflush(stdin);
gets(*s2);
for(i=0;s2[i]!=NULL;i+)
{
if(s1[i]!=s2[i])
{
printf("\n not equal");
flag=0;break;
}
}
if(flag==1)
printf("equal");
getch();
}

program to check how many 1 there on binary code of a number

prg to chk how many 1 there on binary code of a number.......(popular q in interviews)




Program
#include

int main()
{
int i=10;
int count=0;


while(i)
{
if(i &1==1)
{
count++;
}
i>>=1;
}
printf("%d",count);
return 0;
}

program converts an infix string to postfix expression

program converts an infix string to postfix expression....i have kept no place for input..please change the char a[] in void main() and it will act as the input.....if any new symbols are to be added please add them to op[] and also its corresponding order of precedence in prec[]




















#include
#include
#include
#include
int top=0,opn=5;
char op[]={'^','*','/','+','-'};
int prec[]={1,2,2,3,3};
char pop(char s[]);
int isoperand(char);
void push(char s[],char);
int prcdnc(char,char);

void main()
{

char a[]="a+(b*c-(d/e^f)*g)*h",stk[50],c[50],temp,symb;
int l,i=0,r=0;
l=strlen(a);
a[l]=')';
a[l+1]=NULL;
stk[0]='(';
while(a[r]!=NULL)
{
symb=a[r];
if(isoperand(symb))
{
c[i]=symb;
i++;
}
else if(symb==')')
{
while(1)
{
temp=pop(stk);
if(temp=='(')
break;
else
{
c[i]=temp;
i++;
}
}
}
else if(symb=='(')
push(stk,symb);
else
{
while(1)
{
if(stk[top]=='(')
{
push(stk,symb);
break;
}
else if(prcdnc(stk[top],symb))
{
c[i]=pop(stk);
i++;
}
else
{ push(stk,symb);
break;
}
}
}
r++;
}
c[i]=NULL;
printf("\nThe post fix expression is= %s",c);
getch();
}

int prcdnc(char a,char b)
{
int num1,num2,i;
for(i=0;i
{
if(op[i]==a)

num1=prec[i];

if(op[i]==b)
num2=prec[i];
}
if(num2<=num1) return 0; else return 1; } void push(char s[],char a) { top=top+1; s[top]=a; } char pop(char s[]) { char val; val= s[top]; top=top-1; return val; } int isoperand(char a) { int i; if(a=='(' || a==')') return 0; for(i=0;i
{
if(op[i]==a)
return 0;
}
return 1;
}

To find saddle point in a triangle with c programming

Program
#include
#include
void main()
{
int a[5][5],i,j,min,max,m,n,flag=1,p,q;
printf("Enter size of the matrix");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of the matrix");
for(i=0;ia[i][j])
min=a[i][j];
p=i;
q=j;
}
}
for(j=0;ja[p][q])
flag=0;
}
if(flag)
printf("Saddle point %d",a[p][q]);
else
printf("no saddle point");
flag=1;
getch();
}

how to use signature of printf in c programming language

how to use signature of printf... according to our needs.....




Program
#include
main()
{
char p[]={"gaurav kapoor"};
int *name;
name=&p;
printf(name);
return 0;}

Make a Binary tree from Infix expression

Program
Make a Binary tree from Infix expression.


Infix :-> 4 $ 2 * 3 - 3 + 8 / 4 / (1 + 1)


Solution: First separate the operator and operand
___________________________________________________________


Operator:-> $ * - + / / (+)


Operand:-> 4 2 3 3 8 4 1 1
___________________________________________________________


Now separate the operator according to their priority


First priority :-> ( + ) // Because ( ) has greater priority


Second priority :-> $


Third priority :-> * / /


Fourth priority :-> - +
_____________________________________________________
Step 1:
_____________________________________________________


Root will be fixed according to lowest priority


So, In Foruth priority - +


Here + will be the first root of tree (right to left)


+
_________________________________________________
Step 2:
_________________________________________________




Now - will be the left child of root of this tree


Because - is existing in left side of +.


+
/
/
-


_________________________________________________
Step 3:
_________________________________________________


Now in Third priority :-> * / /


Here / will be right child of +.


Because / is existing in right side of +.
+
/ \
/ \
- /


_________________________________________________
Step 4:
_________________________________________________


Here / will be right child of /.


Because / is existing in left side of /.
+
/ \
/ \
- (/) Devide sign (no 1)
/
/
(/) devide sign (no 2)


_________________________________________________
Step 5:
_________________________________________________
Here * will be left child of -.


Because, In infix expression, * is existing in left side of -.


+
/ \
/ \
- (/) Devide sign (no 1)
/ /
/ /
* (/) devide sign (no 2)


_________________________________________________
Step 6:
_________________________________________________


In Second priority:-> $


Here $ will be left child of *.


Because, In infix expression,


$ is existing in left side of *.


+
/ \
/ \
- (/) Devide sign (no 1)
/ /
/ /
* (/) devide sign (no 2)
/
/
$
_________________________________________________
Step 7:
_________________________________________________


In First priority:-> (+)


Here (+) will be left child of / (devide sign no 1).


Because, In infix expression,


$ is existing in left side of *.


+
/ \
/ \
- (/)
/ / \
/ / \
* (/) +
/
/
$


_________________________________________________
Step 8:
_________________________________________________


Now As we know the expression is


Infix :-> 4 $ 2 * 3 - 3 + 8 / 4 / (1 + 1)


Operand:-> 4 2 3 3 8 4 1 1


Now put the operand only


_____________________________________________________


First of all take 4, Here 4 will be left child of $.


Because 4 is left side of $ in expression


+
/ \
/ \
- (/)
/ / \
/ / \
* (/) +
/
/
$
/
/
4
_____________________________________________________


Now take 2 , Here 2 will be right child of $.


Because 2 is right side of $ in expression.


+
/ \
/ \
- (/)
/ / \
/ / \
* (/) +
/
/
$
/ \
/ \
4 2
_____________________________________________________


Now take 3 , Here 3 will be right child of *.


Because 3 is right side of * in expression.


+
/ \
/ \
- (/)
/ / \
/ / \
* (/) +
/ \
/ \
$ 3
/ \
/ \
4 2


_____________________________________________________


Now take 8, Here 8 will be left child of /(devide sign no 2).


Because 8 is left side of / in expression. Here i am not saying that 8 will be right child of +. Because / is the right child of +. And operand won't be root in Tree. So i am taking Here 8 will be left child of /(devide sign no 2).


+
/ \
/ \
- (/)
/ \ / \
/ \ / \
* 3 (/) +
/ \ /
/ \ /
$ 3 8
/ \
/ \
4 2


________________________________________________________


Now take 4, Here 4 will be right child of /(devide sign no 2).


Because 4 is right side of / in the given expression.


+(first +)
/ \
/ \
- (/)
/ \ / \
/ \ / \
* 3 (/) + (second +)
/ \ / \
/ \ / \
$ 3 8 4
/ \
/ \
4 2


________________________________________________________


Now take 1, Here 1 will be left child of + (second +).


Because 1 is right side of /(devide sign no 2) in the given expression.


+ (first +)
/ \
/ \
- (/)
/ \ / \
/ \ / \
* 3 (/) + (second +)
/ \ / \ /
/ \ / \ /
$ 3 8 4 1
/ \
/ \
4 2




________________________________________________________


Now take 1, Here 1 will be right child of + (second +).


Because 1 is right side of + (second +) in the given expression.


+
/ \
/ \
- (/)
/ \ / \
/ \ / \
* 3 (/) +
/ \ / \ / \
/ \ / \ / \
$ 3 8 4 1 1
/ \
/ \
4 2


-------------------------------------------
The final Tree for an expression |
|
Infix :-> 4 $ 2 * 3 - 3 + 8 / 4 / (1 + 1) |
|
-------------------------------------------
|
+ |
/ \ |
/ \ |
- (/) |
/ \ / \ |
/ \ / \ |
* 3 (/) + |
/ \ / \ / \ |
/ \ / \ / \ |
$ 3 8 4 1 1 |
/ \ |
/ \ |
4 2 |
|
-------------------------------------------

easy and basic calculator in c programming language

easy and basic calculator.






just add your own detail.
Program
#include
#include
#define P printf
#define S scanf


int main(){

float x,y,sum,diff,prod,qou;


P("Enter first integer: ");
S("%f",&x);

P("Enter second integer: ");
S("%f",&y);

sum=x+y;
P("\nThe Sum is:%.2f",sum);

diff=x-y;
P("\nThe Difference is:%.2f",diff);

prod=x*y;
P("\nThe Product is:%.2f",prod);

qou=x/y;
P("\nThe Qoutient is:%.2f",qou);


getch();
return 0;

}

CLOCK program in c programming language

Description:
to find out the clock
Program
#include

#include
#include
#include

#include "i2c.h"
#include "lcd.h"

idata char g_strSetting[20] ;

unsigned char day, mon, year ;
unsigned char hh,mm,ss ;
unsigned char g_byLastSec ;

unsigned int iday, imon, iyear ;
unsigned int ihh,imm,iss ;

void main ()
{
/*
// initialize serial port
// 19200 baud assuming P89C51RD2 @ 12 MHz
RCAP2H = 0xff ;
RCAP2L = 0xd9 ;
T2CON = 0x34 ;
SCON = 0x70 ;
*/
TH1 = 0xfd ;
TL1 = 0xfd ;
PCON |= 0x80 ;
SCON = 0x70 ;
TMOD = 0x21 ;
TCON = 0x50 ;


lcd_init() ;
printf("Clock program for Mini51\n") ;
display_row(0,"CLOCK on Mini51") ;
if (RI)
{
// send CR during reset, to goto clock set mode
getchare() ;
while(1)
{
printf("\nEnter data and time in dd mm yy hh mm ss format\n") ;
scanf("%x%x%x%x%x%x",&iday,&imon,&iyear,&ihh,&imm,&iss) ;
day = iday ;
mon = imon ;
year = iyear ;
hh = ihh ;
mm = imm ;
ss = iss ;
printf("You entered:\n%02bx-%02bx-%02bx %02bx:%02bx:%02bx\n",day,mon,year,hh,mm,ss) ;
printf("Press Y to store these values to RTC, any other to discard\n") ;
if (getchare() == 'Y')
{
write_rtc_byte(DAY_ADDR,day) ;
write_rtc_byte(MONTH_ADDR,mon) ;
write_rtc_byte(YEAR_ADDR,year) ;
write_rtc_byte(HOURS_ADDR,hh) ;
write_rtc_byte(MINUTES_ADDR,mm) ;
write_rtc_byte(SECONDS_ADDR,ss) ;
break ;
}
}
}
g_byLastSec = 0xff ;
while(1)
{
day = read_rtc_byte(DAY_ADDR) ;
mon = read_rtc_byte(MONTH_ADDR) ;
year = read_rtc_byte(YEAR_ADDR) ;
hh = read_rtc_byte(HOURS_ADDR) ;
mm = read_rtc_byte(MINUTES_ADDR) ;
ss = read_rtc_byte(SECONDS_ADDR) ;
if (ss != g_byLastSec)
{
sprintf(g_strSetting,"%02bx-%02bx %02bx:%02bx:%02bx",day,mon,hh,mm,ss) ;
display_row(1,g_strSetting) ;
//printf("%s\n",g_strSetting) ;
printf("%02bx-%02bx-%02bx %02bx:%02bx:%02bx\n",day,mon,year,hh,mm,ss) ;
g_byLastSec = ss ;
}
}
}

My Blog List