1. 程式人生 > >"括號匹配, 中綴表示式轉化為字尾表示式, 計算中綴表示式, 計算字尾表示式"完整程式碼

"括號匹配, 中綴表示式轉化為字尾表示式, 計算中綴表示式, 計算字尾表示式"完整程式碼

核心程式碼:

----------------

/* Calculate infix expression */
double 
calc_infix_expression(const char *infixexp)
{
        Stack operator_stk, operand_stk;
        char c = '(', top;
        double partial_res, exp = 0.1;
        double left_operand, right_operand;
        int is_still_digit = 0, is_after_punct = 0;
        
        operand_stk = stack_create(sizeof(double), NULL);
        operator_stk = stack_create(sizeof(char), NULL);
        stack_push(operator_stk, &c);	/* Ensure the stack is not empty */


        while ( (c = *infixexp++) != '\0' ) {
                if ( isdigit(c) || c == '.' ) {		/* 1. operand: digit & '.' */
                        if ( !is_still_digit ) {
                                partial_res = 0;
                                is_still_digit = 1;
                        } 
                        if ( c == '.' )	/* '.' */
                                is_after_punct = 1;
                        else {		/* digit */
                                if ( !is_after_punct )
                                        partial_res = partial_res * 10 +  (c - '0');
                                else {
                                        partial_res += (c - '0') * exp;
                                        exp *= 0.1;
                                }
                        }	
                } else {				/* others */
                        if ( is_still_digit ) {
                                stack_push(operand_stk, &partial_res);
                                is_still_digit = 0;
                                is_after_punct = 0;
                                exp = 0.1;
                        }
                        if ( c == ')' ) {		/* 2: ')' */
                                while ( stack_top_and_pop(operator_stk, &top), top != '(' ) {
                                        stack_top_and_pop(operand_stk, &right_operand);
                                        stack_top_and_pop(operand_stk, &left_operand);
                                        partial_res = calculate(top, left_operand, right_operand);
                                        stack_push(operand_stk, &partial_res);
                                }
                        } else if ( is_operator(c) ) {	/* 3: operators */
                                for ( stack_top(operator_stk, &top);
                                      top != '(' && priority(c) <= priority(top);
                                      stack_top(operator_stk, &top)) {
                                        stack_top_and_pop(operand_stk, &right_operand);
                                        stack_top_and_pop(operand_stk, &left_operand);
                                        partial_res = calculate(top, left_operand, right_operand);
                                        stack_push(operand_stk, &partial_res);
                                        stack_pop(operator_stk);
                                }
                                /* Push the current operator */
                                stack_push(operator_stk, &c);
                        }
                        
                }
        }

        if ( is_still_digit ) {
                stack_push(operand_stk, &partial_res);
                is_still_digit = 0;
                is_after_punct = 0;
                exp = 0.1;
        }

        while ( !stack_is_empty(operator_stk) ) {
                stack_top_and_pop(operator_stk, &top);
                if ( top != '(' ) {
                        stack_top_and_pop(operand_stk, &right_operand);
                        stack_top_and_pop(operand_stk, &left_operand);
                        partial_res = calculate(top, left_operand, right_operand);
                        stack_push(operand_stk, &partial_res);
                }
        }
        stack_top_and_pop(operand_stk, &partial_res);

        return partial_res;	
}

示例:

----------------