Search:

# Grouping-brackets

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

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 =
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 ="";
//==============================================================================\
=======
//==============================================================================\
=======
String text = "";
try{
System.out.print(message);
}
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++)

return ParsedEquation;
}
//==============================================================================\
==
//==============================================================================\
==
//==============================================================================\
==
public static void printResult()
{
Vector ParsedEquation = parse (Equation);
for (int i=0; i<ParsedEquation.size(); i++)
System.out.print(ParsedEquation.elementAt(i));
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);
}
}
}
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 ("-------------------------");
using a SPACE: ");
printResult();
}
}

Share:

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 =
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 ="";
//==============================================================================\
=======
//==============================================================================\
=======
String text = "";
try{
System.out.print(message);
}
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++)

return ParsedEquation;
}
//==============================================================================\
==
//==============================================================================\
==
//==============================================================================\
==
public static void printResult()
{
Vector ParsedEquation = parse (Equation);
for (int i=0; i<ParsedEquation.size(); i++)
System.out.print(ParsedEquation.elementAt(i));
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);
}
}
}
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 ("-------------------------");
using a SPACE: ");
printResult();
}
}

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

//==========================================================================
===========
//==========================================================================
===========
String text = "";
try{
System.out.print(message);
}
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++)

return ParsedEquation;
}
//==========================================================================
======
//==========================================================================
======
//==========================================================================
======
public static void printResult()
{
// Vector ParsedEquation = parse (Equation);
// for (int i=0; i<ParsedEquation.size(); i++)
// System.out.print(ParsedEquation.elementAt(i));
// 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);
}
}
}

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 ("-------------------------");
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.