public class CalculatriceListeSol {
  boolean isListe ; // true si le haut est une liste
  double            hautDouble ; // vaut 0 si le haut est une liste
  CalculatriceListeSol hautListe ;  // vaut null si le haut est un double
  CalculatriceListeSol suite ;

  CalculatriceListeSol (double elt, CalculatriceListeSol old) {
    isListe = false ;
    hautDouble = elt ;
    hautListe  = null ;
    suite = old ;
  }

  CalculatriceListeSol (CalculatriceListeSol elt, CalculatriceListeSol old) {
    isListe = true ;
    hautDouble = 0. ;
    hautListe  = elt ;
    suite = old ;
  }

  private static void error (String msg) {
    System.err.println (msg) ;
    System.exit (2) ;
  }

  static CalculatriceListeSol pushDouble (CalculatriceListeSol pile, double elt) {
    // empile un réel
    return new CalculatriceListeSol (elt, pile) ;
  }

  static CalculatriceListeSol pushListe (CalculatriceListeSol pile, CalculatriceListeSol elt) {
    // empile une liste
    return new CalculatriceListeSol (elt, pile) ;
  }

  static double topDouble (CalculatriceListeSol pile) {
    // haut de pile si c'est un réel
    if (pile == null)
      error ("TopDouble sur pile vide") ;
    if (pile.isListe)
      error ("TopDouble pour une liste") ;
    return pile.hautDouble ;
  }

  static CalculatriceListeSol topListe (CalculatriceListeSol pile) {
    // haut de pile si c'est une liste
    if (pile == null)
      error ("TopListe sur pile vide") ;
    if (!pile.isListe)
      error ("TopListe pour un réel") ;
    return pile.hautListe ;
  }

  static CalculatriceListeSol pop (CalculatriceListeSol pile) {
    // dépile
    if (pile == null)
      error ("Pop sur pile vide") ;
    return pile.suite ;
  }

  static String listToString (CalculatriceListeSol pile) {
    // impression d'une liste
    // version itérative et récursive
    String out = "{ " ;
    while (pile != null) {
      if (pile.isListe)
        out += listToString (pile.hautListe) ;
      else
        out += pile.hautDouble ;
      pile = pile.suite ;
      out += " " ;
    }
    return out + "}";
  }

  static String toString (CalculatriceListeSol pile) {
    // impression du contenu de la pile
    // version itérative
    String out = "" ;
    int rang = 0 ;
    while (pile != null) {
      if (pile.isListe)
        out = rang + ": " + listToString (pile.hautListe) + "\n" + out ;
      else
        out = rang + ": " + pile.hautDouble + "\n" + out ;
      pile = pile.suite ;
      rang++ ;
    }
    return out ;
  }

  static CalculatriceListeSol neg (CalculatriceListeSol pile) {
    // négation du dernier élément de la pile
    double haut = topDouble (pile) ;
    return pushDouble (pop (pile), -haut) ;
  }

  static CalculatriceListeSol add (CalculatriceListeSol pile) {
    // addition des deux derniers éléments de la pile
    double x = topDouble (pile) ; pile = pop (pile) ;
    double y = topDouble (pile) ; pile = pop (pile) ;
    return pushDouble (pile, x + y) ;
  }

  static CalculatriceListeSol sub (CalculatriceListeSol pile) {
    // soustraction des deux derniers éléments de la pile
    double x = topDouble (pile) ; pile = pop (pile) ;
    double y = topDouble (pile) ; pile = pop (pile) ;
    return pushDouble (pile, x - y) ;
  }

  static CalculatriceListeSol mul (CalculatriceListeSol pile) {
    // multiplication des deux derniers éléments de la pile
    double x = topDouble (pile) ; pile = pop (pile) ;
    double y = topDouble (pile) ; pile = pop (pile) ;
    return pushDouble (pile, x * y) ;
  }

  static CalculatriceListeSol div (CalculatriceListeSol pile) {
    // division des deux derniers éléments de la pile
    double x = topDouble (pile) ; pile = pop (pile) ;
    double y = topDouble (pile) ; pile = pop (pile) ;
    return pushDouble (pile, x / y) ;
  }

  static CalculatriceListeSol concat (CalculatriceListeSol pile) {
    // concaténation des deux derniers éléments de la pile
    CalculatriceListeSol x = topListe (pile) ; pile = pop (pile) ;
    CalculatriceListeSol y = topListe (pile) ; pile = pop (pile) ;
    if (x == null) return pushListe (pile, y) ;
    CalculatriceListeSol parcoursx = x;
    while (parcoursx.suite != null) parcoursx = parcoursx.suite ;
    parcoursx.suite = y ;
    return pushListe (pile, x) ;
  }

  static CalculatriceListeSol split (CalculatriceListeSol pile) {
    // éclatement d'une liste en haut de la pile
    CalculatriceListeSol x = topListe (pile) ; pile = pop (pile) ;
    if (x == null) return pile ;
    CalculatriceListeSol parcoursx = x;
    while (parcoursx.suite != null) parcoursx = parcoursx.suite ;
    parcoursx.suite = pile ;
    return x ;
  }

  static CalculatriceListeSol join (CalculatriceListeSol pile, int n) {
    // réunion des n premiers éléments en une liste
    if (n == 0) pushListe (pile, null) ;
    CalculatriceListeSol x = pile ;
    while (n > 1) {
      pile = pile.suite;
      n--;
    }
    CalculatriceListeSol p = pile.suite;
    pile.suite = null;
    return pushListe (p, x) ;
  }

  static CalculatriceListeSol joinInverse (CalculatriceListeSol pile, int n) {
    // réunion des n premiers éléments en une liste
    // en ordre inverse...
    CalculatriceListeSol x = null ;
    while (n > 0) {
      if (pile.isListe)
        x = new CalculatriceListeSol (topListe (pile), x) ;
      else
        x = new CalculatriceListeSol (topDouble (pile), x) ;
      pile = pop (pile) ;
      n--;
    }
    return pushListe (pile, x) ;
  }

  public static String readline () throws java.io.IOException {
    // Cette méthode attend que l'utilisateur tape des données,
    // terminées par "enter" et en fait une chaîne de caractères.
    String ligne = "" ;
    for ( ; ; ) { // boucle infinie
      int r = System.in.read () ;
      if (r == -1)   return "q" ;   // fin de fichier
      char c = (char) r ;
      if (c == '\n') return ligne ; // fin de ligne
      ligne += c ;
    }
  }

  public static void main (String[] args) throws java.io.IOException {
    CalculatriceListeSol p = null ;
    for ( ; ; ) { // boucle infinie
      System.out.println ("Commandes : q NEG + - / * un_nombre { }n") ;
      String commande = readline () ;
      if (commande.equals("q")) System.exit (0) ;
      else if (commande.equals("NEG")) p = neg (p) ;
      else if (commande.equals("+")) {
        if (p.isListe) p = concat (p) ;
        else           p = add (p) ;
      }
      else if (commande.equals("-")) p = sub (p) ;
      else if (commande.equals("*")) p = mul (p) ;
      else if (commande.equals("/")) p = div (p) ;
      else if (commande.equals("{")) p = split (p) ;
      else if (commande.startsWith("}")) {
        commande = commande.substring(1) ;
        int n = Integer.parseInt (commande) ;
        p = join (p, n) ;
      }
      else {
        double x = Double.valueOf (commande) . doubleValue() ;
        p = pushDouble (p, x) ;
      }
      System.out.print (toString (p)) ;
    }
  }
}
