CS253: Software Development with C++

Spring 2020

HW 2

CS253 HW2: Command-Line Arguments!                

Description                

For this assignment, you will write a calculator program that that takes command-line arguments, performs simple arithmetic, and produces the result in the base desired.                 

Arguments                

The first, optional argument is one of -h, -o, or -d, signifying an output base of hexadecimal, octal, or decimal. The default is decimal.                 

After that is a mandatory operator, one of:

After that is a mandatory sequence of at least one integer, of the form:

No more arguments should follow.                 

Operation                

The operation is applied to all the numbers, left-to-right. For example, ./hw2 + 10 20 30 will calculate the result of 10+20+30, or 60. ./hw2 / 1000 5 2 25 will calculate the result of 1000/5/2/25, or 4. The result is displayed in the base indicated by the optional first argument.                 

Quoting                

Unfortunately, the * character is special to bash, and will be replaced by a list of all the files in your current directory. To avoid this, type \* or '*' instead.                 

Distance from 45                

The @ operator returns one of its operands, namely, the operand that is furthest from 45. If the operands are equally distant from 45, the smaller number is returned.                 

Sample Runs                

Here are sample runs, where % is my prompt.

% ./hw2 + 2 2
4
% ./hw2 -d + 1 2 3 4 5
15
% ./hw2 -d - 1 2 3 4 5
-13
% ./hw2 -h - 1 17
-10
% ./hw2 -h - 100 0b101 0x19 017
37
% ./hw2 -o / 13 007
1
% ./hw2 -o / 7 13
0
% ./hw2 '*' 2 123 1
246
% ./hw2 @ 30 70 29
70
% ./hw2 - 0b1001
9
% ./hw2 -h + 64
40
% ./hw2 -h / 64206
face

Output in other bases:                

Observe:

cout        << 20 << ' ' << 30 << '\n';
cout << hex << 20 << ' ' << 30 << '\n';
cout << oct << 20 << ' ' << 30 << '\n';
cout << dec << 20 << ' ' << 30 << '\n';
20 30
14 1e
24 36
20 30

Debugging                

If you encounter “STACK FRAME LINK OVERFLOW”, then try this:

    export STACK_FRAME_LINK_OVERRIDE=ffff-ad921d60486366258809553a3db49a4a

Requirements                

If you have any questions about the requirements, ask. In the real world, your programming tasks will almost always be vague and incompletely specified. Same here.                 

Tar file                

    cmake . && make

How to submit your work:                

Use web checkin, or Linux checkin:                 

    ~cs253/bin/checkin HW2 hw2.tar

How to receive negative points:                

Turn in someone else’s work.