calculatrice.anubis 6.11 KB

 *Project*                             The Anubis Project
   
 *Title*                                A Web Calculator
   
 *Copyright*                     Copyright (c) Alain Prouté 2005. 


 *Author*       Alain Prouté
   

read tools/basis.anubis
read html.anubis
read common.anubis   

public define Printable_tree
script_calculatrice = 

 [
  "<script language=\"javascript\">",
  " function tape(valeur)",
  " {",
  " old = document.calculatrice.ecran.value;",
  " document.calculatrice.ecran.value = old+valeur;",
  " }",
  " function egal()",
  " {",
  " var affichage = document.calculatrice.ecran.value;",
  " document.calculatrice.ecran.value = eval(affichage);",
  " }",
  " function multiplie()",
  " {",
  " old = document.calculatrice.ecran.value;",
  " document.calculatrice.ecran.value = old+'*';",
  " }",
  " function carre()",
  " {",
  " document.calculatrice.ecran.value =",
     " document.calculatrice.ecran.value*document.calculatrice.ecran.value;",
  " }",
  " function pi()",
  " {",
  " document.calculatrice.ecran.value =3.141592",
  " }",
  " function inverse()",
  " {",
  " old = document.calculatrice.ecran.value;",
  " document.calculatrice.ecran.value = 1/old;",
  "} ",
  " function clavier()",
  " {",
    " if(event.keyCode == 96)",
       " {",
       "  document.calculatrice.c0.focus();",
       " tape(0);",
       " }",
       " if(event.keyCode == 97)",
       " {",
       " document.calculatrice.c1.focus();",
       " tape(1);",
       " }",
       " if(event.keyCode == 98)",
       " {",
       " document.calculatrice.c2.focus();",
       " tape(2);",
       " }",
       " if(event.keyCode == 99)",
       " {",
       " document.calculatrice.c3.focus();",
       " tape(3);",
       " }",
       " if(event.keyCode == 100)",
       " {",
       " document.calculatrice.c4.focus();",
       " tape(4);",
       " }",
       " if(event.keyCode == 101)",
       " {",
       " document.calculatrice.c5.focus();",
       " tape(5);",
       " }",
       " if(event.keyCode == 102)",
       " {",
       " document.calculatrice.c6.focus();",
       " tape(6);",
       " }",
       " if(event.keyCode == 103)",
       " {",
       " document.calculatrice.c7.focus();",
       " tape(7);",
       " }",
       " if(event.keyCode == 104)",
       " {",
       " document.calculatrice.c8.focus();",
       " tape(8);",
       " }",
       " if(event.keyCode == 105)",
       " {",
       " document.calculatrice.c9.focus();",
       " tape(9);",
       " }",
       " if(event.keyCode == 107)",
       " {",
       " document.calculatrice.cplus.focus();",
       " tape('+');",
       " }",
       " if(event.keyCode == 109)",
       " {",
       " document.calculatrice.cmoins.focus();",
       " tape('-');",
       " }",
       " if(event.keyCode == 106)",
       " {",
       " document.calculatrice.cfois.focus();",
       " tape('*');",
       " }",
       " if(event.keyCode == 111)",
       " {",
       " document.calculatrice.cslash.focus();",
       " tape('/');",
       " }",
       " if(event.keyCode == 110)",
       " {",
       " document.calculatrice.cpoint.focus();",
       " tape('.');",
       " }",
       " if(event.keyCode == 13)",
       " {",
       " document.calculatrice.cegal.focus();",
       " egal();",
       " }",
       " if(event.keyCode == 46)",
       " {",
       " document.calculatrice.ac.focus();",
       " document.calculatrice.ecran.value = '';",
       " }",
 " }",
 "document.onkeydown = clavier;",
 "</script>"
 ].

public type Calc_Button:
calc_button
  (
   String   name,
   String   value,
   String   on_click
  ).

define List(Calc_Button)
ligne_1 = 

  [
   calc_button("7",  "c7",      "tape(this.value)"),
   calc_button("8",  "c8",      "tape(this.value)"),
   calc_button("9",  "c9",      "tape(this.value)"),
   calc_button("+",  "cplus",   "tape(this.value)"),
   calc_button("AC", "ac",      "this.form.ecran.value=''")
  ].

define List(Calc_Button)
ligne_2 = 

 [
  calc_button("4",  "c4",        "tape(this.value)"),
  calc_button("5",  "c5",        "tape(this.value)"),
  calc_button("6",  "c6",        "tape(this.value)"),
  calc_button("-",  "cmoins",    "tape(this.value)"),
  calc_button("x", "cx",       "carre()"),
 ].


define List(Calc_Button)
ligne_3 = 

 [
  calc_button("1",  "c1",          "tape(this.value)"),
  calc_button("2",  "c2",          "tape(this.value)"),
  calc_button("3",  "c3",          "tape(this.value)"),
  calc_button("/",  "cslash",      "tape(this.value)"),
  calc_button("Pi", "c3.141592",   "pi()"), //tape(this.value)
 ].

 <input type="button" value="p" name="c3.141592" STYLE="font-family:symbol;width=25;height=25" 
  onclick="tape(this.value)">


define List(Calc_Button)
ligne_4 = 

 [
  calc_button("0",   "c0",      "tape(this.value)"),
  calc_button(".",   "cpoint",  "tape(this.value)"),
  calc_button("=",   "cegal",   "egal()"),
  calc_button("x",   "cfois",   "multiplie()"),
  calc_button("1/x", "c1/x",    "inverse()"),
 ].

define List(List(Calc_Button))
all_calc_buttons = [ligne_1, ligne_2, ligne_3, ligne_4].

define List(Cell)
cell_calc
  (
   List(Calc_Button) lcb
  ) =

 if lcb is 
  {
   []       then (List(Cell)) [],
   [h . t]  then

   [
    cell([h_center], button(value(h), name(h), on_click(h), 25, 25))
    . cell_calc(t)
   ]
  }.

define List(Table_row)
calculatrice
  (
   List(List(Calc_Button)) l
  ) =

 if l is 
  {
   []       then (List(Table_row)) [],
   [h . t]  then

    [
     row([], cell_calc(h))
     . calculatrice(t)
    ]
  }.

public define Web_item
calculatrice = 

 form_name("calculatrice",
 table([border(1,1,1)],
  [
   row([],
       [
        cell([h_center], text_input("ecran", 20, ""))
       ]),
   row([],
       [
        cell([absolute_height(15), h_center], 
              bold(text("Calculatrice")))
       ]),
   row([],
       [
        cell([h_center],
             table([], calculatrice(all_calc_buttons)))
       ])
  ])).



public define Web_page
calculatrice
  (
   List(Web_arg) lwa
  ) =

 web_page
  (
   "Calculatrice",
   (List(WebMeta)) [],
   script_calculatrice,
   body
    (
     [], 
      center(calculatrice)
    )).


 web page Bool
calculatrice 
  (
   List(Web_arg)  lwa
  ) =

  print(calculatrice(lwa)).