Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of QT Calculator

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report