私は 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;