Você está na página 1de 27

import java.io.

*;
class sci_calculator
{
double n,sum,sq,n1,mem,c=0,i,n2,s=0;
int f=0,y=0,d,e=0;
String ch,ch2=" ",ch3=" ";
void input()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the no.");
n=Double.parseDouble(br.readLine());
}
void standard()throws IOException
{
String ch1=" ";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input();
sum=n;
mem=sum;
while(true)
{
System.out.println("[ + ]ADDITION");
System.out.println("[ - ]SUBTRACTION");
System.out.println("[ * ]MULTIPLICATION");
System.out.println("[ / ]DIVISION");
System.out.println("[ = ]EQUALS");
System.out.println("[ % ]PERCENTAGE");
System.out.println("[ SQRT ]SQUARE ROOT");
System.out.println("[ M+ ]MEMORY PLUS");
System.out.println("[ M- ]MEMORY MINUS");
System.out.println("[ MR ]MEMORY RECALL");
System.out.println("[ MC ]MEMORY CLEAR");
System.out.println("[ AC ]CALCULATOR ON OR OFF");
System.out.println("[ CE ]CLEAR");
System.out.println("[ OFF ] TERMINATE");
System.out.println("Enter the choice amongst the above given choices");
ch=br.readLine();
if(e==1)
{
sum=n;
e=0;
}
if(f==1)
{
System.out.println(sum);
f=0;
}
if(ch.equalsIgnoreCase("+"))
{
c++;
System.out.println("RESULT = "+sum+" "+ch);
input();
n1=n;
sum=sum+n1;
y=1;
ch2=ch;
}
else if(ch.equalsIgnoreCase("-"))
{
c++;
System.out.println("RESULT = "+sum+" "+ch);
input();
n1=n;
sum=sum-n1;
y=1;
ch2=ch;
}
else if(ch.equalsIgnoreCase("*"))
{
c++;
System.out.println("RESULT = "+sum+" "+ch);
input();
n1=n;
sum=sum*n1;
y=1;
ch2=ch;
}
else if(ch.equalsIgnoreCase("/"))
{
c++;
System.out.println("RESULT = "+sum+" "+ch);
input();
n1=n;
sum=sum/n1;
y=1;
ch2=ch;
}
else if(ch.equalsIgnoreCase("="))
{
ch3=ch;
System.out.println("RESULT = "+sum);
ch1=ch;
double sum1=sum;
do
{
System.out.println("If u want to press this operator perpetually then , enter 1 for continue or
to 0 for quit");
d=Integer.parseInt(br.readLine());
if(d==1)
{

if(ch2.equalsIgnoreCase("+"))
{
sum1+=n1;
i=sum1;
sum=i;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum1-=n1;
i=sum1;
sum=i;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum1*=(sum/n1);
i=sum1;
sum=i;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum1/=n1;
i=sum1;
sum=i;
}
System.out.println("RESULT = " + i);
}
}
while(d==1);
}
else if(ch.equalsIgnoreCase("%"))
{
sum=sum/100;
System.out.println("RESULT = "+sum);
}
else if(ch.equalsIgnoreCase("SQRT"))
{
sq=Math.sqrt(sum);
if(ch1.equals("="))
{
System.out.println(sq);
sum=sq;
}
else
{
sum=sum+sq;
System.out.println(sq);
f=1;
}
}
else if(ch.equalsIgnoreCase("M+"))
{
if(y==1)
{
mem=sum;
}
y=0;
input();
n2=n;
mem=mem+n;
ch1=ch;
do
{
System.out.println("If u want to press this operator perpetually then , enter 1 for continue
or to 0 for quit");
d=Integer.parseInt(br.readLine());
if(d==1)
{

if(ch1.equalsIgnoreCase("M+"))
{
mem+=n2;
}
}
}
while(d==1);
}
else if(ch.equalsIgnoreCase("M-"))
{
if(y==1)
{
mem=sum;
}
y=0;
input();
n2=n;
mem=mem-n;
ch1=ch;
do
{
System.out.println("If u want to press this operator perpetually then , enter 1 for continue
or to 0 for quit");
d=Integer.parseInt(br.readLine());
if(d==1)
{

if(ch1.equalsIgnoreCase("M+"))
{
mem-=n2;
}
}
}
while(d==1);
}
else if(ch.equalsIgnoreCase("MR"))
{
System.out.println("Result = "+mem);
sum=mem;
}
else if(ch.equalsIgnoreCase("MC"))
{
mem=0;
}
else if(ch.equalsIgnoreCase("CE"))
{
sum=0;
}
else if(ch.equalsIgnoreCase("AC"))
{
mem=0;
sum=0;
f=0;
input();
e=1;
}
else if(ch.equalsIgnoreCase("OFF"))
{
break;
}
}
}
void scientific()throws IOException
{
String ch1=" ";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input();
sum=n;
mem=sum;
while(true)
{
System.out.println("[ + ]ADDITION");
System.out.println("[ - ]SUBTRACTION");
System.out.println("[ * ]MULTIPLICATION");
System.out.println("[ / ]DIVISION");
System.out.println("[ = ]EQUALS");
System.out.println("[ % ]PERCENTAGE");
System.out.println("[ SQRT ]SQUARE ROOT");
System.out.println("[ M+ ]MEMORY PLUS");
System.out.println("[ M- ]MEMORY MINUS");
System.out.println("[ MR ]MEMORY RECALL");
System.out.println("[ MC ]MEMORY CLEAR");
System.out.println("[ AC ]CALCULATOR ON OR OFF");
System.out.println("[ CE ]CLEAR");
System.out.println("[ SIN ] SINE ");
System.out.println("[ COS ] COSINE ");
System.out.println("[ TAN ] TANGENT ");
System.out.println("[ LOG ] LOGARITHM ");
System.out.println("[ x2 ] SQUARE ");
System.out.println("[ x3 ] CUBE ");
System.out.println("[ x! ] FACTORIAL ");
System.out.println("[ 3/x ] CUBEROOT ");
System.out.println("[ 10x ] POWTEN ");
System.out.println("[ 1/x ] RECIPROCRATE ");
System.out.println("[ sinh ] SINE H ");
System.out.println("[ cosh ] COSINE H ");
System.out.println("[ tanh ] TANGENT H ");
System.out.println("[ log10 ] LOGARITHM 10 ");
System.out.println("[ OFF ]TERMINATER ");
System.out.println("Enter the choice amongst the above given choices");
ch=br.readLine();
if(e==1)
{
sum=n;
e=0;
}
if(f==1)
{
System.out.println(sum);
f=0;
}
if(ch.equalsIgnoreCase("+"))
{
c++;
System.out.println("RESULT = "+sum+" "+ch);
input();
n1=n;
sum=sum+n1;
y=1;
ch2=ch;
}
else if(ch.equalsIgnoreCase("-"))
{
c++;
System.out.println("RESULT = "+sum+" "+ch);
input();
n1=n;
sum=sum-n1;
y=1;
ch2=ch;
}
else if(ch.equalsIgnoreCase("*"))
{
c++;
System.out.println("RESULT = "+sum+" "+ch);
input();
n1=n;
sum=sum*n1;
y=1;
ch2=ch;
}
else if(ch.equalsIgnoreCase("/"))
{
c++;
System.out.println("RESULT = "+sum+" "+ch);
input();
n1=n;
sum=sum/n1;
y=1;
ch2=ch;
}
else if(ch.equalsIgnoreCase("="))
{
ch3=ch;
System.out.println("RESULT = "+sum);
ch1=ch;
double sum1=sum;
do
{
System.out.println("If u want to press this operator perpetually then , enter 1 for continue or
to 0 for quit");
d=Integer.parseInt(br.readLine());
if(d==1)
{

if(ch2.equalsIgnoreCase("+"))
{
sum1+=n1;
i=sum1;
sum=i;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum1-=n1;
i=sum1;
sum=i;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum1*=(sum/n1);
i=sum1;
sum=i;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum1/=n1;
i=sum1;
sum=i;
}
System.out.println("RESULT = " + i);
}
}
while(d==1);
}
else if(ch.equalsIgnoreCase("%"))
{
sum=sum/100;
System.out.println("RESULT = "+sum);
}
else if(ch.equalsIgnoreCase("SQRT"))
{
sq=Math.sqrt(sum);
if(ch1.equals("="))
{
System.out.println(sq);
sum=sq;
}
else
{
sum=sum+sq;
System.out.println(sq);
f=1;
}
}
else if(ch.equalsIgnoreCase("M+"))
{
if(y==1)
{
mem=sum;
}
y=0;
input();
n2=n;
mem=mem+n;
ch1=ch;
do
{
System.out.println("If u want to press this operator perpetually then , enter 1 for continue
or to 0 for quit");
d=Integer.parseInt(br.readLine());
if(d==1)
{

if(ch1.equalsIgnoreCase("M+"))
{
mem+=n2;
}
}
}
while(d==1);
}
else if(ch.equalsIgnoreCase("M-"))
{
if(y==1)
{
mem=sum;
}
y=0;
input();
n2=n;
mem=mem-n;
ch1=ch;
do
{
System.out.println("If u want to press this operator perpetually then , enter 1 for continue
or to 0 for quit");
d=Integer.parseInt(br.readLine());
if(d==1)
{

if(ch1.equalsIgnoreCase("M+"))
{
mem-=n2;
}
}
}
while(d==1);
}
else if(ch.equalsIgnoreCase("MR"))
{
System.out.println("Result = "+mem);
sum=mem;
}
else if(ch.equalsIgnoreCase("MC"))
{
mem=0;
}
else if(ch.equalsIgnoreCase("CE"))
{
sum=0;
}
else if(ch.equalsIgnoreCase("AC"))
{
mem=0;
sum=0;
f=0;
input();
e=1;
}
else if(ch.equalsIgnoreCase("SIN"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.sin(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.sin(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("COS"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.cos(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.cos(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("TAN"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.tan(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.tan(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("X2"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.pow(sum,2);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.pow(sum,2);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("X3"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.pow(sum,3);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.pow(sum,3);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("LOG"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.log(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.log(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("10x"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.pow(10,sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.pow(10,sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("sinh"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.sinh(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.sinh(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("cosh"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.cosh(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.cosh(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("tanh"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.tanh(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.tanh(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("1/x"))
{
if(ch3.equalsIgnoreCase("="))
{
s=1/sum;
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=1/sum;
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("log10"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.log10(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.log10(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("x!"))
{
if(ch3.equalsIgnoreCase("="))
{
s=1;
for(int i=1;i<=sum;i++)
{
s*=i;
}
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=1;
for(int i=1;i<=sum;i++)
{
s*=i;
}
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("3/x"))
{
if(ch3.equalsIgnoreCase("="))
{
s=Math.cbrt(sum);
System.out.println("RESULT = " + s);
sum=s;
}
else
{
s=Math.cbrt(sum);
System.out.println("RESULT = " + s);
if(ch2.equalsIgnoreCase("+"))
{
sum+=s;
}
else if(ch2.equalsIgnoreCase("-"))
{
sum-=s;
}
else if(ch2.equalsIgnoreCase("*"))
{
sum*=s;
}
else if(ch2.equalsIgnoreCase("/"))
{
sum/=s;
}
else if(ch1.equalsIgnoreCase("M+"))
{
mem+=s;
}
else if(ch1.equalsIgnoreCase("M-"))
{
mem-=s;
}
else
{
sum=s;
}
}
}
else if(ch.equalsIgnoreCase("OFF"))
{
break;
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); sci_calculator ob = new sci_calculator(); String
s;
System.out.println("1.Enter 'STANDARD' for only basic calculations");
System.out.println("2.Enter 'SCIENTIFIC' for fundamental and scientific calculations both");
System.out.println("3.Enter your choice between of two given above");
s=br.readLine();
if(s.equalsIgnoreCase("STANDARD"))
{
ob.standard();
}
else if(s.equalsIgnoreCase("SCIENTIFIC"))
{
ob.scientific();
}
else
{
System.out.println("Sorry ! your entered choice is not available.................");
}
}
}

Você também pode gostar