2014-03-28 17 views
1

我写了一个程序,它将关于工人的不同数据填入表格中。 (名字,姓氏和工资)用Pascal记录找到最高工资值

帮我写一个procesure或函数查找最高薪水值,这名工人的名字,并在控制台中使用一个循环就

我可以让写?

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值?

非常感谢你!

+0

你绝对不知道该怎么办? –

+0

@ 500 - 内部服务器错误我已经尝试过双循环和我在下面描述的过程 – Luchnik

回答

0

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

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

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

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

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; 

所以,现在你可以使用你的第一个用于循环和后这种结构获得的最高薪水值(和名称)而不是第二个。

i:=getmax(); 
writeln(svitoch[i].name); {if you want to write in your case} 
writeln(svitoch[i].lastName); 
writeln(svitoch[i].salary);