Logo Search packages:      
Sourcecode: kbruch version File versions  Download package

ratio task::solve (  )

solves the task and returns the result as ratio

solves the given task and returns the result as a ratio

Definition at line 240 of file task.cpp.

References op_vector, product(), ratio_vector, and ratio::reduce().

Referenced by TaskView::showResult().

{
    ratio ergebnis (0, 1); /* that is the starting point */

    /* this is our pointer on the ratio_vector, set it to the beginning */
    RatioArray::iterator ratio_pointer = ratio_vector.begin();

    /* add a temp operation at the beginning */
    op_vector.insert (op_vector.begin(), ADD);

    /* this is our pointer on the op_vector, set it to the beginning */
    ShortArray::iterator op_pointer = op_vector.begin() + 1;

    /* check, if ratio number and operation number fit together */
    if (ratio_vector.size() != op_vector.size()) {
        kDebug() << "Number of ratios and operations do not fit.";
        return ergebnis;
    }

    do {
        /* we have to decide our next action by the given operation */
        switch (*op_pointer) {
        case ADD :
        case SUB :
            switch (* (op_pointer - 1)) {
                /* we only have to add/sub the next ratio */
            case ADD :
                ergebnis = ergebnis + *ratio_pointer++;
                break;
            case SUB :
                ergebnis = ergebnis - *ratio_pointer++;
                break;
            }
            break;
        case MUL :
        case DIV :
            switch (* (op_pointer - 1)) {
                /* the next ratio is a product, so we have to
                 * compute this product first and than add/sub it */
            case ADD :
                ergebnis = ergebnis +
                           product (ratio_pointer, op_pointer);
                break;
            case SUB :
                ergebnis = ergebnis -
                           product (ratio_pointer, op_pointer);
                break;
            }
            break;
        }
        /* check if we reached the and of the task */
        if (ratio_pointer == ratio_vector.end())
            break;

#ifdef DEBUG
        kDebug() << "Schleifenende";
#endif

    } while (++op_pointer != op_vector.end());

#ifdef DEBUG
    kDebug() << "after do while in solve()";
#endif

    /* if the last operation was an add/sub we haven't add/subed it until now */
    --op_pointer;
    switch (*op_pointer) {
    case ADD :
        ergebnis = ergebnis + *ratio_pointer;
        break;
    case SUB :
        ergebnis = ergebnis - *ratio_pointer;
        break;
    }

    /* erase the temp operation */
    op_vector.erase (op_vector.begin());

    /* before we return the result we have to reduce it */
    ergebnis.reduce();

    return ergebnis; /* return the solution */
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index