0% found this document useful (0 votes)
649 views19 pages

Menu Driven Standard AND Scientific Calculator: Abstract

1. The Menu Based Calculation System project aims to perform normal and scientific calculations using two calculators - Standard and Scientific. 2. The Standard calculator performs simple calculations like addition while the Scientific calculator performs calculations like finding squares or cubes of numbers. 3. The project uses C++ classes and static functions to implement the calculators and menus to select calculation types and enter values.

Uploaded by

Abhishek yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
649 views19 pages

Menu Driven Standard AND Scientific Calculator: Abstract

1. The Menu Based Calculation System project aims to perform normal and scientific calculations using two calculators - Standard and Scientific. 2. The Standard calculator performs simple calculations like addition while the Scientific calculator performs calculations like finding squares or cubes of numbers. 3. The project uses C++ classes and static functions to implement the calculators and menus to select calculation types and enter values.

Uploaded by

Abhishek yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

MENU DRIVEN STANDARD

AND
SCIENTIFIC CALCULATOR

Abstract:
Mathematical programming, together with subordinate programming languages, is the
backbone of any programming language and one of the most important reasons to use
computers. That is because nearly all sciences in the world are dependent on mathematics.
Then, the need for easier methods to do mathematics continues to be one of the most required
things nowadays and will still be obtainable until the end of the world. So, from this point, we are
working to implement a scientific calculator. In C++, some libraries function in mathematics such
as math.h and c math, but these libraries don't give you all the operations one needs, however,
they are the basis on which we build our implementation.
Have you ever tried to put the upper formula in one of your programs, but you found it hard
to be calculated? Have you ever stopped writing one of your programs because you need to use
complex formulas and there isn't a way to calculate them in your programming language? Have
you gone to use programming languages, because you need to use some of their mathematical
power, although at the same time if you have this power in your main language you may
produce a better program?
Therefore, our team has come up with the idea of a menu-driven calculator that performs
Standard and Scientific calculations with the following concepts using c++ which includes the
given below learning objectives:
● Use of c++ classes with static functions
● Generation and calling static functions
● Use the functions of Math.h header file
● Develop and display the main menu and its submenus

1. Introduction:
The Menu Based Calculation System project is aimed at performing different types of
calculations including normal and scientific calculations. In this project, two calculators Standard
and Scientific, are used for performing calculations.

2. Objective:
The Standard calculator helps in performing simple calculations such as addition, multiplication,
etc. while the Scientific calculator helps in performing calculations such as finding the squares
or cube of a number.

3. Class Diagram:
UML Class Diagram

4. Code:
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;

#define new_cal 1
#define old_cal 0

//stand_cal to define standard calculator functions


class stand_calc
{
public:
static double addition(double , double);
static double subtract(double , double);
static double multiplication(double , double);
static double division(double , double *);
static double modulu(double * , double *);

};

//scien_calc to define scientific calculator functions


class scien_calc
{
public:
static double square(double);
static double cube(double);
static double power(double , double);
static double sq_root(double);
static long int fact(double);
static double sin_func(double);
static double cos_func(double);
static double tan_func(double);
};

double stand_calc::addition(double a , double b)


{ return(a+b) ; }

double stand_calc::subtract(double a , double b)


{ return(a-b) ; }

double stand_calc::multiplication(double a , double b)


{ return(a*b) ; }

/*division function will divide the first number by the second number. This function accepts two
arguments, one is a copy of a variable and another is a pointer type because if accepting divisor
s zero, then this function will show a message to enter the divisor again. Using pointer means
that the entered value of the divisor for this function should be updated at the main
function also.*/

double stand_calc::division(double a , double *b)


{ while(*b == 0)
{
cout << "\nCannot divide by zero.";
cout << "\nEnter second number again:";
cin >> *b;
}
return(a/(*b));
}

/*Modulus function will divide the first number by the second number and return the remaining
part of the division. Similar to the division function, it will not accept zero in the divisor. Modulus
cannot be performed on a double number, so we need to convert
it into an integer.*/

double stand_calc::modulu(double *a , double *b)


{
while(*b == 0)
{
cout << "\nCannot divide by zero.";
cout << "\nEnter second number again:";
cin >> *b;
}

//Converting double into an integer .


int x = (int)*a ;
int y = (int)*b ;
if(*a-x>0 || *b-y>0)
cout << "\nConverting decimal number into an integer to perform modulus";

*a = x ;
*b = y ;
return(x%y);

// Declaration of scien_calc class functions starts from here.


double scien_calc::square(double x)
{
return(pow(x , 2)) ;
}

double scien_calc::cube(double x)
{
return(pow(x , 3)) ;

double scien_calc::power(double x , double y)


{
return(pow(x , y)) ;
}

double scien_calc::sq_root(double x)
{
return(sqrt(x)) ;
}

/*Factorial function of the scien_calc class to return a long integer as the factorial of an
acceptable number. This will convert the accepting number into an integer before calculating the
factorial.*/
long int scien_calc::fact(double x)
{
int n = (int)x;
long int f = 1;
while(n > 1)
{
f*=n ;
n--;
}
return f ;
}

double scien_calc::sin_func(double x)
{
return(sin(x)) ;
}

double scien_calc::cos_func(double x)
{
return(cos(x)) ;
}

double scien_calc::tan_func(double x)
{
return(tan(x)) ;
}

//Displaying the menus to enter the option and values.


int main()
{
double num1 , num2 ,num3 , temp ;
int choice1 = 0 , choice2 , flag ;

//Loop of the main menu from where the program starts. It will show the menu to choose the
type of calculator.
do
{
cout << "\n========Type of Calculators=======";
cout << "\n1\tStandard Calculator\n2\tScientific Calculator\n3\tQuit";
cout << "\nChoose the type of Calculator:";
cin >> choice1;
flag = new_cal ;

switch(choice1)
{
case 1:

//Loop to display standard calculator menu.


do
{
cout << "==========Standard Calculator===========";
cout <<
"\n1\tAddition\n2\tSubtraction\n3\tMultiplication\n4\tDivision\n5\tModulus\n6\tReturn to Previous
Menu\n7\tQuit";

//Option 8 will be displayed only when working on old calculations. Here, already a number
is saved in calculator memory.

if(flag == old_cal)
cout << "\n8\tClear Memory";

cout << "\nChoose the type of calculation:";


cin >> choice2;

switch(choice2)
{
case 1:

//If a new calculation is there, then accept the first number else the previous calculation result
will be the first number.

if(flag == new_cal)
{
cout << "Enter first number:";
cin >> num1;

}
else
{
num1=temp;
cout << "\nFirst number is:" << num1 <<endl;
}
cout << "\nEnter second number:";
cin >> num2;

num3=stand_calc :: addition(num1 , num2);


cout << "\nAddition of " << num1 << " and " << num2 <<" is " << num3;

cout << "\nPress any key to continue......";

temp = num3;
flag = old_cal;
break;

case 2:
if(flag == new_cal)
{
cout << "Enter first number:";
cin >> num1;
}
else
{
num1 = temp;
cout << "\nFirst number is:" << num1 << endl;
}
cout << "Enter second number:";
cin >> num2;

num3 = stand_calc :: subtract(num1 , num2);


cout << "\nSubtraction of " << num2 << " from " << num1 << " is " << num3;

cout << "\nPress any key to continue......";


temp = num3;
flag = old_cal;
break;

case 3:
if(flag == new_cal)
{
cout << "Enter first number:";
cin >> num1;

}
else
{
num1 = temp;
cout << "\nFirst number is" << num1 << endl;

}
cout << "Enter second number:";
cin >> num2;

num3 = stand_calc :: multiplication(num1 , num2);


cout << "\nMultiplication of " << num1 << " and " << num2 << " is " << num3;

cout << "\nPress any key to continue......";

temp = num3;
flag = old_cal;
break;

case 4:
if(flag == new_cal)
{
cout << "Enter first number:";
cin >> num1;
}

else
{
num1 = temp;
cout << "\nFirst number is" << num1 << endl;
}
cout << "Enter second number:";
cin >> num2;
num3 = stand_calc :: division(num1 , &num2);
cout << "\nDivision of " << num1 << " by " << num2 << " is " << num3;

cout << "\nPress any key to continue......";

temp = num3;
flag = old_cal;
break;

case 5:
if(flag == new_cal)
{
cout << "Enter first number:";
cin >> num1;
}

else
{
num1 = temp;
cout << "\nFirst number is" << num1 << endl;
}
cout << "Enter second number:";
cin >> num2;

num3=stand_calc::modulu(&num1 , &num2);
cout << "\nModulus of " << num1 << " by " << num2 << " is " << num3;

cout << "\nPress any key to continue......";

temp = num3;
flag = old_cal;
break;

case 6:
cout << "\nReturning to previous menu.";
cout << "\nPress any key to continue......";

break;

case 7:
cout << "\nQuiting..........";
cout << "\nPress any key to continue......";

exit(0);

case 8:
//If a new calculation is going on then option 8 is an invalid option, else 8 is an option to start a
new calculation.
if(flag == new_cal)
{
cout << "\nInvalid choice.";
cout << "\nPress any key to continue......";
}
else
{
temp = 0;
flag = new_cal;
}
break;

default:
cout << "\nInvalid Choice.";
cout << "\nPress any key to continue......";

break;
}

} while(choice2!=6);
break;

case 2:
// Loop to display the scientific calculator menu.
{

do
{

cout << "\n==========Scientific Calculator===========";


cout <<
"\n1\tSquare\n2\tCube\n3\tPower\n4\tFactorial\n5\tSin\n6\tCos\n7\tTan\n8\tReturn to previous
menu\n9\tQuit";

if(flag == old_cal)
cout << "\n10\tClear Memory";
cout << "\nChoose the type of calculation:";
cin >> choice2;

switch(choice2)
{
case 1:
if(flag == new_cal)
{
cout << "Enter number to find square:";
cin >> num1;

}
else
{
num1 = temp;
cout << "\nNumber is" << num1 << endl;

num3 = scien_calc :: square(num1);


cout << "\nSquare of " << num1 << " is " << num3;

cout << "\nPress any key to continue.......";

temp = num3;
flag = old_cal;
break;

case 2:
if(flag == new_cal)
{
cout << "Enter number to find cube:";
cin >> num1;

}
else
{
num1 = temp;
cout << "\nNumber is" << num1 << endl;

num3 = scien_calc :: cube(num1);


cout << "\nCube of " << num1 << " is " << num3;

cout << "\nPress any key to continue.......";

temp = num3;
flag = old_cal;
break;

case 3:
{
if(flag == new_cal)
{
cout << "Enter first number for base to find power:";
cin >> num1;

}
else
{
num1 = temp;
cout << "\nFirst number is:" << num1 << endl;
}
cout << "Enter second number for power to find power:";
cin >> num2;

num3 = scien_calc :: power(num1 , num2);


cout << "\n" << num1 << " to the power " << num2 << " is " << num3;

cout << "\nPress any key to continue....... ";

temp = num3;
flag = old_cal;
}
break;

case 4:
{
if(flag == new_cal)
{

cout << "Enter number to find factorial:";


cin >> num1;

}
else
{
num1 = temp;
cout << "\nNumber to find factorial is " << num1 << endl;

long int num4 = scien_calc :: fact(num1);


cout << "\nFactorial of " << num1 << " is " << num4;

cout << "\nPress any key to continue.......";

temp = num4;
flag = old_cal;
}
break;

case 5:
{
if(flag == new_cal)
{
cout << "Enter number to find sin value:";
cin >> num1;
}
else
{
num1 = temp;
cout << "\nNumber for sin value is:" << num1 << endl;

num3 = scien_calc :: sin_func(num1);


cout << "\nSin value of " << num1 << " is " << num3;

cout << "Press any key to continue.......";

temp = num3;
flag = old_cal;
}
break;

case 6:
{
if(flag == new_cal)
{
cout << "Enter number to find cos value:";
cin >> num1;

}
else
{
num1 = temp;
cout << "\nNumber for cos value is:" << num1 << endl;

num3 = scien_calc :: cos_func(num1);


cout << "\nCos value of " << num1 << " is " << num3;

cout << "Press any key to continue.......";


getchar();
temp = num3;
flag = old_cal;
}
break;

case 7:
{
if(flag == new_cal)
{
cout << "Enter number to find tan value:";
cin >> num1;
}
else
{
num1 = temp;
cout << "\nNumber for tan value is:" << num1 << endl;

num3 = scien_calc :: tan_func(num1);


cout << "\nTan value of " << num1 << " is " << num3;

cout << "Press any key to continue.......";


getchar();
temp = num3;
flag = old_cal;
}
break;

case 8:
{
cout << "\nReturning to previous menu.";
cout << "\nPress any key to continue.......";
getchar();
}
break;

case 9:
{
cout << "\nQuitting..........";
cout << "\nPress any key to continue.......";

getchar();
exit(0);
}
break;

case 10:
{
if(flag == new_cal)
{
cout << "\nInvalid choice.";
cout << "\nPress any key to continue.......";
getchar();
}
else
{
temp = 0;
flag = new_cal;
}
}
break;

default:
{
cout << "\nInvalid choice.";
cout << "\nPress any key to continue.......";
}
break;
}
} while(choice2!=8);
}
break;

case 3:
{
cout << "\nQuitting.......";
cout << "\nPress any key to continue.......";
}

break;

default:
{
cout << "\nInvalid choice.";

cout << "\nPress any key to continue.......";


getchar();
}
break;
}
} while(choice1!=3);
}
5. Results and Discussion:

Standard Calculator

In the above test case, a standard calculator has been used taking an example of
modulus calculation. The user is asked to input the first number and then the second number
and thereafter satisfying the conditions the result is provided.
After the calculations have been completed, the user is asked multiple choices such as
returning to the previous menu which takes back the user to select the calculator type while
storing the previous result, if the user opts to clear the memory then the previous result isn't
saved and therefore will not be a part of any further calculations.
Here the user asks to quit the calculator and hence the program terminates.
Scientific Calculator

In the above test case, a scientific calculator has been used taking an example of factorial
calculation. Similarly, the user is asked to give the input for the further calculations, and
thereafter the result is successfully provided.
The menu is again called for further actions while it stores the value from the previous
result. In this case, the user asks for the square and hence the calculations are performed on
the value stored as the user didn’t clear the memory.
In the end, the user asks to quit the calculator and hence the program terminates.
6. Conclusion:
The program is running successfully and hence the calculator is working appropriately fulling
its objectives with all the required features.

REFERENCES
● Object-Oriented Programming with C++ by E. Balagurusamy
● www.draw.io for making class diagrams

You might also like