コード例 #1
0
ファイル: funky_read.c プロジェクト: gregghz/funky
static int literal_terminator_char(int inputChar) {
    return inputChar==EOF || character_is_break_out_token(inputChar) ||
        is_whitespace(inputChar) || 
        is_paren(inputChar) || 
        is_quotation(inputChar);
        
}
コード例 #2
0
int ExpressionManager::get_precedence(char op){
    int paren_type = is_paren(op);
    if(paren_type != NOT_A_PAREN){
        //then it is a paren
        return paren_type; 
    }
    //otherwise it is an op
    char op_str[1];
    op_str[0] = op;
    op_str[1] = '\0';
    int index = strcspn(operators, op_str);
    return op_precedence[index];

}
コード例 #3
0
string ExpressionManager::infixToPostfix(string infixExpression){
    stack<char> symbols;
    string out_str = "";
    //do tests for the validity of the infix expression
    if(!isBalanced(infixExpression)){
        return invalid;
    }
    if(!op_num_ratio_check(infixExpression)){
        return invalid;
    }

    char exp_str[STR_BUFF_SIZE];
    exp_str[0] = '\0';
    //create a C string from the expression string
    //for use in strtok
    strcat(exp_str, infixExpression.c_str());

    char * token;

    //use strtok to tokenize the string
    //separating by spaces

    token = strtok(exp_str, " ");
    while( 1 ){
        if(token == NULL){
            while(!symbols.empty()){
                out_str += symbols.top();
                out_str += " ";
                symbols.pop();
            }
            break;
        }
        //cout << "token: " << token << "\n";
        if(is_int_num(string(token))){
                out_str += string(token);
                out_str += " ";
        } else if(is_paren( *token ) == OPEN_PAREN){
            symbols.push( *token );
        } else if(is_paren( *token ) == CLOSE_PAREN){
            char top_char = symbols.top();
            symbols.pop(); 
            while(get_precedence(top_char) > OPEN_PAREN){
                //symbols.pop();
                out_str += top_char;
                out_str += " ";
                top_char = symbols.top();
                symbols.pop();
            }
        }  else if(is_op( *token )){
            if(symbols.empty()){
                symbols.push( *token );
            } else {
                char top_char = symbols.top();
                int top_prec = get_precedence(top_char);
                int token_prec = get_precedence( *token );
                while( top_prec >= token_prec ){
                    //go through any tokens with greater than or equal precedence
                    symbols.pop();
                    out_str += top_char;
                    out_str += " ";
                    if(symbols.empty()){
                        break;
                    }
                    top_char = symbols.top();
                    top_prec = get_precedence(top_char);
                }
                symbols.push( *token );
            }
        }
        //increment to the next token
        token = strtok(NULL, " ");
    }
    
    //to remove the last part of the string (the trailing space)
    //if using C++11 you can do out_str.pop_back();
    out_str.erase(out_str.end() - 1);

    return out_str;
}