Pascal中的最大查找程序

时间:2012-12-23 08:16:37

标签: pascal

我正在尝试在Pascal中实现以下算法。 Pascal对我来说是新的,所以我无法理解问题所在。程序试图找到两个整数之间的最大值,如下所示:

program maqsimaluri;
function max(a,b:integer):integer;
begin
if a>=b then
max:=a
else
max:=b;
end;
negon
var a:=5;
var b:=4;
write(max(a,b));
end.

但我收到以下错误

Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
prog.pas(10,5) Error: Illegal expression
prog.pas(10,9) Error: Illegal expression
prog.pas(10,9) Fatal: Syntax error, ";" expected but "identifier A" found
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)

请参阅http://ideone.com/0NH6Km

可能导致此错误的原因以及如何解决?

2 个答案:

答案 0 :(得分:3)

在说出错之前,我建议你缩进代码。例如,您的函数体应该向右移动两个空格。这有助于您捕获一些错误并提高可读性。

现在错误:我真的不知道,为什么你的代码中有一个'negon'指令,但是在ideone上它不存在。但这不是问题,因为您的变量声明是错误的。首先,它们位于错误的位置,因为它们应该放在主程序的关键字“begin”之前。据我所知,你不能在Pascal中的代码中声明变量,但你必须在prehands之前完成。 其次,必须指定类型,变量有。如果变量值适合其类型,Pascal是一种强大的,类型安全的语言和检查。在这种情况下,它可能是'整数'。 第三,你不能在声明中给出变量值。您必须稍后在代码中执行此操作。

我建议你阅读一些关于Pascal编程的基础文章。对于基础知识,即使是wikibook Pascal Tutorial from Wikipedia也足够了。

这是您的代码版本,实际运行并提供正确的输出:

program maqsimaluri;

  function max(a,b:integer):integer;
  begin
    if a>=b then
      max:=a
    else
      max:=b
  end;

var
  a,b: Integer;

begin
  a:=5;
  b:=4;
  write(max(a,b))
end.

答案 1 :(得分:1)

当然,单位数学预定义各种类型的MAX(如内联)

uses math;

var
  a,b: Integer;

begin
  a:=5;
  b:=4;
  write(max(a,b))
end.