Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of QT Calculator

Shulou Source: shulou.com Published: 2022-06-02 06:35:32 10月09日 Update

This article mainly explains "case Analysis of QT Calculator". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "QT Calculator case Analysis"!

How are suffix expressions evaluated? At this point, you need to compare the numbers and operators in the suffix expression there.

If the current element is the operator: 1. Pop up the right Operand from the stack; 2. Pop up the left Operand from the stack; 3. Operate according to symbols; 4. Press the result of the operation into the stack. When the traversal ends, the only number in the stack is taken as the result of the operation.

To describe it in pseudo code is:

In this block, we have to take into account the division of mathematical operations (divided by 0). If it is a floating-point operation, we should avoid making an equal comparison with 0 directly in the code.

The specific code is:

QString QCalculatorDec::calculate (QQueue& exp)

{

QString ret = "Error"

QStack stack

While (! exp.isEmpty ()) {QString e = exp.dequeue (); if (isNumber (e)) {stack.push (e) } else if (isOperator (e)) {QString rp =! stack.isEmpty ()? Stack.pop (): "; QString lp =! stack.isEmpty ()? Stack.pop (): "; QString result = calculate (lp, e, rp); if (result! =" Error ") {stack.push (result) } else {break;}} else {break }} if (exp.isEmpty () & & (stack.size () = = 1) & & isNumber (stack.top () {ret = stack.pop ();} return ret;} the specific code for doing four operations is: QString QCalculatorDec::calculate (QString l, QString op, QString r) {QString ret = "Error" If (isNumber (l) & & isNumber (r)) {double lp = l.toDouble (); double rp = r.toDouble (); if (op = = "+") {ret.sprintf ("% f", lp + rp) } else if (op = = "-") {ret.sprintf ("% f", lp-rp);} else if (op = = "*") {ret.sprintf ("% f", lp * rp) } else if (op = "/") {const double p = 0.000000001; if ((- p)

< rp) && (rp < p) ) { ret = "Error"; } else { ret.sprintf("%f", lp / rp); } } else { ret = "Error"; } } return ret;} 那么我们就把逻辑相关的功能放在expression函数中: 我们在主函数中运行 (3 - 8) * (2 - 6) 这个表达式,结果如下:

At this point, I believe you have a deeper understanding of "QT Calculator case Analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Operations examples case analysis calculators analysis code results expressions contents functions suffixes that is operands numbers operators learning practical deeper four operations elements Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Shulou Tech Info macOS Apple