Use object-oriented method to solve 24 points

zhaozj2021-02-11  177

Algorithm idea

Calculate 24 points, you can abstract description as: subsystem all operational results of all operations of 2444. Under normal circumstances, "Sinteger; , -, *, /> have many properties, such as exchange law, i j = j i, combined law, i (j k) = (i j) k, etc., In order to make me write the code convenient, I removed all the rules (this makes a few times, huh, I believe it can be used to the oven), and add a one-yuan operator ~, ~ i = II belongs to Sinteger Plus a rule that seems to undermine mathematics, the elements in Sinteger can only use it once in an operation, and if this "all operations" will be an unlimited set.

The third generation language cannot be directly operated, so make a conversion, think of the way to use all the operations of the algebraic system <{A, B, C}; , *, /> (A B * C, AB * C, A B / C, (A B) * c ........) use other methods.

Let's use o to represent the binary operator, OI represents the i-th binary operator. {AO1B, AO2B, AO3B, AO1C, AO2C, AO3B, AO1C, AO2C, AO3C ....} are used in [<{{A}, {B]}, which is to return to the collection {a}, {B, C} The calculation results of Cartesian.

[<{a}; ~>] denotes {a}, that is, returns the calculation result on the collection. I remember that I have dropped a lot of nature, so I may not equal B A, so I have to calculate the A B C and C A B, not to push it out. Below is [< ,;>] Description "Act in all calculations of the algebraic system <{A, B, C}; , -, *, />".

[<{a}; ~>], [<{c}; ~>]; oi>]; oi>]

[<{A}; ~>], [<{c}; ~>]; oi>]; oi>]

[<{b}; ~>], [<{c}; ~>]; oi>]; oi>]

[<{B}; ~>], [<{c}; ~>]; oi>]; oi>]

.........

[<{A}; ~>], [<{c}; ~>]; , ->]; , ->] =

[<{A}; ~>], [<{b}, {C}; , ->]; , ->] =

[<{a}; ~>], {(b c), (b-c)}; , ->] =

[<{a}, {(B C), (B-C)}; , ->] =

{A (B C), A (B-C), A- (B C), A- (B-C)}

.......................

Do you find a lot of repetitions, maybe I have a few times more computation above, and the at least a grade number of grade numbers increase. After a round of damage to the math world, it has finally made the problem simplifies the computer.

I remember that the last mathematics class is two years ago, I don't know if the above content is correct.

Draw class diagram

The most effective way to mention the collections in many places above, of course, using iTertor mode, so first define a TITERATOR base class, and later collections are its subclasses.

Observe [<,;>], it consists of three parts (one yuan, only two parts), the comma left, the comma right, the left sign right, where the comma is all [<,;>], the numeral is an operator Now it is now converted into a class TNode. The numeral is a series of operators, so use the TOPERATORITORATOR.

I used [<,;>] description "role in all calculations of the algebraic system <{A, B, C}; , -, *, />". Observing the above, "Algebraic System <{A, B, C }; , -, *, /> "is represented by multiple [<,;>], therefore also requires a TITERATOR, named TTREEITERATOR. Below is a class diagram drawn with ModelMaker 6.2.

The TtreeBuilder is used to build TNODE, such as creating one: [<{a}; ~>], [<{b}; ~>], [<{c}; ~>]; oi> ]; Oi>]

IClientInterface is a minimized interface, most of the secondary developed people will not deal with complex internal structures.

Drawing sequence diagram

When you do this, I want to describe the internal workflow, the activity map is a good method, taking into account the development method of RUP (mainly I will only a little UML), so I decided to draw a sequence map, but painting After the sequence map, the code is written, it may be too simple, there is a chance to find a complex problem. In the sequence diagram, I used some less standard representations to describe the return of the function.

(For the overall layout, I narrowed the picture, a little distortion, please use the picture tool to open this picture)

This is a sequence diagram of all calculations in [<,;>]. Actor first informs TNODE to restart, follow the first calculation result (Eventuate, {A (B C), A (BC), A- (B C), A- (BC)} A (B C)), use Evaluate to evaluate whether there is still an arithmetic (when there is still operator, the representative also has an arithmetic formula, of course, first check the comma-bilateral [<,;>], if they have expression The form of the formula is that there is an expression, there is no need to see if there is still an operator), if there is an evaluation is true, continue to take results with Eventuate. "Continue" is easy, but the painting is difficult, I can't find the corresponding loop symbol in Modelmaker, so I use a vertical line representation range, and the text is used to exit the conditions.

Below is the weird code I wrote in this picture:

function TNode.Evaluate: Boolean; varLeftBool, RightBool: Boolean; beginResult: = false; if (FLeftChild = nil) And (FRightChild = nil) thenbeginResult: = not FOperatorIterator.IsDone; if Result thenFOperatorIterator.Next; Exit; end; LeftBool: = FLeftChild.Evaluate; if LeftBool thenbeginResult: = true; Exit; end; RightBool: = FRightChild.Evaluate; if Rightbool thenbeginFLeftChild.Resume; Result: = true; Exit; end; if not FOperatorIterator.IsDone thenbeginFOperatorIterator.Next; FLeftChild.Resume FRIGHTCHILD.RESUME; Result: = true; exit; end; end; if it feels weird.

(FleftChild = nil) and (frametchild = nil) This sentence detects if this is only one yuan of operator [<; ".

See here, everyone should understand the correspondence between them.

"The role in algebraic system <{a, b, c}; , -, *, />" is a big collection,

Divided into multiple subsets, respectively

, -, *, / actuator (A, B, C)

, -, *, / role (A, C, B)

, -, *, / actuator (B, A, C)

...........

Then extract each element from these subset.

, -, *, / acts on a tetle (A, B, C), using TNODE, which completely sets the TTREEITORATOR, and below is the sequence diagram related to it

function TClientInterface.GetAnswer (aNumArr: IntArray): TStringList; varFResult: TStringList; TI: TTreeIterator; ND: TNode; Num: Double; function GetResult: TStringList; beginif not Assigned (FResult) thenFResult: = TStringList.Create; Result: = FResult ; end; begin {TODO -cMM: interface wizard: Implement interface method} fResult: = nil; TI: = TTreeIterator.Create (aNumArr); TI.First; while true dobeginND: = TI.CurrentItem; ND.Resume; repeattryNum: = Nd.Eventuate; if tcheck.check (num) ThengetResult.add (nd.print '=' floattostr (num)); Except // Capture zero-free exception End; Until (not nd.evaluate); if Ti.isdone Ti.NEXT; END; RESULT:

Actor takes a subset from the TTREEITERATOR, then takes all the elements of the subset. After the element is taken, then take a subset of it all the time.

You may not be able to read a UML book, you can't find the words released by several resources. This should be the trend of technology development, just like today, no one will consider 640 restrictions, so I didn't write this part in my code. I hope to learn is the future technology. Some experiences using ModelMaker

After changing the source code, you must press to keep synchronization with Model. If you forget it, you may see this dialog.

At this time, it is best to press NO, if you press Yes, you may be deleted in Delphi's hard-written code. If you feel that you often use Refresh in Model, you can keep synchronization below.

2. This should be a bug

The combination and aggregation relationship in Modelmaker is actually the same line. Pay attention to it, don't understand the mistake.

3. It is another bug

The Singleton mode in Modelmaker actually generated an error code, I originally want to use this mode in TMonitor.

class function TForm1.AccessInstance (Request: Integer): TForm1; const FInstance: TForm1 = nil; begincase Request of0:; 1: if not Assigned (FInstance) then FInstance: = CreateInstance; 2: FInstance: = nil; elseraise Exception.CreateFmt ('Illegal Request% D in AccessInstance', [Request]); End; Result: = FINSTANCE; END;

The ModelMaker has not much design patterns, and it is also a big defect. Pascal language does not seem to increase