# Lesson 12 - calculator core analysis algorithm (I)

1. Suffix expression

Example:

2. Calculator core algorithm

Solutions

1. Separate infix expression from number and operator

2. Convert infix expression to suffix expression

3. Calculate the final result by suffix expression

3. Separation algorithm idea

Initial num variable is empty, scan string
If a number or decimal point is encountered, record it to the num variable and record the current character with the pre variable
If other characters are encountered (+ - * / ())
First look at the num variable. If it is not empty, put the value of num into the queue and empty num

If a sign is encountered, enter the queue, and pre records the current character

Criteria for judging sign: characters encountered are '+' or '-', and

Pre (previous character) is "" or '(' or '+' - '*' / '

4. Programming experiment

Expression separation algorithm

QCalculatorDec.h

```#ifndef _CALCULATORCORE_H_
#define _CALCULATORCORE_H_

#include <QString>
#include <QStack>
#include <QQueue>

class QCalculatorDec
{
protected:
QString m_exp;
QString m_result;

bool isDigitOrDot(QChar c);
bool isSymbol(QChar c);
bool isSign(QChar c);
bool isNumber(QString s);
bool isOperator(QString s);
bool isLeft(QString s);
bool isRight(QString s);
int priority(QString s);
QQueue<QString> split(const QString& exp);
public:
QCalculatorDec();
~QCalculatorDec();
bool expression(const QString& exp);
QString expression();
QString result();
};

#endif
```

QCalculatorDec.cpp

```#include "QCalculatorDec.h"

#include <QDebug>

QCalculatorDec::QCalculatorDec()
{
m_exp = "";
m_result = "";

QQueue<QString> r = split("+9.11 + ( -3 - 1 ) * -5 ");

for(int i=0; i<r.length(); i++)
{
qDebug() << r[i];
}
}

QCalculatorDec::~QCalculatorDec()
{

}

bool QCalculatorDec::isDigitOrDot(QChar c)
{
return (('0' <= c) && (c <= '9')) || (c == '.');
}

bool QCalculatorDec::isSymbol(QChar c)
{
return isOperator(c) || (c == '(') || (c == ')');
}

bool QCalculatorDec::isSign(QChar c)
{
return (c == '+') || (c == '-');
}

bool QCalculatorDec::isNumber(QString s)
{
bool ret = false;

s.toDouble(&ret);

return ret;
}

bool QCalculatorDec::isOperator(QString s)
{
return (s == "+") || (s == "-") || (s == "*") || (s == "/");
}

bool QCalculatorDec::isLeft(QString s)
{
return (s == "(");
}

bool QCalculatorDec::isRight(QString s)
{
return (s == ")");
}

int QCalculatorDec::priority(QString s)
{
int ret = 0;

if( (s == "+") || (s == "-") )
{
ret = 1;
}

if( (s == "*") || (s == "/") )
{
ret = 2;
}

return ret;
}

bool QCalculatorDec::expression(const QString& exp)
{
bool ret = false;

return ret;
}

QString QCalculatorDec::result()
{
return m_result;
}

QQueue<QString> QCalculatorDec::split(const QString& exp)
{
QQueue<QString> ret;
QString num = "";
QString pre = "";

for(int i=0; i<exp.length(); i++)
{
if( isDigitOrDot(exp[i]) )
{
num += exp[i];
pre = exp[i];
}
else if( isSymbol(exp[i]) )
{
if( !num.isEmpty() )
{
ret.enqueue(num);

num.clear();
}

if( isSign(exp[i]) && ((pre == "") || (pre == "(") || isOperator(pre)) )
{
num += exp[i];
}
else
{
ret.enqueue(exp[i]);
}

pre = exp[i];
}
}

if( !num.isEmpty() )
{
ret.enqueue(num);
}

return ret;
}
```

main.cpp

```#include <QtGui/QApplication>
#include "QCalculatorDec.h"

int main(int argc, char *argv[])
{
QCalculatorDec c;

return 0;
}```

5, summary

Each character in QString is QChar

Qt provides indispensable data structure classes in development

There are three steps to calculate the four expressions

- separation of numbers and symbols

- infix expression to suffix expression

- evaluate the result according to the suffix expression

Keywords: calculator Programming Qt

Added by wardo on Sat, 15 Feb 2020 22:07:31 +0200