From 2439df8b270b4e011c5ff8f845d7a6de5ab8c297 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Wed, 27 Apr 2022 21:07:37 +0000 Subject: [PATCH 1/2] Setting up GitHub Classroom Feedback From 649e0f1e9cda8196ee4d37bbb61bea8a68cb1729 Mon Sep 17 00:00:00 2001 From: lurbanek <90551000+lurbanek@users.noreply.github.com> Date: Wed, 27 Apr 2022 23:10:30 +0200 Subject: [PATCH 2/2] Update kalkulacka.py --- kalkulacka.py | 137 +++++++++++++++++++++----------------------------- 1 file changed, 57 insertions(+), 80 deletions(-) diff --git a/kalkulacka.py b/kalkulacka.py index 1119134..9b11de9 100644 --- a/kalkulacka.py +++ b/kalkulacka.py @@ -1,92 +1,69 @@ -#!/usr/bin/env python3 -# Soubor: kalkulacka.py -# Datum: 28.03.2022 08:31 -# Autor: Marek Nožka, nozka <@t> spseol cz -############################################################################ -import math +import tkinter as tk +from tkinter import Listbox, END +from os.path import basename, splitext -zasobnik = [] +class Application(tk.Tk): + name = basename(splitext(basename(__file__.capitalize()))[0]) + name = "Kalkulacka" + + def __init__(self): + super().__init__(className=self.name) + self.title(self.name) + self.bind("", self.vepsani) + self.var_field = tk.Variable() -def operace(token): - if token.upper() == "Q": - exit() - if token.upper() == "PI": - zasobnik.append(math.pi) - if token == "+": - b = zasobnik.pop() - a = zasobnik.pop() - zasobnik.append(a + b) - if token == "-": - b = zasobnik.pop() - a = zasobnik.pop() - zasobnik.append(a - b) - if token == "*": - b = zasobnik.pop() - a = zasobnik.pop() - zasobnik.append(a * b) - if token == "**": - b = zasobnik.pop() - a = zasobnik.pop() - zasobnik.append(a ** b) - if token == "sin": - a = zasobnik.pop() - zasobnik.append(math.sin(a)) - if token == "cos": - a = zasobnik.pop() - zasobnik.append(math.cos(a)) + self.entry_field = tk.Entry(self, textvariable = self.var_field) + self.entry_field.grid(row = 0) + + self.listbox = Listbox(self) + self.listbox.grid(row = 1) + self.btn_quit = tk.Button(self, text = "Zavřít", command = self.quit) + self.btn_quit.grid() -dva_operandy = {} -dva_operandy["+"] = lambda a, b: a + b -dva_operandy["-"] = lambda a, b: a - b -dva_operandy["*"] = lambda a, b: a * b -dva_operandy["/"] = lambda a, b: a / b -dva_operandy["//"] = lambda a, b: a // b -dva_operandy["**"] = lambda a, b: a ** b + self.cisla = [] + self.priklad = {} + self.priklad["+"] = lambda a, b: a + b + self.priklad["-"] = lambda a, b: a - b + self.priklad["*"] = lambda a, b: a * b + self.priklad["/"] = lambda a, b: a / b + self.priklad["//"] = lambda a, b: a // b + self.priklad["**"] = lambda a, b: a ** b -jeden_operand = {} -jeden_operand["sin"] = math.sin -jeden_operand["cos"] = math.cos -jeden_operand["tg"] = math.tan -jeden_operand["tan"] = math.tan + def vepsani(self, event = None): + raw = self.var_field.get().split() + if len(raw) == 0: + pocet = 1 - -def operace(token): - if token.upper() == "Q": - exit() - if token.upper() == "PI": - zasobnik.append(math.pi) - if token.upper() == "SW": - b = zasobnik.pop() - a = zasobnik.pop() - zasobnik.append(b) - zasobnik.append(a) - if token in dva_operandy.keys(): - if len(zasobnik) >= 2: - b = zasobnik.pop() - a = zasobnik.pop() - zasobnik.append(dva_operandy[token](a, b)) - else: - print("Nemám dost operandů!!!") - if token in jeden_operand.keys(): - if len(zasobnik) >= 1: - a = zasobnik.pop() - zasobnik.append(jeden_operand[token](a)) else: - print("Nemám dost operandů!!!") + pocet = len(raw) + + for i in range(0, pocet): + item = raw[i] + + try: + self.cisla.append(float(item)) + except: + pass + if item in self.priklad.keys(): + if len(self.cisla) >= 2: + b = self.cisla.pop() + a = self.cisla.pop() + self.cisla.append(self.priklad[item](a, b)) + self.listbox.insert(END, self.priklad[item](a, b)) + + self.vysledek() -def zpracuj(radek): - tokeny = radek.split() - for token in tokeny: - try: - zasobnik.append(float(token)) - except ValueError: - operace(token) + def vysledek(self): + self.var_field.set("") + self.listbox.delete(0, END) + for item in self.cisla: + self.listbox.insert(END, item) + def quit(self, event = None): + super().quit() -# čtu vstup -while True: - radek = input(zasobnik.__repr__() + ">>> ") - zpracuj(radek) +app = Application() +app.mainloop()