below is a simple calculator code which I have done. I also would like
to add another thing-brackets, for example if the user puts in ((2+3)+2) * 3 =
answer.
can anyone give me any ideas on how i can add the brackets method.
THANKS,
Rose
import java.io.*;
import java.util.*;
public class NewCalculator
{
private static String Equation ="";
//==============================================================================\
=======
// 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: ");
calculation (ParsedEquation);
}
//==============================================================================\
=
//==============================================================================\
==
// Array of Operands + Calculation
//==============================================================================\
==
public static void 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);
}
}
}
for (int i=0; i<ParsedEquation.size();i++)
System.out.println("\t"+ParsedEquation.elementAt(i));
}
//==============================================================================
//==============================================================================
// 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();
}
}