Advertisement
Guest User

Untitled

a guest
Aug 21st, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  3. <head>
  4. <!-- Page Title -->
  5. <title>Calculator</title>
  6.  
  7. <!-- META TAGS -->
  8. <meta name="description" content="" />
  9. <meta name="keywords" content="" />
  10. <meta name="robots" content="index, follow" />
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
  12. <meta content="text/html; charset=utf-8" http-equiv="content-type" />
  13.  
  14. <!-- Favicon -->
  15. <link rel="shortcut icon" href="favicon.png" />
  16.  
  17. <!-- Google Fonts -->
  18. <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,400italic,300,300italic,600italic,700,700italic,800,800italic" />
  19.  
  20. <!-- CSS Stylesheets -->
  21. <link rel="stylesheet" type="text/css" href="css/style.css" />
  22.  
  23. <!--[if lt IE 9]>
  24. <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  25. <![endif]-->
  26. </head>
  27. <body>
  28. <!-- BEGIN Wrapper -->
  29. <div id="wrapper">
  30. <!-- Hidden infos -->
  31. <input type="hidden" id="number_one" value="0" />
  32. <input type="hidden" id="number_two" value="0" />
  33. <input type="hidden" id="operator" value="+" />
  34. <input type="hidden" id="cancalculate" value="false" />
  35. <input type="hidden" id="operatorselected" value="false" />
  36.  
  37. <!-- Result -->
  38. <input type="text" id="result" value="0" readonly />
  39.  
  40. <!-- Buttons -->
  41. <div class="buttons">
  42. <input type="button" class="button-dark" value="CE" />
  43. <input type="button" class="button-dark" value="C" />
  44. <input type="button" class="button-dark" value="&lArr;" />
  45. <input type="button" class="button-green" value="&divide;" />
  46. </div>
  47. <div class="buttons">
  48. <input type="button" value="7" />
  49. <input type="button" value="8" />
  50. <input type="button" value="9" />
  51. <input type="button" class="button-green" value="X" />
  52. </div>
  53. <div class="buttons">
  54. <input type="button" value="4" />
  55. <input type="button" value="5" />
  56. <input type="button" value="6" />
  57. <input type="button" class="button-green" value="-" />
  58. </div>
  59. <div class="buttons">
  60. <input type="button" value="1" />
  61. <input type="button" value="2" />
  62. <input type="button" value="3" />
  63. <input type="button" class="button-green" value="+" />
  64. </div>
  65. <div class="buttons">
  66. <input type="button" value="" />
  67. <input type="button" value="0" />
  68. <input type="button" value="," />
  69. <input type="button" class="button-blue" value="=" />
  70. </div>
  71. </div>
  72. <!-- END Wrapper -->
  73.  
  74. <!-- BEGIN Scripts -->
  75. <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
  76. <script type="text/javascript">
  77. // when a button was clicked
  78. $("input[type='button']").click(function() {
  79. var result = 0;
  80.  
  81. if(isNumeric($(this).val())) {
  82. if($("#result").val().slice(-1) == ".") {
  83. var cal = 0;
  84. if(!$.parseJSON($("#operatorselected").val())) {
  85. cal = '' + $("#number_one").val() + $(this).val();
  86. $("#number_one").val(cal.toString());
  87. } else {
  88. cal = '' + $("#number_two").val() + $(this).val();
  89. $("#number_two").val(cal.toString());
  90. $("#cancalculate").val("true");
  91. }
  92. $("#result").val(cal.toString());
  93. } else {
  94. var value = 0;
  95. var cal = 0;
  96. if(!$.parseJSON($("#operatorselected").val())) {
  97. value = parseFloat($("#number_one").val());
  98. if(value == 0) {
  99. cal = parseFloat($(this).val());
  100. } else {
  101. cal = '' + value + parseFloat($(this).val());
  102. }
  103. $("#number_one").val(cal.toString());
  104. } else {
  105. value = parseFloat($("#number_two").val());
  106. if(value == 0) {
  107. cal = parseFloat($(this).val());
  108. } else {
  109. cal = '' + value + parseFloat($(this).val());
  110. }
  111. $("#number_two").val(cal.toString());
  112. $("#cancalculate").val("true");
  113. }
  114. $("#result").val(cal.toString());
  115. }
  116. }
  117.  
  118. if(isOperator($(this).val())) {
  119. $("#operator").val($(this).val());
  120. $("#operatorselected").val("true");
  121. }
  122.  
  123. if($(this).val().toLowerCase() == "c") {
  124. reset(true);
  125. }
  126.  
  127. if($(this).val().toLowerCase() == ",") {
  128. var current = 0;
  129. if(!$.parseJSON($("#operatorselected").val())) {
  130. current = parseFloat($("#number_one").val()) + ".";
  131. $("#result").val(current);
  132. $("#number_one").val(current);
  133. } else {
  134. current = parseFloat($("#number_two").val()) + ".";
  135. $("#result").val(current);
  136. $("#number_two").val(current);
  137. }
  138. }
  139.  
  140. if($(this).val().toLowerCase() == "ce") {
  141. if($.parseJSON($("#operatorselected").val())) {
  142. $("#result").val("0");
  143. $("#number_two").val("0");
  144. }
  145. }
  146.  
  147. if($(this).val().toLowerCase() == "โ‡") {
  148. var current = $("#result").val();
  149. if(current.length > 1) {
  150. $("#result").val(current.substring(0, current.length - 1));
  151. } else {
  152. $("#result").val("0");
  153. }
  154.  
  155. if(!$.parseJSON($("#operatorselected").val())) {
  156. $("#number_one").val(parseFloat($("#result").val()));
  157. } else {
  158. }
  159. }
  160.  
  161. if($(this).val() == "=") {
  162. if($.parseJSON($("#cancalculate").val())) {
  163. // calculation
  164. var operator = $("#operator").val().toLowerCase();
  165. if(operator == "+") {
  166. result = parseFloat($("#number_one").val()) + parseFloat($("#number_two").val());
  167. } else if(operator == "-") {
  168. result = parseFloat($("#number_one").val()) - parseFloat($("#number_two").val());
  169. } else if(operator == "x") {
  170. result = parseFloat($("#number_one").val()) * parseFloat($("#number_two").val());
  171. } else if(operator == "รท") {
  172. result = parseFloat($("#number_one").val()) / parseFloat($("#number_two").val());
  173. }
  174. $("#result").val(result);
  175. // reset
  176. reset(false);
  177. }
  178. }
  179. });
  180.  
  181. // reset the calculator
  182. function reset(textreset) {
  183. if(textreset) {
  184. $("#result").val("0");
  185. }
  186. $("#number_one").val("0");
  187. $("#number_two").val("0");
  188. $("#cancalculate").val("false");
  189. $("#operatorselected").val("false");
  190. }
  191.  
  192. // check if the variable a operator, returns bool
  193. function isOperator(string) {
  194. if(string == "+" || string == "-" || string.toLowerCase() == "x" || string == "รท") {
  195. return true;
  196. }
  197. return false;
  198. }
  199.  
  200. // check if the variable numeric or not, returns bool
  201. function isNumeric(n) {
  202. return !isNaN(parseFloat(n)) && isFinite(n);
  203. }
  204. </script>
  205. <!-- END Scripts -->
  206. </body>
  207. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement