2017-10-06 66 views
2
for counter := 1 to lengthofpassword do 
    begin 
    currentletter:=password[counter]; 
    currentascii:=Ord(currentletter); 
    if (96<currentascii<123) OR (64<currentascii<91) OR (47<currentascii<58) then 
    Writeln('valid') 
    else 
    asciicheck:=false; 
    end; 

我知道这段代码是错误的,但我做了解释我想问什么。如何指定if语句的范围?之前,我搞砸了很多if语句,并且我的代码没有按照我想要的方式工作。基本上,我正在制定一个程序,检查用户输入的大小写字母和数字以外的任何内容。这个问题是不同的,因为我正在寻找如何使用Case Of语句解决这个问题。如何在Delphi中为if语句指定多个范围?

for counter := 1 to lengthofpassword do 
    begin 
    currentletter:=password[counter]; 
    currentascii:=Ord(currentletter); 
    if (currentascii<48) AND (currentascii>57) then 
    asciipoints:=asciipoints+1; 
    if (currentascii<65) AND (currentascii>90) then 
    asciipoints:=asciipoints+1; 
    if (currentascii<97) AND (currentascii>122) then 
    asciipoints:=asciipoints+1; 
    Writeln(asciipoints); 
    end; 

我也试着像这样做,但后来意识到这是行不通的,因为如果一个声明是满意的,其他人也不会和计点积分制将不能工作。

+0

什么是你的,当然目前的话题?你有什么需求来解决这个任务吗?否则,您可能需要查看一组char。 – nil

+0

干杯,我结束了使用像97..122为字母表 –

回答

7

很高兴您自己找到答案。

另一种确保密码只包含大小写字符和数字的方法是我试图指出的:定义一个有效的字符set,并检查密码中的每个字符是否为in这些有效字符。

所以像这样定义的一组:

const 
    ValidChars = ['A'..'Z', 'a'..'z', '0'..'9']; 

你可以使用之类的语句

if password[I] in ValidChars then

此语句但是会产生Unicode的德尔福编译器警告,如在类型集限于256个可能的值,并且它们的序数必须介于0和255之间。对于具有65.536值的WideChar,情况并非如此。所以定义的set of char实际上是set of AnsiChar。对于此任务,这是可以接受的,因为每个需要检查的字符都是ASCII,所以使用函数CharInSet将不会生成编译器警告并具有已定义的行为 - 如果密码包含Unicode字符,则返回False

这是生成的代码:

const 
    ValidChars = ['A'..'Z', 'a'..'z', '0'..'9']; 
var 
    I: Integer; 
begin 
    for I := 1 to passwordlength do 
    begin 
    if CharInSet(password[I], ValidChars) then 
     Writeln('valid') // more likely to do nothing and invert the if statement 
    else 
    begin 
     asciicheck := False; 
     Break; // No need to look further, the check failed 
    end; 
    end; 
end; 
+1

''''..'/''应该类似于你要做的事。 33..47。我目前无法验证,所以这没有经过测试。那不包括|和我认为的英镑符号。 – nil

+0

传说,现在,我已将所有允许的符号添加到该集合中,这就像一种魅力。我最终单独添加它们。 –

+2

这真的是适合这项任务的解决方案。很高兴你选择它作为接受的答案。 –

1

感谢上面的评论,我找到了一个解决方案。我结束了使用这样一个案例:

for counter := 1 to lengthofpassword do 
    begin 
    currentletter:=password[counter]; 
    currentascii:=Ord(currentletter); 
     case currentascii of 
     97..122 : asciicheck:=true; 
     65..90 : asciicheck:=true; 
     48..57 : asciicheck:=true; 
     else asciicheck:=false; 
     end; 
    end; 

再次感谢。

6

多个范围是在case声明表达得最淋漓尽致:

begin 
    for counter := 1 to lengthofpassword do 
    begin 
    case Ord(password[counter]) of 
     48..57, 
     65..90, 
     97..122 : 
     Writeln('valid') 
     else 
     asciicheck:=false; 
    end; 
    end; 
end; 

现在,这个工程的字符<#128。如果您使用unicode应用程序并且不希望字符限制为英文字母,则可以使用TCharHelper.IsLetterOrDigit

if password[counter].IsLetterOrDigit then ... 
+0

套声明感谢,这基本上是我想出了。 –

+0

为什么不简单'如果Ord(密码[counter])在[48..57,65 ..90,97..122]然后?看起来更简单。请注意,该设置将被编译为常量。 –

+0

@RudyVelthuis,如果您不知道,编译器会在两种情况下生成相同的代码。所以这更多的是个人喜好选择哪种解决方案。 –