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

MathStructure * Calculator::calculateRPN ( MathOperation  op,
const EvaluationOptions eo = default_evaluation_options,
MathStructure parsed_struct = NULL 
)

Applies a mathematical operation to the first and second value on the RPN stack. The the second value is changed with input from the first value. For example, with OPERATION_SUBTRACT the first value is subtracted from the second. The first value on the stack is removed. If not enough registers is available, then zeros are added.

Parameters:
op Operation.
eo Options for the evaluation and parsing of the expression.
[out] parsed_struct NULL or a math structure to fill with the unevaluated result.
Returns:
The first value on the stack.

Definition at line 1865 of file Calculator.cc.

References MathStructure::add(), MathStructure::clear(), MathStructure::eval(), MathStructure::set(), and MathStructure::transform().

                                                                                                                   {
      MathStructure *mstruct;
      if(rpn_stack.size() == 0) {
            mstruct = new MathStructure();
            mstruct->add(m_zero, op);
            if(parsed_struct) parsed_struct->clear();
      } else if(rpn_stack.size() == 1) {
            if(parsed_struct) {
                  parsed_struct->clear();
                  if(op == OPERATION_SUBTRACT) {
                        parsed_struct->transform(STRUCT_ADDITION, *rpn_stack.back());
                        (*parsed_struct)[1].transform(STRUCT_NEGATE);
                  } else if(op == OPERATION_DIVIDE) {
                        parsed_struct->transform(STRUCT_DIVISION, *rpn_stack.back());
                  } else {
                        parsed_struct->add(*rpn_stack.back(), op);
                  }
            }
            mstruct = new MathStructure();
            mstruct->add(*rpn_stack.back(), op);
      } else {
            if(parsed_struct) {
                  parsed_struct->set(*rpn_stack[rpn_stack.size() - 2]);
                  if(op == OPERATION_SUBTRACT) {
                        parsed_struct->transform(STRUCT_ADDITION, *rpn_stack.back());
                        (*parsed_struct)[1].transform(STRUCT_NEGATE);
                  } else if(op == OPERATION_DIVIDE) {
                        parsed_struct->transform(STRUCT_DIVISION, *rpn_stack.back());
                  } else {
                        parsed_struct->add(*rpn_stack.back(), op);
                  }
            }
            mstruct = new MathStructure(*rpn_stack[rpn_stack.size() - 2]);
            mstruct->add(*rpn_stack.back(), op);
      }
      mstruct->eval(eo);
      autoConvert(*mstruct, *mstruct, eo);
      if(rpn_stack.size() > 1) {
            rpn_stack.back()->unref();
            rpn_stack.erase(rpn_stack.begin() + (rpn_stack.size() - 1));
      }
      if(rpn_stack.size() > 0) {
            rpn_stack.back()->unref();
            rpn_stack.back() = mstruct;
      } else {
            rpn_stack.push_back(mstruct);
      }
      return rpn_stack.back();
}


Generated by  Doxygen 1.6.0   Back to index