Scientific Calculator Project Using C++

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7
At a glance
Powered by AI
The document describes a C++ program for a scientific calculator that supports various arithmetic, trigonometric, logarithmic, power and factorial operations.

The main functions of the program include arithmeticOperations(), trigonometricOperations(), logarithmicOperations(), powerOperations() and factorialOperation() to perform different types of calculations.

The operations supported by the program include basic arithmetic, trigonometric functions, logarithms, powers, roots and factorials.

/*

Scientific Calculator Project Using C++


Developed By:
1.
2.
3.
4.
Under the guidance Mrs. Mamatha A. M. of Padmajeet Infotech.
*/
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string>
#include<sstream>
using namespace std;
class ScienntificCalculator{
/*Function to convert given double value to string*/
private:
string doubleToString(double i)
{
stringstream s;
s << i;
return s.str();
}
/*Function to convert given character value to string*/
string charToString(char i)
{
stringstream s;
s << i;
return s.str();
}
public:

/*Function to perform all Arithmetic operations*/


void arithmeticOperations() {
double operand1, operand2;
string str;
char op;
bool isFirst = true;
do {
if (isFirst) {//Ask for 2 operands only once for an Expression
cout << "\n\tArithmetic Operations\t" << endl;
cout << "Enter operand1: \t";
cin >> operand1;
isFirst = false;
str = doubleToString(operand1);
}
cout
cout
cout
cout
cout

<<
<<
<<
<<
<<

"\t
"\t
"\t
"\t
"\t

+: Addition" << endl;


-: Subtraction" << endl;
*: Multiplication" << endl;
/: Division" << endl;
=: Result" << endl;

cout << "\t C: Clear" << endl;


cout << "\t Q: Exit" << endl;
cout << "\nEnter the Operator \t";
op = _getche();
cout << endl;
if (op != '+' && op != '-' && op != '*' && op != '/' && op != 'C' && op != 'c'
&& op != '='&& op != 'Q' && op != 'q') {
cout << "Invalid Operator" << endl;
continue;
}
if (op != 'C' && op != 'c' && op != '=' && op != 'Q' && op != 'q') {//Skip
asking operand for Clear, Exit and =
cout << "Enter Operand: \t";
cin >> operand2;
str += charToString(op);
str += doubleToString(operand2);
}

switch (op) {
case '+':
operand1 = operand1 + operand2;
cout << str << "=" << operand1 << endl;
break;
case '-':
operand1 = operand1 - operand2;
cout << str << "=" << operand1 << endl;
break;
case '*':
operand1 = operand1*operand2;
cout << str << "=" << operand1 << endl;
break;
case '/':
operand1 = operand1 / operand2;
cout << str << "=" << operand1 << endl;
break;
case '=':
cout << str << "=" << operand1 << endl;
break;
case 'c':
case 'C':
str = "";//reset the string for next expression
isFirst = true;//reset the flag for next expression
break;
case 'Q':
case 'q':
return;
default:
cout << "Invalid Operator" << endl;
}
} while (true);

/*Function to perform all Trigonometric operations*/


void trigonometricOperations() {

char option;
double operand;
do {
cout << "Trigonometric Operations" << endl;
cout << "\t a) sine function" << endl;
cout << "\t b) cosine function" << endl;
cout << "\t c) tangent function" << endl;
cout << "\t d) sine inverse function" << endl;
cout << "\t e) cosine inverse function" << endl;
cout << "\t f) tangent inverse function" << endl;
cout << "\t g) sine hyperbolic function" << endl;
cout << "\t h) cosine hyperbolic function" << endl;
cout << "\t i) tangent hyperbolic function" << endl;
cout << "\t j) sine inverse hyperbolic function" << endl;
cout << "\t k) cosine inverse hyperbolic funtion" << endl;
cout << "\t l) tangent inverse hyperbolic funtion" << endl;
cout << "\t m) Exit" << endl;
cout << "Enter your choice\t:";
option=_getche();
cout << endl;
switch (option)
{
case 'a':
case 'A':
cout << "\nEnter the Angle in Radian: \t";
while (!(cin >> operand)) {
cout << "\nRe-Enter the Angle in Radian: \t";
cin.clear();
cin.ignore(1000, '\n');

endl;

}
cout << "sin(" << operand << ")=" << sin(operand) <<
break;
case 'b':
case 'B':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "cos(" << operand << ")=" << cos(operand) <<

endl;

endl;

break;
case 'c':
case 'C':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "tan(" << operand << ")=" << tan(operand) <<
break;
case 'd':
case 'D':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "asin(" << operand << ")=" << asin(operand) <<

endl;

break;
case 'e':
case 'E':

<< endl;

cout << "Enter the Angle in Radian: \t";


cin >> operand;
cout << "acos(" << operand << ")=" << acos(operand)
break;
case 'f':
case 'F':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "atan(" << operand << ")=" << atan(operand)

<< endl;

endl;

break;
case 'g':
case 'G':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "sinh(" << operand << ")=" << sinh(operand) <<
break;
case 'h':
case 'H':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "cosh(" << operand << ")=" << cosh(operand)

<< endl;

<< endl;

break;
case 'i':
case 'I':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "tanh(" << operand << ")=" << tanh(operand)

break;
case 'j':
case 'J':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "sineInverseHyperbolic(" << operand << ")=" <<
log(operand + sqrt(pow(operand, 2) + 1)) << endl;
break;
case 'k':
case 'K':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "cosineInverseHyperbolic(" << operand << ")="
<< log(operand + sqrt(pow(operand, 2) - 1)) << endl;
break;
case 'l':
case 'L':
cout << "Enter the Angle in Radian: \t";
cin >> operand;
cout << "tangentInverseHyperbolic(" << operand << ")="
<< (log(1 + operand) - log(1 - operand)) / 2 << endl;
break;
case 'm':
case 'M':
return;
default:

}
} while (true);

cout << "Invalid Choice" << endl;

/*Function to perform all Logarithmic operations*/


void logarithmicOperations()
{
double x;
char choice;
do {
cout << "\n \t Logarithmic Operations" << endl;
cout << "\t1) Natural log" << endl;
cout << "\t2) log with base 10" << endl;
cout << "\t3) Exit" << endl;
cout << "enter your choice :\t";
choice = _getche();
cout << endl;
switch (choice)
{
case '1':
cout << "Enter the operand :\t";
cin >> x;
cout << "log(" << x << ")=" << log(x) << endl;
break;
case '2':
cout << "Enter the operand :\t";
cin >> x;
cout << "log10(" << x << ")=" << log10(x) << endl;
break;
case '3':
return;
default:
cout << "Invalid Choice" << endl;
return;
}
} while (true);
}
/*Function to perform all Power related operations*/
void powerOperations() {
char choice;
double operand, power, root;
do {
cout << "\n \t Power Operations" << endl;
cout << "\t 1) Squre" << endl;
cout << "\t 2) Cube" << endl;
cout << "\t 3) Nth power" << endl;
cout << "\t 4) Squre Root" << endl;
cout << "\t 5) Cube Root" << endl;
cout << "\t 6) Nth Root" << endl;
cout << "\t 7) Exit" << endl;
cout << "Enter your choice:\t";
choice = _getche();
cout << endl;
switch (choice) {
case '1':

cout << "Enter the operand:\t";


cin >> operand;
cout << "sqr(" << operand << ")=" << operand * operand <<

endl;

operand << endl;

power) << endl;

break;
case '2':
cout << "Enter the operand:\t";
cin >> operand;
cout << "cube(" << operand << ")=" << operand * operand *
break;
case '3':
cout << "Enter the operand:\t";
cin >> operand;
cout << "Enter the power:\t";
cin >> power;
cout << operand << "^" << power << "=" << pow(operand,
break;
case '4':
cout << "Enter the operand:\t";
cin >> operand;
cout << "squareroot(" << operand << ")=" << sqrt(operand) <<

endl;

break;
case '5':
cout << "Enter the operand:\t";
cin >> operand;
cout << "cuberoot(" << operand << ")=" << pow(operand, (1.0 /

3)) << endl;

(1.0 / root)) << endl;

break;
case '6':
cout << "Enter the operand:\t";
cin >> operand;
cout << "Enter the root value:\t";
cin >> root;
cout << root << "root of " << operand << "=" << pow(operand,

break;
case '7':
return;
default:
cout << "Invalid Choice" << endl;
}
} while (true);

/*Function to find the Factorial of a given number*/


void factorialOperation() {
double operand;
double fact = 1.0;
cout << "\n Enter the Operand: \t";
cin >> operand;
if (operand < 0) {
cout << "Invalid operand" << endl;
}
else if (operand == 0) {

}
else {

}
};

cout << "fact(0)=1" << endl;


for (int i = operand; i >= 1; i--) {
fact = fact*i;
}
cout << "fact(" << operand << ")=" << fact << endl;

void main() {
char choice;
do {
cout << "\n------------------Scientific Calculator---------------------" << endl;
cout << "\t 1) Arithmetic Operations" << endl;
cout << "\t 2) Trigonometric Operations" << endl;
cout << "\t 3) Logorithemic Operations" << endl;
cout << "\t 4) Power Operations" << endl;
cout << "\t 5) Factorial Operation" << endl;
cout << "\t 6) Exit" << endl;
cout << "Enter your choice:\t";
choice = _getche();
cout << endl;
ScienntificCalculator sc;
switch (choice) {
case '1':
//cout<<"Airthemetic operations"<<endl;
sc.arithmeticOperations();
break;
case '2':
//cout<<"Trigonometric operations"<<endl;
sc.trigonometricOperations();
break;
case '3':
//cout << "Logorithemic operations" << endl;
sc.logarithmicOperations();
break;
case '4':
//cout << "Power operations" << endl;
sc.powerOperations();
break;
case '5':
//cout << "Factorial operations" << endl;
sc.factorialOperation();
break;
case '6':
cout << "Thank you for using Scientific Calculator!!!" << endl;
_getch();
return;
default:
cout << "Invalid Choice" << endl;
}
_getch();
} while (true);
}

You might also like