【问题标题】:How to count number of occurrences of a character in an array in Pascal如何计算Pascal数组中字符的出现次数
【发布时间】:2022-01-15 10:30:54
【问题描述】:

我必须编写一个帕斯卡代码,用于计算字符在代码中出现的频率并通过输出模式显示

输入 P2 变化:

编码阶段的第二次尝试 我尝试修改代码。我添加了输出变量writeln('input array of characters');writeln('Number of Occurrences',k);,这应该可以帮助我输出S 字符在代码中整体出现的次数,加上使用forif 命令根据条件展示的最终值,如果频率为 1 则计入S,仍然出现错误,请查看 Input P2 & Output P2

Input P1
function Count(t, s: String): Integer;
var
  Offset, P: Integer;
begin
  Result := 0;
  Offset := 1;
  P := PosEx(t, s, Offset);
  
  while P > 0 do
  begin
    Inc(Result);
    P := PosEx(t, s, P + 1);
  end;
end;

Output P2
Target OS: Linux for x86-64
Compiling main.pas
main.pas(5,3) Error: Identifier not found "Result"
main.pas(7,8) Error: Identifier not found "PosEx"
main.pas(8,3) Error: Identifier not found "unsigned"
main.pas(8,12) Fatal: Syntax error, ";" expected but "identifier N" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode

-------------------------------------------------------------------

Input P2
program p1

var S:string

i:integer
begin
writeln('input array of characters');
k:=O;
for i:=1 to length (S) do
if (S[i])='m') and (S[i+1]='a') then k:=k+1;
writeln('Number of  Occurrences',k);
Readln;
end.

Output P2
Compiling main.pas
main.pas(2,1) Fatal: Syntax error, ";" expected but "VAR" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode

【问题讨论】:

  • 您尝试过什么,您尝试过什么失败了?理想情况下,您应该提供一个minimal reproducible example 来说明您的尝试,并包含有关它如何失败的具体信息,以及错误消息和/或错误输出。 Stack Overflow 不是代码编写服务;最好的问题是那些提供有用信息的问题,以便回答的人可以指导您设计自己的正确答案。请参阅 How to Ask 一个好问题(和 How do I ask and answer homework questions,如果相关)。
  • 我只是想学习编码语言,所以这是我做不好的事情!
  • 不多说,代码的编译方式没有重复,只是在自己的帐户上运行的单行代码
  • 太棒了!,这看起来是一个好的开始。但是当你在这里或任何地方提出问题时,不要说“它一直在崩溃”、“它不工作”等。那是一个字符的腰部。相反,确切地说它是如何崩溃的,或者它不应该做什么。并提供一个显示崩溃的测试用例。那么ts 的输入是什么,它会崩溃吗?顺便说一句,您应该分别计算大写和小写字符还是一起计算?那么“频率......”呢?你决定如何呈现它了吗?

标签: count character frequency pascal


【解决方案1】:

您在第一个块中看到的错误:

找不到标识符“结果”

标准 Pascal 无法识别伪变量 Result。在某些 Pascal 实现中(例如 Delphi),它可用于为函数结果赋值。您正在使用的 Pascal 需要将函数的结果分配给函数的名称。例如:

function Whatever(): integer;
begin
  Whatever := 234;
end;

找不到标识符“PosEx”

并非所有 Pascal 实现都包含 PosEx() 函数。您需要改用Pos()。但是,Pos() 的标准实现不包括PosEx 具有的“搜索开始位置”。因此,您需要放弃 Pos() 并像在“输入 P2”中所做的那样,即遍历每个字符的文本字符并计算出现次数。

找不到标识符“无符号”

看来你已经删除了那个未知的标识符。

您在第二个块中看到的错误:

在输出 P2 中,错误消息应该是清晰的。您在需要分号的地方遗漏了一个分号。实际上,您缺少其中三个。

您还缺少读取用户输入的行:ReadLn(S);

最后,要计算大写和小写字符,您可以使用额外的字符串变量,例如 SU: string,在读取用户输入后将 SU := UpperCase(S) 分配给该变量,然后使用该字符串来计算出现次数。

【讨论】:

  • 尽管我在输入 P2 中将分号放在了 S:String;I:Integer; 之后,但我在输出 P2 中遇到了同样的错误。尽管考虑到ReadLn(S) 低于writeln("Number of occureances",k);。您介意告诉我应该考虑在代码的哪个位置添加第三个分号吗?
  • program p1 后面需要有一个分号。关于ReadLn(S);,努力再想一想,什么时候才是读取用户输入的合适时间。
  • Compiling main.pas main.pas(8,1) Error: Identifier not found "k" main.pas(8,4) Error: Identifier not found "o" main.pas(9,21) Warning: Variable "S" does not seem to be initialized main.pas(10,14) Fatal: Syntax error, "THEN" expected but ")" found Fatal: Compilation aborted Error: /usr/bin/ppcx64 returned an error exitcode 这是怎么回事,我输入了分号,Readln(S); 应该在writeln 之后或更早。不管怎样,我猜变量有问题!
  • 您需要在使用前在Var 部分中声明变量。如果您要求用户输入,您希望在您提出要求之前或之后获得该输入。
  • 从编译输出看来,使用了 Free Pascal。这可能是在 lazarus 中启用 Delphi 模式,或者将 -Sd 传递给 FPC 命令行的问题。默认模式更像 Turbo Pascal。错误的“无符号”位虽然有点奇怪,但建议使用更接近 C/C++ 的不同编译器。
【解决方案2】:

我觉得这更像是你想做的:

function Count(t, s: String): Integer;
var
  Offset,Res, P: Integer;
begin
  Res := 0
  Offset := 1;
  repeat
       P := Pos(t, s, Offset);
       if p>0 then 
          Inc(Res);
       Offset := P+1
  untl P = 0;
  Count := Res; 
end;

现在,如果你没有 Pos,你可以实现它:

Function Pos(const t,s:string; const Start:integer):Integer;
Var
    LS, LT,     {Length}
    IxS, IxT,   {Index)
    R: Integer; {Result}

begin
    R := 0;
{use only one of the two following lines of code}
{if your compiler has length}
    LS := length(S); LT := Length(T);
{If it does not}
    LS := Ord(s[0]); LT := Ord(T[0]);

    if (LS <= LT)  {if target is larger than search string, it's not there}
      and (Start<=LT) and {same if starting position beyond size of S}
       (Start+LT <-LS)  then  {same if search would go beyond size of S} 
    begin  {Otherwise, start the search}
        ixT := 1;
        ixS := Start;
        repeat 
             Inc(R); {or R:= R+1; if INC not available }
             If (S[ixS] <> T[ixT]) then
                  R := 0  {they don't match, we're done}
             else
             begin {Move to next char}
                 Inc(ixS);
                 Inc(ixT);
             end;
        until (R=0) or (ixT>LT); {if search failed or end of target, done}
        Pos := R;
    end;

【讨论】:

    猜你喜欢
    • 2020-05-06
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2021-03-27
    • 2015-04-15
    相关资源
    最近更新 更多