"Programming algorithm and (ii) based algorithm," "Second Week recursion" four arithmetic expression evaluation 4132

https://www.cnblogs.com/xyqxyq/p/10211341.html

The question we can recursive solution, we can first of all an expression is divided into three parts, namely:

(1) the expression: item, plus or minus

(2): factor, multiplication and division

(3) factors: the number of () Expression

These three constitute a recursive relationship, we can see that a required value of the expression, we first asked the value of an item, the required value of an item, we first asked a factor of value, the value of a factor requires that we it is the first look at what constitutes.

It can be combined with parentheses by the expression, but also can be composed by the number, it is found that when the number, we calculate the size of this number.

Floating-point number is broken down into two parts, the first to calculate the size of the integer part, continue to take one and see if the end, not the end of it and then take one, will before by ten and then add this one, then we cin.peek () function can see what this one is, if this one is. we began to calculate the fractional part, and previous similar, each tenfold decrease it.

If the expression is, then, before we continue that process, the expression -> term -> factor, constantly recursive solution.

For the evaluation of the expression, we first calculated the value of the first term, to see if there is a later, yes we can calculate, not ended. After the recursive because when performed after time, we are going to eat behind the characters, we used to put it away.

Finished using + - * / (after) we want to eat it, when we find items is the same process, seek first one, to determine whether there is, then to the next step, generally is the case.

/ * 
Recursive 
four arithmetic expression evaluates 
one hundred training: 4132 
Enter no space 
* / 
#include <the iostream> 
#include <CString> 
#include <the cstdlib> 
#include <The iomanip> // setPrecision () 

the using  namespace STD;
 Double factor_value ( );
 Double term_value ();
 Double expression_value (); 

int main () 
{ 
    Double ANS = expression_value (); 
    COUT << Fixed << setPrecision ( 2 ) ANS << << endl;
     return  0  ;
}

double expression_value() // term add minus
{
    // should have while loop, term add minus many times
    // (a+b+c+d)*b + c+ d 
    double result = term_value();

    bool more = true;
    while (more)
    {
        char c = cin.peek();
        if (c == '+' || c == '-')
        {
            char op = cin.get();
            double value = term_value();
            if (op == '+')
                result += value;
            else
                result -= value;
        }
        else
        {
            more = false;
        }
    }
    return result;
}

double term_value() // factor / *
{
    double result = factor_value();
    while (true) // why here is  while loop ? eg: a*b*c*d + a*b*c
    {
        char op = cin.peek();
        if (op == '*' || op == '/')
        {
            cin.get();
            double value = factor_value();
            if (op == '*')
                result *= value;
            else
                result /= value;
        }
        else
            break;

    }
    return result;

}

double factor_value()
{
    double result = 0;
    char c = cin.peek();
    if (c == '(')
    {
        cin.get();
        result = expression_value();
        cin.get();
    }
    else
    {
        while (isdigit(c))
        {
            result = result * 10 + c - '0';
            cin.get();
            c = cin.peek();
        }
        double tmp = 0;
        if (c == '.')
        {
            double base = 0.1;
            
            cin.get();
            char ch = cin.peek();
            while (isdigit(ch))
            {
                tmp = (ch-'0')*base + tmp;
                base = 0.1*base;
                cin.get();
                ch = cin.peek();
            }

        }
        result += tmp ;
    }
    return result;
}

 

Guess you like

Origin www.cnblogs.com/focus-z/p/11443202.html