当前位置: 首页 > news >正文

就是这个样的粗爆,手搓一个计算器:科学计算器

       作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。

HTML:

<div class="calculator"><div class="display-wrapper"><div class="display" id="display">0</div></div><div class="buttons"><button onclick="clearDisplay()">C</button><button onclick="deleteLast()">←</button><button onclick="appendOperator('(')">(</button><button onclick="appendOperator(')')">)</button><button onclick="appendNumber('7')">7</button><button onclick="appendNumber('8')">8</button><button onclick="appendNumber('9')">9</button><button onclick="appendOperator('/')">/</button><button onclick="appendNumber('4')">4</button><button onclick="appendNumber('5')">5</button><button onclick="appendNumber('6')">6</button><button onclick="appendOperator('*')">*</button><button onclick="appendNumber('1')">1</button><button onclick="appendNumber('2')">2</button><button onclick="appendNumber('3')">3</button><button onclick="appendOperator('-')">-</button><button onclick="appendNumber('0')" class="zero">0</button><button onclick="appendDot()">.</button><button onclick="appendOperator('+')">+</button><button onclick="calculate()">=</button></div><div class="buttons scientific"><button onclick="calculateFunction('sin')">sin</button><button onclick="calculateFunction('cos')">cos</button><button onclick="calculateFunction('tan')">tan</button><button onclick="calculateFunction('log')">log</button><button onclick="calculateFunction('sqrt')">√</button><button onclick="calculateFunction('pow2')">x²</button><button onclick="calculateFunction('pow3')">x³</button><button onclick="calculateFunction('exp')">eˣ</button><button onclick="calculateFunction('ln')">ln</button><button onclick="calculateFunction('fact')">n!</button><button onclick="calculateFunction('reciprocal')">1/x</button><button onclick="calculateFunction('pi')">π</button></div>
</div>

JS:

let displayValue = '0';function updateDisplay() {document.getElementById('display').textContent = displayValue;
}function clearDisplay() {displayValue = '0';updateDisplay();
}function deleteLast() {if (displayValue.length > 1) {displayValue = displayValue.slice(0, -1);} else {displayValue = '0';}updateDisplay();
}function appendNumber(number) {if (displayValue === '0') {displayValue = number;} else {displayValue += number;}updateDisplay();
}function appendOperator(operator) {displayValue += operator;updateDisplay();
}function appendDot() {if (!displayValue.includes('.')) {displayValue += '.';}updateDisplay();
}function calculate() {try {displayValue = eval(displayValue).toString();} catch (error) {displayValue = 'Error';}updateDisplay();
}function calculateFunction(func) {try {let value = parseFloat(displayValue);switch (func) {case 'sin':displayValue = Math.sin(value).toString();break;case 'cos':displayValue = Math.cos(value).toString();break;case 'tan':displayValue = Math.tan(value).toString();break;case 'log':displayValue = Math.log10(value).toString();break;case 'sqrt':displayValue = Math.sqrt(value).toString();break;case 'pow2':displayValue = Math.pow(value, 2).toString();break;case 'pow3':displayValue = Math.pow(value, 3).toString();break;case 'exp':displayValue = Math.exp(value).toString();break;case 'ln':displayValue = Math.log(value).toString();break;case 'fact':displayValue = factorial(value).toString();break;case 'reciprocal':displayValue = (1 / value).toString();break;case 'pi':displayValue = Math.PI.toString();break;}} catch (error) {displayValue = 'Error';}updateDisplay();
}function factorial(n) {if (n === 0 || n === 1) return 1;return n * factorial(n - 1);
}

CSS:

body {font-family: Arial, sans-serif;display: flex;flex-direction: column;align-items: center;justify-content: center;height: 100vh;margin: 0;background-color: #f4f4f9;
}.calculator {width: 350px;background-color: #333;border-radius: 10px;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}.display-wrapper {background-color: #444;padding: 20px;border-radius: 10px 10px 0 0;overflow-x: auto;overflow-y: hidden;
}.display {color: white;font-size: 36px;text-align: right;white-space: nowrap;min-width: 100%;
}.buttons {display: grid;grid-template-columns: repeat(4, 1fr);gap: 1px;
}button {font-size: 18px;padding: 20px;background-color: #555;border: none;color: white;cursor: pointer;
}button:hover {background-color: #666;
}.zero {grid-column: span 1;
}.scientific {grid-template-columns: repeat(4, 1fr);margin-top: 5px;
}.guide {margin-top: 20px;max-width: 350px;color: #666;font-size: 14px;
}

 线上运行,可以直接打开:科学计算器

http://www.lryc.cn/news/460968.html

相关文章:

  • wordpress使用popup弹窗插件的对比
  • 开源OpenStack
  • 基于Spring Boot+vue技术的导游系统设计与实现
  • 软件测试 —— 灰度测试及测试流程!
  • 中科星图GVE(案例)——AI实现光伏面板提取
  • 一种压缩QRCode矩阵以用于存储的方法
  • 鸿蒙HarmonyOS开发:系统服务
  • 【Go】GO语言知识总结浅析
  • GWO-Transformer-LSTM灰狼算法优化深度学习多变量回归预测(Maltab)
  • 上市公司企业供应链抵抗力数据集(2012-2023年)
  • javaWeb项目-ssm+jsp-XX牙科诊所管理系统功能介绍
  • tcp_rmem中有三个值4896 131072 6291456是什么意思,有什么作用?
  • 转行AI产品经理:高薪诱惑,年薪90万不是梦!
  • javaWeb项目-ssm+jsp股票交易管理系统功能介绍
  • CentOS上安装SSL证书教程
  • 单目相机和双目相机定位
  • 【Cadence27】HDL拷贝工程➕Allegro导出DXF和3D文件STP
  • 拓扑学与集合论的关系
  • 设计模式——代理模式(6)
  • 设计模式之-策略模式配合枚举
  • 滑动窗口经典例题
  • PetaLinux工程的常用命令——petalinux-create
  • Unity的Compute Shader如何进行同步?
  • 大数据-171 Elasticsearch ES-Head 与 Kibana 配置 使用 测试
  • git 与 github 同步
  • 数学建模算法与应用 第10章 多元分析及其方法
  • 西门子828d的plc一些信息记录
  • 为啥我的Python这么慢 - 项查找 (二)
  • 计算机毕业设计python+spark知识图谱课程推荐系统 课程预测系统 课程大数据 课程数据分析 课程大屏 mooc慕课推荐系统 大数据毕业设计
  • 阿里 C++面试,算法题没做出来,,,