Saturday, September 15, 2007

Some Nessary Sorting Code

#Bubble Sort


void bubbleSort(int numbers[], int array_size){
int i, j, temp;
for (i = (array_size - 1); i >= 0; i--)
{ for (j = 1; j <= i; j++) { if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}


# Quick Sort For String


///// U must declare ch[][] (for input) as global//////////////////////////


int string_sort(char *ch1,char *ch2)
{
int l1,l2;

l1=strlen(ch1);
l2=strlen(ch2);

if(l1==l2)
{
for(m=0;mch2[m])
return 1;
else if(ch1[m]l2)
return strcmp(ch1,ch2);
else if(l2>l1)
return strcmp(ch1,ch2);

return -1;

}


void quickSort(int array_size)
{
q_sort( 0, array_size - 1);
}


void q_sort( int left, int right)
{
int t,l_hold, r_hold;
char *pivot;

l_hold = left;
r_hold = right;
strcpy(pivot,ch[left]);
while (left <>
{
while (string_sort(ch[right],pivot)>0 && (left <>
right--;

if (left != right)
{ strcpy(ch[left],ch[right]); left++; }

while (string_sort(ch[left],pivot)<0>
left++;

if (left != right) { strcpy(ch[right],ch[left]); right--; }

}

strcpy(ch[left],pivot);
t = left;
left = l_hold;
right = r_hold;

if (left <>
q_sort(left, t-1);

if (right > t)
q_sort(t+1, right);

}

Monday, April 16, 2007

Analog watch in c using bgi graphics

Introduction:
This is an easy effort to make Analog Watch using c language. I think this article will help C programmer to get familiarized with BGI (Borland Graphics Interface) for DOS programs.

Description:
Four steps:
1. Initialize graphics
2. Get system time
3. Draw watch with current system time
4. Logical work
Initialize graphics:
‘initgraph’ method of ‘graphics.h’ header file is used to initializes graphics. Need to set three parameter of this function:

1. Graphics driver
2. Graphics Mode
3. Path of driver

In this project I initialize the graphics in following format

int d=DETECT,m; // variable used for graphics initialization
initgraph(&d,&amp;m,""); //this method initialize graphics

If we write above two lines, this will automatically detect graphics and it become ready to draw. We need to paste *.bgi files to the directory of watch.exe file for detecting graphics driver path. We can set the path of bgi folder under double quote (like initgraph(&d,&m,"e:\\tc3\\bgi"); ).

Get system time:
We need to get and set current time of our system to start our watch.exe. For this we need to execute following code:

void save_biostime()
{
struct time t;
gettime(&t);
hour=t.ti_hour; //hour minute second private variable
minute= t.ti_min;
second= t.ti_sec;

}


Draw watch with current system time:
We simply use some bgi graphics function to draw line, circle and text. Following function is used for drawing watch:

void draw_watch()
{
int c=1;
setcolor(c);
circle(320,250,170);
setfillstyle(1,c);
floodfill(485,250,c);

setcolor(c);
circle(280,250,170); // draw circle
setfillstyle(1,c); //set fill style
floodfill(115,250,c); // filling the circle by desired color


setcolor(1);
circle(300,250,130);
circle(300,250,120);
setfillstyle(1,1);
floodfill(300,121,1);


setcolor(8);
circle(300,250,160);
circle(300,250,170);
setfillstyle(1,8);
floodfill(300,88,8);

setcolor(5);
circle(300,250,161);
circle(300,250,131);
setfillstyle(2,5);
floodfill(300,111,5);


setcolor(9);
circle(300,250,119);
setfillstyle(1,9);
floodfill(300,132,9);
putpixel(300,250,14);

setcolor(0);
outtextxy(292,134,"12");
outtextxy(410,247,"3");
outtextxy(394,191,"2");
outtextxy(353,151,"1");


outtextxy(298,360,"6");
outtextxy(353,344,"5");
outtextxy(394,304,"4");
outtextxy(184,248,"9");
outtextxy(200,302,"8");
outtextxy(240,344,"7");

outtextxy(236,152,"11");
outtextxy(196,192,"10");



}


Logical work:
We know an analog watch is comparable with a circle. We need to imagine three circle for second, minute and hour pins.



Here x = r * cos (t)
y= r * sin (t)
t is the angle between horizontal axis and current line. x is the horizontal component and y is the vertical component of radius r at an angle t. Thus we will convert Polar coordinate to Cartesian coordinate which helps us to draw each second movement of second pin.

We know each 1 sec = 6 degree movement for second pin. So we will iterate our loop by 6 degree.

We also have to convert degree to radian for ‘math.h’ functions.

Code for draw pin:
void draw_pin(double t1,double t2,double t3){

double c,d,e,f;
int p=1;

while(p)
for(int i=t3,a,b;i<=624;i+=6) { if(kbhit()!=0) {p=0; break; } a=100*cos(M_PI*i/180); //for second pin & pin length 100 b=100*sin(M_PI*i/180); //for second pin & pin length 100 c=90*cos(M_PI*t1/180); //for minute pin & pin length 90 d=90*sin(M_PI*t1/180); //for minute pin & pin length 90 e=80*cos(M_PI*t2/180); //for hour pin & pin length 80 f=80*sin(M_PI*t2/180); //for hour pin & pin length 80 setlinestyle(0,0,0); setcolor(14); line(300,250,300+a,250+b); setcolor(1); setlinestyle(0,0,3); line(300,250,300+c,250+d); setcolor(4); line(300,250,300+e,250+f); setlinestyle(0,0,0); delay(1000); setcolor(9); line(300,250,300+a,250+b); setlinestyle(0,0,3); line(300,250,300+c,250+d); line(300,250,300+e,250+f); t1+=0.1; t2+=0.008333333; t3=270; } } Lets try it…… CODE: /**********************************/ // Necessary Header file/***********************************/ #include
#include
#include
#include
#include
#include
#include

double minute,hour,tmp,second; // gloabal variable for time-component
void draw_watch()
{ int c=1;
setcolor(c);
circle(320,250,170);
setfillstyle(1,c);
floodfill(485,250,c);
setcolor(c);
circle(280,250,170);
setfillstyle(1,c);
floodfill(115,250,c);
setcolor(1);
circle(300,250,130);
circle(300,250,120);
setfillstyle(1,1);
floodfill(300,121,1);
setcolor(8);
circle(300,250,160);
circle(300,250,170);
setfillstyle(1,8);
floodfill(300,88,8);
setcolor(5);
circle(300,250,161);
circle(300,250,131);
setfillstyle(2,5);
floodfill(300,111,5);
setcolor(9);
circle(300,250,119);
setfillstyle(1,9);
floodfill(300,132,9);
putpixel(300,250,14);
setcolor(0);
outtextxy(292,134,"12");
outtextxy(410,247,"3");
outtextxy(394,191,"2");
outtextxy(353,151,"1");
outtextxy(298,360,"6");
outtextxy(353,344,"5");
outtextxy(394,304,"4");
outtextxy(184,248,"9");
outtextxy(200,302,"8");
outtextxy(240,344,"7");
outtextxy(236,152,"11");
outtextxy(196,192,"10");


}

void draw_pin(double t1,double t2,double t3){
double c,d,e,f; int p=1;

while(p)
for(int i=t3,a,b;i<=624;i+=6) {
if(kbhit()!=0)
{p=0; break; }
a=100*cos(M_PI*i/180); //for second pin & pin length 100
b=100*sin(M_PI*i/180); //for second pin & pin length 100
c=90*cos(M_PI*t1/180); //for minute pin & pin length 90
d=90*sin(M_PI*t1/180); //for minute pin & pin length 90
e=80*cos(M_PI*t2/180); //for hour pin & pin length 80
f=80*sin(M_PI*t2/180); //for hour pin & pin length 80
setlinestyle(0,0,0);
setcolor(14);
line(300,250,300+a,250+b);
setcolor(1);
setlinestyle(0,0,3);
line(300,250,300+c,250+d);
setcolor(4);
line(300,250,300+e,250+f);
setlinestyle(0,0,0);
delay(1000);
setcolor(9);
line(300,250,300+a,250+b);
setlinestyle(0,0,3);
line(300,250,300+c,250+d);
line(300,250,300+e,250+f);
t1+=0.1;
t2+=0.008333333;
t3=270;
}

}

void save_biostime()
{ struct time t;
gettime(&t);
hour=t.ti_hour; //hour minute second private variable
minute= t.ti_min;
second= t.ti_sec;

}

int main(){

int d=DETECT,m; // variable used for graphics initialization
initgraph(&d,&amp;m,""); //this method initialize graphics
draw_watch(); // method to draw watch
save_biostime(); // method to get & save bios time
tmp=second/60; //converting second to minute
minute=minute+tmp; //setting value for minute pin
tmp=minute/60; //converting minute to hour
hour=hour+tmp; //setting value for hour pin
draw_pin(270+minute*6,270+hour*30,270+second*6); // drawing watch withcurrentbiostime
return 0;}

Saturday, March 10, 2007

Need Help For CHESS (AI Project)

I am also working with Artificial Intelligence project CHESS. Needs help for giving AI to the machine side. Mail me at
nazmulkibria@gmail.com

Friday, March 9, 2007

My Resent work

I am working for developing a multilingual software that can be used by different linguistic people. My work is based on unicode.

We can write in any textarea or textfield by programmatically by

1. Setting its font to my desired language
2. Font must be open type(unicode supported)
3. Unicodes are written in "\uxxxx" where xxxx is a hexadecimal code

Example : textfield.Text="\u0985"; // it is a bangla character

PC Magazine Tips and Solutions

PC World: Latest Technology News

PCWorld.com - Most Popular Downloads of the Week