Computer programming - Lab 4

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

Text processing

1. Fixed-width printing Write a function that takes an unsigned n as parameter, and prints a text read until end of input, fitting n characters per line. Words (sequences of non-whitespace chars) are printed with one space in between. A word that would exceed the line limit will be split inserting a hyphen - as last character on the line (no extra hyphen is needed if the last character that fits is a hyphen itself). Newlines in the original text are observed.
Hint: to handle the end of the line, write a function peek() which returns the next character without consuming it (use ungetc()).

2. Prettyprinting Write a program that properly indents a text read from standard input that has balanced braces { } . The output is formed as follows:

Balanced strings

3. Palindrome Write a function that checks if a string read from input has the following structure: it has exactly one period '.' character, which is in the middle, and is symmetric with respect to the middle (the first character is equal to the last, etc.).

4 Mirrored case Write a program that reads a string and checks that uppercase and lowercase letters are mirrored with respect to the center: the first half is uppercase, the second half lowercase, with the first character matching the last, etc.

5. HTML header tags HTML contains headers of the form <hN>header text</hN> where N is a digit from 1 to 6. Upper or lowercase h are considered equivalent.
Write a program that reads input until EOF and checks that any header tags are correctly formed: a) start and end tags are correctly paired; b) in addition, there are no "gaps" in the level sequence, i.e. tag h4 cannot appear directly following an h2 without an intervening h3.

Expression evaluation

6. 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).

7. 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, or two expressions followed by an operator + - * / and separated by any whitespace.
Example: 7 4 2 + - 5 - means 7 - (4 + 2) - 5
An expression is: Expr ::= Number RestExp where RestExp ::= empty | Expr operator RestExp (where the operator applies to the preceding two expressions)

8. 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).

9. S-expressions An s-expression is either a word formed of letters, or a sequence of zero or more s-expressions, separated by whitespace, between parantheses: (exp1 exp2 ... expn). (They appear in functional languages such as LISP). Extra whitespace before or after parantheses is allowed.
Write a program that reads an s-expression from input, or reports and stops after the first error.
(Write a function that returns 1 (valid) or 0 (invalid). Simpler but less useful: exit program on error).

10. Stacked expressions A stacked expression is either an unsigned number or an expression of the form (number op stackedexpr) where op may be + or *.
Example: (4 * (2 + (3 * 5))) . Whitespace before/inside/after is optional.
Write a function that reads a stacked expression and returns its value.
Report and stop on error: return -1 (preferably) or exit program.

11. Continued fractions A continued fraction has the form 1/(a1+1/(a2+1/(a3+1/.../an))) where ai are positive integers.
Write a function that reads a continued fraction from input and returns its value as floating-point number.


Marius Minea
Last modified: Tue Nov 29 21:50:00 EET 2016