====================================================== CPW (C Programming Workout) Part 1: Reading an Integer ====================================================== ---- VIDEO ------------------------------ https://www.youtube.com/embed/hA-KZdlXteA ----------------------------------------- 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 `0` and is followed by zero or more octal digits, i.e. `0`, ..., `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. `0`, ..., `9`. - In the hexadecimal notation the representation begins with the prefix `0x` and is followed by one ore more hexadecimal digits, i.e. `0`, ..., `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 ---- CODE (type=c) --------------------- 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 ==================== ---- TIKZ ---------------------------------------------------------------------- \begin{tikzpicture}[ shorten >=1pt, node distance=5cm, auto, initial text=Start ] \node[state, initial] (S) {$S$}; \node[state, accepting, above right of=S, fill=green!20] (D) {$D$}; \node[state, accepting, right of=S, fill=green!20] (OT) {$O_t$}; \node[state, accepting, above right of=OT, fill=green!20] (O) {$O$}; \node[state, below right of=OT] (HT) {$H_t$}; \node[state, accepting, above right of=HT, fill=green!20] (H) {$H$}; \node[state, accepting, below right of=S,fill=red!20] (E1) {$E_1$}; \node[state, accepting, right of=HT,fill=red!80] (E2) {$E_2$}; \draw (S) edge[loop above] node{0, '\textvisiblespace'} (S) (S) edge[above,->] node [right] {'1',\dots,'9'} (D) (S) edge[above,->] node [right] {else} (E1) (D) edge[loop above] node{'0',\dots,'9'} (D) (S) edge[above,->] node {'0'} (OT) (OT) edge[above,->] node{'0',\dots,'7'} (O) (OT) edge[above,->] node{'x'} (HT) (HT) edge[above,->] node{else} (E2) (HT) edge[above,->] [above] node{'0',\dots,'9','a',\dots,'f', 'A',\dots,'F'} (H) (H) edge[loop above] node{'0',\dots,'9','a',\dots,'f', 'A',\dots,'F'} (H) (O) edge[loop above] node{'0',\dots,'7'} (O); \end{tikzpicture} -------------------------------------------------------------------------------- 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. ---- CODE (file=session12/get_uint.c, fold) ------------------------------------ #include #include 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"); } } --------------------------------------------------------------------------------