人物:编程神仙:一个比较臭屁的老家伙,但是是Delphi好手。以下简称神仙。
编程精灵:一个头脑有待开发的初学者,总是问一些低级问题,所以经常被神仙敲脑壳。以下简称精灵。一、有关Case关键字与字符串精灵:嗯,嗯,嗯……神仙:你便秘吗?我可以给你几分钟去厕所。精灵:好麻烦啊,我该怎么弄呢,对于一个字符串变量作判断之后去做别的。Case不支持字符串。这些系统设计员,讨厌的要死,设计开关基础数据怎么是这个:1001A, 1001B, 1001C, 1001D…… 又不能用整形。气死了!神仙:我看看你写的过程……精灵(满得以的)Show出了他的过程,如下:procedure SelectType(aType: string);
begin
  if SameText(aType, '1001A') then
  begin
    //Do Something...
  end
  else if SameText(aType, '1001B') then
  begin
    //Do Something...
  end
  else if SameText(aType, '1001C') then
  begin
    //Do Something...
  end
  else if SameText(aType, '1001D') then
  begin
    //Do Something...
  end;
end;
.
.
.精灵:哎哟!你怎么又打我?神仙:首先,你这些else有作用吗?又不是整形或者集合,有else与这样的下坠的连续if有区别吗?  if SameText(aType, '1001A') then
  begin
    //Do Something...
  end;
  if SameText(aType, '1001B') then
  begin
    //Do Something...
  end;
  if SameText(aType, '1001C') then
  begin
    //Do Something...
  end;
  if SameText(aType, '1001D') then
  begin
    //Do Something...
  end;其次,要是数据多了,你也这样?另外如果是动态的数据呢?精灵:…… :(神仙又要打。精灵忙躲避。喊道:老神仙,俺知道错了,您老多担待。给点提示吧。神仙:首先,你看看你有没有重复代码?精灵:就是那些if SameText(aType, XXX) then了。神仙:就是嘛。那不同的地方在哪里?精灵:当然就是索引了,可是不能用Case…… 哎哟,不要打。神仙:可以变相得来嘛。如果没有integer支持,可以给他加上!精灵:加上…… 整形……神仙:你如果参悟Kingron先知的宝书《超级猛料》就会知道了。精灵:我正在看…… 哦,啊,原来如此……神仙:对了,我看到你搜索到了,你的代码可以如下修改:const
  TypeArray: array [0..3] of string =('1001A', '1001B', '1001C', '1001D');
var
  index: integer;
begin
  for index := Low(TypeArray) to High(TypeArray) do
    if SameText(TypeArray[index], aType) then
      Case index of
        0 : //Do Something...
        1 : //Do Something...
        2 : //Do Something...
        3 : //Do Something...
        .
        .特别是Low与High的调用,我知道你的小脑壳知道上限与下限是0和3但是把这些交给编译器去做是个好习惯。for循环比较与你的连续的if有区别吗?精灵:我记下了,记下了。原来Case可以这么用啊。

解决方案 »

  1.   

    我记下了,记下了。原来Case可以这么用啊。
      

  2.   

    我来家电,使我的问题,菠菜回答的。放在一起。
    var
      A,B,C:Boolean;
    Begin
      if A and B and C then 语句1
      else if A and (not B) and (not C) then 语句2
      else if (not A) and B and (not C) then 语句3
      else if (not A) and (not B) and C then 语句4
      else if A and B and (not C)       then 语句5
      else if A and (not B) and C       then 语句6
      else if (not A) and B and C       then 语句7
      else                              then 语句8
    End 回答:
    var
      i : integer;
    begin  
      i := Integer(A) * 100 + Integer(B) * 10 + Integer(C);
      case i of
        111:; // A and B and C
        110:; // A and B and (not C)
        101:; //...
        100:;    011:;
        010:;
        001:;
        000:;
    end;
      

  3.   

    过两天再写,现在…… 没空…… 另外,菠菜的那个还是有点麻烦,不应该这么用Case。To liuxiaoyuzhou(蟀哥)这个我自己写的,其实那个精灵就是我,神仙是我虚构的……那些一连串的if else就是我写的……
      

  4.   

    if SameText(aType, '1001A') then
    begin
      //Do Something...
    end
    else if SameText(aType, '1001B') then
    begin
      //Do Something...
    end
    ---------------------------
    if SameText(aType, '1001A') then
    begin
      //Do Something...
    end;
    if SameText(aType, '1001B') then
    begin
      //Do Something...
    end;
    -------------------------
    这两段代码怎么会没有区别????区别大了!!
    前者正确,后者不加ELSE的话,判断了“1001A”,不管是不是真,都还要去无用地判断“1001B”,怎么会没有区别呢???
      

  5.   

    楼上说得正确,带else的效率相对高一些,特别是当第一句的SameText(aType, '1001A') = true时。继续关注~~~~~~~~~
      

  6.   

    //俺这样用~~
                           //123456789012345678901234
    case Pos(',' + S + ',', ',1001A,1001B,1001C,1001D,') of 
      1: ;
      7: ;
      13: ;
      19: ;
    end;
      

  7.   

    嗬嗬, To SeaWave(NoSound) 这个连续的if加else有没有区别,来源于C语言的开关语句。如果没有else,那个do something后面就会有一个Exit;也就是:if SameText(aType, '1001B') then
    begin
      //Do Something...
      Exit;
    end;因为我就只需要一个判断而已。其实我认为连续的if比加了else的可读性更强一些。灵感来源于《程序设计实践》。怪我,我没有写上。主要是太忙,没有考虑周全就写了。嘻嘻,这个已经不重要了,关键是怎么用Case。我写出了垃圾代码,看了书才知道优势。
      

  8.   

    to:
    zswangII(伴水清清)(职业清洁工) 
    //
    久仰大名,果然名不虚传,厉害!!!
      

  9.   

    to:
    zswangII(伴水清清)(职业清洁工):
     有点意思,学习
      

  10.   

    Case if 能不用的情况下,就都别用;_____________________________________________________________________暮春三月,羊欢草长,天寒地冻,问谁饲狼?人心怜羊,狼心独怆,天心难测,世情如
    霜……{言有尽而意无穷,余意尽在不言中……}
      

  11.   

    师傅…… 什么时候不用if case
      

  12.   

    续……神仙:好,现在你根据这个写一个模块化的设计的如何?看看你的领悟能力。精灵:好的!如果是动态数据,那就需要一个函数了。其实for的作用是一个查找的作用,所以我只要找出它的索引就可以了。然后弄进Case就好办了。function FindStrIndexof(const aCaseString: string;
                            const aCaseArray: array of string): integer;
    var
      index: integer;
    begin
      Result := -1;
      for index := Low(aCaseArray) to High(aCaseArray) do
        if SameText(aCaseArray[index], aCaseString) then
        begin
          Result := index;
          Exit;
        end;
    end;procedure SelectType(aType: string);
    var
      index: integer;
    begin
      index := FindStrIndexof(aType, ['1001A', '1001B', '1001C', '1001D'])
      Case index of
        0 : //Do Something...
        1 : //Do Something...
        2 : //Do Something...
        3 : //Do Something...
        .
        .
      else
        //
      end;
    end;神仙:嗯,脑袋瓜子还行嘛。这样解决不错不错!但是~~~~~~~~~~~~~~!是否可以扩展呢?array of string是否可以是其他形式呢?精灵:嗯。嗯。嗯。对啊,array of string不能直接赋值啊。神仙:-_-ft 我讲数组课的时候你干吗去了?精灵:我去帮荷尔芙小姐搬东西去了……神仙:荷尔芙小姐…… @_@ 神仙突然环顾四周,看看有没有荷尔芙小姐存在…………神仙:嗯,既然你是给荷尔芙小姐搬东西那就暂且原谅你。再给你一次学习的机会。type 
      TCaseType = array of string;function FindStrIndexof(const aCaseString: string;
                            const aCaseArray: TCaseType): integer;
    var
      index: integer;
    begin
      Result := -1;
      for index := Low(aCaseArray) to High(aCaseArray) do
        if SameText(aCaseArray[index], aCaseString) then
        begin
          Result := index;
          Exit;
        end;
    end;精灵:啊啊啊啊啊,把array声明为类型,我怎么想不到呢?神仙:这个方法尚欠考虑,因为是动态数组,输入前还要初始化。赋值之后还要指定长度。而我说的是另外几个方面。并不是给你的数据都一定能用SameText比较的,可能未知的因素很多,比如初始化的时候'1001A'多了一个空格成为了'1001A_'你用SameText就不适合了,多用Trim吗?仅仅是假设。所以需要你用相对比较模糊的查找,其实添加一个子串查找就足够了。用SameText是不区分大小写的。'1001a'和'1001A'区分不出来的。所以你可能需要比较而言精确的查找。function FindStrIndexof(const aCaseString: string;
                            const aCaseArray: array of string;
                            IsExact: Boolean = False;
                            IsBlur : Boolean = False): integer;
    var
      index: integer;
    begin
      Result := -1;
      for index := Low(aCaseArray) to High(aCaseArray) do
      begin
        if IsExact then
        begin
          if (CompareStr(aCaseArray[index], aCaseString) = 0) then
          begin
            Result := index;
            Exit;
          end;
        end    else if IsBlur then
        begin
          if (Pos(aCaseArray[index], aCaseString) > 0) then
          begin
            Result := index;
            Exit;
          end;
        end    else if SameText(aCaseArray[index], aCaseString) then
        begin
          Result := index;
          Exit;
        end;
      end;
    end;功能不需要很强,开关不需要很多,够用就好。(荷尔芙小姐出现)啊。荷尔芙小姐……精灵:神仙,神仙 …… 老不死的跑掉了,谁教我啊。:(……
      

  13.   

    最后:{>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
      过程名称:FindStrIndexof
      作者:    神仙
      日期:    7788年12月23日
      输入参数:aCaseString       需要查询的字符串
                aCaseArray        目的字符串数组
                IsExact           精确查找开关
                IsBlur            模糊查找开关
      返回值  :integer
      功能目的:查找字符在字符串数组中的索引
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<}
    function FindStrIndexof(const aCaseString: string;
                            const aCaseArray: array of string;
                            IsExact: Boolean = False;
                            IsBlur : Boolean = False): integer;
    var
      index: integer;
    begin
      Result := -1;
      for index := Low(aCaseArray) to High(aCaseArray) do
      begin
        if IsExact then
        begin
          if (CompareStr(aCaseArray[index], aCaseString) = 0) then
          begin
            Result := index;
            Exit;
          end;
        end    else if IsBlur then
        begin
          if (Pos(aCaseArray[index], aCaseString) > 0) then
          begin
            Result := index;
            Exit;
          end;
        end    else if SameText(aCaseArray[index], aCaseString) then
        begin
          Result := index;
          Exit;
        end;
      end;
    end;procedure SelectType(aType: string);
    var
      index: integer;
    begin
      index := FindStrIndexof(aType, ['1001A', '1001B', '1001C', '1001D'])
      Case index of
        0 : //Do Something...
        1 : //Do Something...
        2 : //Do Something...
        3 : //Do Something...
        .
        .
      else
        //
      end;
    end;谢谢大家观看。第一篇就此结束。
      

  14.   

    Dlwxn(韩飞子)的那个蛮有意思的。
      

  15.   

    呵呵,算法的书写角度无非就是两个:
    1.从数据结构角度看是时间和空间复杂度
    2.从编写者角度看就是代码书写难度----短代码未必效率高,长代码也未必效率低灵活运用即可....
    ----------------------
    严重同意。
    我正想说:短的“运行时刻代码”,往往是用长的“设计时刻空间”来换取的,你可以在设计时刻将条件、判断全部归化到静态数据里,这样运行时刻的代码可以缩短.......但是程序易读性并不与代码长短成正比或反比的任何关系。
    好的代码并不是短小,而是在几乎不影响效率的情况下,更易读,更易维护。
    ----------------------------------更为严重的同意!同意得不得了!前一阵子跟一个SB等级的VB程序员说printf的用法。简直没法跟他说,用常量或者宏表示一个数字,以及用过程来分析已知数据。而不是直接就printf。根本没法跟VB的程序员说…… 无可救药……
      

  16.   

    呵呵,自己不是说不用else怎么最后又用了
      

  17.   

    设一个不就可以了,为什么要设两个呢?入口参数该精简还是应该精简点
                IsExact           精确查找开关
                IsBlur            模糊查找开关
      

  18.   

    to yue_yt(爬山者) 请你看清楚,上面那个是在单纯过程里面,单纯的过程就是下坠的使用。而我用的那个是在一个循环里面,不一样!>>>设一个不就可以了,为什么要设两个呢?入口参数该精简还是应该精简点一个开关?怎么写?请你show出你的code。另外,我对你说的这句话感到…… 模棱两可,你在说什么呢?