//Try This
// I change the printResult and calculation methods and add calc method
import java.io.*;
import java.util.*;
public class NewCalculator
{
private static String Equation ="";
//calc is a recursive methods
public static String calc(String input)
{
String scomputation="";
String temp="";
int bracketLevel=0;
for(int i=0;i<input.length();i++)
{
if(input.charAt(i)=='(')
{
i++;
bracketLevel++;
while(bracketLevel!=0)
{
if(input.charAt(i)=='(')
bracketLevel++;
if(input.charAt(i)==')')
bracketLevel--;
if(bracketLevel!=0)
temp=temp+input.charAt(i);
i++;
}
i--;
scomputation=scomputation+calc(temp);
}else{
scomputation=scomputation+input.charAt(i);
}
}
Vector ParsedEquation = parse (scomputation);
return calculation (ParsedEquation);
}
//==========================================================================
===========
// Read user input
//==========================================================================
===========
public static String readText(String message){
BufferedReader input;
String text = "";
try{
System.out.print(message);
input = new BufferedReader(new InputStreamReader(System.in));
text = input.readLine();
}
catch(IOException ioe) {
}
return text;
}
//==========================================================================
======
// Converting to Vector
//==========================================================================
======
public static Vector parse(String equation)
{
Vector ParsedEquation = new Vector ();
String [] temp = equation.split ("\\s");
for (int i=0; i<temp.length; i++)
ParsedEquation.addElement (temp[i]);
return ParsedEquation;
}
//==========================================================================
======
//==========================================================================
======
// Printing answer
//==========================================================================
======
public static void printResult()
{
// Vector ParsedEquation = parse (Equation);
// for (int i=0; i<ParsedEquation.size(); i++)
// System.out.print(ParsedEquation.elementAt(i));
System.out.print("\n Answer is: ");
// System.out.println(calculation (ParsedEquation));
System.out.println(calc(Equation));
}
//==========================================================================
=====
//==========================================================================
======
// Array of Operands + Calculation
//==========================================================================
======
public static String calculation (Vector ParsedEquation)
{
char [] operator = {'+','-','*','/'};
for (int i=0; i<operator.length; i++)
{
while (ParsedEquation.contains (String.valueOf (operator[i])) !=
false)
{
int a = ParsedEquation.indexOf("" +operator[i]);
if (a!=-1)
{
String answer = computation ("" +ParsedEquation.get(a-1),operator[i],
"" + ParsedEquation.get(a+1));
ParsedEquation.removeElementAt(a-1);
ParsedEquation.removeElementAt(a-1);
ParsedEquation.removeElementAt(a-1);
ParsedEquation.add(a-1, answer);
}
}
}
//added
String result="";
//changed
for (int i=0; i<ParsedEquation.size();i++)
result=result+ParsedEquation.elementAt(i);
return result;
}
//==========================================================================
====
//==========================================================================
====
// Computation
//==========================================================================
====
public static String computation (String a, char o, String b)
{
double firstOperand = Double.parseDouble(a);
double secondOperand = Double.parseDouble(b);
double result=0;
switch (o)
{
case '+':
result = firstOperand + secondOperand;
break;
case '-':
result = firstOperand - secondOperand;
break;
case '*':
result = firstOperand * secondOperand;
break;
case '/':
result = firstOperand / secondOperand;
break;
}
return "" + result;
}
//==========================================================================
======
// Main Method
//==========================================================================
======
public static void main (String args[])
{ System.out.println ("-------------------------");
System.out.println ("\tCALCULATOR ");
System.out.println ("-------------------------");
Equation = readText("\nPlease enter the mathematical
calculation using a SPACE: ");
printResult();
}
}