Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Mari Sagawa
- --CS 145 Lab 1
- --9.28.11
- --show_words C [input_file [output_file]]
- -------------------------------------------------------------------------------
- --
- -- Program to count or display words.
- -- Author:
- --
- -- Usage:
- -- show_words command [input_file [output_file]]
- -- command is either C or S
- -- if C is given a count of the number of words will be output
- -- if S is given the individual words on each line of the input
- -- will be displayed
- -- input_file is the name of the input data file. If input_file is not
- -- given, input will be taken from standard_input.
- -- output_file is the name of the output data file. If output_file is
- -- not given, output will be sent to standard_output.
- -------------------------------------------------------------------------------
- WITH Ada.Text_IO;
- USE Ada.Text_IO;
- WITH Ada.Integer_Text_IO;
- USE Ada.Integer_Text_IO;
- WITH Ada.Command_Line;
- --FIRST
- PROCEDURE Show_Words IS
- -- Input and Output files
- Input_File,
- Output_File : File_Type;
- -- Count words or show words
- Show_Words : Boolean := False;
- -- Print_Usage prints a usage message.
- --PRINT USAGE
- PROCEDURE Print_Usage IS
- BEGIN
- -- TODO: Output a Usage message. --***DONE***--
- Put("Usage: show_words C | S [infile [outfile]]");
- --null; -- This is only included so that the program will compile
- END Print_Usage;
- --HANDLE ARGUEMENTS
- -- Handle_Arguments is used to process the command line arguments and
- -- do the appropriate set up for the different argument options. This
- -- function returns True if the arguments were handled correctly and
- -- False if there was a problem with the command line arguments.
- FUNCTION Handle_Arguments RETURN Boolean IS
- USE Ada.Command_Line;
- Char : Character;
- BEGIN
- -- TODO: Are the number of arguments correct, at least one and no --***DONE***--
- -- more than three? If not, return False.
- -- Check for sufficient command line arguments
- IF Argument_Count <= 0 OR Argument_Count > 3 THEN
- RETURN False;
- END IF;
- -- TODO: Check to see that the first Argument is either "S" or "C".
- -- If it's S then set Show_Words to True. If it's not S or C, output
- -- an error message and return False.
- IF Argument_Count < 2 THEN
- CASE Char IS
- WHEN 'S' =>
- Show_Words := True;
- WHEN 's' =>
- Show_Words := True;
- WHEN 'C' =>
- Show_Words := False;
- WHEN OTHERS =>
- Put("Please enter C or S. ");
- RETURN False;
- END CASE;
- END IF;
- -- Handle the input and output files
- IF Argument_Count >= 2 THEN
- Open (Input_File, Name => Argument(2), Mode => In_File);
- Set_Input (Input_File);
- END IF;
- IF Argument_Count = 3 THEN
- Create (Output_File, Name => Argument(3), Mode => Out_File);
- Set_Output (Output_File);
- END IF;
- RETURN True;
- END Handle_Arguments;
- --PROCESS INPUT
- -- Process_Input reads the input and produces the required output.
- PROCEDURE Process_Input IS
- -- Input line definition
- Input_Line_Max : CONSTANT Positive := 127;
- SUBTYPE Input_Line_Type IS String (1 .. Input_Line_Max);
- -- Input line and length
- Input_Line : Input_Line_Type; -- Input goes here
- Input_Length : Natural; -- Length of the input
- -- Variables for tracking words
- Word_Count : Natural;
- Word_Start,
- Word_End : Positive; -- Start and end of word
- Word : Boolean;
- I : Positive := 1;
- Char : Character;
- Char_Count : Integer := 0;
- BEGIN
- LOOP
- -- Get the input line
- Get_Line (Input_Line, Last => Input_Length);
- -- An empty line ends the program
- EXIT WHEN Input_Length = 0;
- Word_Count := 0;
- -- TODO: Go through the input line and locate the words. If
- -- Show_Words is True, output each word. If Show_Words is False,
- -- increment Word_Count.
- -- IF Char = ' ' THEN
- -- IF Char /= ' ' THEN
- -- IF Char = ' ' THEN
- -- Word := True;
- -- END IF;
- -- END IF;
- -- END IF;
- FOR N IN Input_Line'First..Input_Line_Max LOOP
- IF Char = ' ' THEN
- Char_Count := Char_Count + 1;
- Put(Char_Count, 0);
- END IF;
- IF Char /= ' ' THEN
- Word_Start := I;
- Char_Count := Char_Count + 1;
- --Put(Char_Count, 0);
- -- IF Char = ' ' THEN
- -- Word_End := Char_Count - 1;
- -- Word_Count := Word_Count + 1;
- -- I := Char_Count;
- -- END IF;
- END IF;
- END LOOP;
- -- IF SHOW_WORDS IS TRUE. Reached the end-of-line, do the right thing.
- IF Show_Words THEN
- Put_Line(Input_Line(Word_Start..10));
- -- Output a blank line after all the words have been output.
- New_Line;
- ELSE
- -- IF SHOW_WORDS IS FALSE. Output the number of words
- Put("Poop");
- Word_Count := Word_Count + 1;
- Put(Word_Count, Width => 0);
- New_Line;
- END IF;
- END LOOP;
- END Process_Input;
- -- Close_Files will close the input and output files if necessary.
- PROCEDURE Close_Files IS
- BEGIN
- IF Is_Open (Input_File) THEN
- Close (Input_File);
- END IF;
- IF Is_Open (Output_File) THEN
- Close (Output_File);
- END IF;
- END Close_Files;
- BEGIN -- show_words
- IF Handle_Arguments THEN
- Put_Line("Whoo!");
- Process_Input;
- Put("Whoo!");
- Close_Files;
- ELSE
- Print_Usage;
- Put("dang");
- END IF;
- END Show_Words;
Add Comment
Please, Sign In to add comment