将两个 C++ 函数转换为 Pascal
Translating two C++ functions to Pascal
我似乎不明白为什么我的翻译没有给出预期的结果。这是 C++ 源代码:
#include <stdio.h>
int minint(int candidate = -1) {
if (candidate - 1 >= 0)
return candidate;
for (int stride = -1, stride2 = 2*stride; ; stride = stride2, stride2 += stride2)
if (stride2 >= 0 || candidate + stride2 >= 0)
return minint(candidate + stride);
}
int maxint(int candidate = 1) {
if (candidate + 1 <= 0)
return candidate;
for (int stride = 1, stride2 = 2*stride; ; stride = stride2, stride2 += stride2)
if (stride2 <= 0 || candidate + stride2 <= 0)
return maxint(candidate + stride);
}
int main() {
(void) printf("Max int is %d\n", maxint());
(void) printf("Min int is %d\n", minint());
return 0;
}
它打印:
Max int is 2147483647
Min int is -2147483648
这是 Pascal 代码(使用 Free Pascal 编译):
program Translation;
function minint (candidate : Longint) : Longint;
var stride, stride2 : Longint;
var bool : Boolean;
begin
bool := false;
if (candidate - 1) >= 0 then
begin
bool := true;
minint := candidate;
end;
if (bool = false) then
begin
stride := -1;
stride2 := 2*stride;
while (stride2 < 0) and (candidate + stride2 < 0) do
begin
stride := stride2;
stride2 += stride2;
end;
minint := minint(candidate + stride)
end;
end;
function maxint (candidate : Longint) : Longint;
var stride, stride2 : Longint;
var bool : Boolean;
begin
bool := false;
if (candidate + 1) <= 0 then
begin
bool := true;
maxint := candidate;
end;
if (bool = false) then
begin
stride := 1;
stride2 := 2*stride;
while (stride2 > 0) and (candidate + stride2 > 0) do
begin
stride := stride2;
stride2 += stride2;
end;
maxint := minint(candidate + stride)
end;
end;
begin
writeln(maxint(1));
writeln(minint(-1));
end.
出于某种原因打印:
1073741825
2147483647
非常非常奇怪。 'maxint' 值大约是所需值的一半,'minint' 值是正值(实际上,这就是 'maxint' 值应该是的值)。
我错过了什么?请记住,我被禁止使用定序器(即诸如 Exit 之类的命令 - 因此是布尔值)和默认参数。
不要 依赖 C++ 中的 underflow/overflow 行为。它是未定义的。
相反,使用限制库来确定您的整数大小限制。
#include <iostream>
#include <limits>
int main() {
std::cout << std::numeric_limits<int>::max() << '\n';
std::cout << std::numeric_limits<int>::min() << '\n';
return 0;
}
在 Pascal 中,LongInt
具有固定大小。
你可以相信它有 4 个字节长。
关于您的 Pascal 代码,您的 "maxint" 函数中有错字。
行:
maxint := minint(candidate + stride)
应该是:
maxint := maxint(candidate + stride)
这个,IMO 相当直译(但不使用 Break
、Continue
或 Exit
)对我有用(注意:Delphi,不是 FPC,但我猜想在FPC中有一个兼容模式):
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
function minint(candidate: Integer = -1): Integer;
var
stride: Integer;
stride2: Integer;
begin
if candidate - 1 >= 0 then
minint := candidate
else
begin
stride := -1;
stride2 := 2 * stride;
while (stride2 < 0) and (candidate + stride2 < 0) do
begin
stride := stride2;
stride2 := stride2 + stride2;
end;
minint := minint(candidate + stride);
end;
end;
它产生以下输出:
Min int is -2147483648
我没有翻译 maxint,但我想你可以自己翻译。
它看起来很像你的翻译(但没有 bool)。
我似乎不明白为什么我的翻译没有给出预期的结果。这是 C++ 源代码:
#include <stdio.h>
int minint(int candidate = -1) {
if (candidate - 1 >= 0)
return candidate;
for (int stride = -1, stride2 = 2*stride; ; stride = stride2, stride2 += stride2)
if (stride2 >= 0 || candidate + stride2 >= 0)
return minint(candidate + stride);
}
int maxint(int candidate = 1) {
if (candidate + 1 <= 0)
return candidate;
for (int stride = 1, stride2 = 2*stride; ; stride = stride2, stride2 += stride2)
if (stride2 <= 0 || candidate + stride2 <= 0)
return maxint(candidate + stride);
}
int main() {
(void) printf("Max int is %d\n", maxint());
(void) printf("Min int is %d\n", minint());
return 0;
}
它打印:
Max int is 2147483647
Min int is -2147483648
这是 Pascal 代码(使用 Free Pascal 编译):
program Translation;
function minint (candidate : Longint) : Longint;
var stride, stride2 : Longint;
var bool : Boolean;
begin
bool := false;
if (candidate - 1) >= 0 then
begin
bool := true;
minint := candidate;
end;
if (bool = false) then
begin
stride := -1;
stride2 := 2*stride;
while (stride2 < 0) and (candidate + stride2 < 0) do
begin
stride := stride2;
stride2 += stride2;
end;
minint := minint(candidate + stride)
end;
end;
function maxint (candidate : Longint) : Longint;
var stride, stride2 : Longint;
var bool : Boolean;
begin
bool := false;
if (candidate + 1) <= 0 then
begin
bool := true;
maxint := candidate;
end;
if (bool = false) then
begin
stride := 1;
stride2 := 2*stride;
while (stride2 > 0) and (candidate + stride2 > 0) do
begin
stride := stride2;
stride2 += stride2;
end;
maxint := minint(candidate + stride)
end;
end;
begin
writeln(maxint(1));
writeln(minint(-1));
end.
出于某种原因打印:
1073741825
2147483647
非常非常奇怪。 'maxint' 值大约是所需值的一半,'minint' 值是正值(实际上,这就是 'maxint' 值应该是的值)。
我错过了什么?请记住,我被禁止使用定序器(即诸如 Exit 之类的命令 - 因此是布尔值)和默认参数。
不要 依赖 C++ 中的 underflow/overflow 行为。它是未定义的。
相反,使用限制库来确定您的整数大小限制。
#include <iostream>
#include <limits>
int main() {
std::cout << std::numeric_limits<int>::max() << '\n';
std::cout << std::numeric_limits<int>::min() << '\n';
return 0;
}
在 Pascal 中,LongInt
具有固定大小。
你可以相信它有 4 个字节长。
关于您的 Pascal 代码,您的 "maxint" 函数中有错字。
行:
maxint := minint(candidate + stride)
应该是:
maxint := maxint(candidate + stride)
这个,IMO 相当直译(但不使用 Break
、Continue
或 Exit
)对我有用(注意:Delphi,不是 FPC,但我猜想在FPC中有一个兼容模式):
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
function minint(candidate: Integer = -1): Integer;
var
stride: Integer;
stride2: Integer;
begin
if candidate - 1 >= 0 then
minint := candidate
else
begin
stride := -1;
stride2 := 2 * stride;
while (stride2 < 0) and (candidate + stride2 < 0) do
begin
stride := stride2;
stride2 := stride2 + stride2;
end;
minint := minint(candidate + stride);
end;
end;
它产生以下输出:
Min int is -2147483648
我没有翻译 maxint,但我想你可以自己翻译。
它看起来很像你的翻译(但没有 bool)。