C Programming Tutorial

 
 
 
 

Image

#include<dos.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#define HEX 0
#define DEC 1
#define OCT 2
#define BIN 3

int i=0,choice=0;

void initmouse()
{
    _AX=0;
    geninterrupt(0x33);
}

void showmouse()
{
    _AX=1;
    geninterrupt(0x33);
}

void hidemouse()
{
    _AX=2;
    geninterrupt(0x33);
}

void getmouse(int *button,int *x,int *y)
{
    _AX=3;
    geninterrupt(0x33);
    *button=_BX;
    *x=_CX;
    *y=_DX;
}

int getnum(char c)//gets the numeric equivalent of a alphanumeric digit
{
    int j;
    char alpha_set[36]=\"0123456789abcdefghijklmnopqrstuvwzyz\";
    for(j=0;j<36;j++)
    {
        if(alpha_set[j]==c)
            break;
    }
    return j;
}

unsigned long convert_to_decimal(char *_num,int radix)
{//converts a number of different base to decimal
    int i,len;
    unsigned long dec=0;
    len=strlen(_num);
    len--;
    for(i=0;_num[i]!=NULL;i++,len--)
    {
        dec+=(getnum(_num[i])*pow(radix,len));
    }
    return dec;
}


void display(int x,int y,char string[80])
{//displays a string at the given coordinates
    gotoxy(x,y);
    cprintf(string);
}

void make_screen_cyan()//The name says it
{
    textbackground(CYAN);
    clrscr();
}

void start_screen()//Draws the Starting screen of the program
{
    i=0;
    hidemouse();// Hides the mouse
    make_screen_cyan();
    textbackground(WHITE);
    textcolor(BLACK);
    display(20,1,\"Radix Converter\");
    display(30,3,\"Press Escape to Quit\");
    textbackground(CYAN);
    display(10,5,\"Hexadecimal:- \");
    display(10,7,\"Decimal    :- \");
    display(10,9,\"Octal      :- \");
    display(10,11,\"Binary     :- \");
    textbackground(WHITE);
    display(23,5,\"                                                      \");
    display(23,7,\"                                                      \");
    display(23,9,\"                                                      \");
    display(23,11,\"                                                      \");
    if(choice==HEX)
        gotoxy(24,5);
    else if(choice==DEC)
        gotoxy(24,7);
    else if(choice==OCT)
        gotoxy(24,9);
    else if(choice==BIN)
        gotoxy(24,11);
    showmouse();
}

void main()
{
    char text[80]=\"\\0\",buffer[80];
    char ch,*charhex,*chardec,*charoct,*charbin;
    int button,mousex,mousey,x,y;
    unsigned long deci;

    initmouse();//Initiates the mouse
    start_screen();
    showmouse();//Shows the mouse
    while(1)
    {
        if(kbhit())
        {
            ch=getch();
            if(ch==27)        //27 is ASCII equivalent of Escpape key
                break;
            if(ch==\'\\b\'&&wherex()>=24)   //If Backspace is pressed & cursor must not go outside input area
            {
                cprintf(\"\\b\");//Move cursor one space backwards
                cprintf(\"%c\",255);//Put there a Space
                cprintf(\"\\b\");//Again move cursor backwards
                if(i!=0)
                    i--;            //pop the char out of the text array
                text[i]=NULL;//put the NULL character to end the text
            }
            else if(wherex()>=24&&ch>=\'0\'&&ch<=\'f\')
            {
                cprintf(\"%c\",ch);
                text[i]=ch;            //push the char to the text array
                text[i+1]=NULL;    //End the array with NULL character
                i++;
            }
            x=wherex(),y=wherey();//Stores the current coordinates of cursor
/*Explanation:- First convert the text string to a decimal. After it is
converted to decimal using convert_to_decimal(), convert the decimal to the required base, 
eg hex,oct or binary using ltoa() in stdlib.h.*/
            switch(choice)
            {
                case HEX:
                    deci=convert_to_decimal(text,16);
                    gotoxy(24,7);
                    printf(\"%ld                             \",deci);
                    gotoxy(24,11);
                    printf(\"%s                              \",ltoa(deci,buffer,2));
                    gotoxy(24,9);
                    printf(\"%s                              \",ltoa(deci,buffer,8));
                    break;
                case DEC:
                    deci=atol(text);
                    gotoxy(24,5);
                    printf(\"%s                             \",ltoa(deci,buffer,16));
                    gotoxy(24,9);
                    printf(\"%s                             \",ltoa(deci,buffer,8));
                    gotoxy(24,11);
                    printf(\"%s                             \",ltoa(deci,buffer,2));
                    break;
                case OCT:
                    deci=convert_to_decimal(text,8);
                    gotoxy(24,7);
                    printf(\"%ld                             \",deci);
                    gotoxy(24,5);
                    printf(\"%s                             \",ltoa(deci,buffer,16));
                    gotoxy(24,11);
                    printf(\"%s                              \",ltoa(deci,buffer,2));
                    break;
                case BIN:
                    deci=convert_to_decimal(text,2);
                    gotoxy(24,5);
                    printf(\"%s                            \",ltoa(deci,buffer,16));
                    gotoxy(24,7);
                    printf(\"%ld                            \",deci);
                    gotoxy(24,9);
                    printf(\"%s                            \",ltoa(deci,buffer,8));
                    break;
            }
            gotoxy(x,y);
        }
        getmouse(&button,&mousex,&mousey);
        mousex++,mousey++;
//The following lines detect where you have clicked & what you selected
        if(mousex/8>23&&mousex/8<50&&mousey/8==4&&button==1)
            choice=HEX,start_screen();
        else if(mousex/8>23&&mousex/8<50&&mousey/8==6&&button==1)
            choice=DEC,start_screen();
        else if(mousex/8>23&&mousex/8<50&&mousey/8==8&&button==1)
            choice=OCT,start_screen();
        else if(mousex/8>23&&mousex/8<50&&mousey/8==10&&button==1)
            choice=BIN,start_screen();
    }

}

 
 
Didn't find what you were looking for? Find more on Program that converts hexadecimal value in decimal, octal and binary.