Integer in Binary Format

Hi everyone…

Following function just show the binary of an integer passed to it.

void showbits (int n)
{
int i,k,andmask;

for (i=15; i>=0; i–)
{
andmask=1<i;
k=n & andmask;
k== 0 ? printf (“0”): printf (“1”);
}
}


Keep Smiling 🙂

Fraction to its simplified form

Hi Everyone..

Following is the code snippet i wrote as an one of my assignment in programming course…It simplifies the fraction to lowest level though code is not optimized. It needs improvement… 🙂

void cancel_fraction (long &upper, long &bottom){int i,j,k,limit;

if (upper<bottom) // Checking for Small number and placing it in limit

limit=upper;

else

limit=bottom;

for (i=2; i<=limit; i++) // (LOOP 1) Main Table Starting From 2 to small Fraction

{

for (j=1; j<=upper; j++) // (LOOP 2) Table Index 1 to num1

{

if (i*j==upper) // (Condition 1) num1 is factor of current table ‘i’ then check for sencond fraction i.e., num2

{

for (k=1; k<=bottom; k++) // (LOOP 3) table index for second fraction

{

if (i*k==bottom) // (Condition 2) num2 is factor of current table ‘i’ then

{

bottom=k; // num2 is divided by table ‘i’ at ‘k’

upper=j; // num1 is also divided by table ‘i’ but at ‘j’ thus

i–; // to divide the fraction again by the same table

} // End of (Condition 2)

} // End of (LOOP 3)

} // End of (Condition 1)

} // End of (LOOP 2)

} // End of (LOOP 1)

}

Keep Smiling 🙂

Making Statment valid

Hi Everyone…

Let us suppose we have following code

int main ()

{

int x = x++;

}

currently this statement will cause undefined behavior. So what should we do with it, i mean to add something before it to make it valid….So here is small solution…

int x=1;

int main ()

{

int x = x++;

}

Defining global variable with same name is one of the solution…

Keep smiling

Posted in General. Tags: . Leave a Comment »

Mouse Interaction using C/C++

Hi everyone…

Following is the headerfile contains functions to interact with mouse. I took the hint from “Let Us C”.

#include <dos.h>

union REGS i,o;

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

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

void GetPositionAndButton(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;
}

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

Usage:
1. call the InitMouse() function to initialize. (Once required)
2. Call the function ShowMousePointer() to make the pointer appear in your application.
3. The function GetPositionAndButton will return the current mouse pointer location in terms of x & y coordinate and the button status, i.e., left or right etc..
4. You can hide your mouse pointer by calling HideMousePointer function.

Now you can capture the mouse activities using following loop.

while (!kbhit())
{
getmousepos (&button,&x,&y);
gotoxy (5,3);
(button &1)== 1 ? printf (“Left Down”): printf (“Left UP”);
gotoxy (20,3);
(button &2 )==2 ? printf (“Rigt Down”): printf (“Right UP”);
gotoxy (1,1);
printf (“x = %3d, y = %3d”,x,y);
}


5. Thats it.


Keep Smiling 🙂