valentina/container/calculator.h
dismine 2b1da2ac32 Documentation for classes.
--HG--
branch : feature
2013-10-07 18:34:14 +03:00

174 lines
5.2 KiB
C++

/****************************************************************************
**
** Copyright (C) 2013 Valentina project All Rights Reserved.
**
** This file is part of Valentina.
**
** Tox is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Tox is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <QString>
#include "vcontainer.h"
/**
* @brief The Calculator class calculate formulas of pattern. Support operation +,-,/,* and braces.
* Can replace name of variables her value.
*/
class Calculator{
public:
/**
* @brief Calculator class constructor.
* @param data pointer to a variable container.
*/
explicit Calculator(const VContainer *data);
/**
* @brief eval calculate formula.
* @param prog string of formula.
* @return value of formula.
*/
qreal eval(QString prog, QString *errorMsg);
private:
Q_DISABLE_COPY(Calculator)
/**
* @brief errorMsg keeps error message of calculation.
*/
QString *errorMsg;
/**
* @brief token current token.
*/
QString token;
/**
* @brief tok internal representation of token.
*/
qint32 tok;
/**
* @brief token_type type of token.
*/
qint32 token_type;
/**
* @brief prog string where keeps formula.
*/
QString prog;
/**
* @brief index number character in string of formula.
*/
qint32 index;
/**
* @brief data container of all variables.
*/
const VContainer *data;
/**
* @brief debugFormula decoded string of formula.
*/
QString debugFormula;
/**
* @brief get_exp calculate formula.
* @return value of formula.
*/
qreal get_exp();
/**
* @brief get_token return next token.
*/
void get_token();
/**
* @brief StrChr checks whether the character belongs to the line.
* @param string string with formula
* @param c character.
* @return true - belongs to the line, false - don't belongs to the line.
*/
static bool StrChr(QString string, QChar c);
/**
* @brief putback returns the readout token back into the flow.
*/
void putback();
/**
* @brief level2 method of addition and subtraction of two terms.
* @param result result of operation.
*/
void level2(qreal *result);
/**
* @brief level3 method of multiplication, division, finding percent.
* @param result result of operation.
*/
void level3(qreal *result);
/**
* @brief level4 method of degree two numbers.
* @param result result of operation.
*/
void level4(qreal *result);
/**
* @brief level5 method for finding unary plus or minus.
* @param result result of operation.
*/
void level5(qreal *result);
/**
* @brief level6 processing method of the expression in brackets.
* @param result result of operation.
*/
void level6(qreal *result);
/**
* @brief primitive method of determining the value of a variable by its name.
* @param result result of operation.
*/
void primitive(qreal *result);
/**
* @brief arith perform the specified arithmetic. The result is written to the first element.
* @param o sign operation.
* @param r first element.
* @param h second element.
*/
static void arith(QChar o, qreal *r, qreal *h);
/**
* @brief unary method changes the sign.
* @param o sign of symbol.
* @param r element.
*/
static void unary(QChar o, qreal *r);
/**
* @brief find_var method is finding variable by name.
* @param s name of variable.
* @return value of variable.
*/
qreal find_var(QString s);
/**
* @brief serror report an error
* @param error error code
*/
void serror(qint32 error);
/**
* @brief look_up Finding the internal format for the current token in the token table.
* @param s name of token.
* @return internal number of token.
*/
static char look_up(QString s);
/**
* @brief isdelim return true if c delimiter.
* @param c character.
* @return true - delimiter, false - do not delimiter.
*/
static bool isdelim(QChar c);
/**
* @brief iswhite checks whether c space or tab.
* @param c character.
* @return true - space or tab, false - don't space and don't tab.
*/
static bool iswhite(QChar c);
};
#endif // CALCULATOR_H