0

我正在尝试在 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

什么可能导致此错误,我该如何解决?

4

2 回答 2

3

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

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

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

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

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.
于 2012-12-23T11:50:02.800 回答
1

当然,单元数学为各种类型预定义了 MAX(作为内联)

uses math;

var
  a,b: Integer;

begin
  a:=5;
  b:=4;
  write(max(a,b))
end.
于 2012-12-23T18:58:26.350 回答