标签: pascal

如何将树递归函数(或算法)转换为循环函数?

我在 pascal (或 delphi )中编写了一个递归树函数,但是当我运行它时,我收到了“内存不足”消息。我需要将这段代码中的计算递归函数转换为非递归函数,你能告诉我怎么做吗:

program testing(input, output);
type
  ptr = ^tr;
  tr = record
    age: byte;
    left, right: ptr;
  end; 

var
  topper: ptr;
  total, day: longint;

  procedure mycreate(var t: ptr);
  var 
    temp:ptr;

  begin
    new(temp);
    temp^.age := 1;
    temp^.left := nil;
    temp^.right := nil;
    t := temp;

  end;

  procedure gooneday(var t: ptr);
  begin
    if t^.age <> 5 then
    begin
      if t^.age = 2 then
        mycreate(t^.left)
      else if t^.age = 3 then
        mycreate(t^.right);
      
      t^.age := t^.age + 1;
      total := total …
Run Code Online (Sandbox Code Playgroud)

delphi algorithm tree recursion pascal

1
推荐指数
1
解决办法
902
查看次数

字符串到缓冲区的额外空格 Filestream.WriteBuffer 方法中隐式的 void 类型转换

有一段时间不需要在这里发帖了,但我在实现文件流时遇到了问题。将字符串写入文件流时,结果文本文件在每个字符之间插入了额外的空格

所以当运行这个方法时:

Function TDBImportStructures.SaveIVDataToFile(const AMeasurementType: integer;
  IVDataRecordList: TIV; ExportFileName, LogFileName: String;
  var ProgressInfo: TProgressInfo): Boolean; // AM
var
  TempString: unicodestring;
  ExportLogfile, OutputFile: TFileStream;
begin
  ExportLogfile := TFileStream.Create(LogFileName, fmCreate);
  TempString :=
    'FileUploadTimestamp, Filename, MeasurementTimestamp, SerialNumber, DeviceID, PVInstallID,'
    + #13#10;
  ExportLogfile.WriteBuffer(TempString[1], Length(TempString) * SizeOf(Char));
  ExportLogfile.Free;

  OutputFile := TFileStream.Create(ExportFileName, fmCreate);
  TempString :=
    'measurementdatetime,closestfiveseconddatetime,closesttenminutedatetime,deviceid,'
    + 'measuredmoduletemperature,moduletemperature,isc,voc,ff,impp,vmpp,iscslope,vocslope,'
    + 'pvinstallid,numivpoints,errorcode' + #13#10;
  OutputFile.WriteBuffer(TempString[1], Length(TempString) * SizeOf(Char));
  OutputFile.Free;
end;
Run Code Online (Sandbox Code Playgroud)

(这是一种精简的测试方法,仅写入标题)。“OutPutFile”生成的 csv 文件读取

'measuredmoduletempera ture等在写字板中查看时,但不是在excel、记事本等中查看。我猜测它的SizeOf(Char)语句在unicode上下文中是错误的,但我不确定什么是正确的插入此处。“ExportLogfile”似乎工作正常,但“OutPutFile”不行

从我在其他地方读到的内容来看,这是问题所在,而不是写字板,请参阅http://social.msdn.microsoft.com/Forums/en-US/7e040fd1-f399-4fb1-b700-9e7cc6117cc4/ unicode-to-files-and-console-vs-notepad-wordpad-word-etc?forum=vcgeneral

大家有什么建议吗?非常感谢,布莱恩

delphi pascal filestream tfilestream

1
推荐指数
1
解决办法
553
查看次数

如何在一个 case 语句中使用多个语句?

所以我正在使用 pascal,并且我想向一种情况添加多个语句。我尝试了这段代码,但收到错误:“错误:常量和 CASE 类型不匹配”

procedure pay;
begin
loop:=loop+1;
CASE loop OF
1: 
writeln('E-Mail: ');
readln(mailO[1]);
writeln('amount: ');
readln(amount[1]);


end;
Run Code Online (Sandbox Code Playgroud)

pascal freepascal turbo-pascal

1
推荐指数
1
解决办法
4137
查看次数

Pascal - 查找元素是否存在于集合中

我有这样的代码:

program Project1;
uses crt;
type alph=set of 'A'..'Z';
var mn:alph;

begin
  clrscr;
  if ('A' in mn) then writeln( 'Yes');
  readln;
end.   
Run Code Online (Sandbox Code Playgroud)

它不打印任何内容,并且引发一些问题: project1.lpr(11,14)警告:变量“mn”似乎没有初始化 我不明白为什么,有什么问题吗?

pascal

1
推荐指数
1
解决办法
2961
查看次数

如何修复语法错误,“;” 预期但发现“ELSE”

嗨,我想运行此代码,但它向我显示了几个错误,请任何人都可以提供帮助

Program Climat;
var clim: integer;
begin
Writeln('entrez degré');
Readln(clim);
if (clim < 0) then
Writeln('c est glacial ');
else 
if (clim < 30) then
Writeln('le temps est doux');
else
Writeln('trés chaud'):
end.
Run Code Online (Sandbox Code Playgroud)

pascal

1
推荐指数
1
解决办法
67
查看次数

Pascal 记录在记录中混合字和字节不能按预期工作

我的数据是这样的(每两个'-'一个字节):

+----+--+----+----+
|0840|0C|00AD|0840|
+----+--+----+----+
Run Code Online (Sandbox Code Playgroud)

如果我使用此代码:错误

Type TAlarmasRATP = record
   Funcion   : word;
   Instancia : byte;
   Err_0     : word;
   Err_1     : word;
end;
Run Code Online (Sandbox Code Playgroud)

函数 = 4008,实例 = 0C,Err_0 = 08AD,Err_1 = 0040

如果我使用此代码:正确

Type TAlarmasRATP = record
   Funcion   : word;
   Instancia : byte;
   Err_0     : array [0..1] of byte;
   Err_1     : array [0..1] of byte;
end;
Run Code Online (Sandbox Code Playgroud)

函数 = 4008,实例 = 0C,Err_0 = AD00,Err_1 = 4008

为什么第一个不起作用?

delphi pascal record

1
推荐指数
1
解决办法
113
查看次数

内联汇编器:无效的引用语法

两天前,我在 Free Pascal 编译器 2.2.0 中遇到以下错误:

Error: Invalid reference syntax
Run Code Online (Sandbox Code Playgroud)

史前史:

我想编译一个 pascal 源代码,其中包含一些适用于平台“i386-linux”的汇编指令

procedure drawpixel(x,y,color: word); assembler;
asm
  mov ax, y
  mov bx, x
  mov dl, color

  mov cx, 320
  mul cx
  add ax, bx
  mov di, ax
  mov [es:di], dl /// Right at [es:di] the inline assembler gives this annoying exception
  ret
end;
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我吗?

assembly pascal freepascal i386

1
推荐指数
1
解决办法
99
查看次数

帕斯卡牛顿拉夫森,结果不是很好

我在 Pascal 中实现了 Newton-Raphson 方法。这很奇怪,因为相同的代码在 C++ 中给出了很好的结果(对于 9,它是 3),但在 Pascal 中,对于 9,它是 3.25,为什么会这样呢?

帕斯卡:

Program NewtonRaphsonIter(output);

{$mode objFPC}

function newton_raphson_iter(a: real; p: real; eps: real; max_i: integer) : real;
var
    x: real;
    i: integer;
begin
    x := a / 2.0;
    i := 0;
    
    repeat
        x := (x + a / x) / 2.0;
        i := i + 1;
        if (x * x = a) then break;
        if (i >= max_i) then break; 
    until abs(x - a / x) > eps; …
Run Code Online (Sandbox Code Playgroud)

c++ pascal newtons-method

1
推荐指数
1
解决办法
126
查看次数

如何以UTF8编码将JSONObject保存到json文件Rad Studio/Delphi

我使用 Rad studio 11。我从 json 文件(文件是 UTF8 编码)读取信息并转换为 jsonobject。然后我对此 jsonobject 进行更改并希望保存到 json 文件。信息已成功写入文件,但文件具有 Windows-1251 编码。需要做什么才能使文件编码为UTF8?它需要我,因为 json 文件包含俄语符号(在 Windows-1251 编码中它看起来像“?”)。

我从这样的文件中读取:

var inputfile:TextFile;
str:string;
...
if OpenDialog1.Execute then  begin
  AssignFile(inputfile, OpenDialog1.FileName);
  reset(inputfile);

  while not Eof(inputfile) do
  begin
    ReadLn(inputfile, str);
    str1 := str1+UTF8ToANSI(str);
  end;

  closefile(inputfile);
end;
Run Code Online (Sandbox Code Playgroud)

我像这样转换为 Jsonobject:

LJsonObj:=TJSONObject.ParseJSONValue(str1) as TJSONobject;
Run Code Online (Sandbox Code Playgroud)

尝试像这样保存 JsonObject:

var   
  listStr: TStringList;
  Size: Integer;
  I: Integer;
  
...
  
  Size := Form3.LJsonObj.Count;
  liststr := TStringList.Create;

  try
    listStr.Add('{');

    if Size > 0 then
      listStr.Add(LJsonObj.Get(0).ToString);
   
    showmessage(LJsonObj.Get(0).ToString);

    for I := 1 to Size …
Run Code Online (Sandbox Code Playgroud)

delphi json pascal rad-studio

1
推荐指数
1
解决办法
3134
查看次数

为什么我的 For 循环只打印最后一个结果?

我正在尝试编写一个 for 循环,它将给定的数字 n 与特定范围内的每个数字(例如 1-10)相乘,并打印每个步骤。我的代码如下所示:

begin
     writeln('FOR');
     for i:=1 to 10 do
     x:=n*i;
     writeln(x);
     readln();
     end;
Run Code Online (Sandbox Code Playgroud)

但是,只会打印最后的结果。如果 n=5,则为 50。我该如何解决这个问题?

亲切的问候,朱利叶斯

没有 x:=n*i; 行,它将打印 0 十次,正如它应该做的那样。另外,在 writeln 行中将 x 替换为 n 将打印 n 10 次。

pascal for-loop

1
推荐指数
1
解决办法
64
查看次数