CS 3113: Project 0

Goals

The goals of this project are to:

Proper Academic Conduct

The code solution to this project must be done individually. Do not copy solutions to this problem from the network and do not look at or copy solutions from others. However, you may use the net for inspiration and you may discuss your general approach with others.

Introduction

You will write a program that takes as input the textual representation of a number and produces a numeric representation in one of several formats (decimal, hexadecimal or octal).

Example input:

one thousand twenty three
Corresponding output (decimal format):
1023
Corresponding output (hexadecimal format):
3ff

Specification

Program Invocation

The command-line interface to your program is as follows:
./project0 [d|h|H|o]

Program Behavior

Your program will accept lines of input until an end-of-file (EOF) is reached, at which point it will terminate.

Possible string inputs adhere to a specific grammar:

DIGIT = one | two | ... | nine
TEENS = ten | eleven | ... | nineteen
TENS = twenty | thirty |... | ninety
DOUBLE = DIGIT | TEENS | TENS [DIGIT]
TRIPLE = DOUBLE | DIGIT hundred [DOUBLE]
INPUT = TRIPLE | TRIPLE thousand [TRIPLE] | TRIPLE million [TRIPLE thousand] [TRIPLE] | zero

where:

Specifically:

Notes:

Examples

Program Invocation
./project0

Input Text

one hundred ninety two
eighteen
twenty three thousand four hundred
one hundred thousand one
Output Text
192
18
23400
100001


Program Invocation

./project0 H

Input Text

one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
twelve million eighteen
fourteen thousand
one hundred one
eleven
one
zero
Output Text
75BCD15
B71B12
36B0
65
B
1
0


Program Invocation

The following example invocations must result in an error message being printed and the program terminating:

./project0 a

./project0 h o

./project0 h o H


Hints

Misc

Parsing Lines

There are lots of ways to parse a string into the individual tokens (tokens are symbols that are separated by white space). Here, we will use the string tokenizer provided in the string library in C.

The local data structures that you need are:

const char SEPARATORS[] = " \t\n";         // Possible separators between tokens

char in_buffer[INBUFSIZE];                 // Input buffer from STDIN
char * args[MAX_ARGS];                     // Array of pointers to strings
char ** arg;                               // Working pointer that steps through the args (a pointer to a pointer to a string)
After receiving an input line into in_buffer (e.g., using fgets()), you can split the characters in the buffer into distinct tokens as follows:

    arg = args;
    *arg++ = strtok(in_buffer,SEPARATORS);   // tokenize input
    while ((*arg++ = strtok(NULL,SEPARATORS)));
Here, SEPARATORS is a string that contains all of the characters that are valid indicators for splitting the input string up. At the end, of the loop, arg will be an array of strings (technically, it is an array of pointers to characters). The arg in the array after the last argument will be set to NULL.

Arrays of Strings

In C, you can declare an array of strings as follows:
const char * table[] = {"alpha", "bravo", "charlie", "delta", "echo", "END"};

Then, you can write code that uses this table. For example, the following will print out all elements of the table and the lengths of each string (except the last one that is being used as a marker):

for(int i = 0; strcmp(table[i], "END"); ++i) {
    printf("%d: %s (%d)\n", i, table[i], strlen(table[i]);
}


Submitting Your Program


Grading Criteria


Testing

You are responsible for generating your own test cases.

We have provided a couple of samples to get you started, but keep in mind that we have other tests that are hidden from you.


Addenda


andrewhfagg at gmail.com

Last modified: Tue Sep 8 14:33:04 2020