Jump to content

calculator

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
I'm making a calculator (want to make it possible for it to draw graphs etc in the future aswell). But now i'm at the point where it must actually start calculating :) so if i give in "(85+6) + 9*5" then it should give me 136 :w00t:

Now if i write code it is possible to do:

int solution = (85+6) + 9*5

and then solution is 136. But i can't do

String string = "" + (85+6) + 9*5;

int i = new Integer(string);

(this gives 9145 as solution)

So is there some way to get this done? Or must i write code to totally split the string apart into just the numeric values and do it manually? Cause i have a feeling it is possible allready as it's basicly what you do with int solution = (85+6) + 9*5

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
 public static void main(String[] args){
		 String string = "" + ((85+6) + 9*5);
		 int i = new Integer(string);
		 System.out.println(i);
}  

as you can see java interprets + as an addition to the previous string, so you need to add ( ) around them, to make it clear it needs to be calculated, not added... 9145 came from 91+45

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
omg, thank you soo much.
why didn't i see this? :p

Quote

9145 came from 91+45


#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
okey, turns out it first calculates it and then puts the result in the String
public static void main(String[] args){
		 String string = "" + ((85+6) + 9*5);
                 //string == "136" allready
		 int i = new Integer(string);
		 System.out.println(i);
}

Thing is that i want to create a "more flexible" way of input by letting the user litterally give (85+6) + 9*5 in as String so
String = " (85+6) + 9*5";
int i = new Integer(string);
this ain't gonna do it. :mad:

So it turned out that i just wrote 96 lines (inclucing '}' and whitespaces) to get it solving ^^

now it gives me a warning:
method 'solve' is too complexe to analyze by data flow algorithm.

this inspection reports those conditions in the specified inspection scope that are always true or false, as well as points out where a runtimeexception may be thrown, based on data flow analysis of the code
Should i worry? (it runs perfect after all)

the complex method :p
few dutch words:
haakjes = brackets.
kars is the abreviation of karakters = character in dutch

(yes i know caracter is spelled wrong)
^Criticism is allowed ^^ just give some comments with it. Not just "you should change this in that" but also mention why.
private void solve(){
        String string = textfield.getText();
        char[] kars = string.toCharArray();
        ArrayList<String> good = new ArrayList<String>();
        List tempArray;
        boolean haakjes, digit = false, decimal = false, caracter = false;
        int firstIndex, lastIndex, j=1;
        double first, last, temp=0;

        for(int i=0 ; i<kars.length ; i++){
            if(Character.isDigit(kars[i]) || kars[i]=='.' || kars[i]==','){
                if(digit){
                    if(decimal){
                        good.set(good.size()-1, "" + (Double.parseDouble(good.get(good.size()-1)) + (Double.parseDouble(""+kars[i])/(10*j))));
                        j++;
                    }
                    else{
                        if(kars[i]!=',' && kars[i]!='.'){
                            good.set(good.size()-1, "" + (Double.parseDouble(good.get(good.size()-1))*10 + Double.parseDouble(""+kars[i])));
                        }
                    }
                    
                    if(kars[i]=='.' || kars[i]==','){
                        decimal = true;
                    }
                }
                else{
                    good.add(""+ kars[i]);
                    digit = true;
                }
                caracter = false;
            }
            else{
                if(caracter && kars[i]!='(' && kars[i]!=')'){
                    good.set(good.size()-1, good.get(good.size()-1) + kars[i]);
                }
                else{
                    good.add("" + kars[i]);
                    digit = false;
                    decimal = false;
                    j=1;
                }

                if(Character.isLetter(kars[i]))
                    caracter = true;
            }
        }

        while(good.size()>1){
            haakjes = false;
            if(good.contains(")") && good.contains("(")){
                lastIndex = good.indexOf(")");
                tempArray = good.subList(0,lastIndex);
                firstIndex = tempArray.lastIndexOf("(");
                if(firstIndex +4 == lastIndex)
                    haakjes = true;
            }
            else{
                firstIndex = -1;
                lastIndex = good.size();
                haakjes = false;
            }

            for(int k=firstIndex+1 ; k<lastIndex; k++){
                if(good.get(k).equals("/") || good.get(k).equals("*")){
                    firstIndex = k-2;
                    lastIndex = k+2;
                    break;
                }
            }

            first = Double.parseDouble(good.get(firstIndex +1));
            last = Double.parseDouble(good.get(firstIndex +3));

            if(good.get(firstIndex +2).equals("+"))
                temp = first + last;
            else    if(good.get(firstIndex +2).equals("-"))
                        temp = first - last;
                    else    if(good.get(firstIndex +2).equals("/"))
                                temp = first / last;
                            else    if(good.get(firstIndex +2).equals("*"))
                                        temp = first * last;

            good.remove(firstIndex+1);
            good.remove(firstIndex+1);
            good.remove(firstIndex+1);
            good.add(firstIndex+1, "" + temp);

            if(firstIndex+4 == lastIndex && haakjes){
                good.remove(firstIndex);
                good.remove(firstIndex+1);
            }
        }
        solution.setText("\n" + string +" = " + good.get(0));
        textfield.setText("");
    }

To test this code just change the 2nd last line into a System.out.println() and scan something in the String (first line). Should be all you need to change to get it working without my GUI class

tomorrow or so, squareroot, exponentiation, tan, cos, sin, etc :lol:
hopefully won't give me as much problems and debugging as today.

Edited by wim DC, 05 October 2009 - 11:26 AM.