CS253: Software Development with C++

Spring 2018

HW 2

CS253 HW2: More datatypes, tagging                

Description                

Write a C++ program called hw2 that behaves as the program from from HW1, except that it:

Tagged Output                

Now, instead of simply producing vnums, the program will produce tagged output. Each vnum will be preceded by an identifying character:

The program must figure out which category (short/int/long) a number should go into.                 

For example, the number three, since it fits into a short, would be tagged with an s, thus: 73 03                 

The number one billion, which fits into an int, would be tagged with an i, thus: 69 40 3b 9a ca 00                 

The serialized output for a character is simply a c followed by the character. For example, 'x' would be serialized as: 63 78                 

A string is serialized as follows:

For example, "Jack" would be serialized as: 53 04 4a 61 63 6b
Don’t be fooled by 04. That is not a simple byte containing the value 4. That is a vnum that happens to be a single byte, because 4 (the length of “Jack”) fits into the bottom four bits of a vnum. A longer string might have a multibyte vnum representing its length.                 

Sample Run                

Here is a sample run. “%” is my prompt:                 

    % cat sample-input 
    5 300 -1

    1
    2 -3 40 500 -6000 70000
    1000000000

    'a'
    'b' ' '
    "Jack Applin"
    % ./hw2 sample-input 
    73 05
    73 11 2c
    73 0f
    73 01
    73 02
    73 0d
    73 10 28
    73 11 f4
    73 2f e8 90
    69 21 11 70
    69 40 3b 9a ca 00
    63 61
    63 62
    63 20
    53 10 0b 4a 61 63 6b 20 41 70 70 6c 69 6e
    % 

char→int conversion                

// Here’s how to convert a char to an int:
int n = 'S';        // It’s just that easy.
cout << hex << n;
53

or via the shell:

% echo -n silcS | xxd
0000000: 7369 6c63 53                             silcS

or going from hex to characters:

% echo -e '\u4a\u61\u63\u6b'
Jack

Requirements                

The requirements are the same as those for HW1, plus:                 

Ask                

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                

How to submit your homework:                

    ~cs253/bin/checkin HW2 hw2.tar

How to receive negative points:                

Turn in someone else’s work.                 

User: Guest                 

Check: HTML CSS
Edit History Source

Modified: 2018-04-02T17:06                 

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building