当前位置:   article > 正文

HTML+CSS+JS制作一个科学计算器_计算器的html和css

计算器的html和css

可以实现基本的加减乘除,三角函数,对数,指数,阶乘,开根号,实现效果如下图:

 1.HTML代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet" href="style.css">
  9. <script src="index.js"></script>
  10. </head>
  11. <body>
  12. <div id="calculator">
  13. <div id="input">
  14. <input type="text" id="Screen" name="Screen" class="screen" value="" readonly>
  15. </div>
  16. <div id="keys">
  17. <input type="button" id="square" onclick="square()" value="x^2" />
  18. <input type="button" id="cube" onclick="cube()" value="x^3" />
  19. <input type="button" id="^" onclick="cal(this.id)" value="^" />
  20. <input type="button" id="factorial" onclick="factorial()" value="n!" />
  21. <input type="button" id="sin" onclick="sin()" value="sin" />
  22. <input type="button" id="cos" onclick="cos()" value="cos" />
  23. <input type="button" id="tan" onclick="tan()" value="tan" />
  24. <input type="button" id="log" onclick="log()" value="log" />
  25. <input type="button" id="sqrt" onclick="sqrt()" value="√" />
  26. <input type="button" id="cbrt" onclick="cbrt()" value="³√" />
  27. <input type="button" id="Back" onclick="back()" value="Back" />
  28. <input type="button" id="C" onclick="clearNum()" value="CE" />
  29. <input type="button" id="7" onclick="cal(this.id)" value="7" />
  30. <input type="button" id="8" onclick="cal(this.id)" value="8" />
  31. <input type="button" id="9" onclick="cal(this.id)" value="9" />
  32. <input type="button" id="/" onclick="cal(this.id)" value="/" />
  33. <input type="button" id="4" onclick="cal(this.id)" value="4" />
  34. <input type="button" id="5" onclick="cal(this.id)" value="5" />
  35. <input type="button" id="6" onclick="cal(this.id)" value="6" />
  36. <input type="button" id="*" onclick="cal(this.id)" value="*" />
  37. <input type="button" id="1" onclick="cal(this.id)" value="1" />
  38. <input type="button" id="2" onclick="cal(this.id)" value="2" />
  39. <input type="button" id="3" onclick="cal(this.id)" value="3" />
  40. <input type="button" id="-" onclick="cal(this.id)" value="-" />
  41. <input type="button" id="." onclick="cal(this.id)" value="." />
  42. <input type="button" id="0" onclick="cal(this.id)" value="0" />
  43. <input type="button" id="=" onclick="equal()" value="=" />
  44. <input type="button" id="+" onclick="cal(this.id)" value="+" />
  45. </div>
  46. </body>
  47. </html>

2.CSS代码

  1. #calculator {
  2. background-color: #eeeeee;
  3. border: 1px solid gray;
  4. margin: 30px auto;
  5. width: 300px;
  6. height: 430px;
  7. padding: 15px;
  8. }
  9. .screen {
  10. width: 284px;
  11. height: 40px;
  12. background-color: #fcfdea;
  13. box-shadow: 2px 3px 3px rgb(181, 187, 181) inset;
  14. border-radius: 4px;
  15. text-align: right;
  16. padding-right: 10px;
  17. font-size: 20px;
  18. margin-top: 5px;
  19. border: none;
  20. outline: 1px solid rgb(136, 127, 127);
  21. transition: all .1s;
  22. }
  23. .screen:hover,.screen:focus{
  24. outline: 1px solid rgb(65, 60, 60);
  25. }
  26. #keys {
  27. height: 350px;
  28. margin-top: 25px;
  29. }
  30. input[type="button"] {
  31. float: left;
  32. width: 75px;
  33. height: 50px;
  34. text-align: center;
  35. background-color: #a6acb86c;
  36. cursor: pointer;
  37. }
  38. input[type="button"]:hover{
  39. background-color: #a6acb8;
  40. }

3.JS代码(重头戏)

  1. function cal(num) {
  2. var n = document.getElementById(num)
  3. if (n === null) return
  4. document.getElementById("Screen").value += n.value;
  5. }
  6. var flag = true;
  7. function equal() {
  8. var value = document.getElementById("Screen").value
  9. if (value.indexOf('^') != -1) {
  10. value = this.pow(value, value.indexOf('^'))
  11. }
  12. // 点击'='切换分数/小数
  13. if (!flag) {
  14. document.getElementById("Screen").value = this.decimalToFractional(eval(value))
  15. flag = true
  16. } else {
  17. document.getElementById("Screen").value = eval(value)
  18. flag = false
  19. }
  20. }
  21. function back() {
  22. var n = document.getElementById("Screen");
  23. n.value = n.value.substring(0, n.value.length - 1);
  24. }
  25. function clearNum() {
  26. document.getElementById("Screen").value = "";
  27. }
  28. function sin() {
  29. document.getElementById("Screen").value = Math.sin(document.getElementById("Screen").value);
  30. }
  31. function cos() {
  32. document.getElementById("Screen").value = Math.cos(document.getElementById("Screen").value);
  33. }
  34. function tan() {
  35. document.getElementById("Screen").value = Math.tan(document.getElementById("Screen").value);
  36. }
  37. function log() {
  38. document.getElementById("Screen").value = Math.log(document.getElementById("Screen").value);
  39. }
  40. function sqrt() {
  41. document.getElementById("Screen").value = Math.sqrt(document.getElementById("Screen").value);
  42. }
  43. function cbrt() {
  44. document.getElementById("Screen").value = Math.cbrt(document.getElementById("Screen").value)
  45. }
  46. function square() {
  47. document.getElementById("Screen").value = Math.pow(document.getElementById("Screen").value, 2);
  48. }
  49. function cube() {
  50. document.getElementById("Screen").value = Math.pow(document.getElementById("Screen").value, 3);
  51. }
  52. function pow(value, pos) {
  53. if (pos != -1) {
  54. let arr = value.split("")
  55. let powVal = Math.pow(arr[pos - 1], arr[pos + 1])
  56. arr.splice(pos - 1, 3, powVal)
  57. value = arr.join("")
  58. return value
  59. }
  60. }
  61. // 阶乘
  62. function factorial() {
  63. function func(num) {
  64. if (num == 1) {
  65. console.log(1);
  66. return 1;
  67. } else if (num == 0) {
  68. console.log(0);
  69. return 1;
  70. } else {
  71. return num * func(num - 1)
  72. }
  73. }
  74. document.getElementById("Screen").value = func(document.getElementById("Screen").value)
  75. }
  76. // 小数化为分数
  77. function decimalToFractional(decimal) {
  78. if (decimal % 1 === 0) {
  79. return decimal
  80. }
  81. const formatDecimal = decimal.toFixed(2)
  82. let denominator = 100
  83. let numerator = formatDecimal * 100
  84. let bigger = 0
  85. function recursion() {
  86. bigger = denominator > numerator ? denominator : numerator
  87. for (let i = bigger; i > 1; i--) {
  88. if (
  89. Number.isInteger(numerator / i) && Number.isInteger(denominator / i)) {
  90. numerator = numerator / i
  91. denominator = denominator / i
  92. recursion()
  93. }
  94. }
  95. }
  96. recursion()
  97. return `${numerator} / ${denominator}`
  98. }

这里因为按钮用的是input:button,而在实现指数运算(pow(a,b))时分别需要上一次的输入和下一次的输入,而js获取下一次的输入实现起来比较麻烦,所以为指数运算的按钮(^)并没有直接绑定实现指数运算的方法,而是绑定的cal(this.id),将指数运算保存起来,在按下“=”时再调用实现指数运算的方法。

在点击“=”得到运算结果后,还可以再次点击“=”,将小数转换为分数(小数为2位精度)。

  1. function equal() {
  2. var value = document.getElementById("Screen").value
  3. if (value.indexOf('^') != -1) {
  4. value = this.pow(value, value.indexOf('^'))
  5. }
  6. // 点击'='切换分数/小数
  7. if (!flag) {
  8. document.getElementById("Screen").value = this.decimalToFractional(eval(value))
  9. flag = true
  10. } else {
  11. document.getElementById("Screen").value = eval(value)
  12. flag = false
  13. }
  14. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/163843?site
推荐阅读
相关标签
  

闽ICP备14008679号