Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. /*
  2. * Calculatrice en Javascript.
  3. *
  4. * Copyright © 2012, Adrien Tétar.
  5. */
  6. //================================== Var definitions. ======================================
  7. /* Two numbers calc will work with. */
  8. var nb1 = 0, nb2 = 0;
  9. /* Operation type. */
  10. var op = 'null';
  11. var cpt = 0;
  12. //============================== Add numbers on 'screen'.===================================
  13. function ajouteChiffre(form,val)
  14. {
  15. form.texte.value += val;
  16. }
  17. //============== Test if a comma has already been appended or not. =========================
  18. function ajoutePoint(form)
  19. {
  20. if (form.texte.value.length == 0)
  21. {
  22. form.texte.value = "0.";
  23. }
  24. else
  25. {
  26. var j = 0;
  27. for (var i=0;i<form.texte.value.length;i++)
  28. {
  29. if (form.texte.value.charAt(i) == ".")
  30. {
  31. j = 1;
  32. break;
  33. }
  34. }
  35. (j == 0) ? (form.texte.value += ".") : (alert("D\351j\340 entr\351."));
  36. }
  37. }
  38. //============================== Set operation type. ========================================
  39. function setOp(opType)
  40. {
  41. if (op == 'null')
  42. {
  43. op = opType;
  44. }
  45. else
  46. {
  47. alert("Vous \352tes d\351j\340 en train de faire une " + op + ".");
  48. form.texte.value = "";
  49. }
  50. }
  51. //===================================== Reset. ===============================================
  52. function raz(form)
  53. {
  54. form.texte.value = "";
  55. form.affichage.value = "";
  56. nb1 = 0, nb2 = 0;
  57. op = 'null';
  58. cpt = 0;
  59. }
  60. //=============================== Store variables. ===========================================
  61. function store(form)
  62. {
  63. if ((form.texte.value != "") && (op == 'null') && (cpt == 0)) // op is set after first var storage
  64. {
  65. nb1 = form.texte.value;
  66. form.texte.value = "";
  67. cpt++;
  68. }
  69. else if ((form.texte.value != "") && (op != 'null') && (cpt == 1))
  70. {
  71. nb2 = form.texte.value;
  72. form.texte.value = "";
  73. cpt++;
  74. }
  75. /* else: do nothing. */
  76. }
  77. /*
  78. * Workaround for unwanted behavior: when entering the first variable and clicking equal the var will get stored.
  79. */
  80. function storeEq(form)
  81. {
  82. if (cpt == 1)
  83. {
  84. store(form);
  85. }
  86. }
  87. //================================== Calculate. ===============================================
  88. function calc(form)
  89. {
  90. /* Break if the user clicked on the equal button without entering 2 variables. */
  91. if (cpt < 2) // We don't use (nbX == 0) check because 0 can be a user-entered value.
  92. {
  93. alert("Il faut deux nombres pour pouvoir faire un calcul.");
  94. }
  95. else
  96. {
  97. if (op == "somme")
  98. {
  99. form.affichage.value = parseInt(nb1) + parseInt(nb2);
  100. }
  101. else if (op == "soustraction")
  102. {
  103. form.affichage.value = parseInt(nb1) - parseInt(nb2);
  104. }
  105. else if (op == "multiplication")
  106. {
  107. form.affichage.value = nb1 * nb2;
  108. }
  109. else if (op == "division")
  110. {
  111. if (nb2 == 0)
  112. {
  113. alert("On ne peut pas diviser par z\351ro.");
  114. }
  115. else
  116. {
  117. form.affichage.value = nb1 / nb2;
  118. }
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement