Formula string evaluation

Formula string evaluation

Title description

Given a string str, str represents a formula, the formula can have integers, addition, subtraction, multiplication, division, and left and right parentheses, and return the calculation result of the formula (note: all operations in the title are integer operations, rounded down, and guaranteed The data is legal, there will be no division by 0, etc.).

Enter a description:

Output a line of string, representing str (1 ≤ lengthstr ≤ 1000) str(1 \leq length_{str} \leq 1000)str(1lengthstr1 0 0 0 ) (Ensure that the result of str calculation will not be divided by zero, int overflow, etc.).

Output description:

Output an integer, which represents the calculation result of the expression.

Example 1
enter
48*((70-65)-43)+8*1
Output
-1816
Example 2
enter
3+1*4
Output
7

answer:

Expression evaluation usually converts the infix expression into a postfix expression, and then in the postfix expression, two stacks are used, one to save the operator and the other to save the number, which can be calculated from front to back.

This problem can also do so, in the infix expression into postfix process, priority should be noted that the operator, where "(" lowest priority, followed by addition and subtraction operator, the highest priority is Multiply and divide operator.

Note : There will be negative numbers in this question, and you need to judge it when you encounter the'-' operator.

Code:
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 1010;

char s[N];
int nums[N];
char ops[N];
int k1, k2;

int grade( char op ) {
    
    
    if (op == '(') return 1;
    if (op == '+' || op == '-') return 2;
    if (op == '*' || op == '/') return 3;
    return 0;
}

void calc( char op ) {
    
    
    int y = nums[ k1-- ];
    int x = nums[ k1-- ];
    int z;
    if ( op == '+') z = x + y;
    else if ( op == '-' ) z = x - y;
    else if ( op == '*' ) z = x * y;
    else z = x / y;
    nums[ ++k1 ] = z;
}

int solve() {
    
    
    k1 = k2 = -1;
    int top = 0, val = 0;
    for (int i = 0; s[i]; ++i) {
    
    
        if ( s[i] >= '0' && s[i] <= '9' ) {
    
    
            val = val * 10 + (s[i] & 15);
            if ( s[ i+1 ] >= '0' && s[ i+1 ] <= '9' ) continue;
            nums[ ++k1 ] = val;
            val = 0;
        }
        else if ( s[i] == '(' ) ops[ ++k2 ] = s[i];
        else if ( s[i] == ')' ) {
    
    
            while ( ops[k2] != '(' ) calc( ops[ k2-- ] );
            k2--;
        }
        else {
    
    
            if ( s[i] == '-' ) {
    
    
                if ( i == 0 || s[ i-1 ] == '(' ) {
    
    
                    val = 0, ++i;
                    while ( s[i] >= '0' && s[i] <= '9' )
                        val = val * 10 + (s[ i++ ] & 15);
                    nums[ ++k1 ] = -val;
                    val = 0, --i;
                    continue;
                }
            }
            while ( k2 >= 0 && grade( ops[k2] ) >= grade( s[i] ) )
                calc( ops[ k2-- ] );
            ops[ ++k2 ] = s[i];
        }
    }
    while ( k2 >= 0 ) calc( ops[ k2-- ] );
    return nums[0];
}

int main( void )
{
    
    
    scanf("%s", s);
    printf("%d\n", solve());
    return 0;
}

Guess you like

Origin blog.csdn.net/MIC10086/article/details/108918129