CPW (C Programming Workout) Part 1: Reading an Integer

Quiz 12: Reading an Unsigned Integer in Decimal, Octal or Hexadecimal Notation

Write a C program get_uint.c that reads an integer in either decimal, octal or hexadecimal notation:

  • In the octal notation the representation has a leading digit and is followed by zero or more octal digits, i.e. , ..., 7.

  • In the decimal notation the representation begins with a digit form 1 to 9 and is followed by zero or more decimal digit, i.e. , ..., 9.

  • In the hexadecimal notation the representation begins with the prefix 0x and is followed by one ore more hexadecimal digits, i.e. , ..., 9, a, ..., f, A, ..., F.

Leading spaces should be ignored. If no integer representation is detected the program is supposed to print no unsigned integer\n, and otherwise the numerical value in decimal notation followed by a newline.

For example:

  • The input 123a\n should print 123\n.

  • The input 0xfF\n should print 255\n.

  • The input 0123\n should print 83\n.

  • The input abc\n should print no unsigned integer\n.

The program should not produce any different output. Your code is required to have a consistent indentation. So for example code like this

1
2
3
4
5
6
7
int
foo(int a) {
    if (a > 42) {
    return 1;
    }
    return 0;
}

is not accepted. And it will not be graded. The rule is simple:

  • A open curly parenthesis (i.e. the '{' punctuator) increases the indent level.

  • A closed curly parenthesis (i.e. the '}' punctuator) decreases the indent level.

You are free to choose your preferred indent level. You can use spaces and tabs for indentation, also mixed, but the tab stop is expected to be 8.

The test for an acceptable coding style is lax for this quiz. But it will be enforced in upcoming quizzes. For example, the limit on the length of lines is 80 columns.

Submit your program with submit hpc quiz12 get_uint.c

Finite State Machine

The C Programm Developed in the Video

Within the two comments TODO: Begin of your Code and TODO: End of your a return statement was added so that this skeleton for the quiz can be compiled in this state without warnings. Of course you should replace this TODO block completely with your implementation. Additional Note: You also have to change the in main() the code for output.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdbool.h>
#include <stdio.h>

int ch;

int
nextCh(void)
{
    ch = getchar();
    return ch;
}

bool
isSpace(int ch)
{
    return ch == ' ';
}

bool
isDecDigit(int ch)
{
    return ch >= '0' && ch <= '9';
}

bool
isOctDigit(int ch)
{
    return ch >= '0' && ch <= '7';
}

bool
getUint(unsigned long long *res)
{
    unsigned long long val = 0;

    // state S
    while (ch == 0 || ch == ' ') {
        nextCh();
    }
    if (ch == '0') {
        nextCh();
        if (ch == 'x') {
            nextCh();
            // state HT, H
            /*
             * TODO: Begin of your Code
             */

            return false; 

            /*
             * TODO: End of your Code
             */
        }
        // state OT or O
        while (isOctDigit(ch)) {
            ch -= '0';
            val = val * 8 + ch;
            nextCh();
        }
        *res = val;
        return true;
    } else if (isDecDigit(ch)) {
        // state D
        while (isDecDigit(ch)) {
            ch -= '0';
            val = val * 10 + ch;
            nextCh();
        }
        *res = val;
        return true;
    } else {
        return false;
    }
}

int
main()
{
    unsigned long long val = 0;

    printf("Type some unsigned integer in decimal, octal or hex: ");
    if (getUint(&val)) {
        printf("Integer in decimal representation: %llu\n", val);
    } else {
        printf("That is not an unsigned integer in decimal!\n");
    }
}