Dan*_*Dan 0 delphi algorithm dictionary boolean-logic delphi-7
我必须在Delphi 7中实现一个决策矩阵.该函数是
CalcNewStatus(actionCode:string; reportType:string; currentStatus:string):string;
例如,在C#中我会使用字典.我怎么能在Delphi 7中做到这一点?
Rob*_*edy 10
将输入字符标准化为从零开始的序数值,事情变得更加容易.从几个类型声明开始:
type
TActionCode = (acA, acN);
TReportType = (rtI, rtF);
TStatus = (sP, sI, sF);
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用具有所有可能状态值的类型定义数组.替换sX为每个点中属于哪个状态值.
const
NextStatus: array[TActionCode, TReportType, TStatus] of TStatus = (
{acA} (// sP, sI, sF
{rtI} ( sX, sX, sX),
{rtF} ( sX, sX, sX)
),
{acN} (
{rtI} ( sX, sX, sX),
{rtF} ( sX, sX, sX)
)
);
Run Code Online (Sandbox Code Playgroud)
那你的功能就是这样:
function CalcNewStatus(const actionCode, reportType, currentStatus: string): string;
var
ac: TActionCode;
rt: TReportType;
s: TStatus;
const
StateChars: array[TState] of Char = ('P', 'I', 'F');
begin
Assert(actionCode <> ''); Assert(reportType <> ''); Assert(currentStatus <> '');
Assert(actionCode[1] in ['A', 'N']);
Assert(reportType[1] in ['I', 'F']);
Assert(currentStatus[1] in ['P', 'I', 'F']);
if actionCode[1] = 'A' then ac := acA else ac := acN;
if reportType[1] = 'I' then rt := rtI else rt := rtF;
if currentStatus[1] = 'P' then s := sP
else if currentStatus[1] = 'I' then s := sI
else s := sF;
Result := StateChars[NextStatus[ac, rt, s]];
end;
Run Code Online (Sandbox Code Playgroud)
如您所见,这些代码大部分用于在字符串和枚举类型之间进行转换.如果可以,请避免使用字符串.尽可能在程序中尽早切换到枚举类型,并且只在需要时才转换回字符串或字符.字符串可以具有任意长度,您实际上不应该处理它,并且字符串也可以具有超出您定义的范围的值.枚举不能,除非你做一些奇怪的事情.此外,编译器不会让你不小心使用期望TReportType的TState值,这将帮助你混淆你的I和F.