我有这样的代码:
begin
RunProgram:=TProcess.Create(nil);
RunProgram.Commandline:='calc.exe';
RunProgram.Execute;
RunProgram.Commandline:='notepad.exe';
RunProgram.Execute;
RunProgram.Free;
end.
Run Code Online (Sandbox Code Playgroud)
我想在执行 calc.exe 后休眠或延迟
编写 Lazarus 程序时,您可以选择使用$APPTYPE console或取消选择项目选项中的Win32 GUI Application (-WG选项)。
我注意到没有这些选项DebugLn并WriteLn引发异常。有没有一种方法来创建一个控制台即使程序没有上述选项,并输出到它编译DebugLn和WriteLn后来呢?
我在 PAscal 中遇到了问题。我想计算素数但没有结果:我的代码是:
Program arrayToFunction;
const
size = 5;
type
a = array [1..size] of integer;
var
aNumbers: a = (1, 7, 3, 4, 5);
iCountNumbers: integer;
function countPrimeNumbers( var arr: a) : integer;
var
i :0..size;
sum,j,count: integer;
begin
sum := 0;count:=0;
for i := 0 to size do
begin
for j := 2 to arr[i] do
begin
if(arr[i] mod j = 0) then sum:=1;
end;
if(sum=0) then count:=count+1;
end;
countPrimeNumbers := count;
end;
begin
iCountNumbers := countPrimeNumbers( aNumbers …Run Code Online (Sandbox Code Playgroud) 在C中,您可以在一行中分配两个变量
b = a = sqrt(10);
Run Code Online (Sandbox Code Playgroud)
在德尔福
b := a := Sqrt(10);
Run Code Online (Sandbox Code Playgroud)
不被允许.
有了Delphi中讨论IfThen的三元运算符?:的"替代" - 相当于C#的三元运算符?总之,IfThen似乎并非绝对必要.
所以也许还有这样的东西:
function AssignAndReturn(var LHS: Integer; RHS: Integer): Integer;
begin
LHS := RHS;
Result := RHS;
end;
(...)
var
a, b: Integer;
begin
b := AssignAndReturn(a, Round(Sqrt(10)));
Run Code Online (Sandbox Code Playgroud)
我不是想"让一切看起来像C".我刚才注意到,有时再次在同一行中"重用"赋值的右侧会很好.(参见Lazarus/Free Pascal:如何改进while循环的编码风格(避免使用无限循环),例如,每次传递都会重新分配布尔表达式.)
FreePascal,Delphi模式。经过一些实验,我发现该代码已编译,FPC告诉我类过程必须是“静态的”。但是我的问题是:那为什么运算符Equal不需要“静态”并且可以正常编译?我也无法理解“类”过程和“类”与“静态”有什么区别(例如,在Python中,classmethod您获取参数-引用该类,在staticmethod-中您没有此类参数)。
type TPos = record
FLine: Word;
FPos: Word;
class procedure Init(out a: TPos); static;
class operator Equal(a, b: TPos): Boolean;
end;
Run Code Online (Sandbox Code Playgroud)
PS。我设置“ delphi”标签的原因是:1)它是在delphi模式下编写的2)因为我找到了与Delphi相同的文档:关于类和静态关键字。
使用此程序,我试图读取文件并将其随机打印到控制台。我想知道是否必须为此使用数组。例如,我可以将字符串分配到数组中,然后从数组中随机打印。但是,我不确定该如何处理。另一个问题是,当前程序无法从文件中读取第一行。我有一个text.txt包含
1. ABC
2. ABC
...
6. ABC
Run Code Online (Sandbox Code Playgroud)
下面是我的代码。
type
arr = record
end;
var
x: text;
s: string;
SpacePos: word;
myArray: array of arr;
i: byte;
begin
Assign(x, 'text.txt');
reset(x);
readln(x, s);
SetLength(myArray, 0);
while not eof(x) do
begin
SetLength(myArray, Length(myArray) + 1);
readln(x, s);
WriteLn(s);
end;
end.
Run Code Online (Sandbox Code Playgroud)
请让我知道如何解决这个问题!
我的 Pascal 程序在主 begin语句和 4 个程序中有一个菜单。在每个程序中,我都会向用户确认他们是否要返回菜单,否则程序将退出,但是每次程序要退出时,它都会再次返回菜单。
procedure quit;
begin
writeln('<Enter> to quit...');
readln;
end
procedure error;
begin
writeln('Error. Try Again...');
readln;
end;
procedure option1;
begin
clrscr;
writeln('this is option 1');
writeln('would you like to continue? (y/n)');
readln(confirm);
if confirm = 'y' then
begin
writeln('something will happen...');
end;
if confirm = 'n' then
begin
writeln('Return to main menu ? (y/n)');
readln(option);
if option = 'y' then
exit
else
quit;
end;
end;
procedure option2;
begin
clrscr;
writeln('this is option2');
writeln('would …Run Code Online (Sandbox Code Playgroud) 我有一个情况:
procedure Compile();
begin
//stuff
CompileBatch();
end;
procedure CompileBatch();
begin
//stuff
end;
Run Code Online (Sandbox Code Playgroud)
但这显然不起作用,因为在 Compile 中还没有找到标识符“CompileBatch”。是否有任何解决方法,或者我是否必须在 Compile 中重写所有 CompileBatch 代码?我正在使用 Free Pascal。
program test;
uses
sysutils;
const
BUFLEN = 20;
var
Buf0: array[0..BUFLEN-1] of char;
Buf1: array[1..BUFLEN] of char;
s: string;
begin
// Fillchar...
// StrPLCopy...
SetString(s, Buf1, Length(Buf1));
// Error: Incompatible type for arg no. 2: Got "Array[1..20] Of Char", expected "PChar"
SetString(s, Buf0, Length(Buf0));
// compiles ok
end.
Run Code Online (Sandbox Code Playgroud)
在 C/C++ 中,我们经常使用数组名作为指针。在 Delphi 中,我们也可以像上面的例子那样实现。
尽管如此,Buf1在 FreePascal 中使用数组SetString()会导致错误,而Buf0数组工作得很好。数组的基本索引应该无关紧要,对吗?
是否有任何文件证明这种行为是合理的?
在 FPC 3.0.4 和 Delphi 10.3 中测试。
所以我试图在 fpc 中实现一个 C 风格的堆栈结构,
主要原则是:每个元素都有一个值和一个指向下一个元素的指针,堆栈本身只包含一个指向它最后一个元素的指针
type stackNode = class
public
val: integer;
next: ^stackNode;
end;
type stack = class
private
top: ^stackNode;
public
constructor Create();
procedure push(v: integer);
procedure clear();
function pop():integer;
function back():integer;
destructor Destroy();
end;
Run Code Online (Sandbox Code Playgroud)
这是我push在 C++ 中的实现方式:
void push(_t _v) {
top = new stackNode<_t>(_v, top);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在 pascal 中实现基本相同的代码时
procedure stack.push(v: integer);
var
temp: ^stackNode;
begin
temp := top;
new(top);
top^.next := temp;
top^.val := v;
end;
Run Code Online (Sandbox Code Playgroud)
new()当我尝试将第二个元素推入堆栈时,它在调用后给我一个分段错误。
有些东西告诉我 pascalnew()并没有 …
freepascal ×10
pascal ×7
lazarus ×6
delphi ×5
turbo-pascal ×2
declaration ×1
dynamic ×1
pascalscript ×1
procedure ×1