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 either in 8 (starts with 0), base 16 (starts with 0x or 0X) or base 10 (default).

3. 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.

4. 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.

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.

Character processing

6. C comments Write a program that prints its input with C-style comments filtered out. (both multiline and single-line comment syntax).

7. Words in line Write a program that counts the words in one input line. A word is a sequence of characters which are not whitespace (isspace()).

8. Word count The Unix program wc counts the number of characters, words and lines in the input (or in any file). A word is a sequence of non-whitespace characters (i.e., isspace() returns false).
Write a program that in addition to printing total chars, words and lines in the input:
a) prints for any line the number of words in that line
b) prints the number of lines in each paragraph. Paragraphs are separated by at least one empty line (only the newline character).

9. LaTeX commands. In LaTeX, a command consists of a backslash, an identifier (letters only), and an optional sequence of arguments placed within braces {}, e.g. \setcounter{page}{3} . Write a program that filters out any LaTeX commands in standard input, printing only the rest of the text.
A slightly simpler version (without the initial identifier) is solved in lecture 3.

10. Assignment statements. Write a program that checks if each input line has the form
identifier = identifier ;
with optional whitespace anywhere. If there is a format mismatch, terminate the program with an error message. An identifier is a sequence of letters.

11. SVG path length. An SVG path consists of the following commands: L or l (draws a line from the current point to the indicated coordinates), M or m (moves the current point), each followed by two real numbers, representing x and y coordinates (absolute for uppercase commands, relative for lowercase). Commands are separated by whitespace. Write a program that reads a sequence of commands and computes the total length of the path drawn. Initial coordinates are assumed (0, 0).
Example: l 1.4142135 1.4142135 M 1 1 L 2 1 draws a line at 45 degrees of length 2 (within 7 digits precision), then moves the cursor to (1, 1), then draws a line to (2, 1) (of length 1), thus the program should print 3 (using default precision).
You may use the following function to read a number:

double readf(void)
{
  double x = 0;
  scanf("%lf", &x);
  return x;
}

Marius Minea
Last modified: Tue Nov 29 21:45:00 EEST 2016