Prograniramo kalkulator

Super Kalkulator


<!DOCTYPE html>
<html lang="bs">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Super Kalkulator</title>


<style>
    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }

    body {
        min-height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: Arial, sans-serif;
        background: linear-gradient(135deg, #0f172a, #1e3a8a);
    }

    .calculator {
        width: 350px;
        padding: 22px;
        border-radius: 25px;
        background: rgba(255, 255, 255, 0.12);
        box-shadow: 0 20px 50px rgba(0,0,0,0.4);
        backdrop-filter: blur(15px);
    }

    h1 {
        text-align: center;
        color: white;
        margin-bottom: 15px;
        font-size: 25px;
    }

    .display {
        width: 100%;
        height: 80px;
        border: none;
        border-radius: 15px;
        margin-bottom: 18px;
        padding: 15px;
        text-align: right;
        font-size: 35px;
        color: white;
        background: rgba(0,0,0,0.35);
        outline: none;
    }

    .buttons {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 10px;
    }

    button {
        height: 65px;
        border: none;
        border-radius: 16px;
        font-size: 22px;
        font-weight: bold;
        cursor: pointer;
        transition: 0.15s;
        background: rgba(255,255,255,0.9);
    }

    button:hover {
        transform: scale(1.05);
        background: white;
    }

    button:active {
        transform: scale(0.95);
    }

    .operator {
        background: #38bdf8;
        color: white;
    }

    .clear {
        background: #ef4444;
        color: white;
    }

    .equal {
        background: #22c55e;
        color: white;
    }

    .zero {
        grid-column: span 2;
    }

    .footer {
        color: #cbd5e1;
        text-align: center;
        margin-top: 15px;
        font-size: 13px;
    }
</style>
<div class="calculator">

    <h1>⚡ SUPER KALKULATOR</h1>

    <input 
        type="text" 
        id="display" 
        class="display" 
        value="0" 
        readonly
    >

    <div class="buttons">

        <button class="clear" onclick="clearDisplay()">C</button>
        <button onclick="deleteLast()">⌫</button>
        <button onclick="addToDisplay('%')">%</button>
        <button class="operator" onclick="addToDisplay('/')">÷</button>

        <button onclick="addToDisplay('7')">7</button>
        <button onclick="addToDisplay('8')">8</button>
        <button onclick="addToDisplay('9')">9</button>
        <button class="operator" onclick="addToDisplay('*')">×</button>

        <button onclick="addToDisplay('4')">4</button>
        <button onclick="addToDisplay('5')">5</button>
        <button onclick="addToDisplay('6')">6</button>
        <button class="operator" onclick="addToDisplay('-')">−</button>

        <button onclick="addToDisplay('1')">1</button>
        <button onclick="addToDisplay('2')">2</button>
        <button onclick="addToDisplay('3')">3</button>
        <button class="operator" onclick="addToDisplay('+')">+</button>

        <button class="zero" onclick="addToDisplay('0')">0</button>
        <button onclick="addToDisplay('.')">.</button>
        <button class="equal" onclick="calculate()">=</button>

    </div>

    <div class="footer">
        HTML • CSS • JavaScript
    </div>

</div>


<script>

    const display = document.getElementById("display");

    function addToDisplay(value) {

        if (display.value === "0") {
            display.value = value;
        } 
        else {
            display.value += value;
        }
    }


    function clearDisplay() {
        display.value = "0";
    }


    function deleteLast() {

        display.value = display.value.slice(0, -1);

        if (display.value === "") {
            display.value = "0";
        }
    }


    function calculate() {

        try {

            let result = eval(display.value);

            display.value = result;

        } catch {

            display.value = "Greška";
        }
    }


    // Kontrola preko tastature
    document.addEventListener("keydown", function(event) {

        const key = event.key;

        if (
            "0123456789+-*/.%".includes(key)
        ) {
            addToDisplay(key);
        }

        if (key === "Enter") {
            calculate();
        }

        if (key === "Escape") {
            clearDisplay();
        }

        if (key === "Backspace") {
            deleteLast();
        }

    });

</script>

Leave a Comment

Your email address will not be published. Required fields are marked *