You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ada数组存储用户输入、打印输出及重复词统计报错排查

Ada程序运行报错排查与修复方案

原代码核心错误

  • 输入逻辑完全不符合需求:循环100次调用Get_Line会强制要求用户输入100行内容,和"单次输入、空格分隔内容"的需求相悖;且Character'Value()仅能解析格式为'x'的单字符字面量,传入整行字符串必然触发类型转换运行错误。
  • 数据结构设计缺陷:仅定义了定长字符数组存储原始字符,未设计单词拆分、单词与计数绑定的逻辑,完全无法实现重复单词统计功能。
  • 输出逻辑错误:遍历数组时每次都将整个Data数组强转为字符串输出,会重复打印100次完整内容,无实际意义。
  • 缺失边界处理:未考虑输入长度不足、连续空格、空输入等边界场景,运行时极易触发约束错误。

修复后可运行实现代码

代码实现了全部需求:接收空格分隔的用户输入、拆分单词存入数组、打印存储内容、统计重复单词出现次数,做了基础边界兼容。

with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Strings;
use Ada.Strings;

procedure Main is
   -- 配置参数:最大支持单词数、单个单词最大长度、输入行最大长度
   Max_Word_Count : constant := 100;
   Max_Word_Len   : constant := 50;
   Max_Input_Len  : constant := 1000;

   subtype Word_Type is String(1 .. Max_Word_Len);
   -- 存储拆分后单词、对应出现次数的数组
   Word_List   : array (1 .. Max_Word_Count) of Word_Type;
   Word_Counts : array (1 .. Max_Word_Count) of Integer := (others => 0);
   Valid_Word_Num : Integer := 0;

   Input_Buffer : String(1 .. Max_Input_Len);
   Input_Length : Natural;
   Search_Start : Positive;
   Word_End     : Natural;
   Temp_Word    : Word_Type := (others => ' ');
   Word_Exist   : Boolean;
begin
   Put_Line ("Please enter values (use space as separator)");
   -- 一次性读取整行用户输入
   Get_Line (Input_Buffer, Input_Length);

   if Input_Length = 0 then
      Put_Line ("Error: empty input received.");
      return;
   end if;

   -- 按空格拆分所有单词
   Search_Start := 1;
   while Search_Start <= Input_Length loop
      -- 跳过连续空格
      while Search_Start <= Input_Length and then Input_Buffer(Search_Start) = ' ' loop
         Search_Start := Search_Start + 1;
      end loop;
      exit when Search_Start > Input_Length;

      -- 定位当前单词的结束位置
      Word_End := Search_Start;
      while Word_End <= Input_Length and then Input_Buffer(Word_End) /= ' ' loop
         Word_End := Word_End + 1;
      end loop;
      Word_End := Word_End - 1;

      -- 提取单词到定长格式,尾部补空格对齐类型
      Temp_Word := (others => ' ');
      Temp_Word(1 .. Word_End - Search_Start + 1) := Input_Buffer(Search_Start .. Word_End);

      -- 检查单词是否已存在,存在则计数累加,不存在则新增条目
      Word_Exist := False;
      for I in 1 .. Valid_Word_Num loop
         if Word_List(I) = Temp_Word then
            Word_Counts(I) := Word_Counts(I) + 1;
            Word_Exist := True;
            exit;
         end if;
      end loop;

      if not Word_Exist then
         Valid_Word_Num := Valid_Word_Num + 1;
         Word_List(Valid_Word_Num) := Temp_Word;
         Word_Counts(Valid_Word_Num) := 1;
      end if;

      Search_Start := Word_End + 1;
   end loop;

   -- 打印数组存储的所有单词
   Put_Line ("=== Stored word list ===");
   for I in 1 .. Valid_Word_Num loop
      Put_Line (Trim (Word_List(I), Right));
   end loop;

   -- 打印单词出现次数统计
   Put_Line ("=== Word frequency statistics ===");
   for I in 1 .. Valid_Word_Num loop
      Put ("Word: " & Trim(Word_List(I), Right) & ", Occurrences: ");
      Put_Line (Integer'Image(Word_Counts(I)));
   end loop;
end Main;

代码使用说明

  • 编译运行后直接输入空格分隔的内容,按回车即可得到结果
  • 默认支持最多100个不重复单词,单个单词最长50字符,输入行最长1000字符,可通过修改代码开头的常量值调整上限
  • 自动过滤连续多余空格,空输入会直接给出错误提示

内容的提问来源于stack exchange,提问作者ABDUL HASEEB.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 08:00:58