编写程序以显示用户姓名的前3个辅音和用户姓氏的最后3个字母以及ID的每个第二个数字

时间:2014-06-18 17:03:05

标签: delphi delphi-xe

我正在尝试编写一个程序来显示用户姓名的前3个辅音和用户姓氏的最后3个字母以及ID的每个第二个数字,并在第7个位置添加一个星号。我得到的ID部分正确但不是名称和姓氏部分。

procedure TForm1.N31Click(Sender: TObject);


var
 Name,Surname,ID,newname,newsurname,newID,newstring:string;
 i,j,k:integer;
begin
newname:='';
newsurname:='';
newid := '';
name := UPPERCASE(inputbox('Name','First name',''));
Surname := inputbox('Surname','Last name','');
ID := Inputbox('ID','ID boy','');
for i := length(name) downto 1 do
begin
if name[i] in ['A','E','I','O','U'] then
 delete(name,i,1);
 newname := newname + name[i];
 newname := copy(newname,1,3);
 end;

 begin
j := length(surname);
 newsurname := copy(surname,j-2,3);
  end;
 for k := 1 to length(id) do
   newid := newid + id[2*k];

 newstring := (newname+newsurname+newID);
 insert('*',newstring,7);

 redoutput.Lines.Add(newstring) ;

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

我将所有解析代码移动到一个函数,您只需传递名字,姓氏和ID,然后获取返回的生成值。这是一个测试应用程序就是这样做的 - 然后你可以使用你想要的任何方式将值传递给它。

program Project1;

uses
  SysUtils;

function GetID(const sName, sSurname, sID: string): string;
var
  i: Integer;
const
  // You can remove the lower case letters if you want to always
  // convert sName to upper case first.
  VowelChars = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'];
begin
  Result := '';
  for i := 1 to Length(sName) do
  begin
    // Earlier versions of Delphi: if not (sName[i] in VowelChars) then
    if not CharInSet(Name[i], VowelChars) then 
      Result := Result + sName[i];
    if Length(Result) = 3 then
      Break;
  end;

  // Last 3 characters of Surname
  Result := Result + Copy(sSurName, Length(sSurName) - 2, 3);

  // Every even digit of ID
  for i := 1 to Length(sID) do
    if not Odd(i) then
      Result := Result + sID[i];
  // Star in 7th position
  if Length(Result) > 7 then
    Result[7] := '*';
end;

begin
  WriteLn(GetID('James', 'Johnson', '123456ABCD'));
  ReadLn;
end.

答案 1 :(得分:-1)

我会重写代码的主要部分,如下所示

i:= 0;
j:= 0;
newname:= '';
while i < length (name)
 begin
  inc (i);
  if not (name[i] in vowels) then
   begin
    inc (i);
    newname:= newname + name[i];
    if j = 3 then i:= length (name);
   end;
 end;

i:= length (surname);
if i > 3
 then newsurname:= copy (surname, i - 3, 3) 
 else newsurname:= surname;

newid:= '';
for k := 1 to length (id) do newid := newid + id[2*k];
result:= newname + newsurname + newID);
insert ('*', result, 7);