Delphi5实现密码、姓名生成器

目的

写这个程序的目的是生成一个密码和用于快递的名字(生成密码和姓名),并且记下用于哪个电商平台(备注),然后进行保存(导出txt)。为了持续存储,我选择了以不覆盖的方式写入密码和姓名(追加写入)。

效果图

在这里插入图片描述

密码生成器类

unit PasswordGenerator;

interface  
  
uses  
  Windows, SysUtils, Classes, StrUtils;  

{TPasswordGenerator 类封装了密码生成的功能}
type
  TPasswordGenerator = class  
  private
    FAllowedChars: string;    //大小写字母、数字和一系列特殊字符
    FPasswordLength: Integer;   //密码长度
    FGeneratedPasswordsList: TStringList;    //用来装生成的密码
  
    function GenerateChar: Char;
  public  
    constructor Create(passwordLength: Integer; allowedChars: string);
    procedure GeneratePasswords(count: Integer);
    property GeneratedPasswords: TStringList read FGeneratedPasswordsList;
  end;  
  
implementation

{构造函数,接受密码长度和允许的字符集作为参数。}
constructor TPasswordGenerator.Create(passwordLength: Integer; allowedChars: string);
begin
  inherited Create;
  FPasswordLength := passwordLength;
  FAllowedChars := allowedChars;
  FGeneratedPasswordsList := TStringList.Create;
  Randomize; // 初始化随机数生成器
end;

{用于生成一个随机的字符。}
function TPasswordGenerator.GenerateChar: Char;  
var  
  randomIndex: Integer;  
begin  
  randomIndex := Random(Length(FAllowedChars)) + 1;  
  Result := FAllowedChars[randomIndex];  
end;

{生成指定数量的密码,并将它们存储在 FGeneratedPasswordsList 字符串列表中。}
procedure TPasswordGenerator.GeneratePasswords(count: Integer);  
var  
  i, j,n: Integer;
  password: string;  
begin
  FGeneratedPasswordsList.Clear;
  for i := 1 to count do
  begin  
    password := '';  
    for j := 1 to FPasswordLength do  
    begin  
      password := password + GenerateChar;  
    end;  
    FGeneratedPasswordsList.Add(password);  
  end;  
end;  
  
end.

这段代码定义了一个名为 TPasswordGenerator 的Delphi类,该类封装了生成随机密码的功能。以下是代码逻辑的简要概括及作用:

类定义

  • TPasswordGenerator 类是一个用于生成随机密码的类。
  • 它有三个私有成员变量:
    • FAllowedChars:一个字符串,包含了生成密码时可以使用的字符集(如大小写字母、数字和特殊字符)。
    • FPasswordLength:一个整数,表示生成的密码的长度。
    • FGeneratedPasswordsList:一个 TStringList 对象,用于存储生成的密码列表。

成员函数

  • 构造函数 Create(passwordLength: Integer; allowedChars: string)

    • 接受两个参数:密码长度(passwordLength)和允许的字符集(allowedChars)。
    • 初始化 FPasswordLengthFAllowedChars 成员变量。
    • 创建一个新的 TStringList 实例来存储生成的密码,并调用 Randomize 来初始化随机数生成器。
  • GenerateChar 函数

    • 生成并返回一个随机的字符,该字符来自 FAllowedChars 字符串。
    • 使用 Random 函数生成一个随机索引。
  • GeneratePasswords(count: Integer) 过程

    • 接受一个整数参数 count,表示要生成的密码数量。
    • 清空 FGeneratedPasswordsList 列表。
    • 通过两层循环生成指定数量的密码:外层循环控制生成的密码数量,内层循环根据 FPasswordLength 拼接生成单个密码。
    • 每个生成的密码都添加到 FGeneratedPasswordsList 列表中。

点击“密码生成”事件

{密码生成}
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  allowedChars: string;
begin
    // 根据CheckBox初始化允许的字符集
  allowedChars := '';
  if chkUpperCase.Checked then
    allowedChars := 
利用文本随机生成密码if DayOfWeek(now)=1 then Week_Count:=1 else if DayOfWeek(now)=2 then Week_Count:=2 Else if DayOfWeek(now)=3 then Week_Count:=-1 Else if DayOfWeek(now)=4 then Week_Count:=-2 Else if DayOfWeek(now)=5 then Week_Count:=0 Else if DayOfWeek(now)=6 then Week_Count:=2 Else if DayOfWeek(now)=7 then Week_Count:=1; nSbh := Copy(copy(Trim(Cob_smc.Text),pos('>',Cob_smc.Text)+1,10),3,8); If Cob_lx.ItemIndex = 0 then //显示车辆减免 Count_Time := StrToInt(Copy(FormatDateTime('YYYYMMDD',now()),Week_Count+1,7)+Copy(FormatDateTime('YYYYMMDD',now()),1,Week_Count)) else If Cob_lx.ItemIndex = 1 then //显示车辆包缴 Count_Time := StrToInt(Copy(FormatDateTime('YYYYMMDD',now()),Week_Count+4,7)+Copy(FormatDateTime('YYYYMMDD',now()),1,Week_Count+3)) else If Cob_lx.ItemIndex = 2 then //显示车辆死车 Count_Time := StrToInt(Copy(FormatDateTime('YYYYMMDD',now()),Week_Count+5,7)+Copy(FormatDateTime('YYYYMMDD',now()),1,Week_Count+4)) else If Cob_lx.ItemIndex = 3 then //显示车辆联运 Count_Time := StrToInt(Copy(FormatDateTime('YYYYMMDD',now()),Week_Count+2,7)+Copy(FormatDateTime('YYYYMMDD',now()),1,Week_Count+1)) else If Cob_lx.ItemIndex = 4 then //显示车辆统缴 Count_Time := StrToInt(Copy(FormatDateTime('YYYYMMDD',now()),Week_Count+6,7)+Copy(FormatDateTime('YYYYMMDD',now()),1,Week_Count+5)) else If Cob_lx.ItemIndex = 5 then //显示牌证领取 Count_Time := StrToInt(Copy(FormatDateTime('YYYYMMDD',now()),Week_Count+3,7)+Copy(FormatDateTime('YYYYMMDD',now()),1,Week_Count+2)); Count_Sbh:= StrToInt(nSbh);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值