Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Grouping-brackets

  Asked By: Ketan    Date: Oct 07    Category: Java    Views: 1219
  

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.


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();
}
}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Scott Simmons     Answered On: Oct 07

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();
}
}

 
Answer #2    Answered By: Raju Srinivas     Answered On: Oct 07

//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();
}
}

 
Didn't find what you were looking for? Find more on Grouping-brackets Or get search suggestion and latest updates.




Tagged: