Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

java - Compute total from scanning string from left to right instead of right to left

I am basically trying to figure out a technique in which I can scan a string from left to right and it will give me the integer of it. Starting from right to left is quiet easy as shown in the code below. I am just struggling on finding a way to add each integer to make it the correct value it is supposed to be.

Output: Would you like to enter an expression? y enter an expression 2567 value = 7652

I am trying to get the output to be in the correct order.

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;
    }
question from:https://stackoverflow.com/questions/66057000/compute-total-from-scanning-string-from-left-to-right-instead-of-right-to-left

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...