Introduction :
A scientific calculator is an essential and important tool for students, engineers, scientists and anyone dealing with complex mathematical calculations. Using C or C++ graphics programming language, you can develop a program of scientific calculator that enables users to calculate their mathematical problems on a graphics screen. To create a scientific calculator using C or C++ graphics programming language, you need to require handling graphics using graphics.h library of Turbo C++ IDE and give input and button press using mouse pointer or cursor.
By utilizing the event handling mechanism of mouse pointer, you have to capture mouse clicks performed by the user. This will allow users to interact with the application for updating the display area in real-time. In this article, I shall explore you how to create a scientific calculator using C or C++ graphics programming language. Using graphics.h library of Turbo C++ IDE and math.h library of C programming language, you can develop a scientific calculator (graphical user interface) that allows users to interact with the calculator and perform various mathematical calculations. Here, I create a scientific calculator program, using C or C++ graphics programming language with source code.
What is scientific calculator :
A scientific calculator is a type of calculator designed to perform complex mathematical calculations and scientific functions, performing data analysis, and simplifying lengthy calculations. It is a more advanced version of a basic calculator. It is specifically used by students, professionals, scientists, engineers and researchers.
Scientific calculators typically come with a wide range of features such as arithmetic operations (addition, subtraction, multiplication and division), scientific operations (sine, cosine, tangent, logarithms, exponentials, square roots and more), statistical operations (mean, standard deviation, regression analysis and probability distributions), complex numbers operations, matrix operations etc.
About the program :
In the program of scientific calculator, there are some button with different value or text such as 0 to 9 numbers, basic arithmetic, trigonometric functions etc. Here, I implement methods for user input from the graphical interface with mouse pointer interactions. Using the mouse pointer user can input numbers, selecting functions and display the output of the calculated result on the graphical interface.
After run the program on your pc, you can see a scientific calculator in graphics mode. Now, you can calculate mathematical problems as your need. To calculate arithmetic operations (addition, subtraction, multiplication and division), input any number using mouse click, then click “+”, “-“, “x” or “/” button as you like. Now enter another number. At last, click “=” button for see the output of calculated results on the display area.
number >> “+”, “-“, “x” or “/” >> number >> “=”
To calculate scientific operations (sine, cosine, tangent), input any number then click “sin”, “cos” or “tan” button. Now you can see the result on the display area.
number >> “sin”, “cos” or “tan”
Remember that, the number you input is in degree. If you input number in radian you have to click “rad” before click on “sin”, “cos”, “tan” button.
number >> “rad” >> “sin”, “cos” or “tan”
To calculate logarithms and square, input any number then click “log” or “x2“. You can see the result.
number >> “log” or “x2“
There are another text (“sin-1“, “cos-1“, “tan-1“,”in” and “x3“) in the above button. To get these results just clicks “shift” button before “sin-1“, “cos-1“, “tan-1“,”in” and “x3” button.
number >> “shift” >> “sin-1“, “cos-1“, “tan-1“,”in” or “x3“
Note that, for sin-1” and “cos-1 input number will be fractional.
For radian number input number then “rad” then “shift” then “sin-1“, “cos-1” or “tan-1“
number >> “rad” >> “shift” >> “sin-1“, “cos-1” or “tan-1“
If you click on “AC” button, the calculator will be reset. To stop the program of calculator, click on “stop” button. To calculate the percentage of a number first click on any number then click on “%” and click on “=” button.
number >> “%” >> “=”
To calculate the factorial of a number, first input any number then click on “x!” button and you will see the result in the display area.
number >> “x!”
To calculate the square root of a number first click on any number then click on “√” and click on “=” button. After that you see the result in the display area.
number >> “√” >> “=”
To calculate the exponent of a number first click on any number then click on “^” then click on another number and click on “=” button. After that you see the result in the display area.
number >> “^” >> number >> “=”
The “π” is a constant value which is used in various math problems. If you want to input negative number, just click on number and click on “+/-” button.
number >> “+/-“
There are three empty boxes or button on the calculator. You can customize these buttons as you like. You can add more mathematical operations in these buttons.
Explanation of the program :
At the beginning of the program, first include the necessary header files such as graphics.h, stdio.h, conio.h, dos.h and math.h. The graphics.h library is used for Initialize the graphics mode and graphical functions. The stdio.h library is used for printf() function. The conio.h library is used for gotoxy() function. To handle the mouse pointer, you have to include the dos.h library. The union type of REGS variables, delay() and int86() functions are defined in dos.h library. For use mathematical function, you have to include math.h library.
After that in the main() function, declare union type of REGS variables regs_in and regs_out. Then, declare the necessary variables for the scientific calculator program. Using initgraph() function initializes the graphics mode where you can draw the calculator interface. Now, initialize the mouse pointer to set ax to 0 and call the int86() function and set the mouse pointer to set ax to 4. Next, restrict the mouse pointer for calculator using ax to 7 and 8. Finally, display the mouse pointer using ax to 1. You have to know how to use mouse pointer in C or C++ programming language.
The setbkcolor() is used to set the background color of the graphics window and setcolor() for color of different shapes of the calculator. To create various styles and color of the different shapes, you can use setfillstyle() function. Next, you can create whole calculator, display area and buttons or boxes using bar() function. The settextstyle() and outtextxy() use to write text in the buttons or boxes of the calculator. Next, run two while loop in the program. In the inner while loop, get the position and status of mouse pointer set ax to 3.
Now according to the position and status (click) of mouse pointer initialize the value of different variables in “if” statements. After that, according to different values of various variables display numbers, mathematical sign and calculated results in the display area using printf() function. At last, close the graphics window using closegraph() function.
How run the program :
If you have not installed Turbo C++ IDE on your pc, first install it. After that, open the Turbo C++ IDE and create a C or C++ file with .c or .cpp extension. Now, copy the below source code of scientific calculator program by C or C++ graphics programming language and paste in your C or C++ file in the Turbo C++ IDE. If you do not know how to copy paste in the Turbo C++ IDE, see my other post. You have to also know how to use graphics.h in Turbo C++ IDE.
Source code :
The following code is the source code of a scientific calculator program using the C or C++ graphics programming language.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int main(void)
{
union REGS regs_in, regs_out;
int x = 100, y = 100, i = 0, j = 0, k = 0, l = 0, n = 0, click = 0;
int c = 1, g = 0, r = 0, u = 0, v = 0, w = 0, b = 0, d = 0, p[10];
double s = 0, s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0, s6 = 0;
int a = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0;
int a7 = 0, a8 = 0, a9 = 0, a10 = 0;
unsigned long int t1 = 1, t = 0, tt = 0;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
regs_in.x.ax = 0;
int86(0x33, ®s_in, ®s_out);
regs_in.x.ax = 4;
regs_in.x.cx = 390;
regs_in.x.dx = 455;
int86(0x33, ®s_in, ®s_out);
regs_in.x.ax = 7;
regs_in.x.cx = 100;
regs_in.x.dx = 390;
int86(0x33, ®s_in, ®s_out);
regs_in.x.ax = 8;
regs_in.x.cx = 100;
regs_in.x.dx = 455;
int86(0x33, ®s_in, ®s_out);
regs_in.x.ax = 1;
int86(0x33, ®s_in, ®s_out);
setbkcolor(2);
setcolor(4);
setfillstyle(1, 4);
bar(100, 100, 390, 455);
setfillstyle(1, 14);
bar(110, 105, 380, 140);
setfillstyle(1, 1);
for (i = 0; i <= 1; i++)
{
for (j = 0; j <= 4; j++)
{
bar(110 + k, 155 + l, 160 + k, 205 + l);
k += 55;
if (j == 4)
k = 0;
}
l += 55;
}
k = 0;
l = 0;
setfillstyle(1, 1);
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 5; j++)
{
bar(110 + k, 270 + l, 150 + k, 310 + l);
k += 45;
if (j == 5)
k = 0;
}
l += 45;
}
setcolor(15);
settextstyle(1, 0, 1);
outtextxy(115, 152, "sin");
setcolor(2);
outtextxy(113, 180, "sin");
outtextxy(140, 178, "-1");
setcolor(15);
outtextxy(170, 152, "cos");
setcolor(2);
outtextxy(168, 180, "cos");
outtextxy(195, 178, "-1");
setcolor(15);
outtextxy(230, 152, "tan");
setcolor(2);
outtextxy(222, 180, "tan");
outtextxy(250, 178, "-1");
setcolor(15);
outtextxy(285, 152, "log");
setcolor(2);
outtextxy(285, 180, "in");
setcolor(15);
outtextxy(340, 156, "x");
outtextxy(353, 150, "2");
setcolor(2);
outtextxy(340, 180, "x");
outtextxy(353, 175, "3");
setcolor(15);
settextstyle(1, 0, 3);
// outtextxy(115, 152 + 60, "");
outtextxy(170, 152 + 60, "rad");
settextstyle(1, 0, 2);
outtextxy(223, 155 + 58, "shift");
outtextxy(283, 155 + 58, "AC");
outtextxy(333, 155 + 55, "stop");
setcolor(15);
settextstyle(1, 0, 4);
outtextxy(120, 270, "7");
outtextxy(165, 270, "8");
outtextxy(210, 270, "9");
outtextxy(255, 270, "*");
outtextxy(300, 270, "/");
outtextxy(345, 270, "%");
outtextxy(120, 270 + 45, "4");
outtextxy(165, 270 + 45, "5");
outtextxy(210, 270 + 45, "6");
outtextxy(255, 270 + 45, "+");
outtextxy(300, 270 + 45, "-");
outtextxy(345, 270 + 45, "=");
outtextxy(120, 270 + 90, "1");
outtextxy(165, 270 + 90, "2");
outtextxy(210, 270 + 90, "3");
outtextxy(255, 270 + 90, "x!");
// outtextxy(300, 270 + 90, "");
settextstyle(1, 0, 3);
outtextxy(345, 270 + 90, "û");
settextstyle(1, 0, 4);
outtextxy(120, 270 + 135, "0");
outtextxy(165, 270 + 135, ".");
outtextxy(210, 270 + 135, "^");
outtextxy(255, 270 + 135, "ã");
settextstyle(1, 0, 1);
outtextxy(292, 270 + 140, "+/-");
// outtextxy(335, 270 + 140, "");
in:
i = 0;
while (1)
{
while (1)
{
regs_in.x.ax = 3;
int86(0x33, ®s_in, ®s_out);
click = regs_out.x.bx;
x = regs_out.x.cx;
y = regs_out.x.dx;
/*sin*/
if ((x >= 110 && x <= 160) && (y >= 155 && y <= 205) && click == 1)
{
g = 1;
i = 6;
r = 1;
v = 10;
a4 = 1;
a7 = 1;
break;
}
/*cos*/
if ((x >= 165 && x <= 215) && (y >= 155 && y <= 205) && click == 1)
{
g = 1;
i = 6;
r = 1;
v = 11;
a4 = 1;
a7 = 1;
break;
}
/*tan*/
if ((x >= 220 && x <= 270) && (y >= 155 && y <= 205) && click == 1)
{
g = 1;
i = 6;
r = 1;
v = 12;
a4 = 1;
a7 = 1;
break;
}
/*log*/
if ((x >= 275 && x <= 325) && (y >= 155 && y <= 205) && click == 1)
{
g = 1;
i = 6;
r = 1;
v = 9;
a4 = 1;
a7 = 1;
break;
}
/* pow*/
if ((x >= 330 && x <= 380) && (y >= 155 && y <= 205) && click == 1)
{
r = 1;
v = 8;
u = 13;
a6 = 0;
break;
}
/**/
if ((x >= 110 && x <= 160) && (y >= 210 && y <= 260) && click == 1)
{
break;
}
/*rad*/
if ((x >= 165 && x <= 215) && (y >= 210 && y <= 260) && click == 1)
{
g = 1;
r = 1;
a1 = 1;
break;
}
/*shift*/
if ((x >= 220 && x <= 270) && (y >= 210 && y <= 260) && click == 1)
{
r = 1;
a = 1;
a6 = 1;
a8 = 1;
break;
}
/*ac*/
if ((x >= 275 && x <= 325) && (y >= 210 && y <= 260) && click == 1)
{
r = 1;
u = 10;
break;
}
/*stop*/
if ((x >= 330 && x <= 380) && (y >= 210 && y <= 260) && click == 1)
{
goto out;
}
/*7*/
if ((x >= 110 && x <= 150) && (y >= 270 && y <= 310) && a3 == 0 && click == 1)
{
n = 7;
r = 1;
a2 = 1;
break;
}
/*8*/
if ((x >= 155 && x <= 195) && (y >= 270 && y <= 310) && a3 == 0 && click == 1)
{
n = 8;
r = 1;
a2 = 1;
break;
}
/*9*/
if ((x >= 200 && x <= 240) && (y >= 270 && y <= 310) && a3 == 0 && click == 1)
{
n = 9;
r = 1;
a2 = 1;
break;
}
/* * */
if ((x >= 245 && x <= 285) && (y >= 270 && y <= 310) && click == 1)
{
u = 3;
r = 5;
break;
}
/* / */
if ((x >= 290 && x <= 330) && (y >= 270 && y <= 310) && click == 1)
{
u = 4;
r = 6;
break;
}
/* % */
if ((x >= 335 && x <= 375) && (y >= 270 && y <= 310) && click == 1)
{
u = 5;
v = 1;
r = 7;
a4 = 1;
break;
}
/*4*/
if ((x >= 110 && x <= 150) && (y >= 315 && y <= 355) && a3 == 0 && click == 1)
{
n = 4;
r = 1;
a2 = 1;
break;
}
/*5*/
if ((x >= 155 && x <= 195) && (y >= 315 && y <= 355) && a3 == 0 && click == 1)
{
n = 5;
r = 1;
a2 = 1;
break;
}
/*6*/
if ((x >= 200 && x <= 240) && (y >= 315 && y <= 355) && a3 == 0 && click == 1)
{
n = 6;
r = 1;
a2 = 1;
break;
}
/* + */
if ((x >= 245 && x <= 285) && (y >= 315 && y <= 355) && click == 1)
{
u = 1;
r = 3;
break;
}
/* - */
if ((x >= 290 && x <= 330) && (y >= 315 && y <= 355) && click == 1)
{
u = 2;
r = 4;
break;
}
/* = */
if ((x >= 335 && x <= 375) && (y >= 315 && y <= 355) && click == 1)
{
r = 2;
a3 = 1;
a5 = 1;
break;
}
/*1*/
if ((x >= 110 && x <= 150) && (y >= 360 && y <= 400) && a3 == 0 && click == 1)
{
n = 1;
r = 1;
a2 = 1;
break;
}
/*2*/
if ((x >= 155 && x <= 195) && (y >= 360 && y <= 400) && a3 == 0 && click == 1)
{
n = 2;
r = 1;
a2 = 1;
break;
}
/*3*/
if ((x >= 200 && x <= 240) && (y >= 360 && y <= 400) && a3 == 0 && click == 1)
{
n = 3;
r = 1;
a2 = 1;
break;
}
/* x! */
if ((x >= 245 && x <= 285) && (y >= 360 && y <= 400) && click == 1)
{
r = 1;
u = 9;
v = 5;
a4 = 1;
break;
}
/* */
if ((x >= 290 && x <= 330) && (y >= 360 && y <= 400) && click == 1)
{
break;
}
/* sqrt */
if ((x >= 335 && x <= 375) && (y >= 360 && y <= 400) && click == 1)
{
r = 13;
u = 12;
a4 = 1;
break;
}
/*0*/
if ((x >= 110 && x <= 150) && (y >= 405 && y <= 445) && a3 == 0 && click == 1)
{
n = 0;
r = 1;
a2 = 1;
break;
}
/* . */
if ((x >= 155 && x <= 195) && (y >= 405 && y <= 445) && a3 == 0 && click == 1)
{
n = 0;
r = 1;
g = 1;
i = 0;
a2 = 1;
a4 = 1;
break;
}
/* ^ */
if ((x >= 200 && x <= 240) && (y >= 405 && y <= 445) && click == 1)
{
r = 10;
u = 8;
v = 4;
break;
}
/* pai */
if ((x >= 245 && x <= 285) && (y >= 405 && y <= 445) && click == 1)
{
r = 1;
v = 2;
a2 = 1;
a4 = 1;
break;
}
/* +/- */
if ((x >= 290 && x <= 330) && (y >= 405 && y <= 445) && click == 1)
{
r = 1;
v = 3;
a9 = 1;
break;
}
/* */
if ((x >= 335 && x <= 375) && (y >= 405 && y <= 445) && click == 1)
{
break;
}
}
if (u == 10)
{
setfillstyle(1, 14);
bar(110, 105, 380, 140);
k = 0;
l = 0;
v = 0;
n = 0;
a = 0;
a1 = 0;
a2 = 0;
a3 = 0;
a4 = 0;
a5 = 0;
a6 = 0;
a8 = 0;
a9 = 0;
a10 = 0;
g = 0;
r = 0;
t = 0;
u = 0;
w = 0;
s3 = 0;
s4 = 0;
s5 = 0;
s6 = 0;
b = 0;
c = 0;
s = 0;
s1 = 0;
s2 = 0;
goto in;
}
if (r > 0)
{
p[i] = n;
i++;
b++;
delay(200);
n = 0;
if (r == 2)
{
v = 0;
if (u == 1)
{
c = 0;
g = 0;
w = 1;
s3 = s2 + s1;
}
if (u == 2)
{
c = 0;
g = 0;
w = 1;
s3 = s2 - s1;
}
if (u == 3)
{
c = 0;
g = 0;
w = 1;
s3 = s2 * s1;
}
if (u == 4)
{
c = 0;
g = 0;
w = 1;
s3 = s2 / s1;
}
if (u == 5)
{
c = 0;
g = 0;
w = 1;
s3 = s2 / 100;
}
if (u == 8 && a2 == 1)
{
c = 0;
g = 0;
w = 1;
s3 = pow(s2, s1);
}
if (u == 12)
{
c = 0;
g = 0;
w = 1;
s3 = sqrt(s2);
}
s5 = s3;
t = (int)s3;
if (t == s3)
{
setfillstyle(1, 14);
bar(110, 105, 380, 140);
gotoxy(25, 8);
printf("%0.0f", s3);
s5 = s3;
}
else
{
s5 = s3;
s4 = s3;
setfillstyle(1, 14);
bar(110, 105, 380, 140);
c = 0;
while (1)
{
s4 = s4 * 10;
d = (int)s4;
if (d % 10 == 0)
break;
c++;
}
if (c == 1)
{
gotoxy(25, 8);
printf("%0.1f", s3);
}
else if (c == 2)
{
gotoxy(25, 8);
printf("%0.2f", s3);
}
else if (c == 3)
{
gotoxy(25, 8);
printf("%0.3f", s3);
}
else if (c == 4)
{
gotoxy(25, 8);
printf("%0.4f", s3);
}
else if (c == 5)
{
gotoxy(25, 8);
printf("%0.5f", s3);
}
else if (c == 6)
{
gotoxy(25, 8);
printf("%0.6f", s3);
}
else
{
gotoxy(25, 8);
printf("%f", s3);
}
}
}
if (r == 1)
{
if (v == 2)
{
g = 1;
i = 7;
s1 = 22.0 / 7;
}
if (g > 0)
{
for (j = 0; j <= i - 1; j++)
{
s = s1 + (float)p[j] / pow(10, i - 1);
}
if (a == 1 && a1 == 0)
{
i = i - 1;
b = b - 1;
}
if (a1 == 1 && a == 0)
{
i = i - 1;
b = b - 1;
}
if (a1 == 1 && a == 1)
{
i = i - 1;
b = b - 1;
}
if (a3 == 1)
{
s = s3;
setfillstyle(1, 14);
bar(110, 105, 380, 140);
}
if (v == 8 && a == 1 && a2 == 1)
{
s = pow(s, 3);
i = i + 5;
}
if (v == 8 && a == 0 && a2 == 1)
{
s = pow(s, 2);
i = i + 6;
}
if (v == 9 && a == 0 && a2 == 1)
{
s = log10(s);
}
if (v == 9 && a == 1 && a2 == 1)
{
s = log(s);
}
if (v == 10 && a == 0 && a1 == 0)
{
s = sin(s * 3.142857 / 180);
}
if (v == 10 && a == 0 && a1 == 1)
{
s = sin(s);
}
if (v == 11 && a == 0 && a1 == 0)
{
s = cos(s * 3.142857 / 180);
}
if (v == 11 && a == 0 && a1 == 1)
{
s = cos(s);
}
if (v == 12 && a == 0 && a1 == 0)
{
s = tan(s * 3.142857 / 180);
}
if (v == 12 && a == 0 && a1 == 1)
{
s = tan(s);
}
if (v == 10 && a == 1 && a1 == 0)
{
s = asin(s);
s = s * 180 / 3.142857;
}
if (v == 10 && a == 1 && a1 == 1)
{
s = asin(s);
}
if (v == 11 && a == 1 && a1 == 0)
{
s = acos(s);
s = s * 180 / 3.142857;
}
if (v == 11 && a == 1 && a1 == 1)
{
s = acos(s);
}
if (v == 12 && a == 1 && a1 == 0)
{
s = atan(s);
s = s * 180 / 3.142857;
}
if (v == 12 && a == 1 && a1 == 1)
{
s = atan(s);
}
a3 = 0;
s3 = s;
if (v == 3)
{
s = s * (-1);
i = i - 1;
}
if (a2 == 1 && a3 == 0)
{
if (i == 1)
{
gotoxy(40 - b, 8);
printf("%0.0f.", s);
}
else if (i == 2)
{
gotoxy(40 - b, 8);
printf("%0.1f", s);
}
else if (i == 3)
{
gotoxy(40 - b, 8);
printf("%0.2f", s);
}
else if (i == 4)
{
gotoxy(40 - b, 8);
printf("%0.3f", s);
}
else if (i == 5)
{
gotoxy(40 - b, 8);
printf("%0.4f", s);
}
else if (i == 6)
{
gotoxy(40 - b, 8);
printf("%0.5f", s);
}
else
{
gotoxy(40 - b, 8);
printf("%f", s);
}
}
}
else
{
for (j = 0; j <= i - 1; j++)
{
s = s * 10 + (float)p[j];
}
if (a == 1)
{
s = s / 10;
b = b - 1;
}
if (v == 8 && a == 1)
{
if (a5 == 1)
{
s = s5 * 10;
a5 = 0;
setfillstyle(1, 14);
bar(110, 105, 380, 140);
}
s = pow(s, 3) / 1000;
s6 = s;
s3 = s;
}
if (v == 8 && a == 0)
{
if (a5 == 1)
{
s = s5 * 10;
a5 = 0;
setfillstyle(1, 14);
bar(110, 105, 380, 140);
}
s = pow(s, 2) / 100;
s6 = s;
s3 = s;
b = b - 1;
}
if (v == 3)
{
s = s * (-1) / 10;
}
if (v == 5)
{
if (a3 == 1)
{
a10 = 1;
s = s5 * 10;
setfillstyle(1, 14);
bar(110, 105, 380, 140);
}
t = (int)s;
if (t == s)
{
t = t / 10;
while (t > 1)
{
t1 = t1 * t;
t--;
}
s = (float)t1;
t1 = 1;
b = b - 1;
s3 = s;
}
}
if (a7 == 1 && a8 == 1)
{
a2 = 1;
a6 = 0;
a7 = 0;
a8 = 0;
}
if (a2 == 1 && a6 == 0)
{
tt = (int)s;
if (a3 == 1 && tt != s)
{
if (a10 == 1)
{
s = s3;
a10 = 0;
gotoxy(40 - b, 8);
printf("%0.0lf", s);
}
else
{
s = s6 * 1.0;
gotoxy(40 - b, 8);
printf("%lf", s);
}
a3 = 0;
s3 = s;
}
else
{
gotoxy(40 - b, 8);
printf("%0.0lf", s);
}
}
}
}
if (r > 2)
{
setfillstyle(1, 14);
bar(110, 105, 380, 140);
for (i = 0; i <= 9; i++)
p[i] = 0;
v = 0;
a = 0;
a1 = 0;
if (w == 1)
s2 = s3;
else
s2 = s1;
s4 = s2;
i = 0;
b = 0;
s = 0;
if (a9 == 1)
{
a2 = 1;
a3 = 0;
}
t = (int)s2;
if (t == s2 && a2 == 1)
{
gotoxy(15, 8);
printf("%0.0f", s2);
}
else
{
c = 0;
while (1)
{
s4 = s4 * 10;
d = (int)s4;
if (d % 10 == 0)
break;
c++;
}
if (a4 == 1)
a3 = 0;
if (a2 == 1 && a3 == 0)
{
if (c == 1)
{
gotoxy(15, 8);
printf("%0.1f", s2);
}
else if (c == 2)
{
gotoxy(15, 8);
printf("%0.2f", s2);
}
else if (c == 3)
{
gotoxy(15, 8);
printf("%0.3f", s2);
}
else if (c == 4)
{
gotoxy(15, 8);
printf("%0.4f", s2);
}
else if (c == 5)
{
gotoxy(15, 8);
printf("%0.5f", s2);
}
else if (c == 6)
{
gotoxy(15, 8);
printf("%0.6f", s2);
}
else
{
gotoxy(15, 8);
printf("%f", s2);
}
}
}
if (a2 == 1)
{
if (r == 3)
{
c = 0;
g = 0;
a3 = 0;
gotoxy(27, 8);
printf("+");
}
if (r == 4)
{
c = 0;
g = 0;
a3 = 0;
gotoxy(27, 8);
printf("-");
}
if (r == 5)
{
c = 0;
g = 0;
a3 = 0;
gotoxy(27, 8);
printf("*");
}
if (r == 6)
{
c = 0;
g = 0;
a3 = 0;
gotoxy(27, 8);
printf("/");
}
if (r == 7)
{
c = 0;
g = 0;
a3 = 0;
gotoxy(27, 8);
printf("%");
}
if (r == 10)
{
c = 0;
g = 0;
a3 = 0;
gotoxy(27, 8);
printf("^");
}
if (r == 13)
{
c = 0;
g = 0;
a3 = 0;
gotoxy(27, 8);
printf("û");
}
}
}
r = 0;
}
s1 = s;
n = 0;
s = 0;
}
out:
closegraph();
return 0;
}
Output :
After running the above program successfully, you can calculate various math problem in the scientific calculator build by C or C++ graphics programming language. You can also see the output of scientific calculator program in my YouTube channel.
Conclusion :
At the end of the article, you have successfully learned how to create a scientific calculator using C or C++ graphics programming language. Throughout this article, I showed you how build a scientific calculator with graphical user interface (GUI) where user can input number by mouse pointer and calculate various mathematical tasks. In the program, there may be some error which you can solve. Thank you for visiting my website.