Results 1 to 1 of 1
Thread: PEMDAS CAlculator
- 08-13-2011, 06:25 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
PEMDAS CAlculator
I have been writing code for a calculator that will be able to read what the user inputs like a whole arithmetical statement, when i did this i have run into problems. I have been using mainly if statements and while but i would like to know if there is anything in the java library that could make it easier for me to type this code. oh yeah and i would like for it to be able to read the pemdas order of operations. I am using split to separate and isolate what the user inputs and it goes into arrays. then i am using if statements to analyze what the user inputs
.import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class main {
public static void main(String args[]){
Scanner george = new Scanner(System.in);
String s = george.next();
String[] geor= getIntegers(s);
int[] ints = convertFromString(geor);
String[] operat = getOperators(s);
double answer = ints[0];
for(int x = 0; x<operat.length;x++){
if (operat[x].equals("^")){
if(x ==0){
answer = Math.pow(answer,ints[x+1]);
}
else if(x==operat.length-1){
ints[x]= (int) Math.pow(ints[x],ints[x+1]);
}
else{
}
}
}
for(int x = 0; x<operat.length;x++){
if (operat[x].equals("/")){
if(x ==0){
answer = answer/ints[x+1];
}
}
if (operat[x].equals("*")){
if(x ==0){
answer = answer*ints[x+1];
}
}
}
for(int x = 0; x<operat.length;x++){
if (operat[x].equals("+")){
if(x ==0){
answer = answer+ints[x+1];
}
}
if (operat[x].equals("-")){
if(x ==0){
answer = answer-ints[x+1];
}
}
}
System.out.print(answer);
}
// seperate numbers from other characters
public static String[] getIntegers(String s){
String regex = "[-\\+*/()\\^]";
String[] geo= s.split(regex);
return deletingTheExtraWhiteSpace(geo);
}
public static String[] getOperators(String s){
String regex = "[1234567890.]";
String[] ge= s.split(regex);
return deletingTheExtraWhiteSpace(ge);
}
public static String[] deletingTheExtraWhiteSpace(String[] s){
List<String> list = new ArrayList<String>(Arrays.asList(s));
list.removeAll(Arrays.asList(""));
return list.toArray(EMPTY_STRING_ARRAY);
}
public static int[] convertFromString(String[] s){
if(s != null){
int intarray[]= new int[s.length];
for(int i = 0; i <s.length; i++){
intarray[i] = Integer.parseInt(s[i]);
}
return intarray;
}
return null;
}
private static final String[] EMPTY_STRING_ARRAY = new String[0];
}
Similar Threads
-
Help! URGENT.. i need to be able to evaluate PEMDAS expressions using stacks..
By a_aagain in forum New To JavaReplies: 4Last Post: 04-01-2009, 12:25 PM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM -
any can help me? About MDAS and PEMDAS rules.
By darlineth in forum New To JavaReplies: 12Last Post: 09-07-2008, 02:21 PM -
calculator java following pemdas rule
By sassygirl_kea9@yahoo.com in forum New To JavaReplies: 2Last Post: 07-10-2008, 04:15 AM -
any can help me? About MDAS and PEMDAS rules and Infix-Prefix, Infix-Postfix rules
By darlineth in forum New To JavaReplies: 1Last Post: 07-05-2008, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks