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");
    }
}