Let’s make an Advanced Calculator using C++ | Calculator In C++

Let’s make an Advanced Calculator using C++ | Calculator In C++

Introduction

Hello, coders. Welcome to codewithrandom. Today we will be learning how to make an advanced calculator using the math library of C++. A calculator is every programmer’s first program that gives the feeling of joy and achievement. 

Some of us stop after making a basic calculator while the rest make something more to it. It can be improving the UI, improving the operations. It could be anything.

Today we will make an advanced calculator which gives us an option to perform much more than the basic arithmetic operations. We can execute Trigonometric, Exponential, and Logarithmic operations, along with the basic arithmetic operations.


About the UI


Now that we have a clue of what we have as a whole let’s discuss how we will implement it. I will be discussing all the parts of the program that needs explanation. 

Let’s see the image of the UI we will have after completing the compilation of the program.



So, first, we are offered to choose the operation from the available ones.

After choosing one of the choices, we give the input accordingly. We are also offered more choices in that operation if any exist.

Example When we choose arithmetic, we can also the required operation.


After finishing an operation, we ask the user if they would like to continue or terminate the process.


Concepts Used


Before we go into the explanation, let’s see the concepts of C++ you should know to understand the code and replicate it.

  • Switch Case — To switch between the options
  • While loops — For infinite loop of switch case 
  • Basics of C++ functions — To make the program seem clean and reusable for further expansions.

Code

1:    
2:
#include<iostream>
3: #include<cmath>
4:
5: using namespace std;
6:
7: void arithmetic(){
8: int op = 0;
9: float A = 0;
10: float B = 0;
11:
12: cout<<"Select opeartionn";
13: cout<<"[1] Additionn";
14: cout<<"[2] Substractionn";
15: cout<<"[3] Productn";
16: cout<<"[4] Divisionn";
17:
18: cin>>op;
19: cout<<"Enter the number:";
20: cin>>A;
21:
22: cout<<"Enter second number:";
23: cin>>B;
24:
25: cout<<"Result: ";
26:
27: switch(op){
28: case 1:
29: cout<<(A+B);
30: break;
31: case 2:
32: cout<<(A-B);
33: break;
34: case 3:
35: cout<<(A*B);
36: break;
37: case 4:
38: cout<<(A/B);
39: break;
40: default:
41: cout<<"Invalid operation";
42: break;
43: }
44: cout<< endl;
45: }
46:
47: void trigonometric(){
48: int op = 0;
49: float val = 0.0;
50: cout<<"Selectn";
51: cout<<"[1] Sinen";
52: cout<<"[2] Cosinen";
53: cout<<"Op: ";
54: cin>>op;
55: cout<<"Enter value: ";
56: cin>>val;
57: if(op == 1){
58: cout<<sin(val);
59: }
60: else if(op == 2){
61: cout<<cos(val);
62: }
63: else{
64: cout<<"Invalid operation";
65: }
66: cout<<endl;
67: }
68:
69: void exponential(){
70: float base = 0.0;
71: float eee = 0.0;
72:
73: cout<<"Enter base:";
74: cin>>base;
75: cout<<"Enter expnent: ";
76: cin>>eee;
77: cout<<pow(base, eee)<<endl;
78: }
79:
80: void logarithmic(){
81: float value = 0.0;
82: cout<<"Enter the value to calculate the log(e): ";
83: cin>>value;
84: cout<<log(value)<<endl;
85: }
86:
87: int main(){
88: int sel = 0;
89: char choice = 'y';
90: cout<<"Advanced Calculatorn";
91:
92: cout<<"[1] Arithmeticn";
93: cout<<"[2] Trigonometricn";
94: cout<<"[3] Exponentialn";
95: cout<<"[4] Logarithmicn";
96: cout<<"Your choice:";
97:
98: while(choice == 'y'){
99: cout<<"Enter the type of operation you want to calculaten";
100: cin>>sel;
101:
102: switch(sel){
103: case 1:
104: arithmetic();
105: break;
106: case 2:
107: trigonometric();
108: break;
109: case 3:
110: exponential();
111: break;
112: case 4:
113: logarithmic();
114: break;
115: default:
116: cout<<"Invalid Operation";
117: }
118:
119: cout<<"Do you want to continue? y/n"<<endl;
120: cin>>choice;
121: if(choice == 'n'){
122: break;
123: }
124: }
125: return 0;
126: }

Code Explanation


The main() function 


Let’s start by evaluating the main(). In the main function, we start a while-loop for taking the input from the switch case. It goes on until the user enters n which stands for “no” in this case.

In the while loop, we have a switch-case containing the list of mathematical operations we provide. Inside switch-case, we call the respective functions of the case, followed by taking the input for the re-start of the loop. 


The arithmetic() function


Inside the arithmetic() function, we provide addition, subtraction, multiplication, and division operations. But, why use a switch-case here?

If-else and else if loops are also an option. We use switch-case because it’s made for cases where we provide multiple options to the user.


In this calculator, we only operate on two inputs. So, we take two variables as input for the arithmetic operations. The rest is self-describing in that function.


The trigonometric() function


In general, A calculator provides the result based on the choice of degree or radians. In our calculator, we get the result as an input of radians. If we enter 30 as an input to the sine function, it considers 30 radians. So, don’t get confused thinking it is a wrong answer.


For this tutorial, we only provide two trigonometric functions sine and cosine. For the calculation of output, we directly use the sin() and cos() functions from the math.h library. 


We didn’t use a switch-case here because we only need 2 cases. If you want to incorporate more then, it is better to use a switch-case.


The exponential() function


We finished two of the primary operations. The exponential function is more of a smaller and easier to write function. 

Two things in an exponential are needed for the calculation of the result.

  • The base — Number should be raised or multiplied by itself for power number times.
  • The power — Number decides how much the base should raise and how many times to do it?

So, we take these two things from the user and calculate the exponential of the base. To do this, there is an inbuilt function called pow(base, exponent).  

As an expansion to it, if you want to challenge yourself, try to write the program for pow(base, exponent). Do share it in the comments. 


The logarithmic() function 


It is a straightforward function. We take the user input and calculate the log value of that value to the base ten. For clarity, this is what it means.

#image showing log function


So, one thing to note here is we are calculating only for the log to the base 10 and not any other number. Here also I would like to give a challenge. Try to write a function for the log of any base and any value. 

I will try to tell the answer in the coming articles.


Conclusion


We have reached the end of the article but we have a lot more projects in C++ coming. It is just to start with an interesting and fun project.

If you have enjoyed the article and learned something new today, let us know in the comments. Check out some more articles here. 


1) Folder Tabs using CSS

2) Bootstrap 4 responsive navbar

Visit our Instagram Page for more awesome content on Web development.

Thank you.

Happy Reading 🙂

Leave a Reply