Nada

Laboration 6 - Molecular graphics

This program aims to complete the results of the previous lab. It checks the formula syntax, as before, and then builds and draws a model of the molecule. The dialogue may look like this:
   Molecule: Si(C3(COOH)2)4(H2O)7
   Molecule: 
and in the graphics window, the syntactical structure of the formula appears:



Build a molecular tree

At the same time as your program performs the formula checking, it shall build the tree in the picture. Each box corresponds to an object of the following class ("ruta" means a graphics box):
class Ruta:          
    atom="( )"
    num=1    
    next=None
    down=None
The funktion readgroup creates an empty box by rutan = Ruta() and then calls readatom and readnum to set the correct values for atom and num. For a group in parentheses, the call from readgroup to readmol should return a submolecule that is attached underneath rutan.down.

When readgroup is finished, it returns the box object to the statement calling it, which probably looks like

    mol=readgroup()
and is the first statement in readmol. What to do with mol.next is something you must figure out yourself. Finally readmol returns the complete structure to readformel, which returns it to the original call
    mol = readformel()
in the main program, where mol is a pointer to the upper left node in the syntax tree.

Draw the molecular tree

Now, the main program has to draw the structured molecule. Python graphics is not part of this course, so we hand you a module /info/grudat06/molgrafik.py from which you may import the method show and by calling
   show(mol)
display the picture of the molecule in a graphics window. The structure being recursive, naturally the drawing algorithm implements a recursive idea and you are required to formulate this idea. If you cannot invent it by yourself, feel free to look at the molgrafik code.

If the program terminates directly, the graphics will only show for a fraction of a second, so make sure you have a loop for inputting new formulas!

Extra tasks for higher marks (choose one)

The molecular weight may be computed recursively by a single call weight(mol). First, formulate a very recursive idea for the weight, then program it! Modify molgrafik.py such that the weight is displayed in the graphics window.

Brilliantly solved by................................. affirms............................ the ...............


Sidansvarig: <henrik@nada.kth.se>
Senast ändrad 1 februari 2007
Tekniskt stöd: <webmaster@nada.kth.se>