Guest User

Untitled

a guest
Apr 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. --Mari Sagawa
  2. --CS 145 Lab 1
  3. --9.28.11
  4.  
  5. --show_words C [input_file [output_file]]
  6. -------------------------------------------------------------------------------
  7. --
  8. -- Program to count or display words.
  9. -- Author:
  10. --
  11. -- Usage:
  12. -- show_words command [input_file [output_file]]
  13. -- command is either C or S
  14. -- if C is given a count of the number of words will be output
  15. -- if S is given the individual words on each line of the input
  16. -- will be displayed
  17. -- input_file is the name of the input data file. If input_file is not
  18. -- given, input will be taken from standard_input.
  19. -- output_file is the name of the output data file. If output_file is
  20. -- not given, output will be sent to standard_output.
  21. -------------------------------------------------------------------------------
  22. WITH Ada.Text_IO;
  23. USE Ada.Text_IO;
  24. WITH Ada.Integer_Text_IO;
  25. USE Ada.Integer_Text_IO;
  26. WITH Ada.Command_Line;
  27.  
  28. --FIRST
  29. PROCEDURE Show_Words IS
  30.  
  31. -- Input and Output files
  32. Input_File,
  33. Output_File : File_Type;
  34.  
  35. -- Count words or show words
  36. Show_Words : Boolean := False;
  37.  
  38. -- Print_Usage prints a usage message.
  39. --PRINT USAGE
  40. PROCEDURE Print_Usage IS
  41. BEGIN
  42. -- TODO: Output a Usage message. --***DONE***--
  43. Put("Usage: show_words C | S [infile [outfile]]");
  44. --null; -- This is only included so that the program will compile
  45. END Print_Usage;
  46.  
  47.  
  48.  
  49. --HANDLE ARGUEMENTS
  50. -- Handle_Arguments is used to process the command line arguments and
  51. -- do the appropriate set up for the different argument options. This
  52. -- function returns True if the arguments were handled correctly and
  53. -- False if there was a problem with the command line arguments.
  54. FUNCTION Handle_Arguments RETURN Boolean IS
  55. USE Ada.Command_Line;
  56. Char : Character;
  57. BEGIN
  58. -- TODO: Are the number of arguments correct, at least one and no --***DONE***--
  59. -- more than three? If not, return False.
  60.  
  61. -- Check for sufficient command line arguments
  62. IF Argument_Count <= 0 OR Argument_Count > 3 THEN
  63. RETURN False;
  64. END IF;
  65.  
  66.  
  67.  
  68. -- TODO: Check to see that the first Argument is either "S" or "C".
  69. -- If it's S then set Show_Words to True. If it's not S or C, output
  70. -- an error message and return False.
  71. IF Argument_Count < 2 THEN
  72. CASE Char IS
  73. WHEN 'S' =>
  74. Show_Words := True;
  75. WHEN 's' =>
  76. Show_Words := True;
  77. WHEN 'C' =>
  78. Show_Words := False;
  79. WHEN OTHERS =>
  80. Put("Please enter C or S. ");
  81. RETURN False;
  82. END CASE;
  83. END IF;
  84.  
  85.  
  86. -- Handle the input and output files
  87. IF Argument_Count >= 2 THEN
  88. Open (Input_File, Name => Argument(2), Mode => In_File);
  89. Set_Input (Input_File);
  90. END IF;
  91. IF Argument_Count = 3 THEN
  92. Create (Output_File, Name => Argument(3), Mode => Out_File);
  93. Set_Output (Output_File);
  94. END IF;
  95.  
  96. RETURN True;
  97. END Handle_Arguments;
  98.  
  99.  
  100. --PROCESS INPUT
  101. -- Process_Input reads the input and produces the required output.
  102. PROCEDURE Process_Input IS
  103.  
  104. -- Input line definition
  105. Input_Line_Max : CONSTANT Positive := 127;
  106. SUBTYPE Input_Line_Type IS String (1 .. Input_Line_Max);
  107.  
  108. -- Input line and length
  109. Input_Line : Input_Line_Type; -- Input goes here
  110. Input_Length : Natural; -- Length of the input
  111.  
  112. -- Variables for tracking words
  113. Word_Count : Natural;
  114. Word_Start,
  115. Word_End : Positive; -- Start and end of word
  116.  
  117. Word : Boolean;
  118. I : Positive := 1;
  119. Char : Character;
  120. Char_Count : Integer := 0;
  121. BEGIN
  122. LOOP
  123. -- Get the input line
  124. Get_Line (Input_Line, Last => Input_Length);
  125.  
  126. -- An empty line ends the program
  127. EXIT WHEN Input_Length = 0;
  128.  
  129. Word_Count := 0;
  130.  
  131.  
  132.  
  133. -- TODO: Go through the input line and locate the words. If
  134. -- Show_Words is True, output each word. If Show_Words is False,
  135. -- increment Word_Count.
  136. -- IF Char = ' ' THEN
  137. -- IF Char /= ' ' THEN
  138. -- IF Char = ' ' THEN
  139. -- Word := True;
  140. -- END IF;
  141. -- END IF;
  142. -- END IF;
  143.  
  144. FOR N IN Input_Line'First..Input_Line_Max LOOP
  145. IF Char = ' ' THEN
  146. Char_Count := Char_Count + 1;
  147. Put(Char_Count, 0);
  148. END IF;
  149.  
  150. IF Char /= ' ' THEN
  151. Word_Start := I;
  152. Char_Count := Char_Count + 1;
  153. --Put(Char_Count, 0);
  154. -- IF Char = ' ' THEN
  155. -- Word_End := Char_Count - 1;
  156. -- Word_Count := Word_Count + 1;
  157. -- I := Char_Count;
  158. -- END IF;
  159. END IF;
  160. END LOOP;
  161.  
  162.  
  163. -- IF SHOW_WORDS IS TRUE. Reached the end-of-line, do the right thing.
  164. IF Show_Words THEN
  165. Put_Line(Input_Line(Word_Start..10));
  166.  
  167. -- Output a blank line after all the words have been output.
  168. New_Line;
  169. ELSE
  170. -- IF SHOW_WORDS IS FALSE. Output the number of words
  171. Put("Poop");
  172. Word_Count := Word_Count + 1;
  173. Put(Word_Count, Width => 0);
  174. New_Line;
  175. END IF;
  176.  
  177. END LOOP;
  178. END Process_Input;
  179.  
  180. -- Close_Files will close the input and output files if necessary.
  181. PROCEDURE Close_Files IS
  182. BEGIN
  183. IF Is_Open (Input_File) THEN
  184. Close (Input_File);
  185. END IF;
  186. IF Is_Open (Output_File) THEN
  187. Close (Output_File);
  188. END IF;
  189. END Close_Files;
  190.  
  191.  
  192. BEGIN -- show_words
  193.  
  194. IF Handle_Arguments THEN
  195. Put_Line("Whoo!");
  196. Process_Input;
  197. Put("Whoo!");
  198. Close_Files;
  199. ELSE
  200. Print_Usage;
  201. Put("dang");
  202. END IF;
  203.  
  204. END Show_Words;
Add Comment
Please, Sign In to add comment