2

我想编写一个 Ada 程序,用适用的 HTML 实体替换 Latin1 字符,但我的代码不起作用:text.txt并且converted.txt始终相同。我的导师说代码是正确的。提前致谢!

这是我的代码:

with Ada.Text_IO;
procedure Entity_Converter is
   use Ada.Text_IO;

   Source : File_Type;
   Target : File_Type;
   Source_Char : Character;
begin
   Open (Source, In_File, "test.txt");
   Create (Target, Out_File, "converted.txt");
   while not End_Of_File (Source) loop
      Get (Source, Source_Char);
      case Source_Char is
         when 'ä' =>
            Put (Target, "ä");
         when 'Ä' =>
            Put (Target, "Ä");
         when 'ö' =>
            Put (Target, "ö");
         when 'Ö' =>
            Put (Target, "Ö");
         when 'ü' =>
            Put (Target, "ü");
         when 'Ü' =>
            Put (Target, "Ü");
         when 'ß' =>
            Put (Target, "ß");
         when others =>
            Put (Target, Source_Char);
      end case;
   end loop;
   Close (Source);
   Close (Target);
end Entity_Converter;
4

2 回答 2

3

结果取决于源文本和测试文件的编码。

要解决前者,请使用包的常量Ada.Characters.Latin_1

with Ada.Characters.Latin_1;
use Ada.Characters.Latin_1;
...
   case Source_Char is
      when LC_A_Diaeresis =>
         Put (Target, "ä");
      when UC_A_Diaeresis =>
         Put (Target, "Ä");
      ...
      when LC_German_Sharp_S =>
         Put (Target, "ß");
      when others =>
         Put (Target, Source_Char);
   end case;

后者取决于您的编辑器。

于 2011-12-27T15:32:52.003 回答
1
于 2011-12-27T16:21:58.343 回答