C++

Build a Calculator in C++

From the very beginning history of technology, we have known many machines that have been invented by scientists. One of the earliest computing machines was a calculator that was invented for the simple sum, add, multiply and divide calculations. That was a huge big machine compared to the current calculator gadget that is quite little and portable. But, different programming languages provide us with the opportunity to do more mathematical calculations like calculators within programs without a calculator. So, In this guide, we will be showing you the programs to create a calculator in the C++ platform of the Ubuntu 20.04 system.

Example 01:

So, let’s start the first example with the terminal shell opening using the shortcut key, i.e., “Ctrl+Alt+T”. You need to have a c++ file in which you will do your c++ coding. We are creating it within the shell console using the “touch” query of the Ubuntu 20.04 system. The file name is “calculator.cc”. This file can be accessed from the “home” directory and can be opened in any of the editors provided by Ubuntu 20.04. Therefore, we have used the “GNU Nano” editor of Ubuntu 20.04 that comes built-in in it. Both the commands are shown in the appended image.

The program is started from the “iostream” header file inclusion and standard “std” namespace. This program will only perform 4 basic calculator operations, i.e., sum, subtract, multiply, and divide. A main() function is started with the declaration of two float variables v1 and v2, and one character type variable, “sign”.The first “cout” clause asks the user to add the operator sign in the terminal and save it to the variable “sign” using the “cin” clause. The other cout statement asks the user to add two float values and save them to the variables v1 and v2 consecutively using the “cin” statement. We have utilized the “switch” statement to perform the calculator operations.

The “switch” statement uses the “sign” variable as a switch value. According to this “sign” variable value, the switch cases will get executed. If the user pressed “+” on asking for an operator, it will execute the “+” case and add the values of v1 and v2 variables and display them on the shell. If the user adds the “-“ sign upon asking, it will execute the “-“ case and subtract the v2 value from the v1 value. If the user adds the “*” sign, it will execute the “*” case and multiply v1 with v2. The “/” case will be executed if the user adds the “/” sign upon asking, and the variable v1 value will be divided by value v2. The default case will be executed if the user added anything other than the “+,-,/,*” sign upon asking the operator “sign”. This was all about the calculator creation in the C++ program. Let’s just save it with “Ctrl+S” and quit the editor to get back to the terminal.

It’s time to compile our newly created calculator code with c++ compiler. You have to make sure that your Ubuntu 20.04 system must have g++ configured on it using the “apt” package. Thus, we have been using the “g++” compiler command here to compile the file “calculator.cc”. This was successful, and we have executed the file with the “./a.out” query. The user has entered the “/” sign upon asking the operator. Then a user has been asked to add two float values, and the user has added “7.6” and “4”. The calculator program has calculated the division result and displayed it at the next consecutive line shown in the image below.

After running the file again, we have added the “+” operator and two float values. The program has calculated the sum of both the numbers shown below.

Again executing the file, we have added the “-“ sign and added two numbers. The first number is 3, and the second is 9. After the program has performed the subtraction “3-9”, it results in “-6”.

After executing the file again, the user added “*” and two float numbers. Both the numbers have been multiplied together using the calculator program.

In the last execution, we added the “%” sign, which is not mentioned in any switch statement case, and added two numbers. Thus, the default case of the switch statement got executed and shows that the user has added the wrong operator.

Example 02:

Let’s have another example of creating a calculator using user-defined functions. We have started this program with some of the main header files needed to execute this file, i.e., iostream, math.h, stdio.h, an stdlib.h. After using the standard namespace in the program, we have declared the function prototypes for the functions we will use in our calculator application, i.e., sum, minus, multiply, divide, square, and square root.

The main() function has been started with the declaration of character type variable “sign”. The cout statement is implemented here to ask a user to add an operator of his/her choice. This operator will be collected through the “cin” clause and saved to the variable “sign”. The switch statement utilizes this operator entered by the user in the “sign” variable to perform the particular function according to a specific case, i.e. “+,-,/,*,s,q”. The default case will be executed if any other operator is used. The main() method is closed here.

The very first method is the “Sum()” function to add more than 1 number. After initializing some variables, it asks for the total numbers of values a user wants to add up together and save it to the variable “t”. The user is asked to enter values one-by-one in “val” and summed together in the variable “sum” until the total “t” of the loop. The sum will be displayed.

The user will input two numbers in v1 and v2. The value of v1 will be subtracted from v2 and saved to the third variable, “sub”. The subtraction value “sub” will be displayed.

The Multiply() function is similar to the “Sum()” function but a single difference of the “*” sign instead of “+”. Make sure to initialize variable “mul” as 1 at the start.

The Divide() function is the same as the “Minus()” function with a single change of the “/” sign.

The “Square()” function is to multiply the value with itself to get a square as shown.

To find the square root of a value, the built-in “sqrt()” method is utilized here.

We have added the “*” sign, 7 numbers, and got a multiplication result in our first run.

In the second run, we calculated the sum of 9 integers added by the user as input.

The “subtraction” result of 2 integers is shown below.

Here is the division calculation of 2 integer numbers.

The last execution shows the result of square and square root functions.

Conclusion:

This article contains very simple, detailed, and easy-to-do examples to create a simple calculator in C++ language while working on Ubuntu 20.04 terminal. We have made use of switch statements to achieve this goal of creating a calculator. I hope it will be quite helpful.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.