Computer programming - Lab 3

Processing numbers

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

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

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

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

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

6. XML tags. An XML file contains tags delimited by < and > . A start tag starts with < . An end tag starts with </ . Both end with > . An empty-element tag starts with < and ends with /> . Inside these delimiters, a tag may contain first a name, and then a whitespace-separated list of attributes, which are pairs of the form name=value, for example <img src="file.jpg" alt='some text'/>.
a) Write a program that reads an XML-structured input and prints out all tag names and attribute names, one line per tag (example: img src alt).
b) (optional). Check that start tags and end tags are correctly nested (with matching names). To avoid working with strings, you can hash strings to integers; use for example a function that updates hash = 33*hash + c for each character in the string.

Expression evaluation

7. Prefix expressions. Write a program that computes and prints the value of an expression in prefix form, read from standard input. An expression is a natural number, or an operator + - * / followed by two expressions preceded by any whitespace.
Example: - 2 + - 7 4 3 means 2 - (7 - 4 + 3).

8. Postfix expressions. Write a program that computes and prints the value of an expression in postfix form, read from standard input. An expression is a natural number, two expressions followed by an operator + - * / and separated by any whitespace.
Example: 7 4 2 + - 5 - means 7 - (4 + 2) - 5
Hint: Option 1: use a stack. Option 2: Equivalently, an expression is: Expr ::= Number RestExp and RestExp ::= empty | Expr operator RestExp (where the operator applies to the preceding two expressions)

9. From prefix to postfix. Write a program that reads an expression in prefix form (as defined in problem 7) and writes it out in postfix form (as defined in problem 8).

10. From infix to postfix. Adapt the expression calculator done in class to read an expression from input and print it in postfix form.
Instead of returning values, each of the functions will now print. No parantheses are needed in postfix form.

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). 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 Oct 7 23:45:00 EEST 2013