Calculator iOS structures - data structure portion

This week learning task is preparing its own iOS calculator app, this blog is mainly used for the data structure to explain the problems encountered
first is to build calculator app based on objective -c language, (oc), the language is characteristic oc based on c language, but this data structure to use calculator expression evaluation, which is a stack of features, but does not stack oc file, you need to build their own stack Here Insert Picture Description
for this calculator on addition, subtraction, so here I am use of the structure, in order to achieve the effect of storage.
First of expression requires two memory storage stack is a character, and the other is to store operation results, so here model.h file declares two types of structures Here Insert Picture Description
some basic techniques then you need to write stack

StackHere Insert Picture Description

Pop

Here Insert Picture Description

Judgment is empty

Here Insert Picture Description

A distant operator priority symbolHere Insert Picture Description

This basically solved the problem oc in the stack of
the rest is in accordance with the language of c before you can basically achieved. Before you can see the details written blog stack of applications (expression evaluation)
but also have some basic implementation issues is the question of the digital output of
the output is like this before Here Insert Picture Description
but apparently this is not consistent with the needs of our needs is the need to have a fractional decimals so here it is the integer integer uses NSNumber
so that you can solve part of the problem output of the
Here Insert Picture Description
output follows Here Insert Picture Description
Here Insert Picture Description
first glance does not seem much of a problem, but a closer look there are still questions the accuracy of the conversion
follows
Here Insert Picture Description
Here Insert Picture Description
this is not to engage me?
In fact, this appears if you are careful people will find that there are structural problems defined above problem
Here Insert Picture Description
reason is because
when @ "0.01" is converted to float, often becomes 0.009999799 this form, as float type can not be accurately saved, the system will choose a closest value instead.

The double type can have better accuracy.
So instead double will be solved
as
Here Insert Picture Description
I write to you a little sigh must be rigorous, a little bit of a gap type will have a different result
, but seems to have found some problems is continuous decimal number addition and subtraction there will be some problems
, but at the prompt large data Gangster
find conversion method instead NSNumber * c = [NSNumber numberWithFloat: b];
there is no problem
** ** but why is this fact, I have a little is not clear at present
, but later I changed the structure of the double float or can
Baidu a moment there is no clear explanation
felt the issue should be the method of
feel this method should bring their own problems conversion accuracy,

Finally, in order for this app to add some methods, such as adding a brace matching, if not directly match the error
because it is relatively simple calculator only a small parenthesis, you can directly use a number appears on the left parenthesis ++, parentheses appear - if there At this time, the right parenthesis is an initial value of the error value, or if the initial value of the error is not completed until the determined
code to achieve
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Published 34 original articles · won praise 4 · Views 736

Guess you like

Origin blog.csdn.net/weixin_44824650/article/details/102258471