CS253 HW5: Options!                
Changes                
Updated CMakeLists.txt
to add *.h
to hw5.tar
,
because my solution has a *.h
files. Yours may or may not.
                
Description                
For this assignment, you will improve upon your previous work in
HW3, adding command-line options. This is a complete program,
not a library.
                
Arguments                
The first command-line arguments should be options:
                
-
-r
integer-
integer -
Specify the acceptable input range of numbers.
If this option is not given, assume 0–99, as in HW1.
-
-f
-
-
-s
-
If
-f
is given, display the first line of output
(the one with the ‘x
’ characters).
If -s
is given, display the second line of output
(the one without the ‘x
’ characters).
If neither -f
nor -s
is given, act as if both were given.
-
-c
string -
When writing the second line, use string instead of a comma.
-
-v
-
Announce each filename to standard output, just before reading
the integers inside it. Before reading standard input,
announce that.
This is the Colorado State University CS253 web page
https://cs.colostate.edu/~cs253/Fall21/HW5
fetched by unknown <unknown> with Linux UID 65535
at 2024-11-21T18:20:11 from IP address 3.138.32.53.
Registered CSU students are permitted to copy this web page for personal
use, but it is forbidden to repost the information from this web page to the
internet. Doing so is a violation of the rules in the CS253 syllabus,
will be considered cheating, and will get you an F in CS253.
Sample Runs                
Here are sample runs, where %
is my prompt.
                
% cat CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
project(hw5)
# Are we in the wrong directory?
if (CMAKE_SOURCE_DIR MATCHES "[Hh][Ww]([0-9])$"
AND NOT PROJECT_NAME MATCHES "${CMAKE_MATCH_1}$")
message(FATAL_ERROR "Building ${PROJECT_NAME} in ${CMAKE_SOURCE_DIR}")
endif()
# Using -Wall is required:
add_compile_options(-Wall)
# These compile flags are highly recommended, but not required:
add_compile_options(-Wextra -Wpedantic)
# Optional super-strict mode:
add_compile_options(-fmessage-length=80 -fno-diagnostics-show-option
-fstack-protector-all -g -O3 -std=c++17 -Walloc-zero -Walloca
-Wctor-dtor-privacy -Wduplicated-cond -Wduplicated-branches
-Werror -Wextra-semi -Wfatal-errors -Winit-self -Wlogical-op
-Wold-style-cast -Wshadow -Wunused-const-variable=1
-Wzero-as-null-pointer-constant)
# add_compile_options must be BEFORE add_executable.
# Create the executable from the source file main.cc:
# No, you don’t have to have a separate CountSort.cc file.
# Whatever .cc files you have that need to be compiled must be listed here:
add_executable(${PROJECT_NAME} main.cc CountSort.cc)
# Create a tar file every time:
add_custom_target(${PROJECT_NAME}.tar ALL COMMAND
tar -cf ${PROJECT_NAME}.tar *.cc *.h CMakeLists.txt)
% cmake . && make
… cmake output appears here …
Scanning dependencies of target hw5
… make output appears here …
% head data data2
==> data <==
101 103 109
105 103 103
==> data2 <==
199 100 199
% ./hw5 -r 100-109 <data
101x1 103x3 105x1 109x1
101,103,103,103,105,109
% ./hw5 -sc ' & ' -r 100-400 data data2
100 & 101 & 103 & 103 & 103 & 105 & 109 & 199 & 199
% ./hw5 -vfc/ -sr 100-200 <data data2 data2
Reading from data2
Reading from data2
100x2 199x4
100/100/199/199/199/199
% ./hw5 -fr1000000-1000999 ~cs253/pub/million
1000001x500000 1000987x500000
Debugging                
If you encounter “STACK FRAME LINK OVERFLOW”, then try this:
export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a
Hints                
- Use getopt(). Seriously, use it. Don’t do this yourself. You will
have to actually read the manual page for getopt(). This is a
skill that you must acquire to be a good programmer.
- Don’t take any significant action when an option (e.g.,
-f
)
is encountered. Instead, just set a boolean flag that indicates
action to be taken later. Options don’t usually do things;
instead, they modify how things will be done later.
- The argument to
-c
is not necessarily a single character.
Requirements                
All requirements from HW3 still apply, with these additions:
- The first two arguments are no longer the range of acceptable integers.
That’s the purpose of
-r
.
- Produce an error message and stop the program if:
- a bad option or bad option argument is given
(the message must contain the bad option and argument, if present)
-r
or -c
are given more than once
-c
is given without an explicit or implicit -s
.
- the second number given to
-r
is less than the first number.
- A space after an option taking an argument (
-r
or -c
)
is optional. -r1-09
and -r 1-09
are both valid.
- Options must precede filenames. Option processing must stop at the
first argument that isn’t an option (or an argument to an option,
e.g.,
-c +
). This command must attempt to treat -v
as
a file, which will probably fail:
./hw5 infile -v
- Option bundling:
./hw5 -fs
is equivalent to ./hw5 -f -s
./hw5 -sfvr091-65535
is equivalent to
./hw5 -s -f -v -r 091-65535
./hw5 -f-s
is invalid.
- The next non-space character after an option that requires an
argument must be the argument itself, not another option flag.
./hw5 -cs X
will treat s
as the argument to -c
,
and will then regard X
as a filename, which will probably fail.
- “This is too hard!”
I already told you to use getopt(). It does a lot of this for you.
“But I don’t understand the man page!”
Excellent—you have found the heart of the assignment.
Learning has begun.
Tar file                
- The tar file for this assignment must be called:
hw5.tar
- It must contain:
- source files (
*.cc
) required to build
- any header files (
*.h
) required to build
CMakeLists.txt
- This command must produce the program
hw5
(note the dot):
cmake . && make
- At least
-Wall
must be used every time g++ runs.
How to submit your work:                
In Canvas, check in the
file
hw5.tar
to the assignment “HW5”.
It’s due 10:00:00ᴘᴍ MT Saturday, with a 24-hour late period for a 25% penalty.
                
How to receive negative points:                
Turn in someone else’s work.