Wednesday, December 30, 2009

PUZZLE USING C

#include"dos.h"
#include"math.h"
#include"stdio.h''
#include"conio.h"
#include"stdlib.h"
#include"graphics.h"

#define N 500

int function[35];
int strobe[5] = { 0,0,0,0,0 };
int store[16] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
int inputs[16];
int output=0,count=0,decieq,choice;

int driver=DETECT,mode;

/***********FUNCTIONS FOR BEGINING GUI***************/

struct stars
{

int x,y,x1,y1,z,c;

}STARS[N];

void init()
{

int i,t;

for(i=0;i
{

STARS[i].c=2;
STARS[i].x = random(640);
STARS[i].y = random(460);
STARS[i].x1= random(320);
STARS[i].y1= random(220);

t=random(325);
if(t==0)
STARS[i].z=t+random(25)+1;//TO AVOID ERROR(division by 0)

else
STARS[i].z=t;

}

}

void draw()
{

int i;

for(i=0;i
{

if(STARS[i].y<480)
putpixel(STARS[i].x,STARS[i].y,STARS[i].c);
}

}

void erase()
{

int i;

for(i=0;i
putpixel(STARS[i].x,STARS[i].y,0); //putting black
//color at stars position

}
/* if you use z+=2 stars will go away from you , but remember to
change the condition :-
if(STARS[i].z<1) z=""> to if(STARS[i].z>325) {STARS[i].z=1;} */
void move()
{

int i;
for(i=0;i
{

STARS[i].x=((STARS[i].x1 * 640)/STARS[i].z)+320;//320 x-origin
STARS[i].y=((STARS[i].y1 * 300)/STARS[i].z) +220;//220 y-origin
STARS[i].z-=2;

if(STARS[i].z<1)
STARS[i].z=325;

else if(STARS[i].z<162)
STARS[i].c=15;

else STARS[i].c=7;

}

}

void showstars()
{

int i;

init();
while(!kbhit())
{

setcolor(3); //3 for cyan
settextstyle(1,0,3);
outtextxy(230,5,"PROJECT ON");
settextstyle(10,0,3);
setcolor(i);
outtextxy(170,30,"MULTIPLEXERS");

setcolor(5);
settextstyle(1,0,3);
outtextxy(350,320,"Made by:-");
setcolor(i);
outtextxy(420,345,"Himanshu Sardana");
move();
draw();
delay(15);
erase();
i++;

}

closegraph();
restorecrtmode();

}

void welcome()
{

int i, j, x, y, color;
int MaxColors;
struct palettetype palette;
struct viewporttype vp;
int height, width;

initgraph(&driver,&mode,"f: cgi");

MaxColors = getmaxcolor() + 1;

textcolor(4+BLINK);
printf(" WELCOME");
textcolor(15);

getpalette( &palette );
setviewport(0,50,630,470,1);
getviewsettings( &vp );

width = (vp.right - vp.left) / 15; //width
height = (vp.bottom - vp.top) / 10; // height

x = y = 0; // Start in upper corner
color = 1; // Begin at 1st color

for( j=0 ; j<10>
{ // 10 rows of boxes

for( i=0 ; i<15>
{ // 15 columns

setfillstyle( SOLID_FILL, color++ );//Set the color
bar( x, y, x+width, y+height ); // Draw the box
x += width + 1; // Advance to next col
color = 1 + (color % (MaxColors - 2));// Set new color
} // End of i loop

x = 0; // Goto 1st column
y += height + 1; // Goto next row

} // End of j loop

while( !kbhit() ) // Until a key is hit
setpalette( 1+random(MaxColors - 2), random( 65 ) );


setallpalette( &palette );
getch();
closegraph();
restorecrtmode();
initgraph(&driver,&mode,"f: cgi");
showstars();

}

/*********Begining GUI ends.Text mode begins*********/

/**************MULTIPLEXER FUNCTIONS*****************/

void message(int num) //gives a message to user
{ //everytime select input is entered

printf("Only first %d select inputs considered.rest rejected(if
any)",num);

}

int check(int num) //For checking that a particular no.
{ //is present in function or not

int ctr2;

for(ctr2=0;ctr2
if(function[ctr2]==num)
return (1); //true

return(0); //false

}

void check_invalid_strobe(int ctr2) //for checking invalid select
input
{

if(strobe[ctr2]>1 || strobe[ctr2]<0)
{

printf("Invalid select input");
exit(2);

}

}

void decide_output(int count) //decides whether output is 1 or 0
{ //for normal mux implementation

int ctr2;

for(ctr2=0;ctr2
{

if(decieq==function[ctr2])
{

output = 1;
break;

}

}

}

void decide_complex_inputs(int num) //decides inputs of a Mux which
{ //implements a function of a
//higher
//level Mux
int ctr2;

for(ctr2=0;ctr2
{

if(check(ctr2))
{

if(check(ctr2+num))
inputs[ctr2]=1;

else
{

inputs[ctr2]=!(strobe[0]);
store[ctr2] = 0;

}

}

else
{

if(check(ctr2+num))
{

inputs[ctr2]=strobe[0];
store[ctr2] = 1;

}

else
inputs[ctr2]=0;

}

}

}

void display_complex_input(int num) //displays inputs of a Mux which
{ //implements a function of ahigher
//level Mux
int i;

for(i=0;i
{

if(store[i]==0)
printf("Input %d = %s(%d)",i,"a bar",inputs[i]);

else if(store[i]==1)
printf("Input %d = %c(%d)",i,'a',inputs[i]);

else
printf("Input %d = %d",i,inputs[i]);

}

}

void take_input(int num) //for taking select input in normal Mux
{

int ctr2;

printf("Enter the select input: ");
for(ctr2=0;ctr2
{

scanf("%d",&strobe[ctr2]);
check_invalid_strobe(ctr2);

}

}

void take_input_a(int num) //for taking input a in case of complex
{ //implementation of a function

printf("Enter the value of a(MSB of select input for %d:1) : ",num);
scanf("%d",&strobe[0]);
check_invalid_strobe(0);
printf("Only one value of MSB considered.rest rejected(if any)");

}

void take_complex_select_input(int num)
{

int ctr2;

printf("Enter the select input: ");
for(ctr2=1;ctr2
{

scanf("%d",&strobe[ctr2]);
check_invalid_strobe(ctr2);

}
printf("Only first %d select inputs considered.rest
rejected(ifany)",num-1);
printf("Press any key to continue....");
getch();

}

void display_output()
{

printf("Output is : %d",output);
printf("Input %d is selected as output.",decieq);
printf("Press any key to continue.....");
getch();

}

void display_complex_output()
{

output = inputs[decieq];
printf("Output is: %d",output);
printf("Input %d is selected as output.",decieq);
printf("Press any key to continue....");
getch();

}

void two_is_to_one()
{

take_input(1);
message(1);

decieq = strobe[0]*1;

decide_output(count);
display_output();

}

void four_is_to_one()
{

int ctr2;

take_input(2);
message(2);

decieq = strobe[0]*2 + strobe[1]*1;

decide_output(count);
display_output();

}

void eight_is_to_one()
{

int ctr2;

take_input(3);
message(3);

decieq = strobe[0]*4 + strobe[1]*2 + strobe[2]*1;

decide_output(count);
display_output();

}

void sixteen_is_to_one()
{

int ctr2;

take_input(4);
message(4);

decieq = strobe[0]*8 + strobe[1]*4 + strobe[2]*2 + strobe[3]*1;

decide_output(count);
display_output();

}

void thirtytwo_is_to_one()
{

int ctr2;

take_input(5);
message(5);

decieq = strobe[0]*16 + strobe[1]*8 + strobe[2]*4 + strobe[3]*2 +
strobe[4]*1;

decide_output(count);
display_output();

}

void complex_two_is_to_one()
{

int ctr2;

take_input_a(4);
take_complex_select_input(2);
message(1);
decide_complex_inputs(2);

decieq = strobe[1]*1;

clrscr();
display_complex_input(2);
display_complex_output();

}

void complex_four_is_to_one()
{

int ctr2;

take_input_a(8);
take_complex_select_input(3);
message(2);
decide_complex_inputs(4);

decieq = strobe[1]*2 + strobe[2]*1;

clrscr();
display_complex_input(4);
display_complex_output();

}

void complex_eight_is_to_one()
{

int ctr2;

take_input_a(16);
take_complex_select_input(4);
message(3);
decide_complex_inputs(8);

decieq = strobe[1]*4 + strobe[2]*2 + strobe[3]*1;

clrscr();
display_complex_input(8);
display_complex_output();

}

void complex_sixteen_is_to_one()
{

int ctr2;
take_input_a(32);
take_complex_select_input(5);
message(4);
decide_complex_inputs(16);

decieq = strobe[1]*8 + strobe[2]*4 + strobe[3]*2 + strobe[4]*1;

clrscr();
display_complex_input(16);
display_complex_output();

}

/**********MULTIPLEXING FUNCTIONS END*******************/

/****************CIRCUIT GUI****************************/


void common_gui(int num)
{

int y[32],i,i2,k;
double j=log(num)/log(2);
char str[2];
int stepsize=((280+num*3)-180)/(j+1);

for(i=0;i<32;i++)
y[i]=80 + (i+1)*10;

initgraph(&driver,&mode,"f: cgi");

outtextxy(20,5,"RED: logic level 1");
outtextxy(250,5,"Press any key to continue.....");
outtextxy(20,15,"GREEN:logic level 0");

for(i=0;i
{

if(check(i))
{

setcolor(WHITE);
circle(150,y[i],2);
setfillstyle(SOLID_FILL,RED);
floodfill(150,y[i],WHITE);

}

else
{

setcolor(15);
circle(150,y[i],2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(150,y[i],WHITE);

}

line(154,y[i],180,y[i]);

}

rectangle(180,40,280+num*3,y[i-1]+40);
line(280+num*3,(y[i-1]-40)/2 + 60,280+3*num+40,(y[i-1]-40)/2 + 60);

if(output==1)
{

setcolor(15);
circle(280+num*3+44,(y[i-1]-40)/2 + 60,4);
setfillstyle(SOLID_FILL,RED);
floodfill(280+num*3+44,(y[i-1]-40)/2 + 60,WHITE);

}

else
{

setcolor(15);
circle(280+num*3+44,(y[i-1]-40)/2 + 60,4);
setfillstyle(SOLID_FILL,GREEN);
floodfill(280+num*3+44,(y[i-1]-40)/2 +60,WHITE);

}
outtextxy(280+num*3+50,(y[i-1]-40)/2 + 60," Y(Output = )");
itoa(output,str,10);
outtextxy(280+num*3+150,(y[i-1]-40)/2 + 60,str);
itoa(num,str,10);

outtextxy(140,y[i-1]+72," Block Diagram of ");
outtextxy(290,y[i-1]+72,str);
outtextxy(293,y[i-1]+72," : 1 Mux");

outtextxy((280+num*3-180)/2 + 150,y[(i-1)/2],str);
outtextxy((280+num*3-180)/2 + 160,y[(i-1)/2]," : 1");
outtextxy((280+num*3-180)/2 + 150,y[(i-1)/2+2]," MUX");

outtextxy(280+num*3+170,(y[i-1]-40)/2 + 60,"(I ");
itoa(decieq,str,10);
outtextxy(280+num*3+185,(y[i-1]-40)/2 + 60,str);
outtextxy(280+num*3+192,(y[i-1]-40)/2 + 60," )");

outtextxy(130,y[0]-5,"I0");
itoa(num-1,str,10);
outtextxy(126,y[i-1],"I");
outtextxy(133,y[i-1],str);

for(i2=0;i2
for(k=0;k<20;k++)
{
line(180+(i2+1)*stepsize,y[i-1]+40,180+(i2+1)*stepsize,y[i-1]+60);
if(strobe[i2]==0)
{

setcolor(15);
circle(180+(i2+1)*stepsize,y[i-1]+62,2);
setfillstyle(SOLID_FILL,2);
floodfill(180+(i2+1)*stepsize,y[i-1]+62,15);

}

else
{

setcolor(15);
circle(180+(i2+1)*stepsize,y[i-1]+62,2);
setfillstyle(SOLID_FILL,4);
floodfill(180+(i2+1)*stepsize,y[i-1]+62,15);

}

}

outtextxy(185+(i2)*stepsize,y[i-1]+62,"S0");
itoa(j-1,str,10);
outtextxy(162+stepsize,y[i-1]+62,"S ");
outtextxy(170+stepsize,y[i-1]+62,str);

if(num==2)
{
setcolor(0);
outtextxy(162+stepsize,y[i-1]+62,"S0");
setcolor(15);

}

getch();

}

void common_complex_gui(int num)
{

int y[32],i,i2,k;
double j=log(num)/log(2);
char str[2];
int stepsize=((280+num*3)-180)/(j+1);

for(i=0;i<32;i++)
y[i]=80 + (i+1)*10;

initgraph(&driver,&mode,"f: cgi");

outtextxy(20,5,"RED: logic level 1");
outtextxy(250,5,"Press anu key to continue.....");
outtextxy(20,15,"GREEN:logic level zero");

for(i=0;i
{

if(inputs[i]==1)
{

setcolor(WHITE);
circle(150,y[i],2);
setfillstyle(SOLID_FILL,RED);
floodfill(150,y[i],WHITE);

}

else
{

setcolor(15);
circle(150,y[i],2);
setfillstyle(SOLID_FILL,GREEN);
floodfill(150,y[i],WHITE);

}

line(154,y[i],180,y[i]);

}

rectangle(180,40,280+num*3,y[i-1]+40);
line(280+num*3,(y[i-1]-40)/2 + 60,280+3*num+40,(y[i-1]-40)/2 + 60);

if(output==1)
{

setcolor(15);
circle(280+num*3+44,(y[i-1]-40)/2 + 60,4);
setfillstyle(SOLID_FILL,RED);
floodfill(280+num*3+44,(y[i-1]-40)/2 + 60,WHITE);

}

else
{

setcolor(15);
circle(280+num*3+44,(y[i-1]-40)/2 + 60,4);
setfillstyle(SOLID_FILL,GREEN);
floodfill(280+num*3+44,(y[i-1]-40)/2 +60,WHITE);

}

outtextxy(280+num*3+50,(y[i-1]-40)/2 + 60," Y(Output = )");
itoa(output,str,10);
outtextxy(280+num*3+150,(y[i-1]-40)/2 + 60,str);
itoa(num,str,10);

outtextxy(140,y[i-1]+72," Block Diagram of ");
outtextxy(290,y[i-1]+72,str);
outtextxy(293,y[i-1]+72," : 1 Mux");

outtextxy((280+num*3-180)/2 + 150,y[(i-1)/2],str);
outtextxy((280+num*3-180)/2 + 160,y[(i-1)/2]," : 1");
outtextxy((280+num*3-180)/2 + 150,y[(i-1)/2+2]," MUX");

outtextxy(280+num*3+170,(y[i-1]-40)/2 + 60,"(I ");
itoa(decieq,str,10);
outtextxy(280+num*3+185,(y[i-1]-40)/2 + 60,str);
outtextxy(280+num*3+192,(y[i-1]-40)/2 + 60," )");

outtextxy(130,y[0]-5,"I0");
itoa(num-1,str,10);
outtextxy(126,y[i-1],"I");
outtextxy(133,y[i-1],str);

for(i2=1;i2<=j;i2++)
for(k=0;k<20;k++)
{
line(180+(i2)*stepsize,y[i-1]+40,180+(i2)*stepsize,y[i-1]+60);
if(strobe[i2]==0)
{

setcolor(15);
circle(180+(i2)*stepsize,y[i-1]+62,2);
setfillstyle(SOLID_FILL,2);
floodfill(180+(i2)*stepsize,y[i-1]+62,15);

}

else
{

setcolor(15);
circle(180+(i2)*stepsize,y[i-1]+62,2);
setfillstyle(SOLID_FILL,4);
floodfill(180+(i2)*stepsize,y[i-1]+62,15);

}

}

outtextxy(185+(i2-1)*stepsize,y[i-1]+62,"S0");
itoa(j-1,str,10);
outtextxy(162+stepsize,y[i-1]+62,"S ");
outtextxy(170+stepsize,y[i-1]+62,str);

if(num==2)
{
setcolor(0);
outtextxy(162+stepsize,y[i-1]+62,"S0");
setcolor(15);

}

getch();

}

/************CIRCUIT GUI ENDS**********************/

/********************MAIN FUNCTION*****************/

void main()
{

int ctr=0;

welcome();

clrscr();
printf("Prog to implement a function using a multiplexer logic:-");

printf("Enter the function in minterms only.");
printf("Enter the function in form a b c d where a
printf("Press enter or provide spaces after each input.");
printf("Any value in input function cannot be repeated.");
printf("Enter the select input in form of 1's & 0's.");
printf("All the entered inputs are assumed to be high.");
printf("Maximun value of the minterm that can be accepted :31");

textcolor(4+BLINK);
textbackground(3);
cprintf("Press any key to continue.......");
textcolor(15);
textbackground(0);

getch();
printf("How many input will you provide: ");
scanf("%d",&count);

if(count>32)
{

printf("Invalid function input.inputs can't be greater than 32");
exit(1);

}

clrscr();
printf("Enter the function to be implemented : ");
for(ctr=0;ctr
{

scanf("%d",&function[ctr]);

if(function[ctr]>31)
{

printf("Invalid function input");
exit(1);

}

}

printf("Only first %d inputs considered.rest rejected(if any)",count);

fflush(stdin);

if(function[count-1] <= 1)
{
two_is_to_one();
common_gui(2);

}

else if(function[count-1] <= 3)
{

label1:

printf("Implement using:-1.)4:1 Mux2.)2:1 MuxEnter your choice?");
scanf("%d",&choice);

switch(choice)
{

case 1: four_is_to_one();
common_gui(4);
break;

case 2: complex_two_is_to_one();
common_complex_gui(2);
break;

default : printf("Invalid choice");
goto label1;

}

}

else if(function[count-1] <= 7)
{

label2:

printf("Implement using:-1.)8:1 Mux 2.)4:1 Mux Enter your choice?");
scanf("%d",&choice);

switch(choice)
{

case 1: eight_is_to_one();
common_gui(8);
break;

case 2: complex_four_is_to_one();
common_complex_gui(4);
break;

default : printf("Invalid choice");
goto label2;

}

}

else if(function[count-1] <= 15)
{

label3:

printf("Implement using:-1.)16:1 Mux2.)8:1 MuxEnter your choice?");
scanf("%d",&choice);

switch(choice)
{

case 1: sixteen_is_to_one();
common_gui(16);
break;

case 2: complex_eight_is_to_one();
common_complex_gui(8);
break;

default : printf("Invalid choice");
goto label3;

}

}

else if(function[count-1] <= 31)
{

label4:

printf("Implement using:-1.)32:1 Mux2.)16:1 MuxEnter your choice?");
scanf("%d",&choice);

switch(choice)
{

case 1: thirtytwo_is_to_one();
common_gui(32);
break;

case 2: complex_sixteen_is_to_one();
common_complex_gui(16);
break;

default : printf("Invalid choice");
goto label4;

}

}

getch();

}

/**************MAIN FUNCTION
ENDS****************************************/

Monday, November 2, 2009

PROGRAM FOR IMPLEMENTING IO STREAMS IN C++

#include"iostream.h"
#include"conio.h"
#include"math.h"
int main()
{
int count=0;
char c;
char name[20];
clrscr();
cout<< "\n\n-------------Get() & put() Function--------\n\n";
cout<< "INPUT Text\n";
cin.get(c);
while(c!='\n')
{
cout.put(c);
count++;
cin.get(c);
}
cout<< "\n\n No. of characters="<< count <<"\n";

cout<<"\n\n-------Getline() & Write Function()-------\n\n";
cout<<"Enter name\n";

cin.getline(name,10);
cout<<"Printing name with Write() size 5 characters \n\n";
cout.write(name,5);

cout<< "\n\n------------Width() Dunction--------\n\n";
cout<< "ITEMS";
cout.width(8);
cout<< "COST";
cout.width(15);
cout<<"Total Value"<<"\n";

cout<<"\n\n-------Precision() Function--------\n";
cout.precision(5);
cout<< sqrt(3) << "\n";

cout<< "\n\n------Fill() Function \n";
cout.fill('$');
cout.width(15);
cout<< "SAI";

getch();
return 0;
}

TEMPLATES FOR CONSTRUCTORS...VECTOR PROGRAM



include
#include
const size=3;
template <>
class vector
{
T *v;
public:
vector()
{
v=new T[size];
for(int i=0;i < i="0;iv[i]*y.v[i];
return sum;
}};
int main()
{
clrscr();
float x[3]={1.1,2.2,3.3};
float y[3]={4.4,5.5,6.6};
vector <> v1;
vector <> v2;
v1=x;
v2=y;
float r=v1*v2;
cout<<"r="<<<"\n";
getch();
return 0;
}

//virtual functions usin c++

#include"iostream.h"
class Base
{
public:
void display()
{
cout<<"\n Display base";
}
virtual void show()
{
cout<<"\n show base";
}
};
class Derived : public Base
{
public:
void display()
{
cout<<"\n Display derived";
}
void show()
{
cout<<"\n show derived";
}
};
int main()
{
Base B;
Derived D;
Base *bptr;
cout<<"\n bptr points to Base \n";
bptr=&B;
bptr->display(); //calls Base version
bptr->show(); //calls Base version

cout<<"\n\n bptr points to Derived \n";
bptr=&D;
bptr->display(); //calls Base version
bptr->show(); //calls Derived version

return 0;
}

// function overloading usin c++

#include"iostream.h"

//Declarations (prototypes)

int volume(int);
double volume(double, int);
long volume(long, int, int);

int main()
{
cout<< volume(10) <<"\n";
cout<< volume(2.5,8) <<"\n";
cout<< volume(100L,75,15) <<"\n";

return 0;
}

//Function definitions

int volume(int s) //cube
{
return(s*s*s);
}
double volume(double r, int h) //cylinder
{
return(3.14519*r*h);
}
long volume(long l, int b, int h) //rectangular box
{
return(l*b*h);
}

// Virtual Base class

#include"iostream.h"

class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"Roll No:"< }
};

class test:virtual public student
{
protected:
float part1,part2;
public:
void get_marks(float x, float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"Marks obtained:"<<"\n"<<"Part1="< }
};

class sports : public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"Sports wt:"< }
};

class result:public test, public sports
{
float total;
public:
void display(void);
};

void result::display(void)
{
total=part1+part2+score;

put_number();
put_marks();
put_score();

cout<<"Total Score:"<}

int main()
{
result student_1;
student_1.get_number(678);
student_1.get_marks(30.5,25.5);
student_1.get_score(7.0);
student_1.display();
return 0;
}

//Runtime polymorphism c++

#include"iostream.h"
#include"string.h"
#include"conio.h"
class media
{
protected:
char title[50];
float price;
public:
media(char *s,float a)
{
strcpy(title,s);
price=a;
}
virtual void display() { } //empty vitual function
};

class book:public media
{
int pages;
public:
book(char *s, float a, int p):media(s,a)
{
pages=p;
}
void display();
};
class tape:public media
{
float time;
public:
tape(char *s, float a, float t):media(s,a)
{
time=t;
}
void display();
};
void book::display()
{
cout<<"\n Title:"< cout<<"\n Pages:"< cout<<"\n Price:"<}
void tape::display()
{
cout<<"\n Title:"< cout<<"\n Play time:"< cout<<"\n price:"<}
int main()
{
char *title=new char[30];
float price, time;
int pages;

clrscr();

//Books details
cout<<"\n Enter Book Details\n";
cout<<"Title:";
cin>>title;
cout<<"Price :";
cin>>price;
cout<<"Pages:";
cin>>pages;

book book1(title,price,pages);

//tape Details
cout<<"\n Enter Tape Details\n";
cout<<"Title:";
cin>>title;
cout<<"Price:";
cin>>price;
cout<<"Play time (mins):";
cin>>time;

tape tape1(title,price,time);

media*list[2];
list[0]=&book1;
list[1]=&tape1;
cout<<"\n Media Details:";
cout<<"\n.........BOOK........";
list[0]->display();

cout<<"\n.....TAPE.....";
list[1]->display();
getch();
return 0;
}