CoolFace
Apppublic

mhassanraza255/Programming_Calculator

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py75 linesDownload Raw Back to root
1import streamlit as st2 3def and_gate(a, b):4    return a & b5 6def or_gate(a, b):7    return a | b8 9def xor_gate(a, b):10    return a ^ b11 12def not_gate(a):13    return ~a14 15def to_hex(value):16    return hex(value)17 18def to_dec(value):19    return value20 21def to_bin(value):22    return bin(value)23 24def keypad_input(label):25    """Generates a keypad for numeric input"""26    digits = [str(i) for i in range(10)] + ["A", "B", "C", "D", "E", "F"]27    user_input = ""28 29    for digit in digits:30        if st.button(digit, key=f"keypad_{label}_{digit}"):31            user_input += digit32 33    if st.button("Clear", key=f"keypad_{label}_clear"):34        user_input = ""35 36    return user_input37 38st.title("Programming Calculator with Logic Gates")39 40operation = st.selectbox("Select Operation", ["AND", "OR", "XOR", "NOT"])41 42a = keypad_input("First Value")43 44if operation != "NOT":45    b = keypad_input("Second Value")46else:47    b = None48 49if st.button("Calculate"):50    try:51        a = int(a, 0)  # Automatically detects HEX, BIN, or DEC52        if b:53            b = int(b, 0)54 55        if operation == "AND":56            result = and_gate(a, b)57        elif operation == "OR":58            result = or_gate(a, b)59        elif operation == "XOR":60            result = xor_gate(a, b)61        elif operation == "NOT":62            result = not_gate(a)63        else:64            st.error("Invalid operation")65            result = None66 67        if result is not None:68            st.success(f"Result in HEX: {to_hex(result)}")69            st.success(f"Result in DEC: {to_dec(result)}")70            st.success(f"Result in BIN: {to_bin(result)}")71 72    except Exception as e:73        st.error(f"Error: {str(e)}")74 75