1.简要介绍
计算器的设计主要包含代码和ui两部分,ui较简单这里不做介绍,代码十分重要。这里计算器设计的思路,主要是对字符串进行处理,通过正确解析字符串,得到结果。但是四则运算存在优先级的问题,不能直接根据四则运算符进行解析,需要采用其他的方法。
这里引出表达式,项和因子的概念,因子主要包括负号、数和小括号,项主要包括因子和乘除,表达式则主要包括项和加减。一个计算式可以看成多个表达式的加减,一个表达式为因子的乘除,一个因子代表一个数或是一个小括号。据此可以设计出符合要求的计算器。
2.代码详解
//解析表达式,由公式字符串计算结果,
QVariant CalculatorWidget::evalExpression(const QString &str, int &pos) const
{
//先解析第一项
QVariant result = evalTerm(str, pos);
//当没达到字符串末尾时,依此计算前两项的值,并把计算的值赋给第一项,重复计算,直到计算完毕
while (str[pos] != QChar::Null) {
QChar op = str[pos];
//如果第一项后面不是+或-,则把结果返回
if (op != '+' && op != '-')
return result;
//如果后面为+或者-,则继续解析第二项
++pos;
//pos向后移动,获取第二项
QVariant term = evalTerm(str, pos);
//判断前两项是不是double,如果是,把结果计算出来,作为第一项的值,然后继续计算第三项
if (result.type() == QVariant::Double
&& term.type() == QVariant::Double) {
if (op == '+') {
result = result.toDouble() + term.toDouble();
} else {
result = result.toDouble() - term.toDouble();
}
}//如果不是double,结果无效
else {
result = Invalid;
}
}
//最后返回结果
return result;
}
//解析项,和解析表达式类似
QVariant CalculatorWidget::evalTerm(const QString &str, int &pos) const
{
//先获取第一个因子
QVariant result = evalFactor(str, pos);
//循环更新,先计算第一个因子和第二个因子的值,更新第一个,继续计算
while (str[pos] != QChar::Null) {
QChar op = str[pos];
if (op != '*' && op != '/')
return result;
++pos;
//解析第二个因子
QVariant factor = evalFactor(str, pos);
if (result.type() == QVariant::Double
&& factor.type() == QVariant::Double) {
if (op == '*') {
result = result.toDouble() * factor.toDouble();
} else {
//如果是除法,要判断除数不等于0
if (factor.toDouble() == 0.0) {
result = Invalid;
} else {
result = result.toDouble() / factor.toDouble();
}
}
} else {
result = Invalid;
}
}
return result;
}
//解析因子,最复杂,有括号,正负问题,处理括号问题时,用到了递归。
QVariant CalculatorWidget::evalFactor(const QString &str, int &pos) const
{
QVariant result;
bool negative = false;
//判断是否负号开头
if (str[pos] == '-') {
negative = true;
++pos;
}
//判断有没有左括号
if (str[pos] == '(') {
++pos;
//递归调用,括号里视为表达式
result = evalExpression(str, pos);
//直到右括号出现
if (str[pos] != ')')
result = Invalid;
++pos;
} else {
QString token;
//当前位置是字母或数字,或者等于.存入token
while (str[pos].isNumber() || str[pos] == '.') {
token += str[pos];
++pos;
}
bool ok;
result = token.toDouble(&ok);
if (!ok)
result = Invalid;
}
//如果以负号开头,对整个处理结果取负号
if (negative) {
if (result.type() == QVariant::Double) {
result = -result.toDouble();
} else {
result = Invalid;
}
}
return result;
}
以上就是三个最主要的函数,可以从一个字符串中解析得到计算结果。
3.运行结果
以上可以看出,计算器能够计算出正确的结果。
4.附录
代码链接https://download.csdn.net/download/com1098247427/21714828