一.表达式分离算法分析
二.分离算法实现
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;
}
输出:
"+9.11"
"+"
"("
"-3"
"-"
"1"
")"
"*"
"-5"