Using Qt achieve calculator function

Learn more Embedded Linux, Qt and embedded microcontrollers public concern number "playing embedded":

                                                                            

Before training requirements we use Qt implement functions of the calculator, I use a single linked list to achieve the functions of the calculator, will be the source to others.

We first of its implementation and the basic functions listed for everyone

Source link: https://download.csdn.net/download/Groot_Lee/11991924 within or public number "playing embedded" Reply qita0001 get

I. Implementation

Because I have to use a linked list to achieve the function, so I am going to use the knowledge to explain this part of the list. However, this tutorial will not cover the basics of the list.

1, the data stored in each list

This part is very simple, that is, every time I type a value before encountering operation symbol (+, -, *, /, ()), will deposit my a global variable, when faced with operators, create two a list of memory, I will be before you type the numbers and operators respectively stored in two memory. When the equal sign occurs, starts reading the content list, which relates to and subtract, multiply and divide operations and priority determination.

2, and the arithmetic operations and parentheses priority determines

About priority. We first detected a high priority operator, in this case, we are first detected there are no parentheses, brackets have first to be operational in brackets, no brackets continue to go down, detect multiply and divide, there is arithmetic, do not continue to go down computing addition and subtraction.

Operator on operator.

(1) We introduce arithmetic operations, addition, subtraction because the principle is the same, so we chose the same description just fine.

 

The figure is what I typed 2 + 3, when I enter the equal sign, the program finds the + sign this node, he's on one node and the next node must be numeric computation we need, so we found a plus sign a node and the next node is 2, and 3. Because we judge to be a plus, so call "plus" of this function, 3 and 2 together, the resulting new value of 5

                                           

5 will be assigned to the node where the position of 2, 3 and processing nodes note at this time is not changed. Then the next operation is to our + and 3 deleted, and point to the next node address where the address 3 of 5 next, our example only double-digit operation, it should be pointing to the next address, etc. node number is located.

  

At this time, the program determines the next node of the equal sign is NULL, so the end of this operation, is output to the screen 5.

(2) Next comes what is in brackets operations. Priority method for determining brackets with the outer parentheses are the same, so no longer introduced.

                               

This figure is a calculation formula with the bracket 3 * (5 + 3). We have detected brackets, so the first operation in brackets. We first detected is right parenthesis, right parenthesis at this time we have set ourselves as a head node definition, then left parenthesis detected, the detected left parenthesis left parenthesis we believe is set to become the tail node. Then the cycle parentheses head node to define our own tail node. When we find a plus, his previous and next node must be operational data we want, we will be a good op 5 + 3 = 8 where 5 to replace the contents of the node.

At this point arithmetic formula becomes so, then the next node 8 where we will address refers to a right parenthesis, and 3 + memory will be deleted.

 

In this case the formula is simplified into a so. Next, remove the left and right brackets, should operation at this time, the next address at the address * No. 8 is located, the next address is located 8 refers to a node address of the right bracket, the left and right brackets deletion node is located.

 

Simplified equation become like this, when the above operation and 3 + 5 is the same as the operation carried out, is no longer repeated.

Second, the basic functions and precautions            

Function
1, Math
2, parentheses operator and parentheses highest priority
3, a clear key
4, point operation

Shortcuts
1,0-9 and 0-9 on the keyboard correspond to decimal and decimal
2, back delete: Backspace
. 3, is cleared: Delete
. 4, corresponding to the left and right on the keyboard bracket about the brackets
5, equal to: numeric keypad Enter
. 6, subtraction multiplication and division arithmetic operations corresponding to the numeric keypad

规则
1、不允许嵌套小括号,只允许一层,但允许有多个
    例如 : 1+(2*(3+2))不允许;1+3*(2+3)+4*(2+2)允许。
2、乘法必须加括号,不允许省略
3、首先输入符号则在首端补0
4、如果以符号结尾则默认符号与等于之间为0

错误提示
1、符号书写错误1 : 连续出现两个或者以上运算符
2、括号错误1 : 左右括号总数是奇数
2、括号错误2 : 左右括号写反
3、括号错误3 : 连续出现两个左括号
4、括号错误4 : 出现类似n(x+x)
5、括号错误5 : 出现类似(x+x)n

发布了24 篇原创文章 · 获赞 35 · 访问量 1万+

Guess you like

Origin blog.csdn.net/Groot_Lee/article/details/103211381