php教程

php代码:三角函数计算器,正弦余弦计算器

php教程 51源码 2023-11-23 人阅读

本文帮你提供一个基本的PHP代码示例来设计一个正弦与余弦等三角函数的计算器,你可以进行进一步的美化和响应式布局。并达到三角函数计算的功能!


php代码:三角函数计算器,正弦余弦计算器-第1张图片-文煞网站目录网


<!DOCTYPE html>
<html>
<head>
  <title>三角函数计算器</title>
  <style>
    body {
      font-family: Arial, sans-serif;
    }
    .container {
      max-width: 600px;
      margin: 0 auto;
      padding: 20px;
    }
    h1 {
      text-align: center;
    }
    .form-group {
      margin-bottom: 20px;
    }
    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }
    input[type="number"] {
      width: 100%;
      padding: 5px;
    }
    input[type="submit"] {
      display: block;
      width: 100%;
      margin-top: 10px;
      padding: 10px;
      font-size: 16px;
      background-color: #337ab7;
      color: #fff;
      border: none;
      cursor: pointer;
    }
    .result {
      text-align: center;
      margin-top: 20px;
      font-size: 18px;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>三角函数计算器</h1>
    <form method="post">
      <div class="form-group">
        <label for="angle">角度(度):</label>
        <input type="number" id="angle" name="angle" step="any" required autofocus>
      </div>
      <input type="submit" value="计算">
    </form>
    <?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      $angle = $_POST['angle'];
      $angle_rad = deg2rad($angle);
      $sin = sin($angle_rad);
      $cos = cos($angle_rad);
      echo "<div class='result'>正弦(sin)值:{$sin}</div>";
      echo "<div class='result'>余弦(cos)值:{$cos}</div>";
    }
    ?>
  </div>
</body>
</html>


这段代码创建了一个简单的三角函数计算器页面。页面使用了简单的CSS样式来美化,但你可以根据自己的需求增加更多的样式和布局。用户可以在角度输入框中输入一个角度(单位为度),然后点击计算按钮。PHP代码将使用`sin()`和`cos()`函数计算输入角度的正弦和余弦值,并将结果显示在页面上。希望这个基础示例能够帮助你开始设计一个美观响应式的三角函数计算器。如果有其他问题,请随时提问。

版权声明:文章搜集于网络,如有侵权请联系本站,转载请说明出处:https://www.51yma.cn/jiaocheng/php/1329.html
文章来源:文煞PHP笔记网-https://www.wensha.info/post/1195.html