Ada的信件转换程序

时间:2013-01-30 04:28:59

标签: ada

我在Ada中制作了一个字母转换程序,它将采用任何小写字母并将其更改为大写,反之亦然。整个过程一直有效,直到你到达我的argument_count = 3部分。它应该将文本输出到另一个文件,它根本就不起作用。我的异常错误when others=> error不断弹出。

当你将它输入终端或命令行时,它应该是这样的:

./case_converter u plain.text output

With Ada.Text_IO, Ada.Command_Line, Ada.Characters.Handling;
Use Ada.Text_IO, Ada.Command_Line, Ada.Characters.Handling;

Procedure case_conversion is

    Argument_Error : Exception; --the excpetion raise for bad command-line arguments.
    Input, Output  : File_Type;-- input and output files, if specified.
    File           : File_Type; 
    Eol           : Boolean;
    Char           : Character;

Function To_Upper (upper : Character) Return Character is  

Begin

    Case upper is 

        When 'a' =>
            Return 'A';
        When 'b' =>
            Return 'B';
        When 'c' =>
            Return 'C';
        When 'd' =>
            Return 'D';
        When 'e' =>
            Return 'E';
        When 'f' =>
            Return 'F';
        When 'g' =>
            Return 'G';
        When 'h' =>
            Return 'H';
        When 'i' => 
            Return 'I';
        When 'j' =>
            Return 'J';
        When 'k' =>
            Return 'K';
        When 'l' =>
            Return 'l';
        When 'm' =>
            Return 'M';
        When 'n' =>
            Return 'N';
        When 'o' =>
            Return 'O';
        When 'p' =>
            Return 'P';
        When 'q' =>
            Return 'Q';
        When 'r' =>
            Return 'R';
        When 's' =>
            Return 'S';
        When 't' =>
            Return 'T';
        When 'u' =>
            Return 'U';
        When 'v' =>
            Return 'V';
        When 'w' =>
            Return 'W';
        When 'x' =>
            Return 'X';
        When 'y' =>
            Return 'Y';
        When 'z' =>
            Return 'Z';
        When Others =>
            Return char; -- This will let me know if there is a bug in the code, and to just return char.
    end case;
end To_Upper;


Function To_Lower (lower : Character) Return character is              

Begin

    Case lower is

        When 'A' =>
            Return 'a';
        When 'B' =>
            Return 'b';
        When 'C' =>
            Return 'c';
        When 'D' =>
            Return 'd';
        When 'E' =>
            Return 'e';
        When 'F' =>
            Return 'f';
        When 'G' =>
            Return 'g';
        When 'H' =>
            Return 'h';
        When 'I' =>
            Return 'i';
        When 'J' =>
            Return 'j';
        When 'K' =>
            Return 'k';
        When 'L' =>
            Return 'l';
        When 'M' =>
            Return 'm';
        When 'N' =>
            Return 'n';
        When 'O' =>
            Return 'o';
        When 'P' =>
            Return 'p';
        When 'Q' =>
            Return 'q';
        When 'R' =>
            Return 'r';
        When 'S' =>
            Return 's';
        When 'T' =>
            Return 't';
        When 'U' =>
            Return 'u';
        When 'V' =>
            Return 'v';
        When 'W' =>
            Return 'w';
        When 'X' =>
            Return 'x';
        When 'Y' =>
            Return 'y';
        When 'Z' =>
            Return 'z';
        When Others =>
            Return char; 

    end case;
End To_Lower;


begin

        If argument_count < 1 then
            Raise Argument_Error;
        end if;

           IF Argument_Count > 1 THEN 
        null;     
   END IF; 

   IF Argument_Count > 2 THEN 
        null; 
   END IF;   

      If argument_Count = 1 then
            loop          
                look_Ahead (Char, Eol);       
                    if Argument (1) = "U" or Argument (1) = "u" then
                           Get (char);
                           Char := (To_Upper(char));
                          Put (char);
                        elsif
                               Argument (1) = "L" or Argument (1) = "l" then
                            Get (char);
                            Char := (To_Lower(char));
                            Put (char);
                       end if;  
                   end loop;
            end if;

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

        If Argument_Count = 2 then            
           Open (file, In_File, Argument (2));
        end if;

        While Not End_of_File (file) loop
               Look_Ahead (File, Char, EoL);

                If Argument (1) = "U" or Argument (1) = "u" then
                   Get (file, char);
                   Char := (To_Upper(char));
                   Put (char);
                elsif
                       Argument (1) = "L" or Argument (1) = "l" then
                    Get (file, char);
                    Char := (To_Lower(char));
                    Put (char);
               end if;     


    If EoL then
               New_Line;
        end if; 
end loop;

------------------------------------------------------------    
          If Argument_Count = 3 then
            Open (file, In_File, Argument (2));
            Create (Output, Out_file, Argument(3));



              While Not End_of_File (file) loop 
             -- Look_Ahead (file, Char, EoL);
                              If EoL then
                             New_Line(output);
                         end if;   
                 If Argument (1) = "U" or Argument (1) = "u" then


                           Get (file, char);
                           Char := (To_Upper(char));
                           Put (output, char);

                       elsif Argument (1) = "L" or Argument (1) = "l" then                           
                               ---------
                                  If Eol then
                                      new_line (output);
                                  end if;
                                Get (file, char);
                                Char := (To_Lower(char));
                                Put (output, char);

                      end if;             
    end loop;
end if;


   IF Is_Open(input) THEN 
      Close(Input); 
   END IF; 

   IF Is_Open(Output) THEN 
      Close(Output); 
   END IF;



EXCEPTION
   WHEN Name_Error =>
      Put_Line("The file " & Argument(2) & " cannot be used for input");
   WHEN OTHERS =>
      Put_Line("Usage: case_converter U|L [infile [outfile]]");

end case_conversion;

3 个答案:

答案 0 :(得分:4)

为什么不Ada.Characters.Handling.To_Upper(和To_Lower)功能?

如果您确实需要检查字符是否使用字母Is_Letter

答案 1 :(得分:3)

使用when others捕获异常通常是一个坏主意,除非您确定可能会引发异常(甚至非常有经验的开发人员也可能会出错)。

在你的情况下,你应该注释掉“别人的时候”;当你运行你的程序并发生异常时,它会被报告(通常是它发生的行号,但不是 - 在我的机器上 - 在这种情况下)。它有助于获得堆栈跟踪:这在Mac OS X上比在其他机器上更难,我写了here

我跑了

$ ./case_conversion u case_conversion.adb t  

Execution terminated by unhandled exception
Exception name: ADA.IO_EXCEPTIONS.STATUS_ERROR
Message: file not open
Call stack traceback locations:
0x1000103bc 0x10000c9eb 0x100001e9a 0x100001a19

并用

解码堆栈跟踪
$ atos -o case_conversion 0x1000103bc 0x10000c9eb 0x100001e9a 0x100001a19
system__file_io__check_read_status (in case_conversion) (s-fileio.adb:207)
ada__text_io__end_of_file (in case_conversion) (a-textio.adb:304)
_ada_case_conversion (in case_conversion) (case_conversion.adb:177)
main (in case_conversion) (b~case_conversion.adb:265)

并查看了case_conversion.adb:177

173    if Argument_Count = 2 then
174       Open (File, In_File, Argument (2));
175    end if;
176
177    while not End_Of_File (File) loop

那么,如果Argument_Count为3,会发生什么?

如果你没有在Mac OS X上运行,而不是atos尝试addr2line

答案 2 :(得分:-1)

Shark说得对吧BTW。愚蠢的是不要使用内置例程。

但是,如果你想出于某种奇怪的原因想要自己编写,那么这些字母是连续的并且在上下相同的顺序中这一事实使得你的方式更加愚蠢。请注意,您的那些大的鸣笛案陈述中的每个条目都是相同的干扰。要进行每个案例输入,您只需复制前一个案例并将两边的值增加一。这是我们发明计算机帮助我们的重复机械活动的确切类型,不是吗? : - )

在两者之间进行转换的传统方法是在整理顺序中添加两组字符之间的差异(大多数人选择字母'A')。例如:

Upper_To_Lower_Difference : constant := character'pos('a') - character'pos('A');

function To_Upper (Lower : character) return character is
begin
    if Lower in 'a'..'z' then
        return character'val(character'pos(Lower) + Upper_To_Lower_Difference);
    end if;
    return Lower;
end To_Upper;