Delphi uses state diagrams to determine the type of string data

xiaoxiao2021-03-06  14

Delphi uses state diagrams to determine the type of string data

When we write a program, you will always encounter data types for strings, such as integer, floating point. And we use the state map

to determine the type of string data.

Type the string to determine that we generally need to parse it, we need to identify its current state and the next time in the parsing process, such as integer, floating point. After analyzing, we can draw its final state so that our result of our type is the final state, through which we can reach the effect.

figure 1

There are five small circles in the figure represent five states (the circle of the dashed line represents the intermediate state, the solid circle represents the final state. Of course, there is still a state I have not drawn: Non-data types), they are: stateinit, respectively, StateDiag (Symbol), StateDot (decimal point), StateInt (integer), StateFloat, arrow indicates the parsing process of the string, and the rectangular representation parsed each character.

Let's take an example! Such as string: "- 19.2. Parlect it into '-' → '1' → '9' → '.' → '2'. According to Figure 1: Start status is Stateinit, parsing the first character '-' → getting its status is statediag,

Analysis of the second character '1' → get its state setint, parsing the character '9' → getting its state StateInt, parsing characters'. '→ Get it is statefloat → ... → get it to the last state is StateFloat. This way we get the final result: this string corresponds to floating-point data. Below is the Delphi source code :

{*****************************

// author = py

// Time =

20/06/04

// function = is integer (float) or not in string;

******************************

Unit Pfunction;

Interface

// Uses windows;

{* Result: 0. Integer

* 1. Float

* -1. Error}

Function isnumberrfloat (aenterstr: pchar): integer;

IMPLEMENTATION

Function isnumberrfloat (aenterstr: pchar): integer;

Function isnumber (anum: char): boolean;

Begin

Result: = FALSE;

IF (ANUM> = '0') and (Anum <= '9') THEN

RESULT: = true;

END;

Function issymbol (Anum: char): boolean;

Begin

Result: = FALSE;

IF ((ANUM = ' ') or (ANUM = '-')) THEN

RESULT: = TRUE;

END;

Function isdot (anum: char): boolean;

Begin

Result: = FALSE;

IF (ANUM = '.') THEN

RESULT: = TRUE;

END;

TYPE TSTATEBIT = (Stateinit, Stateint, StateDiag, StateDot, StateFloat, Error); // Static Enum

Var mch: char; // char of string

MI: Integer; // Circle Num

MINX: Integer; // length of string;

MSTATE: TSTATEBIT; // Current State

Begin

MINX: = Length (Aenterstr);

MSTATE: = stateinit;

RESULT: = -1;

For mi: = 0 to minx-1 do

Begin

MCH: = aenterstr [mi];

Case MState of

Stateinit: Begin

IF IsNumber (MCH) THEN Begin

MSTATE: = StateInt;

end

Else IF (Issymbol (MCH)) THEN Begin

MSTATE: = statediag;

end

Else if Isdot (MCH) THEN Begin

MSTATE: = stated;

end

Else MState: = Error;

END;

Stateint: Begin

IF Isdot (MCH) THEN Begin

MSTATE: = statefloat;

end

Else IF (IsNumber (MCH)) THEN Begin

MSTATE: = StateInt;

end

Else MState: = Error;

END;

StateDiag: Begin

IF IsNumber (MCH) THEN Begin

MSTATE: = StateInt;

end

Else if Isdot (MCH) THEN Begin

MSTATE: = stated;

end

Else MState: = Error;

END;

StateDot: Begin

IF IsNumber (MCH) THEN Begin

MSTATE: = statefloat;

end

Else MState: = Error;

END;

StateFloat: Begin

IF IsNumber (MCH) THEN Begin

MSTATE: = statefloat;

end

Else MState: = Error;

END;

END;

END;

IF (MState = StateInt) Then Result: = 0

Else if (mState = statefloat) THEN Result: = 1;

END;

End.

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

New Post(0)