Go backward to 6.7 Decision Trees and Modulus Arithmetic
Go up to 6 Labs
Go forward to 6.9 Floating Point Precision
6.8 Streams
Looking through a stream of characters for a specific
pattern or sequence is frequently done by computers.
For an example, think
about what a compiler does with a program. It needs to look
for comments, operators, identifiers, and constants.
In this lab I want you to do a bit of text processing yourself.
(Not quite a compiler for sure.) You
can use your standard input, or if you wish to read ahead
in your book and
process the input from a file. Both approaches are fine.
Extra credit will be given if you take the "UNIX" approach
of looking on the command line for a file name, and if it exists
use it, otherwise use the standard input (keyboard or
redirected input).
Write a program that process an input stream doing the
following:
- Ignore all input on a line after the semicolon.
The ";" will be used to mark off comments.
- Counting the input lines. Including those that only have comments
- Looking for and converting binary numbers. Keeping the last
one for each line. Summing all of the last binary numbers on each
line.
- Counting letters (a-z and A-Z) on each line. Summing the
total number of letters.
- Counting digits (0-9) on each line. Summing the
total number of digits.
After each line read it prints out the following information:
- Whether or not binary number was found.
- If a binary number was found,
the decimal value of the last one.
- The number of letters.
- The number of digits.
At the end of the file print out the following totals:
- The total number of lines.
- The total of all of the last binary numbers.
- The total number of letters.
- The total number of digits.
Given the following example input.
This has a comment; And the comment is discarded. 101010010010
A hidden binary number 9991999; Of 1
1010109AAAA10; The last binary number is decimal 2
This output should be in the following format:
No Binary Number 15 Letters 0 Digits
1 19 Letters 7 Digits
2 4 Letters 9 Digits
3 Lines --------------------------------------------
3 38 Letters 16 Digits
Test your program with the following sample data set.
It is available by anonymous ftp at
ftp://red.cscwc.pima.edu/pub/csc130/stream.dat.
; This is the test date for the lab "Stream"
; Louis Taber 3/29/98
; CSC130 Pima Community College
; Blank Line
ABCDEFGHIJKLMNOPQRSTUVWXYZ ; Uppercase letters
abcdefghijklmnopqrstuvwxyz ; Lower case letters
1234567890 ; Digits - binary number: 0
11111111 ; Binary number 255
1000000000000000 ; This may give you
; a negative number (OK)
; Do you know why?
10101010a10101010b111 ; Last binary number: 7
[]|--+=*#@ ; Some special symbols
The last Line
Turn in a copy of your program and its output when used with the above test data marked with:
- Your name
- TABER CSC130
- Lab 6.8: Streams
- The compiler and OS that you used.
Place the lab in the instructor hand in box
in BUS R6E, the "terminal room".
Instructor: ltaber@pima.edu** Red's Home page** The Pima College Site