Monday, May 16, 2011

Graphical Calculator Design

/*
Created By Rana Hamid
http://uvacode.blogspot.com/
+8801717623876
http://facebook.com/ranahamid07/
*/

//Graphical Calculator Design

#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
class Window;
Window * const SCREEN=(Window *)0;
enum buttonstatus{unpushed,pushed};
enum boolean{false,true};

class Window
{
 protected:
   Window * ptr;
   int left,right,top,bot;
   int delta,deltacolor,centercolor;
 public:Window(){}
   Window(Window *ptro,int l,int t,int r,int b,int dc,int cc);
   void Display();
};

class Border:public Window
{
 public:
   Border(Window *ptro,int l,int t,int r,int b,int dc=DARKGRAY,int cc=BLUE)
      :Window(ptro,l,t,r,b,dc,cc){}
};

class Button:public Window
{
   char text[20];
   buttonstatus bs;
 public:
   Button(Window *ptro,int l,int t,int r,int b,int cc=BLACK,char *tx="")
      :Window(ptro,l,t,r,b,BLACK,cc)
   {strcpy(text,tx);bs=unpushed;}
   void Click();
   void Display();
};

class Output:public Window
{
   char text[80];
 public:
   Output(Window *ptro,int l,int t,int r,int b,int dc=BLUE,int cc=WHITE)
      :Window(ptro,l,t,r,b,dc,cc){}
   void Text(char *ch);
   void Number(double d);
};

Window::Window(Window* ptro,int l,int t,int r,int b,int dc,int cc)
{
 ptr=ptro;left=l;right=r;top=t;bot=b;deltacolor=dc;centercolor=cc;
 delta=(-r-l-b-t)/150+3;
 if(ptr!=SCREEN)
 {
  left+=ptr->left;
  right+=ptr->right;
  top+=ptr->top;
  bot+=ptr->bot;
 }
}

void Window::Display()
{
 setcolor(WHITE);
 int p[10];
 p[0]=left;p[1]=top;p[2]=right;p[3]=top;p[4]=right;p[5]=bot;
 p[6]=left;p[7]=bot;p[8]=left;p[9]=top;
 setfillstyle(SOLID_FILL,deltacolor);
 fillpoly(5,p);
 rectangle(left+delta+1,top+delta,right-delta-1,bot-delta);
 setfillstyle(SOLID_FILL,centercolor);
 floodfill(left+(right-left)/2,top+delta+1,WHITE);
}

void Button::Click()
{
 bs=pushed;
 Button::Display();
 sound(500);delay(10);nosound();
 delay(250);
 bs=unpushed;
 Button::Display();
 sound(400);delay(10);nosound();
}

void Button::Display()
{
 Window::Display();
 moveto(left+(right-left)/2+1,top+(bot-top)/2);
 settextjustify(CENTER_TEXT,CENTER_TEXT);
 settextstyle(SANS_SERIF_FONT,HORIZ_DIR,USER_CHAR_SIZE);
 setusercharsize(5,8,2,3);
 setcolor(WHITE);
 outtext(text);
 moveto(left,top);
 lineto(left+delta,top+delta);
 moveto(right,top);
 lineto(right-delta,top+delta);
 moveto(left,bot);
 lineto(left+delta,bot-delta);
 moveto(right,bot);
 lineto(right-delta,bot-delta);
 setfillstyle(SOLID_FILL,LIGHTGRAY);
 if(bs==unpushed)
   {
    floodfill(left+(right-left)/2,top+1,WHITE);
    floodfill(left+1,top+(bot-top)/2,WHITE);
   }
 else
   {
    floodfill(left+(right-left)/2,bot-1,WHITE);
    floodfill(right-1,top+(bot-top)/2,WHITE);
   }
}

void Output::Text(char *ch)
{
 Display();
 moveto(right-delta,top+(bot-top)/2);
 settextjustify(RIGHT_TEXT,CENTER_TEXT);
 settextstyle(SANS_SERIF_FONT,HORIZ_DIR,USER_CHAR_SIZE);
 setusercharsize(5,8,1,1);
 setcolor(BLACK);
 outtext(ch);
}

void Output::Number(double d)
{
 char buffer[80];
 sprintf(buffer,"%16.2lf",d);
 Output::Text(buffer);
}

void main()
{
 int gd,gm;
 gd=EGA;
 gm=EGAHI;
 initgraph(&gd,&gm,"\tc\bgi");
 Border br1(SCREEN,240,30,480,330);
 Output o1(&br1,20,20,-20,-240,BLUE,WHITE);
 Button b0(&br1,30,230,-175,-35,BLACK,"0");
 Button b1(&br1,30,180,-175,-85,BLACK,"1");
 Button b2(&br1,78,180,-127,-85,BLACK,"2");
 Button b3(&br1,127,180,-78,-85,BLACK,"3");
 Button b4(&br1,30,130,-175,-135,BLACK,"4");
 Button b5(&br1,78,130,-127,-135,BLACK,"5");
 Button b6(&br1,127,130,-78,-135,BLACK,"6");
 Button b7(&br1,30,80,-175,-185,BLACK,"7");
 Button b8(&br1,78,80,-127,-185,BLACK,"8");
 Button b9(&br1,127,80,-78,-185,BLACK,"9");
 Button bdiv(&br1,175,80,-30,-185,BLUE,"/");
 Button bmul(&br1,175,130,-30,-135,BLUE,"*");
 Button bsub(&br1,175,180,-30,-85,BLUE,"-");
 Button bdot(&br1,78,230,-127,-35,BLACK,".");
 Button bclr(&br1,127,230,-78,-35,RED,"clr");
 Button badd(&br1,175,230,-30,-35,BLUE,"+=");
 br1.Display();o1.Display();
 b7.Display();b8.Display();b9.Display();
 b4.Display();b5.Display();b6.Display();
 b1.Display();b2.Display();b3.Display();
 b0.Display();bdot.Display();bclr.Display();
 bsub.Display();bdiv.Display();badd.Display();bmul.Display();
 o1.Number(0.0);
 const char ESC=27;
 char ds[80],tb[80],ch,oper;
 int num=0;
 boolean isfirst=true,chain=false;
 double num1,num2,ans;
 while((ch=getch())!=ESC)
      {
       if(ch>='0'&&ch<='9'||ch=='.')
    {
     switch(ch)
    {
     case '0':b0.Click();break;
     case '1':b1.Click();break;
     case '2':b2.Click();break;
     case '3':b3.Click();break;
     case '4':b4.Click();break;
     case '5':b5.Click();break;
     case '6':b6.Click();break;
     case '7':b7.Click();break;
     case '8':b8.Click();break;
     case '9':b9.Click();break;
     case '.':bdot.Click();break;
    }
}        }}