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 🙂

Scan a Valid Integer value

Hi Everyone…

As you all know that in C there is no function that can validate the entered string as an correct integer. Whenever you enter ’12d3′ where an integer value is expected your program will through an exception. In order to cope with this I wrote the following function and tried to consider every possiblity (-ve integers, +ve integers, backspace while entering character and other alpha numeric characters….). I have written this function using Turbo C/C++ 3.0 compiler.  

I have also written functions for others (long integer, float, double) tell me if you want any of them or just change the one given below. 😉 

1. How to use this function:

Create either a header file and put this function there or just copy paste this function in your source file and call this function where you want to read an integer from user…e.g.

int main ()

{

int i;

printf (“Please Enter an Integer: “);

i = short_integer();

printf (“You entered %d”, i);

}

// Definition for short_integer function goes here after you main

2. Listing for short_integer function

 int I,J,C;

char CH,*string;

char text[4096];

int short_integer()

{

int y=wherey(),x=wherex();

long number;

check:

J=0;

C=0;

for (I=0; I<=20; I++)

string[I]=NULL;

gotoxy (x,y);

while ((CH=getch())!=’\r’)

{

if (isdigit(CH) && J<=4 || CH==’-‘)

{

printf (“%c”,CH);

C=1;

string[J]=CH;

J=J+1;

}

else if (CH==’\b’)

{

string[–J]=NULL;

if (J>=0)

{

printf (“\b \b”);

if (J==0)

C=0;

}

else if (J<=0)

{

J=0;

C=0;

printf (“\a”);

}

}

else

{

if (J<=4)

printf (” \b\a”);

else

printf (“\a”);

/*gettext (1,1,80,25,text);

clrscr();

textcolor (RED+BLINK);

gotoxy (23,12); cprintf (“Wrong Integer Value Entered”);

gotoxy (25,13); cprintf (“Press Enter to ReEnter”);

while (getch()!=’\r’);

textcolor (7);

clrscr();

puttext (1,1,80,25,text);

gotoxy (y,x);

printf (” “);

gotoxy (y,x);

goto check; */

}

}

if (C==0)

goto check;

number=atol(string);

if (number>=32767)

{

/*gettext (1,1,80,25,text);

clrscr();

textcolor (RED+BLINK);

gotoxy (26,12); cprintf (“Integer Out of Range”);

gotoxy (25,13); cprintf (“Press Enter to ReEnter”);

while (getch()!=’\r’);

textcolor (7);

clrscr();

puttext (1,1,80,25,text);*/

gotoxy (x,y);

printf (” \a”);

gotoxy (x,y);

goto check;

}

return (number);

}

Keep Smiling 🙂

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 🙂