我有一个非常古老的 Pascal 程序。单元顶部是一组常量,如下所示:
feature1 = $01;
feature2 = $02;
feature3 = $04;
feature4 = $08;
feature5 = $10;
数据库中有一列代表属性,并且有上述特征组合的总和。我认为它可能非常有用,即使在今天它也可以让我们避免复杂的情况 条件块,但我想阅读更多相关内容并查看更多此类代码的解释良好的示例。因此,如果您之前看到类似的内容,我将不胜感激一些文章和练习的链接,因为我什至不知道我必须在 Google 中搜索什么。
这是一个使用 Free Pascal 中常量的示例
Program HexConstants;
Const
(* In Pascal a hexadecimal numeric literal is indicated by prefixing a numeric literal with a $ *)
feature1 = $01;
feature2 = $02;
feature3 = $04;
feature4 = $08;
feature5 = $10;
Var
features : integer;
procedure PrintFeatureDecimalValues();
Var
feature : integer;
begin
feature := feature1;
Writeln(' feature1 = ', feature);
features := feature2;
Writeln(' feature2 = ', feature2);
features := feature3;
Writeln(' feature3 = ', feature3);
features := feature4;
Writeln(' feature4 = ', feature4);
features := feature5;
Writeln(' feature5 = ', feature5);
Writeln('__________________________________');
end;
procedure ShowFeaturesThatAreSet(features : integer);
begin
if features = 0 then
Writeln('No features are set');
if features and feature1 = feature1 then
Writeln('Feature 1 is set');
if features and feature2 = feature2 then
Writeln('Feature 2 is set');
if features and feature3 = feature3 then
Writeln('Feature 3 is set');
if features and feature4 = feature4 then
Writeln('Feature 4 is set');
if features and feature5 = feature5 then
Writeln('Feature 5 is set');
Writeln('__________________________________');
end;
Begin
PrintFeatureDecimalValues;
features := feature1 + feature3;
ShowFeaturesThatAreSet(features);
features := feature4 + feature5;
ShowFeaturesThatAreSet(features);
features := 0;
ShowFeaturesThatAreSet(features);
features := feature1 + feature2 + feature3 + feature4 + feature5;
ShowFeaturesThatAreSet(features);
End.