0% found this document useful (0 votes)
191 views

C++ Class 12 Standard Calculator

This document defines classes for a menu-driven calculator application with three modes: standard, scientific, and binary conversions. The standard calculator performs basic math functions while the scientific calculator handles advanced functions like factorials, logarithms, and trigonometry. Binary conversions allows decimal to binary and vice versa number conversions. The main function displays a menu and directs input to the appropriate class for processing based on the user's selection.

Uploaded by

Nivesh Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views

C++ Class 12 Standard Calculator

This document defines classes for a menu-driven calculator application with three modes: standard, scientific, and binary conversions. The standard calculator performs basic math functions while the scientific calculator handles advanced functions like factorials, logarithms, and trigonometry. Binary conversions allows decimal to binary and vice versa number conversions. The main function displays a menu and directs input to the appropriate class for processing based on the user's selection.

Uploaded by

Nivesh Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~Menu-based Calculator Application~~~


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<iomanip.h>
class Std_Calc
{
int a,b,d; long double c;
char ch;
public:
long double Process( long double, long double, char);
void Show();
void Show_m()
{ cout<<"***********************************";
cout<<"\n\n* Welcome to Standard Calculator*\n\n";
cout<<"***********************************";
Show();
}
};
class Sci_Calc: public Std_Calc
{
unsigned int i,f,n,r,c,npr,ncr,a,p;
int ch,c_h;
void Process(int);
void Fact();
void npr_func();
void ncr_func();
void t_ratio();
void h_func();
void pow_e();
void sQrt();
void n_root();
void fract_root();
void nat_log();
void com_log();
public:
void Show()
{
cout<<"\n*************************************";
cout<<"**Welcome To Scientific Calculator**";
cout<<"*************************************";
cout<<"\n\nEnter your choice:\n\n";
cout<<"1. Factorial\n";
cout<<"2. nPr\n";
cout<<"3. nCr\n";
cout<<"4. Trignometric Ratios\n";
cout<<"5. Hyperbolic Functions\n";
cout<<"6. Value of power of 'e'\n";
cout<<"7. Square root\n";
cout<<"8. nth root\n";
cout<<"9. (1/n)th root\n";
cout<<"10. Natural Logarithm\n";
cout<<"11. Common Logarithm\n";
cout<<"12. Basic Mathematical Functions (+,-,*,/)\n\n";

cin>>ch;
Process(ch);
}
};
class Bin_Con
{
int a,b,c; int Bin_arr[20];
void Conv_1();
void Conv_2();
public:
void Show();
void Process(int);
};
void Bin_Con::Show()
{
cout<<"***********************************";
cout<<"\n\n**Welcome To Binary Conversions**\n";
cout<<"***********************************";
cout<<"\n\n\tEnter your choice\n";
cout<<"\n\t1. Decimal to Binary\n";
cout<<"\t2. Binary to Decimal\n\t";
cin>>c;
Process(c);
}
void Bin_Con::Process(int c)
{
switch(c)
{
case 1:
Conv_1();
break;
case 2:
Conv_2();
break;
default:
cout<<"\nINVALID CHOICE!!\n";
}
}
void Bin_Con::Conv_1()
{
cout<<"\nEnter Decimal number to be converted to Binary\n";
cin>>a;
while (a!=1)
{
b=a%2;
cout<<b<<endl;
a=a/2;
if (a==1)
{
cout<<a;
break;
}

}
cout<<"\n\nNOTE:- Read from Bottom to Top\n";
}
void Bin_Con::Conv_2()
{
cout<<"\nEnter number of digits in Binary Number\n";
cin>>a;
cout<<"Enter the digits of Binary Number\n";
for(b=0;b<a;b++)
{
cin>>Bin_arr[b];
}
c=0;
for(b=0;b<a;b++)
{
c=c+(Bin_arr[b]*(pow(2,b)));
}
cout<<"Decimal representation of given Binary number is "<<c;
}

void Std_Calc::Show()
{
cout<<"\n\n\tEnter your choice:\n\n";
cout<<"\n\t1. Addition\n\t2. Multiplication\n\t3. Subt
raction(a-b)\n\t4. Division(a/b)\n\t5. Modulus(a%b)\n\n\t";
cin>>ch;
cout<<"\n\t Enter two numbers (a and b):\n\t ";
cin>>a;cout<<"\t ";cin>>b;
switch(ch)
{
case '1':
c=a+b;
cout<<"\nResult is "<<c<<endl;
break;
case '2':
c=a*b;
cout<<"\nResult is "<<c<<endl;
break;
case '3':
c=a-b;
cout<<"\nResult is "<<c<<endl;
break;
case '4':
if(b!=0)
{
c=(float)a/b;
cout<<"\nResult is "<<c
<<endl;
}
else
cout<<"\nDivision by zero is I
NVALID!!!\n\n";
break;

case '5':
if(b!=0)
{
d=a%b;
cout<<"\nResult is "<<
d<<endl;
}
else
cout<<"\nDivision by zer
o is INVALID!!!\n\n";
break;
default : cout<<"\nINVALID CHOICE!!!!\n\n";
}
}
void Sci_Calc::Process(int a)
{
switch(a)
{
case 1:
Fact();
break;
case 2:
npr_func();
break;
case 3:
ncr_func();
break;
case 4:
t_ratio();
break;
case 5:
h_func();
break;
case 6:
pow_e();
break;
case 7:
sQrt();
break;
case 8:
n_root();
break;
case 9:
fract_root();
break;
case 10:
nat_log();
break;
case 11:
com_log();
break;
case 12:
Std_Calc::Show();
break;
default : cout<<"\nInvalid choice!!";
}
}
void Sci_Calc::Fact()

{
cout<<"\n\nEnter number\n";
cin>>n;
f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<"\nFactorial of "<<n<<" is "<<f;
}
void Sci_Calc::npr_func()
{
cout<<"\n\nEnter numbers\n";
cin>>n>>r;
f=1;
for(i=1;i<=n;i++)
{
f*=i;
}
c=1;
for(i=1;i<=(n-r);i++)
{
c*=i;
}
npr = f/c;
cout<<"\nNpr : "<<npr;
}
void Sci_Calc::ncr_func()
{
cout<<"\n\nEnter numbers\n";
cin>>n>>r;
f=1;
for(i=1;i<=n;i++)
{
f*=i;
}
c=1;
for(i=1;i<=(n-r);i++)
{
c*=i;
}
a=1;
for(i=1;i<=r;i++)
{
a*=i;
}
ncr=f/(c*a);
cout<<"\nNcr : "<<ncr;
}
void Sci_Calc::t_ratio()
{
cout<<"\n\nEnter number (angle): ";
cin>>n;
cout<<"\nEnter Trignometric Ratio : ";
cout<<"1. Sine\n";
cout<<"2. Cosine\n";
cout<<"3. Tangent\n";
cout<<"4. Cotangent\n";
cout<<"5. Secant\n";
cout<<"6. Cosecant\n";

cin>>c_h;
switch(c_h)
{
case 1:
f=sin(n);
break;
case 2:
f=cos(n);
break;
case 3:
f=tan(n);
break;
case 4:
f= 1/(tan(n));
break;
case 5:
f=1/(cos(n));
break;
case 6:
f=1/(sin(n));
break;
default:
cout<<"\nInvalid Choice!!\n";
}
cout<<"\nTrignometric Ratio of angle "<<n<<" is "<<f;
}
void Sci_Calc::h_func()
{
cout<<"\n\nEnter number(angle) : ";
cin>>n;
cout<<"\nEnter your choice : ";
cout<<"\n1. Cosh\n";
cout<<"2. Sinh\n";
cout<<"3. Tanh\n";
cin>>c_h;
switch(c_h)
{
case 1:
f=cosh(n);
break;
case 2:
f=sinh(n);
break;
case 3:
f=tanh(n);
break;
default:
cout<<"\nInvalid choice\n";
}
cout<<"\nHyperbolic function of "<<n<<" is "<<f;
}
void Sci_Calc::pow_e()
{
cout<<"\n\nEnter number : ";
cin>>n;
f=exp(n);
cout<<"\nValue of e raised to "<<n<<" is "<<f;
}
void Sci_Calc::sQrt()
{

cout<<"\n\nEnter number : ";


cin>>n;
f=sqrt(n);
cout<<"\nSquare root of "<<n<<" is "<<f;
}
void Sci_Calc::n_root()
{
cout<<"\n\nEnter number : ";
cin>>n;
cout<<"\nEnter power : ";
cin>>a;
f= pow(n,a);
cout<<"\n"<<n<<" raised to power "<<p<<" is "<<f;
}
void Sci_Calc::fract_root()
{
cout<<"\n\nEnter number : ";
cin>>n;
cout<<"\nEnter power (1/p) : ";
cin>>p;
a=1/p;
f=pow(n,a);
cout<<"\n1/"<<p<<" th root of "<<n<<" is "<<f;
}
void Sci_Calc::nat_log()
{
cout<<"\n\nEnter number : ";
cin>>n;
f=log(n);
cout<<"\nNatural logarithm of "<<n<<" is "<<f;
}
void Sci_Calc::com_log()
{
cout<<"\n\nEnter number : ";
cin>>n;
f=log10(n);
cout<<"\nCommon lograthim of "<<n<<" is "<<f;
}
void main()
{
Std_Calc ob1;
Sci_Calc ob2;
Bin_Con ob3;
int ch1;
char ch;
ch='y';
cout<<setw(45)<<":::::WELCOME!:::::\n";
while(ch=='y'||ch=='Y')
{
cout<<setw(56)<<"##########################################"<<endl;
cout<<setw(56)<<"#####
MENU-BASED CALCULATOR
#####"<<endl;
cout<<setw(56)<<"##########################################";
cout<<"\n\t1. Standard Calculator\n\t2. Scientific Calculator\n\t3. Bi
nary Conversions\n\n\t Enter your choice:-";
cin>>ch1;

switch(ch1)
{
case 1:
ob1.Show_m();
break;
case 2:
ob2.Show();
break;
case 3:
ob3.Show();
break;
default:
cout<<"\nINVALID CHOICE!!!\n";
}
cout<<"\nWant another? (Y or N)\n";
cin>>ch;
clrscr();
while(ch!='\0')
{
if((ch=='y'||ch=='Y')||(ch=='n'||ch=='N'))
{
break;
}
else
{
cout<<"INVALID CHOICE!!! Enter choice again ";
}
cin>>ch;
if((ch=='y'||ch=='Y')||(ch=='n'||ch=='N'))
{ clrscr();
break;
}
else
{ clrscr();
continue;
}
}
}
}

You might also like