Wednesday, July 25, 2012

A Calculator in C Using Graphics & Mouse Operations




A Calculator in C Using Graphics & Mouse Operations

#include"graphics.h"
#include"dos.h"
#include"stdio.h"
#include"math.h"
union REGS i,o;
char *text[]={ "7","8","9","*",
       "4","5","6","/",
       "1","2","3","+",
       "0","00",".","-",
      "M","M+","M-","+/-",
      "MR","MC","x^2","sr",
      "OFF","AC","CE","="};

int k=0,pass,op,prop,newnum=1,bt,memo=1,d=0,sq;
long double num=0,accum,m;


void normalbutton(int ,int ,int ,int,char**);

void main()

{
int gd=DETECT,gm,x1,x2,y1,y2,i,j,maxx,maxy,x,y,button;
char *text1[]={""","T","o"," ","K","n","o","w"," ","a","b","o",
     "u","t"," ","m","e"," ","l","o","g","o","n"," ",":"};
char *text2[]={"w","w","w",".","g","e","o","c","i","t","i","e","s",
    ".","c","o","m","/","t","a","l","k","d","e","e","p",
    "e","s","h"};

initgraph(&gd,&gm,"");

if(initmouse()==0)
 {
  closegraph();
  restorecrtmode();
  printf("
Mouse driver not loded");
  exit(1);
  }
  showmouseptr();
//  x=y=50;
  movemouseptr(&x,&y);

setbkcolor(11);
setcolor(1);
rectangle(198,140,417,163);
rectangle(199,141,418,164);
rectangle(197,139,416,162);
rectangle(185,130,430,450);
rectangle(184,129,431,451);
rectangle(182,127,433,454);
rectangle(181,126,434,453);


//setfillstyle(SOLID_FILL,3);
//bar(200,142,415,161);

outtextxy(200,50,"A Calculator in C");
outtextxy(200,100,"Press OFF button to exit....");
y1=140;
y2=160;

    for(j=0;j<7;j++)
 {
  x1=200;
  x2=235;
  y1+=40;
  y2+=40;

   for(i=0;i<4;i++)
  {
   normalbutton(x1,x2,y1,y2,text);
   x1+=60;
   x2+=60;
   }
 }


 while(1)
  {


    getmousepos(&button,&x,&y);


    y1=140;
    y2=160;
/*
    if( (x>400&&x<450) && (y>400&&y<420) )
  {    if((button & 1)==1)
      {    sound(500);
    delay(5);
    exit();
      }
  }
*/
    for(j=0;j<7;j++)
 {
  x1=200;
  x2=235;
  y1+=40;
  y2+=40;

   for(i=0;i<4;i++)
  {

     if((x<x2&&x>x1)&&(y<y2&&y>y1))
   {
    if((button & 1)==1)
     {  gotoxy(28,10);
     //    printf("%d",ch=*text[j*4+i]);
     //    printf("char is %c",ch);
    bt=j*4+i;
 //    printf("char is %d",j*4+i);
    setcolor(11);
    outtextxy(x1+12,y1+7,text[j*4+i]);

    if(num>pow(10.0,18))
    exit();
    sound(500);delay(10);nosound();
    delay(250);
    sound(400);delay(10);
    nosound();
     switch (bt)
     {
     case  8 :
       addnum(1);
       break;
     case  9 :
       addnum(2);
       break;
     case  10 :
       addnum(3);
       break;
     case  4 :
       addnum(4);
       break;
     case  5 :
       addnum(5);
       break;
     case  6 :
       addnum(6);
       break;
     case  0 :
       addnum(7);
       break;
     case  1 :
       addnum(8);
       break;
     case  2 :
       addnum(9);
       break;
     case  12 :
       addnum(0);
       break;
     case  11 :
       // plus
       operation(1);
       break;

     case  15 :
       // minus
       operation(2);
       break;
     case  3 :
       // multiplication
       operation(3);
       break;
     case  7 :
       // division
       operation(4);
       break;
     case 13:
       doublezero();
       break;
     case  14 :
       decimal();
       break;

     case 16:
       mem();
       break;
     case 20:
       recallmem();
       break;
     case 19:
       plusminus();
       break;
     case 17:
       plusm();
       break;
     case 18:
       minusm();
       break;
     case 21:
       clearm();
       break;
     case 22 :
       square();
       break;
     case 23:

       sqroot();
       break;
     case 24:
      // OFF
       hidemouseptr();
       setcolor(1);
       for(j=0;j<20;j++)
     {
    for(i=75;i<481;i+=20)

       line(0,0+i+j,640,j+0+i);

       delay(100);
     }
       setcolor(14);
       outtextxy(225,200,"Thanks for using it !");
       delay(2000);
       setcolor(13);
    for(j=0;j<20;j++)
     {
    for(i=0;i<640;i+=20)

       line(0+i+j,0,j+0+i,640);

       delay(100);
     }
       setcolor(1);

       for(i=0;i<25;i++)
      {
    outtextxy(75+10*i,200,text1[i]);
       sound(3000);
       delay(50);
       nosound();
      }
       for(i=0;i<29;i++)
      {
    outtextxy(125+10*i,225,text2[i]);
       sound(3000);
       delay(50);
       nosound();
      }
     

       delay(2500);
       sound(5000);
       delay(10);
       exit();
       break;
     case 25:
      allclear();
      break;

     case 26:
      clear();
      break;

     case 27:
     //  equalto
       operation(5);
       break;



     }
     setcolor(1);
    outtextxy(x1+12,y1+7,text[j*4+i]);

     }
   }
    x1+=60;
    x2+=60;

   }

 }

  }



}

void normalbutton(int x1,int x2,int y1,int y2,char **text)
{
setcolor(15);
rectangle(x1-2,y1-2,x2+1,y2+1);
rectangle(x1-1,y1-1,x2+2,y2+2);
setcolor(7);
rectangle(x1,y1,x2+2,y2+2);
rectangle(x1,y1,x2+1,y2+1);

setfillstyle(SOLID_FILL,14);
bar(x1,y1,x2,y2);
setcolor(1);
outtextxy(x1+12,y1+7,text[k]);
k++;
}
/* initmouse */
initmouse()
{
 i.x.ax=0;
 int86 (0x33,&i,&o);
 return(o.x.ax);
}
hidemouseptr()
{
     i.x.ax=2;
     int86(0x33,&i,&o);
}

/* displays mouse pointer */
showmouseptr()
{
 i.x.ax=1;
 int86(0x33,&i,&o);
 return 0;
}

/*gets mouse coordinates and button status*/

getmousepos(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;
 return 0;
}
/* Move mouse ptr to x,y */
movemouseptr(int *x,int *y)
{
 i.x.ax=4;
 int86(0x33,&i,&o);
 o.x.cx=*x;
 o.x.dx=*y;
 return 0;
}

addnum(int pass)
{   if(sq)
    newnum=1;

    if(newnum)
      {      if(d)
    {
      num=pass/(pow(10.0,d));
      d++;
      newnum=0;
    }
     else
    {  num=pass;
       newnum=0;
    }
      }
    else
    {
    /*  if(num==0)
      {    if(d)
    {
      num=num+pass/(pow(10.0,d));
      d++;
    }
      else
      num=pass;
       }
    */
    //     else
       {    if(d)
    {
       if(num<0)
     num=num-pass/(pow(10.0,d));
       else
     num=num+pass/(pow(10.0,d));
      d++;
    }
    else
    {
    num=num*10+pass;
    }
    }
     }
 printf("%25.5Lf",num);
}

operation(int opr)
{  long double pnum;
   pnum=num;

   if(newnum && (prop != 5) && memo)
   {
   }
   else
  {  newnum=1;
     d=0;
     sq=0;
     switch(prop)
    {
     case 1:
    accum=accum+pnum;
    break;
     case 2:
    accum=accum-pnum;
    break;
     case 3:
    accum=accum*pnum;
    break;
     case 4:
    accum=accum/pnum;
    break;
     default:
       accum=pnum;
     }
  }
    prop=opr;
    num=accum;
 printf("%25.5Lf",num);
}

allclear()
{
 sq=0;
 accum=0;
 num=0;
 d=0;
 newnum=1;
 printf("%25.5Lf",num);
}

mem()
{
 m=num;
}
recallmem()
{
 memo=0;
 printf("%25.5Lf",m);
 num=m;
}
plusminus()
{ if(num!=0)
  {    num*=-1;
    printf("%25.5Lf",num);
  }
}


plusm()
{
m+=num;
}
minusm()
{
m-=num;
}
clearm()
{
m=0;
}
decimal()
{
 if(!d)
  {d=1;
    if(newnum==1)
    {
     num=0;
    }
  printf("%25.5Lf",num);
  }
}

square()
{ sq=1;
 num*=num;
 printf("%25.5Lf",num);
// newnum=1;
}
sqroot()
{ sq=1;
 num=pow(num,0.5);
 printf("%25.5Lf",num);
// newnum=1;
}
doublezero()
{
 if(d)
   {
    // num=num+pass/(pow(100.0,d));
     d++;
     d++;
   }
 else
  num*=100;
 printf("%25.5Lf",num);
}

clear()
{
num=0;
printf("%25.5Lf",num);
}


--
Hackerx Sasi
Don't ever give up.
Even when it seems impossible,
Something will always
pull you through.
The hardest times get even
worse when you lose hope.
As long as you believe you can do it, You can.

But When you give up,
You lose !
I DONT GIVE UP.....!!!

with regards
prem sasi kumar arivukalanjiam

No comments:

Post a Comment

Slider

Image Slider By engineerportal.blogspot.in The slide is a linking image  Welcome to Engineer Portal... #htmlcaption

Tamil Short Film Laptaap

Tamil Short Film Laptaap
Laptapp

Labels

About Blogging (1) Advance Data Structure (2) ADVANCED COMPUTER ARCHITECTURE (4) Advanced Database (4) ADVANCED DATABASE TECHNOLOGY (4) ADVANCED JAVA PROGRAMMING (1) ADVANCED OPERATING SYSTEMS (3) ADVANCED OPERATING SYSTEMS LAB (2) Agriculture and Technology (1) Analag and Digital Communication (1) Android (1) Applet (1) ARTIFICIAL INTELLIGENCE (3) aspiration 2020 (3) assignment cse (12) AT (1) AT - key (1) Attacker World (6) Basic Electrical Engineering (1) C (1) C Aptitude (20) C Program (87) C# AND .NET FRAMEWORK (11) C++ (1) Calculator (1) Chemistry (1) Cloud Computing Lab (1) Compiler Design (8) Computer Graphics Lab (31) COMPUTER GRAPHICS LABORATORY (1) COMPUTER GRAPHICS Theory (1) COMPUTER NETWORKS (3) computer organisation and architecture (1) Course Plan (2) Cricket (1) cryptography and network security (3) CS 810 (2) cse syllabus (29) Cyberoam (1) Data Mining Techniques (5) Data structures (3) DATA WAREHOUSING AND DATA MINING (4) DATABASE MANAGEMENT SYSTEMS (8) DBMS Lab (11) Design and Analysis Algorithm CS 41 (1) Design and Management of Computer Networks (2) Development in Transportation (1) Digital Principles and System Design (1) Digital Signal Processing (15) DISCRETE MATHEMATICS (1) dos box (1) Download (1) ebooks (11) electronic circuits and electron devices (1) Embedded Software Development (4) Embedded systems lab (4) Embedded systems theory (1) Engineer Portal (1) ENGINEERING ECONOMICS AND FINANCIAL ACCOUNTING (5) ENGINEERING PHYSICS (1) english lab (7) Entertainment (1) Facebook (2) fact (31) FUNDAMENTALS OF COMPUTING AND PROGRAMMING (3) Gate (3) General (3) gitlab (1) Global warming (1) GRAPH THEORY (1) Grid Computing (11) hacking (4) HIGH SPEED NETWORKS (1) Horizon (1) III year (1) INFORMATION SECURITY (1) Installation (1) INTELLECTUAL PROPERTY RIGHTS (IPR) (1) Internal Test (13) internet programming lab (20) IPL (1) Java (38) java lab (1) Java Programs (28) jdbc (1) jsp (1) KNOWLEDGE MANAGEMENT (1) lab syllabus (4) MATHEMATICS (3) Mechanical Engineering (1) Microprocessor and Microcontroller (1) Microprocessor and Microcontroller lab (11) migration (1) Mini Projects (1) MOBILE AND PERVASIVE COMPUTING (15) MOBILE COMPUTING (1) Multicore Architecute (1) MULTICORE PROGRAMMING (2) Multiprocessor Programming (2) NANOTECHNOLOGY (1) NATURAL LANGUAGE PROCESSING (1) NETWORK PROGRAMMING AND MANAGEMENT (1) NETWORKPROGNMGMNT (1) networks lab (16) News (14) Nova (1) NUMERICAL METHODS (2) Object Oriented Programming (1) ooad lab (6) ooad theory (9) OPEN SOURCE LAB (22) openGL (10) Openstack (1) Operating System CS45 (2) operating systems lab (20) other (4) parallel computing (1) parallel processing (1) PARALLEL PROGRAMMING (1) Parallel Programming Paradigms (4) Perl (1) Placement (3) Placement - Interview Questions (64) PRINCIPLES OF COMMUNICATION (1) PROBABILITY AND QUEUING THEORY (3) PROGRAMMING PARADIGMS (1) Python (3) Question Bank (1) question of the day (8) Question Paper (13) Question Paper and Answer Key (3) Railway Airport and Harbor (1) REAL TIME SYSTEMS (1) RESOURCE MANAGEMENT TECHNIQUES (1) results (3) semester 4 (5) semester 5 (1) Semester 6 (5) SERVICE ORIENTED ARCHITECTURE (1) Skill Test (1) software (1) Software Engineering (4) SOFTWARE TESTING (1) Structural Analysis (1) syllabus (34) SYSTEM SOFTWARE (1) system software lab (2) SYSTEMS MODELING AND SIMULATION (1) Tansat (2) Tansat 2011 (1) Tansat 2013 (1) TCP/IP DESIGN AND IMPLEMENTATION (1) TECHNICAL ENGLISH (7) Technology and National Security (1) Theory of Computation (3) Thought for the Day (1) Timetable (4) tips (4) Topic Notes (7) tot (1) TOTAL QUALITY MANAGEMENT (4) tutorial (8) Ubuntu LTS 12.04 (1) Unit Wise Notes (1) University Question Paper (1) UNIX INTERNALS (1) UNIX Lab (21) USER INTERFACE DESIGN (3) VIDEO TUTORIALS (1) Virtual Instrumentation Lab (1) Visual Programming (2) Web Technology (11) WIRELESS NETWORKS (1)

LinkWithin