A Simple Calculator With Graphics And Mouse Operation In C

                                                    Simple Calculator

We make a calculator using graphics and mouse operation in c language. In this calculator we can add, subtraction, multiplication and division between two numbers. After resulting ,we can again add , subtract, multiply, divide with the former result.

#include<stdio.h>

#include<conio.h>

#include<dos.h>

#include<graphics.h>

 

Here, we use “#include<graphics.h>” header files for

draw different shapes, display text in different fonts, change  color  and many more. Using functions of graphics.h  in Turbo C compiler for making our calculator with a particular design “#include<dos.h>”contains functions for handling interrupts, producing sound, date and time functions etc. It is Borland specific and works in Turbo C compiler.

 

We can use some functions to making this calculator,the functions are given below:

 

  • int initmousedetect();
  • void showpointer();
  • void openwindow();
  • void closewindow();

 

  • void hidepointer();
  • void getmouseposition(int *,int*,int*);
  • void restrictpointer(int,int,int,int);

 

 

 

How it works:

Firstly when we press any key button, the getmouseposition() takes three arguments. Here contains button , x co-ordinate  and y co-ordinates. In this function we get the position of the pressing key. Then it goes to

 

“if((x>=c && x<=c+40)&&(y>=d&&y<=d+40))”.

                 here  c=240;   d=100;

After  checking this operation it goes  to

“ if((button&1)==1)” 

this operation, after confirming it, the array index

“Char input=inp[a][b]”

“inp[a][b]” it contains the character into char input.

The character input now goes to switch case function, after completing the case it operates the operation between the numbers and print the result

 

How Button works:   (The given code is hereJ)

For (a=0; a<4 ; a++) {

c=240;

d=100;

for (b=0 ; b<4;b++)

{

if((x>=c&&x<=c+40) && (y>=d&&y<=d+40))

{

if((button&1) ==1)

{

While ((button&1 )==1)

{

Setcolor(WHITE);

rectangle (c ,d,c+40,d+40);

rectangle (c-1,d-1,c+41,d+41);

rectangle (c-2, d-2, c+42 ,d+42);

break;

}

delay(100);

setcolor(WHITE);

char    input=inp[a][b];

Then input contains digit from inp[a][b],,Then this input goes to switch case operation,After matching it,it contains operation the following instruction.

How can we use this calculator:

After compiling and running this code ,new window is opening here shows the welcome page, then we have to press any key to go ahead, then it goes to our calculator, here we have to input any digit by clicking on it by mouse. After that we have to press any operation then we have to click on other digit then we have to press “=”.After that we will get result, Not only that, we can also operates the result with the new digit or number.

we can add, subtract, multiply, divide on the user defined given number. After that we have to press any key, then it goes to our last page, here contains thank you and references.

To do mouse programming we use “include <dos.h>”. We use a function called int86() to access     ” interrupts”.

we use “int   initmousedetect()” for  detecting whether mouse click or not. Here we use “void  openwindow()” to open a new window, “void  closewindow()” for closing window, for get mouse position we use “void getmouseposition (int *,int*,int*)” this function ,for taking a particular point in mouse we use “void restrictpointer(int,int,int,int)”.We now restrict the mouse in particular rectangle .

We create a function called restrict which takes

four parameters, two Cartesian points each containing one

x coordinate and one y coordinate.

First point mentions the top of the rectangle while second point mention the bottom point of rectangle.

char input;

char*inpu[4][4]={“1″,”2″,”3″,”4″,”5″,”6″,”7″,”8”,     “9”,”0″,”+”,”-” ,”*”,”/”,”clr”,”=”};

Here we input characters ,that will a part of our calculator, by input variable.

“initgraph (&driver,&mode,”C:\\TC\\BGI“)” ,we use this   for   graphics operation of making shapes of calculator.

 

We use “union REGS in, out” .
REGS is the union of two structures. The union REGS has been defined dos.h and it is used to pass information to and from the functions, int86, int86x, intdos and intdosx.

“long double addnum(int);

char opr;”

Here we use addnum function to take  many digits at a  same time ,which contains call that:

long double addnum(int getnum)

{

num = num*10+getnum;

return(num);

};

when we take operation from keyboard ,it then in switch case,after matching operation type it operates the digit.At the last ,the result will go to printf, which type is long double.

For making calculator ,we use some graphics functions such as outtextxy(),setcolor(),bar(),rectangle(),gotoxy().For clear  screen device we are using cleardevice();

  • initwindow(): This function initializes the graphics system by opening a graphics window of the specified size. The first two parameters (width and height) are required but all other parameters have default values.

bar(): This function is used to draw a two dimensional rectangular filled bar which takes four parameters as the co-ordinates of the bar. First two arguments are the starting point and last two arguments are the ending point.

  • line(): This function is used to draw a line which takes four parameters as the co-ordinates of the line. First two arguments are the starting point and last two arguments are the ending point.
  • setcolor(): This function is used to set the current drawing color to the new color.
  • outtextxy(): This function takes three parameters. First two are the co-ordinates and the third parameter is the string or the text we want to display.
  • settextstyle(): This function is used to change the way in which text appears. It takes three parameters. First one is font style, second one is direction and third one is text size.

“while(!kbhit()) “:  kbhit function is used to determine if a key has been pressed or not. To use kbhit function in our program we should include the header file “conio.h”. If a key has been pressed then it returns a non zero value otherwise returns zero.

After completing switch case operation, it goes to printf() and it prints the result in our graphical calculator screen.At the last part of code ,we print “THANK YOU” by outtextxy() graphics function.

 

Firstly we open a window with welcom page:

https://drive.google.com/file/d/1U0lw2iUjEWr4W0uJvpFNwJNbXJIn2FpU/view?usp=sharing

Then it goes to our calculator:

https://drive.google.com/file/d/1vFBzwUAI73_1sRC2zyPT1lQFFa2Aiisi/view?usp=sharing

Ending point of our project :

https://drive.google.com/file/d/1C-UwYcerLzCdLmp9puzLRPvHoQwtMomk/view?usp=sharing

Here you can download our project:

https://github.com/sayeediku/CALCULATOR_PROJECT/blob/master/CALCULATOR_PROJECT.cpp

 

 Reference:

For our mouse operation we took help from:

“www botskool.com”“https://www.dreamincode.net/forums/topic/150609-graphics-calculator/?fbclid=IwAR1y-jijSwqxsjNuzDGNlf9aF1BmJtQ9xzkDflnfCZXgJGMMVwOXgOkuBP8

 

Written by:   

                     

MD.Sifatur Rahman(180206)

MD.SohanurRahman(180218)

MD.Sayeedi Mottakin(180227)

MD.Asif Anam(180228)

Thank you