亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

字符串的四則運算表達式

chnmagnus / 911人閱讀

摘要:支持括號小數(shù)負數(shù)也行運算數(shù)字運算符沒有括號正常運算括號內(nèi)的值都取完了,刪除括號左右括號齊全先算括號內(nèi)的一個包含數(shù)字的列表和一個包含運算符的列表形式可能會有空字符串符號列表同時出現(xiàn)從左至右運算同時出現(xiàn)從左

    public static void main(String[] args) {
        // 支持括號 小數(shù) 負數(shù)
        String statement = "-10/(4.5+5.5)*(-4-6+20)/-2"; // 10/(-2) 也行
        System.out.println(calculate(statement));
    }

    @SuppressWarnings("unchecked")
    private static double calculate(String statement){
        Object[] result = filter(statement);
        // 運算數(shù)字
        List numList = (List) result[0];
        // 運算符
        List symbolList = (List) result[1];
        while (!symbolList.isEmpty()) {
            int index = symbolList.indexOf("(");
            if (index == -1) {
                // 沒有括號正常運算
                realCalculate(numList, symbolList);
            } else {
                int right = symbolList.indexOf(")");
                if (right == index + 1) {
                    // 括號內(nèi)的值都取完了,刪除括號
                    symbolList.remove(index);
                    symbolList.remove(index);
                    continue;
                }
                // 左右括號齊全 先算括號內(nèi)的
                if (right != -1) {
                    List doubles = numList.subList(index, right);
                    List subChars = symbolList.subList(index + 1, right);
                    realCalculate(doubles, subChars);
                }
            }
        }
        return numList.get(0);
    }

    /**
     * @return 一個包含數(shù)字的列表和一個包含運算符的列表
     */
    private static Object[] filter(String statement) {
        // 形式 123,456,789 可能會有空字符串
        StringBuilder nums = new StringBuilder();
        // 符號列表
        List symbolList = new LinkedList<>();
        for (int i = 0; i < statement.length(); i++) {
            char c = statement.charAt(i);
            if (c == "-" && (i == 0 || statement.charAt(i - 1) == "(" 
            || statement.charAt(i - 1) == "*" || statement.charAt(i - 1) == "/")) {
                nums.append(c).append(statement.charAt(i + 1));
                i++;
            } else if (Character.isDigit(c) || c == ".") {
                nums.append(c);
            } else {
                symbolList.add(c);
                nums.append(",");
            }
        }

        String[] ss = nums.toString().split(",");
        List numList = new ArrayList<>();
        for (String num : ss) {
            if (!num.isEmpty()) {
                numList.add(Double.parseDouble(num));
            }
        }
        return new Object[]{numList, symbolList};
    }

    private static void realCalculate(List numList, List symbolList) {
        while (!symbolList.isEmpty()) {
            int index = symbolList.indexOf("*"), tmp;
            double value = 0.0D;
            if (index != -1 && (tmp = symbolList.indexOf("/")) != -1) {
                // 同時出現(xiàn) * / 從左至右運算
                if (index < tmp) {
                    value = numList.remove(index) * numList.remove(index);
                } else {
                    index = tmp;
                    value = numList.remove(index) / numList.remove(index);
                }
            } else if (index != -1) {
                value = numList.remove(index) * numList.remove(index);
            } else if ((index = symbolList.indexOf("/")) != -1) {
                value = numList.remove(index) / numList.remove(index);
            } else if ((index = symbolList.indexOf("+")) != -1 && (tmp = symbolList.indexOf("-")) != -1) {
                // 同時出現(xiàn) + - 從左至右運算
                if (index < tmp) {
                    value = numList.remove(index) + numList.remove(index);
                } else {
                    index = tmp;
                    value = numList.remove(index) - numList.remove(index);
                }
            } else if (index != -1) {
                value = numList.remove(index) + numList.remove(index);
            } else if ((index = symbolList.indexOf("-")) != -1) {
                value = numList.remove(index) - numList.remove(index);
            }
            // 刪除運算符
            symbolList.remove(index);
            // 將計算結(jié)果放回列表,待下次計算
            numList.add(index, value);
        }
    }
總結(jié)一下

我的方法是先從括號的算起,根據(jù)運算符索引查找運算數(shù)索引,從而進行計算,算完后刪除運算符和運算數(shù),并將運算結(jié)果放回待運算的列表

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/75051.html

相關(guān)文章

  • Javascript語句 - Javascript語法基礎(chǔ) - Javascript核心

    摘要:多數(shù)運算符都是由標點符號表示,比如和。通常會根據(jù)需要對操作數(shù)進行類型轉(zhuǎn)換左值是一個古老的屬于,它是指表達式只能出現(xiàn)在賦值運算符的左側(cè)。也稱為嚴格相等運算符,它用來檢測兩個操作數(shù)是否嚴格相等。運算符的檢測規(guī)則是和運算符的求反。 源代碼: https://github.com/RobinQu/Programing-In-Javascript/blob/master/chapters/...

    lavnFan 評論0 收藏0
  • js-數(shù)據(jù)運算

    摘要:跳過第二個運算子的機制,被稱為短路有些程序員喜歡用它取代結(jié)構(gòu)等價于運算符可以多個連用返回第一個布爾值為的表達式的值。 一、運算符概述 1、定義 JavaScript中運算符主要用于連接簡單表達式,組成一個復雜的表達式 2、運算符類別 算數(shù)運算符 賦值表達式 比較表達式 布爾運算符 位運算符 二、算數(shù)運算符 1、加法運算符(Addition):x + y 加法運算符是在運行時決定,到...

    sf190404 評論0 收藏0
  • JS語言核心——“表達運算符”

    摘要:原始表達式直接量保留字變量原始表達式表達式的最小單位表達式中的短語,解釋器會將其計算為一個結(jié)果對象和數(shù)據(jù)的初始化表達式對象直接量和數(shù)組直接量,它們和布爾直接量不同,它們不是原始表達式函數(shù)定義表達式函數(shù)直接量也不是原始表達式屬性訪問表達式語法 1 原始表達式 直接量、保留字、變量 原始表達式(primary expression):表達式的最小單位 表達式:JavaScript中的短語...

    李增田 評論0 收藏0
  • JS基礎(chǔ)學習03「表達運算符」

    摘要:函數(shù)定義表達式。對象創(chuàng)建表達式。需要注意的是,大多數(shù)運算符都是由標點符號表示的,比如和。也就是說,空字符串將被當作,布爾值將被當作。對于和,則分別調(diào)用函數(shù)并取得字符串和。 表達式 表達式是由數(shù)字、運算符、數(shù)字分組符號(如括號)、自由變量和約束變量等以能求得數(shù)值的有意義排列方法所得的組合。JavaScript 表達式主要有以下幾種形式: 原始表達式:常量、變量、保留字。 對象、數(shù)組初始...

    dcr309duan 評論0 收藏0
  • 《JavaScript 闖關(guān)記》之表達運算

    摘要:函數(shù)定義表達式。對象創(chuàng)建表達式。也就是說,空字符串將被當作,布爾值將被當作。如果有一個操作數(shù)是對象數(shù)值或布爾值,則調(diào)用它們的方法取得相應的字符串值,然后再應用前面關(guān)于字符串的規(guī)則。對于和,則分別調(diào)用函數(shù)并取得字符串和。 表達式 表達式是由數(shù)字、運算符、數(shù)字分組符號(如括號)、自由變量和約束變量等以能求得數(shù)值的有意義排列方法所得的組合。JavaScript 表達式主要有以下幾種形式: ...

    Render 評論0 收藏0
  • python基礎(chǔ)教程:運算對象、運算符、表達和語句

    摘要:用一行表示它們的關(guān)系就是運算對象運算符表達式語句運算對象和運算符構(gòu)成表達式,表達式構(gòu)成語句運算對象運算對象就是由各種對象構(gòu)成的集合,這些對象里面有些是常量,有些是變量。 編程的本質(zhì)就是數(shù)據(jù)和運算,數(shù)據(jù)由基本數(shù)據(jù)類型、數(shù)據(jù)結(jié)構(gòu)來表示,運算就是對這些數(shù)據(jù)的各種操作,基本的加減乘除、是非判斷、流程控制等等。這些操作就是今天我們要講的運算符、表達式和語句。 showImg(http://upl...

    stdying 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<