Final task

ICP'96 - INVEX Brno 1996

Table formatter

Write a program which will be used to format tables. Name the program xxtab (where xx is your starting number). The program will have following parameters:

        xxtab [-n] table_description formatted_table
 
-n
is an optional parameter which controls length of the line used for formatting. Line length n is a positive integer number. Default value, if the parameter is not specified, is 80.
table_description
is name of text file which contains description of the table. Description language is described below.
formatted_table
is name of text file where the formatted table should be written.

Description of the table format

Each text file contains definition for only one table. Table consists of individual cells which are written line by line. Every that line is started with tag <TR>.

Each line can contain definitions of individual cells. These definitions are started by tag <TH> or <TD>. First (<TH>) have one additional functionality -- it centers contents of cells in both horizontal and vertical direction.

Characters '+', '|' a '-' will be used for drawing of borders when formatting a table. There must be at least one space between the vertical border and text in the cells.

Any number of immediately following spaces, tabulators and end of lines in the input file will be interpreted as a single space. Spaces surrounding format tags are ignored.

Special way must be used for writing characters '<', '>' and '&':

Character Transcription
< &lt;
> &gt;
& &amp;

Let's look at a small example for better understanding:

Table description Formatted table
<TR><TH>Year<TH>Income per   head
<TR><TD>1987<TD>3
<TR><TD>1988<TD>3.5
<TR><TD>1989<TD>3.2
+------------------------+
| Year | Income per head |
|------+-----------------|
| 1987 | 3               |
|------+-----------------|
| 1988 | 3.5             |
|------+-----------------|
| 1989 | 3.2             |
+------------------------+

Note: Notice that several spaces between words "per" and "head" were substituted by a single space.


Tasks

Following tasks must be solved in the order in which they are presented. In case you do not solve task with smaller number one with higher number will not be evaluated.

  1. Write a program which will be able to format tables according to the previous stated rules. Text in cell will be aligned only into single line (this means without word-wrapping longer text into several lines). In case that table can not fit into the specified area, message 'Error: Table is too long to fit line.' will be written on the screen.
  2. Improve your program in the way that optional attribute ALIGN which controls aligning of cell contents can be used with tags <TD> and <TH>. Value of the attribute may be one of following:
    left aligning to the left
    right aligning to the right
    center centering

    Example:

    Table description Formatted table
    
    <TR><TH>foo<TH ALIGN=RIGHT>bar
    <TR><TD ALIGN=CENTER>fooa<TD ALIGN=LEFT>bara
    <TR><TD>foob<TD>barb
    <TR><TD>abrakadabra<TD ALIGN=LEFT>zig-zag
      
    
    +-----------------------+
    |     foo     |     bar |
    |-------------+---------|
    |     fooa    | bara    |
    |-------------+---------|
    | foob        | barb    |
    |-------------+---------|
    | abrakadabra | zig-zag |
    +-----------------------+
      
  3. Improve your program in the way that optional attributes COLSPAN and ROWSPAN can be used with tags <TD> and <TH>. A positive integer can be assigned to this attributes, that controls how many cells will be joined in to one cell in horizontal or vertical direction.

    Example:

    Table description Formatted table
    
    <TR><TH ROWSPAN=2>Year<TH COLSPAN=2>Sales
    <TR>                  <TH>Product A<TH>Product B
    <TR><TD>1994          <TD>4 600    <TD>2 100
    <TR><TD>1995          <TD>4 900    <TD>1 900
    <TR><TD>1996          <TD>5 750    <TD>3 160
        
    
    +------------------------------+
    |      |         Sales         |
    | Year |-----------------------|
    |      | Product A | Product B |
    |------+-----------+-----------|
    | 1994 | 4 600     | 2 100     |
    |------+-----------+-----------|
    | 1995 | 4 900     | 1 900     |
    |------+-----------+-----------|
    | 1996 | 5 750     | 3 160     |
    +------------------------------+
      

    Note: Do not forget, that tag <TH> is used for specifying a cell that is centered also in vertical direction.

  4. Extend your program in the way that it will be able to wrap contents of cells into several lines in order to fit wide tables into specified area. Width of particular columns will the program select automatically so that rates between amount of text in cell and area of the cell will be even.

    A line can be broken at the place of space character between words. In case that there is no suitable space, breaking can occur at the place of character '-'. The error message will be shown onto screen in case that there is no way to break text in cells into several lines to fit the table into desired width.

    Example:

    Table description
    <TR><TH>Name<TH>Symbol<TH>Definition
    <TR><TD>meter<TD>m    <TD>The meter is the unit of length;
                              it is equal to distance which light
                              overcome in 1/299 792 458 seconds in
                              vacuum.
    <TR><TD>kilogram<TD>kg<TD>The kilogram is the unit of mass;
                              it is equal to the mass of the
                              international prototype of kilogram (a
                              cylinder of platinum-iridium).
    
    Formatted table
              1         2         3         4         5         6         7
    01234567890123456789012345678901234567890123456789012345678901234567890123456789
    
    +------------------------------------------------------------------------------+
    |   Name   | Symbol |                       Definition                         |
    +----------+--------+----------------------------------------------------------+
    | meter    | m      | The meter is the unit of length; it is equal to          |
    |          |        | distance which light overcome in 1/299 792 458 seconds   |
    |          |        | in vacuum.                                               |
    +----------+--------+----------------------------------------------------------+
    | kilogram | kg     | The kilogram is the unit of mass; it is equal to the     |
    |          |        | mass of the international prototype of kilogram (a       |
    |          |        | cylinder of platinum-iridium).                           |
    +------------------------------------------------------------------------------+
      

    Note: Ruler above the table is printed just for your better orientation and for emphasizing that the table does not spread beyond the specified length of line (default 80 characters). Ruler is not part of a formatted table.

  5. Extend your program in the way that an optional attribute VALIGN which controls vertical alignment of cell contents can be used with tags <TD> and <TH>. It can be one of following values:
    top aligning to top
    bottom aligning to bottom
    middle center in vertical direction

    Example:

    Table description
    <TR><TH>Name<TH>Symbol<TH>Definition
    <TR><TD VALIGN=BOTTOM>meter<TD VALIGN=MIDDLE ALIGN=RIGHT>m
             <TD ALIGN=CENTER>The meter is the unit of length;
                              it is equal to distance which light
                              overcome in 1/299 792 458 seconds in
                              vacuum.
    <TR><TD VALIGN=MIDDLE>kilogram<TD>kg
              <TD ALIGN=RIGHT>The kilogram is the unit of mass;
                              it is equal to the mass of the
                              international prototype of kilogram (a
                              cylinder of platinum-iridium).
    
    Formatted table
    
              1         2         3         4         5         6         7
    01234567890123456789012345678901234567890123456789012345678901234567890123456789
    
    +------------------------------------------------------------------------------+
    |   Name   | Symbol |                       Definition                         |
    +----------+--------+----------------------------------------------------------+
    |          |        |     The meter is the unit of length; it is equal to      |
    |          |      m |  distance which light overcome in 1/299 792 458 seconds  |
    | meter    |        |                       in vacuum.                         |
    +----------+--------+----------------------------------------------------------+
    |          | kg     |     The kilogram is the unit of mass; it is equal to the |
    | kilogram |        |       mass of the international prototype of kilogram (a |
    |          |        |                           cylinder of platinum-iridium). |
    +------------------------------------------------------------------------------+
      

Notes

Tags and attributes are case insensitive.

Your program and all its modules should be stored in the directory C:\ICP. Name the modules in such way that the two first letters of its name are your starting number.

Write your starting number, list of all used modules and list of task which are solved in your program as a comment at the beginning of the program.

There are several files with extension tbl in the directory C:\ICP. These files contain description of several tables and you can use them for testing purposes.

Permitted aid is English vocabulary, books which describe programming language, its environment (IDE) and standard libraries. You must not use books with description of various methods, algorithms and data structures.

You can get total of 100 points. 60 out of them are used for evaluating functionality, 30 for evaluating efficiency (smart and fast algorithms, sophisticated data structures). Last ten points are used for documentation (description of algorithm, comments, naming convention, neat way of writing code).

Task Maximal no. of points assigned for functionality
1 20
2 5
3 10
4 15
5 10

Programme - Results