【问题标题】:Finding maximum salary value with Pascal Record使用 Pascal Record 查找最高工资值
【发布时间】:2014-03-28 14:36:10
【问题描述】:

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

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

我可以使用循环吗?

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;

但是如何获得 x y z 值呢?

非常感谢!

【问题讨论】:

  • 你完全不知道该怎么办?
  • @500 - 内部服务器错误我已经尝试过双循环和我在下面描述的过程

标签: function record pascal procedure turbo-pascal


【解决方案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);

【讨论】:

    【解决方案2】:

    嗯,很明显,您需要查看您现在拥有的工人列表(数组),寻找薪水最高的那个。

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

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

    提示:您应该使用Low(YourArray) 作为循环的起点,并使用High(YourArray) 作为循环的停止点,这样您可以传递给该数组中的函数的worker 数量没有限制.

    【讨论】:

      猜你喜欢
      • 2012-07-31
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      相关资源
      最近更新 更多