Калькулятор

ццууй

23232323\цв\

івцівц

<div class="universal-calculator">
    <style>
        .universal-calculator {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100%;
            background-color: #f0f0f0;
            padding: 20px;
        }
        .calculator {
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        .logo {
            margin-bottom: 20px;
        }
        .display {
            width: 100%;
            height: 50px;
            font-size: 20px;
            margin-bottom: 10px;
            text-align: right;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
            box-sizing: border-box;
        }
        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 10px;
        }
        .buttons button {
            padding: 15px;
            font-size: 18px;
            border-radius: 5px;
            border: none;
            background-color: #75bfff;
            cursor: pointer;
        }
        .buttons button:hover {
            background-color: #d0d0d0;
        }
        .buttons .wide {
            grid-column: span 2;
        }
        .buttons .tall {
            grid-row: span 2;
        }
    </style>

    <div class="calculator">
        <img src="https://universalcalc.com/wp-content/uploads/2024/08/cropped-logo9.png" alt="Universal Calculator Logo" class="logo">
        <input type="text" class="display" id="display" readonly>
        <div class="buttons">
            <button onclick="clearDisplay()">C</button>
            <button onclick="backspace()">←</button>
            <button onclick="insert('/')">/</button>
            <button onclick="insert('*')">*</button>
            <button onclick="insert('7')">7</button>
            <button onclick="insert('8')">8</button>
            <button onclick="insert('9')">9</button>
            <button onclick="insert('-')">-</button>
            <button onclick="insert('4')">4</button>
            <button onclick="insert('5')">5</button>
            <button onclick="insert('6')">6</button>
            <button onclick="insert('+')">+</button>
            <button onclick="insert('1')">1</button>
            <button onclick="insert('2')">2</button>
            <button onclick="insert('3')">3</button>
            <button onclick="calculate()" class="tall">=</button>
            <button onclick="insert('0')" class="wide">0</button>
            <button onclick="insert('.')">.</button>
            <button onclick="toggleSign()">±</button>
            <button onclick="insert('(')">(</button>
            <button onclick="insert(')')">)</button>
            <button onclick="insert('Math.sin(')">sin</button>
            <button onclick="insert('Math.cos(')">cos</button>
            <button onclick="insert('Math.tan(')">tan</button>
            <button onclick="insert('Math.log10(')">log</button>
            <button onclick="insert('Math.sqrt(')">√</button>
            <button onclick="insert('Math.pow(')">x<sup>y</sup></button>
            <button onclick="insert('Math.exp(')">e<sup>x</sup></button>
            <button onclick="insert('Math.PI')">π</button>
        </div>
    </div>

    <script>
        function insert(value) {
            document.getElementById('display').value += value;
        }

        function clearDisplay() {
            document.getElementById('display').value = '';
        }

        function backspace() {
            let display = document.getElementById('display').value;
            document.getElementById('display').value = display.substring(0, display.length - 1);
        }

        function calculate() {
            try {
                let expression = document.getElementById('display').value;
                expression = expression.replace(/\^/g, '**');
                document.getElementById('display').value = eval(expression);
            } catch (err) {
                alert("Error in the expression");
                clearDisplay();
            }
        }

        function toggleSign() {
            let display = document.getElementById('display').value;
            if (display.startsWith('-')) {
                document.getElementById('display').value = display.substring(1);
            } else {
                document.getElementById('display').value = '-' + display;
            }
        }

        document.addEventListener('keydown', function(event) {
            const key = event.key;

            if (key.match(/[0-9\+\-\*\/\(\)\.\^]/)) {
                insert(key);
            } else if (key === 'Enter') {
                calculate();
            } else if (key === 'Backspace') {
                backspace();
            } else if (key === 'Escape') {
                clearDisplay();
            }
        });
    </script>
</div>