A high-efficiency algorithm for account code using tree structure

zhaozj2021-02-08  194

A high-efficiency algorithm for account code using tree structure

Matsumoto Electric Industrial Co., Ltd. Computer Department

Shu Yu

---- In many common financial software, subject code is generally displayed in a tree structure. To achieve this, the usual practice is to use multiple (nested) cycles, even recursive, etc., to "weave" into a tree, but not only the algorithm is complex, but also low execution efficiency. I have explored a simple and efficient algorithm in actual development applications, and in this and the basin, only the best solutions are found in the throwing bricks. The implementation method in Delphi is described below.

One. Table Structure

---- First establish the data table code.db of the following structure, and enter some test data:

Field name Type length Description

Acode Character Type 20 Account Code

Aname Character Type 30 Accate Code Name

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

Table I)

---- Among them, the data type of the account code acode must be characterized by a character (must), the length is determined by specific requirements, if you want to support the six-level encoding, and the code structure is "3-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2 ", The length of this field is not less than 18, while other fields do not require. In addition, you have to build an index for the field ACODE (remember) because it is sorted with it.

two. Programming

---- 1. Create a new project: CodeTree.drp, the main form is named frmmain, and the unit stores main.pas. Add a TTREEVIEW control to frmmain, named TVECode, aimagelist, named Imgicon, and load three icon and bmp, finally add a TTABLE control, name TBLCode.

FRMMAIN and the properties of each control are set by Table (2):

Component property settings

FRMMAIN CAPTION 'Accident Code'

Font Song No. 9

Borderstyle Bsdialog

TVWCode IMGICON

Readonly True

IMGICON ImageList is loaded into three icons

BtnClose Caption (C)

Table II)

---- 2. The complete source code for unit main.pas is as follows:

Unit main;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics,

Controls, Forms, Dialogs,

DB, DBTABLES, COMCTRLS, IMGLIST, STDCTRLS;

Type

TFORM1 = Class (TFORM)

TTREEVIEW;

TBLCODE: TTABLE;

ImageList1: timagelist;

Btnclose: tbutton;

Procedure formcreate (Sender: TOBJECT);

Procedure BtnCloseclick (Sender: TOBJECT);

Private

{Private Declarations}

Function LoadCode (Crtbl: TdbDataSet): Integer;

Function Getle: SFORMAT, Scode: String: Integer; Public

{Public declarations}

END;

VAR

FORM1: TFORM1;

Const

ScodeFormat = '322222'; // Account Code Structure

SfirstNODETXT = 'account code'; // The first node displayed

IMPLEMENTATION

{$ R * .dfm}

// The following function is the key part of this article,

Its main function is to use a cycle in the code.db table

// Account code and account code name display

Function TFORM1.LOADCODE (CRTBL: TDBDataSet): Integer;

Var NowID, SNAME, Showtxt: String;

I, level: integer;

MYNODE: ARRAY [0..6] of ttreenode;

/ / Save all level nodes and support 6 levels (focus)

Begin

Screen.cursor: = CRHOURGLASS;

Level: = 0;

WITH CRTBL DO

Begin

Try

IF NOT ACTIVE THEN OPEN;

First;

TVWcode.Items.clear;

// The following is an increase in the first item

MyNode [Level]: = tvwcode.Items.add

(TVWCode.topItem, SfirstNODetxt);

MyNode [Level] .imageIndex: = 0;

MyNode [Level] .SelectedIndex: = 0;

// The above is the first item

While not Eof do

Begin

Nowid: = trim (FieldByName ('acode'); asstring);

Showtxt: = NOWID '' FieldByname ('Aname'). Asstring;

Level: = GetLevel (ScodeFormat, NOWID);

// Return the number of collars

/ / The following is an increased child

// The following uses a sub-node to the parent node with the previous node

if level> 0 THEN / / Make sure the code meets the standard

Begin

MyNode [level]: = tvwcode.Items.addchild

(MyNode [Level-1], Showtxt);

MyNode [Level] .imageIndex: = 1;

MyNode [level] .selectedIndex: = 2;

END;

// The above is increasing child

NEXT;

END;

Finally

CLOSE;

END;

END;

MYNODE [0] .expand (false); // Expand the first node

Screen.cursor: = CRDEFAULT;

END;

// The above function displays the account code and account code name in the code.db table.

/ / The function of the following function is to return a class number of code,

Parameter SFormat Transfer account code structure;

// Parameter scode passes a certain account code

Function TFORM1.GETLEVEL

(SFormat, Scode: String): Integer;

Var I, Level, Ilen: Integer

Begin

Level: = - 1; // Return -1 if the code does not meet the standard

Ilen: = 0;

IF (SFormat <> ') and (scode <>') THEN

For i: = 1 to length (srmat) do

Begin

Ilen: = Ilen STRTOINT (SFormat [i]);

If longth (scode) = Ilenbegin

Level: = i;

Break;

END;

END;

Result: = Level;

END;

/ / The function of the above function is to return a class number of code.

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

With tblcode do

Begin

DatabaseName: = paramstr (1);

// Make TBLCODE's DatabaseName points to the path where the application is located

Tablename: = 'code.db'; // Point to Data Table Code.db

Open;

IndexfieldNames: = 'acode';

/ / Sort by field ACODE (do not miss)

END;

Loadcode (tblcode);

END;

Procedure TFORM1.BTNCLOSECLICK (Sender: TOBJECT);

Begin

CLOSE;

END;

End.

---- Among them, constant scodeformat is the code structure of account code, and its definition rules must match the values ​​of field ACODE in the data table code..db. So in practical applications, when users add account code, they must strictly check their norms, only the library can only be added in accordance with the code structure defined in advance.

---- Function GetLevel is the number of levels of the account code, for example, there is a subject code "10102", in the case of "322222", the code structure Getlevel ('322222', '10102') will return Integer 2.

---- Of course, the core of this article is a loadCode function that uses a loop to traverse all records of the data table code.db, display the contents of the field acode and anames according to the level. In this function, the two-dimensional array MYNODE [0..6] is highly important, here, it acts like a stack similar to the recursive. Because in the TTREEVIEW to add a sub-node, addChild (Node: Ttreenode; Const S: String), you want to specify a parent node as a parameter, while the number of code levels of the parent node must be added to the code level of the node to reduce 1 So just use an array to dynamically save and specify that the parent node is successful.

three. operation result

---- Well, now copy the code.db to the same directory as the executable, press the F9 key to compile operation, the effect of this person is as shown in Figure (1). As long as it is improved on the above basis, if you increase the maintenance function, you can move to the actual application. Of course, this algorithm can not only be used on account code, and other similar tree structures can work. I have applied this algorithm to [account code], [Material List (BOM)], [Warehouse Management] and [Material Main Document] and other modules to achieve satisfactory results.

---- The above program is compiled in Chinese Windows 9X, Delphi 4 C / S environment.

转载请注明原文地址:https://www.9cbs.com/read-2247.html

New Post(0)