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

MathStructure Calculator::calculate ( string  str,
const EvaluationOptions eo = default_evaluation_options,
MathStructure parsed_struct = NULL,
MathStructure to_struct = NULL,
bool  make_to_division = true 

Calculates an expression. The expression should be unlocalized first with unlocalizeExpression().

str Expression.
eo Options for the evaluation and parsing of the expression.
[out] parsed_struct NULL or a math structure to fill with the result of the parsing of the expression.
[out] to_struct NULL or a math structure to fill with unit expression parsed after "to".
make_to_division If true, the expression after "to" will be interpreted as a unit epxression to convert the result to.
The result of the calculation.

Definition at line 2160 of file Calculator.cc.

References EvaluationOptions::auto_post_conversion, convert(), CompositeUnit::countUnits(), MathStructure::eval(), CompositeUnit::generateMathStructure(), parse(), EvaluationOptions::parse_options, POST_CONVERSION_BASE, POST_CONVERSION_BEST, ParseOptions::preserve_format, separateToExpression(), MathStructure::set(), and MathStructure::setUndefined().

      string str2;
      separateToExpression(str, str2, eo);
      MathStructure mstruct;
      parse(&mstruct, str, eo.parse_options);
      if(parsed_struct) {
            ParseOptions po = eo.parse_options;
            po.preserve_format = true;
            parse(parsed_struct, str, po);
      if(!str2.empty()) {
            Unit *u = getUnit(str2);
            if(u) {
                  if(to_struct) to_struct->set(u);
                  return convert(mstruct, u, eo);
            for(size_t i = 0; i < signs.size(); i++) {
                  if(str2 == signs[i]) {
                        u = getUnit(real_signs[i]);
            if(u) {
                  if(to_struct) to_struct->set(u);
                  return convert(mstruct, u, eo);
            CompositeUnit cu("", "temporary_composite_convert", "", str2);
            if(to_struct) to_struct->set(cu.generateMathStructure(make_to_division));
            if(cu.countUnits() > 0) return convertToCompositeUnit(mstruct, &cu, eo);
      } else {
            if(to_struct) to_struct->setUndefined();
            switch(eo.auto_post_conversion) {
                  case POST_CONVERSION_BEST: {
                        return convertToBestUnit(mstruct, eo);
                  case POST_CONVERSION_BASE: {
                        return convertToBaseUnits(mstruct, eo);
                  default: {}
      return mstruct;

Generated by  Doxygen 1.6.0   Back to index