Computer programming - Lab 3

If any questions remain about these problems, please ask during office hours.

Processing numbers

1. Hex printing Write a function that prints a number, digit by digit, in base 16.

2. Read a C integer Write a function that returns an unsigned number read from input, given either in base 8 (starts with 0), base 16 (starts with 0x or 0X) or base 10 (default).

3. Read a real number Write a function that reads (digit by digit) and returns the value of a real number which only has a fractional part (starting with the dot until no more digit is found). You need to write a recurrence relation that describes how the next digit is added to the already existing part.
Next, combine this function with readnat (or a similar function) and produce a function that reads and returns a complete real number, with integer and fractional part.

4. Thousands separators Write a function that returns a number read from input with mandatory comma as a thousands separator. That is, reading 1,234,567 should return the number 1234567.
Hint: while reading digits to build the number as usual, keep a count of digits in the current group. The first group may have 1, 2, or 3 digits. A comma must be followed by exactly 3 digits, otherwise the format is wrong.

5. Prime factors Write a function that decomposes a number into prime factors, and prints out the result, in the form: 18=2*3^2. Try both a recursive and an iterative solution.
Warning, ^ does *not* denote exponentiation in C.
Hint: keep as helper the last prime factor tried, so you can continue with a larger (odd) number.

Recursive expressions

Solve these problems in a similar way to the prefix expression example done in class. Decide based on the current character read whether the base case or the recursive case applies.

6. S-expressions Define an expression recursively as either an unsigned number, or a paranthesized expression of the form (op expr expr ... expr) where op is either + or * and is applied to one or more whitespace-separated expressions, everything enclosed in parantheses. Write a function that reads an expression and returns its value.
(Such expressions are called s-expressions and originate in LISP).
Hint: decide based on the first chracter (digit or open paranthesis) whether to follow the base case or recursive case. For the sequence of expressions, stop recursion or iteration when encountering the closing paranthesis.

7. Stacked fractions A stacked fraction has the form number1/number2 or number1/(number2+stackedfraction). Numbers are unsigned. Write a function that reads such a fraction and returns its value as floating-point number.
First assume there are no spaces. Then try to allow arbitrary whitespace within the expression.

8. Polynomial A polynomial in x is written as a0+x*(a1+x*(...+an))) . Write a function that takes as parameter a real value for x, reads a polynomial from input and returns its value. First, assume coefficients are unsigned integers. Optionally, extend your code to allow signed integers (with -) and spaces anywhere.
Hint: First, write on paper some polynomials of degree 0, 1, 2 to establish the recursive structure for the polynomial, and thus for the function you need to write.
Handle errors by printing out one error message when the input does not match the required format. If you return the value NAN (not a number, a value of floating point type from math.h) this will propagate in all computations and your top-level function will also return NAN.

Marius Minea
Last modified: Thu Oct 12 7:40:00 EEST 2017