尝试验证文本文件中的字符时陷入循环
Stuck on a loop while trying to verify characters from a text file
我必须编写一个程序,从文本文件中读取一些 "random" 字母和数字字符串,并检查它们是否满足使它们成为有效密码的某些条件。
条件是:-刚好有4位数字
-正好有8个字符
-至少有一个大写字母和至少一个小写字母
程序读取文件并输出有效密码的数量。
这是文本文件的格式:
"eR68G12a 91jY643ebjp eRty74kLh 24fG92 aj85gt32 dGb9357jKoup2 "(单行)
代码:
´´´
Program Ej23_version3;
var
char1,char2:char;
mayus,minus:boolean; // mayus and minus would be uppercase and lowercase respectively
cantDigitos,cantCaracteres,contrasenasValidas:integer;
datos:text;
Begin
assign(datos,'Datos_guia3_ej23.txt'); reset(datos);
contrasenasValidas := 0;
char1 := ' ';
Read(datos,char2);
while not eof(datos) do
Begin
mayus := false; minus := false; cantDigitos := 0; cantCaracteres := 0;
if (char1 = ' ') and (char2 <> ' ') then //check if its the beggining of the word
Begin
while not eof(datos) and (char2 <> ' ') do
Begin
cantCaracteres := cantCaracteres + 1;
if char2 = UPCASE(char2) then // if the character2 is equal to the uppercase version of the character2, character2 is uppercase
mayus := true
else
if (char2 in ['0'..'9']) then
cantDigitos := cantDigitos + 1
else
minus := true;
if eof(datos) then // when it reaches the end of the file, it also reads and checks the last character
if char2 = UPCASE(char2) then
mayus := true
else
if (char2 in ['0'..'9']) then
cantDigitos := cantDigitos + 1
else
minus := true;
End;
if minus and mayus and (cantDigitos = 4) and (cantCaracteres = 8) then //if all conditions are met, the password is valid and its added to the counter
contrasenasValidas := contrasenasValidas + 1;
char1 := char2; Read(datos, char2); //char2 should be an empty character by this point, so it passes that value to char1 and reads the next character
End
End;
WriteLn(contrasenasValidas);
End.
但是当我 运行 它时,它只是卡在那里,只有提示滴答作响
问题在于您读取文件的方式(逐个字符)。
最好一下子看完,把八个字的字串一个一个看完
uses
SysUtils;
var
LFile: TextFile;
LStr, LSubStr: string;
LStartIndex: integer;
LExit: boolean;
begin
AssignFile(LFile, 'Datos_guia3_ej23.txt');
Reset(LFile);
ReadLn(LFile, LStr); // Get the whole line
LStartIndex := 1; // Search all 8 characters sequences, starting from the first character
LExit := FALSE;
repeat
LSubStr := Copy(LStr, LStartIndex, 8);
if Length(LSubStr) = 8 then
begin
// Here check other conditions
// ...
Inc(LStartIndex);
end else
LExit := TRUE;
until LExit;
CloseFile(LFile);
end.
我必须编写一个程序,从文本文件中读取一些 "random" 字母和数字字符串,并检查它们是否满足使它们成为有效密码的某些条件。
条件是:-刚好有4位数字 -正好有8个字符 -至少有一个大写字母和至少一个小写字母
程序读取文件并输出有效密码的数量。
这是文本文件的格式: "eR68G12a 91jY643ebjp eRty74kLh 24fG92 aj85gt32 dGb9357jKoup2 "(单行)
代码: ´´´
Program Ej23_version3;
var
char1,char2:char;
mayus,minus:boolean; // mayus and minus would be uppercase and lowercase respectively
cantDigitos,cantCaracteres,contrasenasValidas:integer;
datos:text;
Begin
assign(datos,'Datos_guia3_ej23.txt'); reset(datos);
contrasenasValidas := 0;
char1 := ' ';
Read(datos,char2);
while not eof(datos) do
Begin
mayus := false; minus := false; cantDigitos := 0; cantCaracteres := 0;
if (char1 = ' ') and (char2 <> ' ') then //check if its the beggining of the word
Begin
while not eof(datos) and (char2 <> ' ') do
Begin
cantCaracteres := cantCaracteres + 1;
if char2 = UPCASE(char2) then // if the character2 is equal to the uppercase version of the character2, character2 is uppercase
mayus := true
else
if (char2 in ['0'..'9']) then
cantDigitos := cantDigitos + 1
else
minus := true;
if eof(datos) then // when it reaches the end of the file, it also reads and checks the last character
if char2 = UPCASE(char2) then
mayus := true
else
if (char2 in ['0'..'9']) then
cantDigitos := cantDigitos + 1
else
minus := true;
End;
if minus and mayus and (cantDigitos = 4) and (cantCaracteres = 8) then //if all conditions are met, the password is valid and its added to the counter
contrasenasValidas := contrasenasValidas + 1;
char1 := char2; Read(datos, char2); //char2 should be an empty character by this point, so it passes that value to char1 and reads the next character
End
End;
WriteLn(contrasenasValidas);
End.
但是当我 运行 它时,它只是卡在那里,只有提示滴答作响
问题在于您读取文件的方式(逐个字符)。
最好一下子看完,把八个字的字串一个一个看完
uses
SysUtils;
var
LFile: TextFile;
LStr, LSubStr: string;
LStartIndex: integer;
LExit: boolean;
begin
AssignFile(LFile, 'Datos_guia3_ej23.txt');
Reset(LFile);
ReadLn(LFile, LStr); // Get the whole line
LStartIndex := 1; // Search all 8 characters sequences, starting from the first character
LExit := FALSE;
repeat
LSubStr := Copy(LStr, LStartIndex, 8);
if Length(LSubStr) = 8 then
begin
// Here check other conditions
// ...
Inc(LStartIndex);
end else
LExit := TRUE;
until LExit;
CloseFile(LFile);
end.