1

我编写了一个程序,将有关工人的不同数据填充到表中。(姓名、姓氏和薪水)

帮我写一个程序或函数来查找最高工资值和这个工人的名字,然后写在控制台

我可以使用循环来制作它吗?

program labasix;

type firma = record
  name : string;
  lastName : string;
  salary : integer;
end;

var
  svitoch : array[1..12] of firma;
  i : integer;
  countOfWorkers : integer;
begin
  write('Number of workers (not more than 12): ');
  readln(countOfWorkers);
  writeln();

  for i := 1 to countOfWorkers do
    begin
      write('Name: '); readln( svitoch[i].name );
      write('lastName: '); readln( svitoch[i].lastName );
      write('Salary: '); readln( svitoch[i].salary );
      writeln();
    end;

   for i := 1 to countOfWorkers  do
     begin
        { what code must be here ??? }
     end;
end.

一定有这样的东西

procedure findMax(x, y, z: integer; var m: integer); 

begin
   if x > y then
      m:= x
   else
      m:= y;
   if z > m then
      m:= z;
end;

但是如何获得 xyz 值?

太感谢了 !

4

2 回答 2

1

这是一个简单的函数,它返回包含数组中薪水最大值的索引。在此之后将其粘贴到您的代码中:

type firma = record
  name : string;
  lastName : string;
  salary : integer;
end;

这是功能:

function getmax():integer;
var max:integer;
begin
     max:=1;
     for i:=2 to countOfWorkers do
     begin
         if svitoch[i].salary > svitoch[max].salary then
         max:=i;
     end;
     getmax:=max;
end;

因此,现在您可以通过在第一个 for 循环之后使用此结构而不是第二个循环来获得最高工资值(和名称)。

i:=getmax();
writeln(svitoch[i].name);  {if you want to write in your case}
writeln(svitoch[i].lastName);
writeln(svitoch[i].salary);
于 2014-08-20T20:55:58.820 回答
0

好吧,显然你需要查看你现在拥有的工人列表(数组),寻找薪水最高的那个。

因此,编写一个接受该数组作为参数的函数(不是过程)。

该函数应该将第一个工人的薪水存储到一个变量中,然后遍历其余的工人;如果工人的工资高于您已经存储的工资,请将存储的值替换为新的更高的值并继续循环。当您到达列表的末尾时,您已经存储了最高薪水,然后您将其从您的函数中返回。

提示:您应该将Low(YourArray)其用作循环的起点和循环High(YourArray)的停止点,这样您可以传递给该数组中的函数的工作人员数量就没有限制。

于 2014-03-28T23:22:27.953 回答