从左到右而不是从右到左扫描字符串计算总数

问题描述

我基本上是想找出一种技术,我可以从左到右扫描一个字符串,它会给我它的整数。 从右到左开始很安静,如下面的代码所示。 我只是在努力寻找一种方法添加每个整数以使其成为它应该的正确值。

输出: 您想输入表达式吗? 是 输入表达式 2567 值 = 7652

我正在努力使输出按正确的顺序排列。

e = String input // string needing to be converted to double
p = 0 // position
// If a substring of e whose rightmost character is at position p
    // represents a number (possibly with a decimal point,possibly negative),// return the numerical value of the substring as a double value and
    // re-position p just to the left of the substring.

    private double formNum() {

        double total = 0.0;
        int count = 0;
        int flag = 0;
        double mult = 1;
        char c,d;
        do {
            c = e.charat(p); // examine the current character in the string (from right to left)
            if (c == '.') {
                flag = 1; // set a flag to remember that the character is a decimal point
            } else {
                // if the current character is a digit,convert to its
                // int value and include it in the value corresponding to the string.
                if ((c >= '0') && (c <= '9')) {
                    
                    total = total + mult * (c - '0');
                    mult = mult * 10.0;
                    if (flag == 0) {
                        count++; // count the number of digits to the right of a possible decimal point
                    }
                } else {
                    if (c == '(' && e.charat(p + 1) == '-') {
                        total = -total; // an underscore character represents a negative value
                    }
                }
            }
            p++; // Prepare to move to the next character to the left.
                    // This is a private non-static method because it changes the member variable p
            d = '?';
            if (p <= e.length() - 1) {
                d = e.charat(p); // the next character to the left
            }
        } while ((p <= e.length() - 1) && (((d <= '9') && (d >= '0')) || (d == '_') || (d == '.')));// check for a valid
                                                                                                    // character
        if (flag == 1) {
            total = total / Math.pow(10,count * 1.0); // compute the value taking into account
                                                        // the number of decimal places
        }
        return total;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)