Java計算器--CALance

之前用過C語言的圖形界面寫了一個電腦版的計算器,但效果並不是很好;因此這次花了接近一週的時間,利用Java語言寫了一個Android計算器APP–CALance,不僅寫了手機端的(Meizu),同時還寫了手錶端的(ticwatch);

其實計算器的原理都是一樣的,主要的原理是:
1.利用數據結構中的四則運算,中綴專後綴,後綴再計算結果;
2.Java上面,我是利用了List中的ArrayList來存儲數據的(類似於棧來存儲數據);

代碼方面主要分爲三部分:
1.將字符串截取出來(將數字與符號分離開);
2.中綴表達式轉化成後綴表達式;
3.後綴表達式計算出結果;

1.將字符串截取出來(將數字與符號分離開):

public class jiequ_str {

    public String jiequ_str(char[] b){
        int count_zuo = 0,count_you = 0;
        String t;
        List<String> c = new ArrayList<String>();       //利用集合List中的ArrayList來存儲字符數組;
        String a = "";

        for (int i = 0; i < b.length; i++) {
            if(b[i] == '+'){
                if(!a.equals("")){
                    c.add(a);
                    a = "";
                }
                c.add(b[i]+"");

            }else if(b[i] == '-'){
                if(i==0||b[i-1] == '('){
                    a += b[i];
                }else{
                    if(!a.equals("")){
                        c.add(a);
                        a = "";
                    }
                    c.add(b[i]+"");
                }
            }else if(b[i] == '*'){
                if(!a.equals("")){
                    c.add(a);
                    a = "";
                }
                c.add(b[i]+"");
            }else if(b[i] == '/'){
                if(!a.equals("")){
                    c.add(a);
                    a = "";
                }
                c.add(b[i]+"");
            }else if(b[i] == '='){
                if(!a.equals("")){
                    c.add(a);
                    a = "";
                }
                c.add(b[i]+"");
            }else if(b[i] == '(')
            {
                if(count_you>count_zuo){    //判斷,如果右括號比左括號先出現的話,則輸出"ERROR";
                    return "ERROR";
                }
                count_zuo++;        //累計左括號的數量;
                if(!a.equals("")){
                    c.add(a);
                    a = "";
                }
                c.add(b[i]+"");
            }else if(b[i] == ')'){
                count_you++;        //累計右括號的數量;
                if(!a.equals("")){
                    c.add(a);
                    a = "";
                }
                c.add(b[i]+"");
            }else if(b[i]=='出'){    //如果字符數組的第一個字符等於"出",則輸出"出錯";
                return "出錯";
            }
            else {
                if ((b[i] == '.' && i > 0 && i < b.length-1 && (b[i+1]=='.'))||( i== 0&&b[i] == '.')
                        ||(b[i] == '.' && i > 0 && i < b.length-3 && (b[i+1]=='.'||b [i+2]=='.')) ){
                    return "ERROR";  //如果小數點連續出現或者間接出現的話,則輸出"ERROR";
                }
                a += b[i];
            }
        }
        if(count_you != count_zuo)      //如果左括號與右括號的數量不一致的話,則輸出"ERROR";
        return "ERROR";

        for(int i=0;i<c.size();i++)     //在編譯器上顯示;
            System.out.println(c.get(i));

       t = new mid_to_end().mid_to_end(c);
        return t;
    }
}

2.中綴表達式轉化成後綴表達式:

public class mid_to_end {

    public String mid_to_end(List<String> c){
        String t;
        int x=-1,y=-1;
        List<String> a = new ArrayList<String>();   //用List中的ArrayList來存符號;
        List<String> b = new ArrayList<String>();   //用List中的ArrayList來存數字;
        System.out.println("size:"+c.size());      //在編譯器上顯示;
        for(int i=0;i<c.size();i++){
            if(c.get(i).equals("(")){
                x++;
                a.add('('+"");
            }else if(c.get(i).equals(")")){
                if(a.get(x).equals("(")){
                    a.remove(x);
                    x--;
                }else{
                    while(!a.get(x).equals("(")){
                        y++;
                        b.add(a.get(x));
                        a.remove(x);
                        x--;
                    }
                    a.remove(x);
                    x--;
                }
            }else if(c.get(i).equals("+")||c.get(i).equals("-")){

                if(x!=-1&&(a.get(x).equals("+")||a.get(x).equals("-")
                        ||a.get(x).equals("*")||a.get(x).equals("/"))){
                    y++;
                    b.add(a.get(x));
                    a.remove(x);
                    a.add(c.get(i));
                }else{
                    x++;
                    a.add(c.get(i));
                }
            }else if(c.get(i).equals("*")||c.get(i).equals("/")){

                if(x!=-1&&(a.get(x).equals("*")||a.get(x).equals("/"))){
                    y++;
                    b.add(a.get(x));
                    a.remove(x);
                    a.add(c.get(i));
                }else{
                    x++;
                    a.add(c.get(i));
                }
            }else if(c.get(i).equals("=")){
                while(x!=-1){
                    y++;
                    b.add(a.get(x));
                    x--;
                }
            }else{
                y++;
                b.add(c.get(i));
            }

        }
       t = new end_to_result().end_to_result(b);
        return t;
    }
}

3.後綴表達式計算出結果:

public class end_to_result {

    public String end_to_result(List<String> b){

        List<String> x = new ArrayList<String>(); //創建一個新的ArrayList;

        int y = b.size();
        int z = y;
        double a;

        while(y!=0){            //將b中的元素倒入新的ArrayList x中;
            x.add(b.get(y-1));
            y--;
        }
        //開始進行將元素進行計算;
        for(int i=z-1;i>=0;i--) {
            if (x.get(i).equals("+")) {
                if(i==x.size()-1||i==x.size()-2){
                    return "ERROR";
                }
                a = Double.valueOf(x.get(i + 2)) + Double.valueOf(x.get(i + 1));
                x.remove(i + 2);
                x.remove(i + 1);
                x.set(i,a + "");
            } else if (x.get(i).equals("-")) {
                if(i==x.size()-1||i==x.size()-2){
                    return "ERROR";
                }
                a = Double.valueOf(x.get(i + 2)) - Double.valueOf(x.get(i + 1));
                x.remove(i + 2);
                x.remove(i + 1);
                x.set(i, a + "");
            } else if (x.get(i).equals("*")) {
                if(i==x.size()-1||i==x.size()-2){
                    return "ERROR";
                }
                a = Double.valueOf(x.get(i + 2)) * Double.valueOf(x.get(i + 1));
                x.remove(i + 2);
                x.remove(i + 1);
                x.set(i, a + "");
            } else if (x.get(i).equals("/")) {
                if(i==x.size()-1||i==x.size()-2){
                    return "ERROR";
                }
                a = Double.valueOf(x.get(i + 2)) / Double.valueOf(x.get(i + 1));
                x.remove(i + 2);
                x.remove(i + 1);
                x.set(i,a + "");
            } else {
                continue;
            }
        }
        if(x.size() == 0)
            return 0+"";        //如果文本內容的長度爲0,則輸出0;
        return Double.valueOf(x.get(0))+"";
    }
}

最後,代碼的實現效果如下:
在手機端上:
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
在手錶端上:
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
end……

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章