lehrkraefte:sbtsnr:python:logic

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
lehrkraefte:sbtsnr:python:logic [2022/09/30 18:06]
Karlheinz Schubert created
lehrkraefte:sbtsnr:python:logic [2022/11/04 18:21] (current)
Olaf Schnürer [... und nun noch etwas rumgesponnen ...]
Line 1: Line 1:
-Hallo+====== Entwicklung eines Programms zur Darstellung von logischen Operationen --> Wahrheitstabellen ====== 
 + 
 + 
 +<code python wahrheitstabelle.py> 
 +""" 
 +Print of Logic tables 
 +""" 
 +import operator 
 + 
 +anzahlVariablen = 2 
 +x1 = False 
 +x2 = False 
 + 
 +for index in range(2**anzahlVariablen): 
 +    print( 
 +        f"{index}  | {x2} {x1}     {x2 and x1}    {x2 or x1}     {x2 ^ x1}" 
 +    ) 
 +    x1 = not x1 
 +    if (index + 1) % 2 == 0: 
 +        x2 = not x2 
 + 
 + 
 +</code> 
 +<hidden Show Code> 
 +<code python wahrheitstabelle-step1.py> 
 +""" 
 +Print of Logic tables 
 +""" 
 +import operator 
 + 
 +anzahlVariablen = 2 
 +x1 = False 
 +x2 = False 
 + 
 +Ueberschrift = " Index |    x2    x1  |  and   or   xor" 
 +print(Ueberschrift) 
 +print("-" * len(Ueberschrift)) 
 +for index in range(2**anzahlVariablen): 
 +    print( 
 +        f"{index}  |{x2} {x1}     {x2 and x1}    {int(x2 or x1)}     {int(x2 ^ x1)}" 
 +    ) 
 +    x1 = not x1 
 +    if (index + 1) % 2 == 0: 
 +        x2 = not x2 
 + 
 +</code> 
 +</hidden> 
 +<code python wahrheitstabelle-step2a.py> 
 +""" 
 +Print of Logic tables 
 +""" 
 +import operator 
 + 
 +anzahlVariablen = 3 
 +x1 = False 
 +x2 = False 
 +x3 = False 
 + 
 +Ueberschrift = " Index |   x3     x2     x1  |  and   or   xor" 
 +print(Ueberschrift) 
 +print("-" * len(Ueberschrift)) 
 +for index in range(2**anzahlVariablen): 
 +    print( 
 +        f"{index:5}  |{x3:5}  {x2:5} {x1:5}     {int(x3 and x2 and x1)}    {int(x3 or x2 or x1)}     {int(x3 ^ x2 ^ x1)}" 
 +    ) 
 +    x1 = not x1 
 +    if (index + 1) % 2 == 0: 
 +        x2 = not x2 
 +    if (index + 1) % 4 == 0: 
 +        x3 = not x3 
 + 
 + 
 +</code> 
 +<code python wahrheitstabelle-step2b.py> 
 +""" 
 +Print of Logic tables 
 +""" 
 +import operator 
 + 
 +anzahlVariablen = 3 
 +x1 = False 
 +x2 = False 
 +x3 = False 
 + 
 +Ueberschrift = "Index  |   x3     x2    x1    |   and       or        xor " 
 +print(Ueberschrift) 
 +print("-" * len(Ueberschrift)) 
 +for index in range(2**anzahlVariablen): 
 +    print( 
 +        f"{index:5}  | {str(x3):5}  {str(x2):5} {str(x1):5}     {str(x3 and x2 and x1):5}    {str(x3 or x2 or x1):5}     {x3 ^ x2 ^ x1}" 
 +    ) 
 +    x1 = not x1 
 +    if (index + 1) % 2 == 0: 
 +        x2 = not x2 
 +    if (index + 1) % 4 == 0: 
 +        x3 = not x3 
 + 
 + 
 +</code> 
 +<code python wahrheitstabelle-step3.py> 
 +""" 
 +Print of Logic tables 
 +""" 
 +import operator 
 + 
 +anzahlVariablen = int(input("Anzahl Variablen [1...9]: ")) 
 +x = [] 
 +for index in range(anzahlVariablen): 
 +    x.append(False) 
 + 
 + 
 +def Oder(): 
 +    Wert = False 
 +    for index in range(anzahlVariablen): 
 +        Wert = Wert or x[index] 
 +    return Wert 
 + 
 + 
 +def Und(): 
 +    Wert = True 
 +    for index in range(anzahlVariablen): 
 +        Wert = Wert and x[index] 
 +    return Wert 
 + 
 + 
 +def ExOder(): 
 +    Wert = False 
 +    for index in range(anzahlVariablen): 
 +        Wert = Wert ^ x[index] 
 +    return Wert 
 + 
 + 
 +def Erhoehen(nr, feld): 
 +    for index in range(1, anzahlVariablen + 1): 
 +        if (nr + 1) % index == 0: 
 +            x[index - 1] = not x[index - 1] 
 + 
 + 
 +UeberschriftxTeil = "" 
 +for index in range(anzahlVariablen): 
 +    UeberschriftxTeil += f"x{index+1} 
 +Ueberschrift = f"    Index    |   {UeberschriftxTeil} |  and   or   xor" 
 +print(Ueberschrift) 
 +print("-" * len(Ueberschrift)) 
 + 
 +for Zeile in range(2**anzahlVariablen): 
 +    # xTeil = [x[xIndex] for xIndex in range(len(x))] 
 +    xTeil = "" 
 +    for index in range(anzahlVariablen, 0, -1): 
 +        xTeil += f"{x[index-1]:2} 
 +    print(f"{Zeile:5}  ({Zeile:02X})  |  {xTeil}  | {Und():3}  {Oder():3}  {ExOder():3}"
 +    Erhoehen(Zeile, x) 
 +</code> 
 + 
 +====== Funktion Erhoehen oben korrigiert ====== 
 + 
 +<code python wahrheitstabelle-step3-verbessert.py> 
 +""" 
 +Truth tables 
 +""" 
 +import operator 
 + 
 +anzahlVariablen = int(input("Anzahl Variablen [1...9]: ")) 
 +x = anzahlVariablen * [False] 
 + 
 +def Oder(): 
 +    Wert = False 
 +    for index in range(anzahlVariablen): 
 +        Wert = Wert or x[index] 
 +    return Wert 
 + 
 +def Und(): 
 +    Wert = True 
 +    for index in range(anzahlVariablen): 
 +        Wert = Wert and x[index] 
 +    return Wert 
 + 
 +def ExOder(): 
 +    Wert = False 
 +    for index in range(anzahlVariablen): 
 +        Wert = Wert ^ x[index] 
 +    return Wert 
 + 
 +def Erhoehe(nr, feld): 
 +    index = anzahlVariablen - 1 
 +    while feld[index] == True: 
 +        x[index] = False 
 +        index -= 1 
 +    if index >= 0: 
 +        x[index] = True 
 + 
 +UeberschriftxTeil = "" 
 +for index in range(anzahlVariablen): 
 +    UeberschriftxTeil += f"x{index+1} 
 +Ueberschrift = f"    Index    |   {UeberschriftxTeil} |  and   or   xor" 
 +print(Ueberschrift) 
 +print("-" * len(Ueberschrift)) 
 + 
 +for Zeile in range(2**anzahlVariablen): 
 +    xTeil = "" 
 +    for index in range(anzahlVariablen): 
 +        xTeil += f"{x[index]:2} 
 +    print(f"{Zeile:5}  ({Zeile:02X})  |  {xTeil}  | {Und():3}  {Oder():3}  {ExOder():3}"
 +    Erhoehe(Zeile, x) 
 +</code> 
 + 
 +====== ... und nun noch etwas rumgesponnen ... ====== 
 + 
 +<code python wahrheitstabelle-step3-per-binaerdarstellung.py> 
 +""" 
 +Truth tables 
 +""" 
 +leerzeichen = 4 # sollte mindestens 4 sein 
 + 
 +# Unicode-Nummern gefunden auf https://unicode-table.com/en/blocks/box-drawing/ 
 +vertikal = '\u2502' # '\uff5c' 
 +horizontal = '\u2500' # '\u23af' 
 +kreuzung = '\u253c' 
 + 
 +anzahlVariablen = int(input("Anzahl Variablen [1...9]: ")) 
 + 
 +UeberschriftVariablenTeil = "".join([f'{f"x{index + 1}":>{leerzeichen}}' for index in range(anzahlVariablen)]) 
 +UeberschriftLogikTeil = f"{'and':>{leerzeichen}}" + f"{'or':>{leerzeichen}}" + f"{'xor':>{leerzeichen}}" 
 +print(UeberschriftVariablenTeil + " " + vertikal + UeberschriftLogikTeil) 
 +print(horizontal * (len(UeberschriftVariablenTeil) + 1) + kreuzung + horizontal * len(UeberschriftLogikTeil)) 
 + 
 +for index in range(2 ** anzahlVariablen): 
 +    binaer = f'{index:0{anzahlVariablen}b}' 
 +    binearMitAbstand = "".join([""] + [f'{z:>{leerzeichen}}' for z in binaer]) 
 +    # # ausführlich: 
 +    # und = "1" if binaer.count("1") == anzahlVariablen else "0" 
 +    # oder = "1" if binaer.count("1") > 0 else "0" 
 +    # xOder = "1" if binaer.count("1") % 2 == 1 else "0" 
 +    # print(binearMitAbstand + " vertikal" + f"{und:>{leerzeichen}}" + f"{oder:>{leerzeichen}}" + f"{xOder:>{leerzeichen}}"
 + 
 +    # kurz: 
 +    print(binearMitAbstand + " " + vertikal + f"{binaer.count('1') == anzahlVariablen:>{leerzeichen}}" + f"{binaer.count('1') > 0:>{leerzeichen}}" + f"{binaer.count('1') % 2 == 1:>{leerzeichen}}"    
 +</code> 
 + 
 + 
 +====== Turtle-Test ====== 
 + 
 +<code python> 
 +from turtle import * 
 +speed(0) 
 +shape("turtle"
 + 
 +n = 11 
 +s = 230 
 + 
 +pencolor("red"
 +pensize(4) 
 +fillcolor("yellow"
 + 
 +a = 300 
 +penup() 
 +goto(0.7*a, 0.5*a) 
 +pendown() 
 +setheading(90) 
 +pendown() 
 + 
 +begin_fill() 
 +for i in range(n): 
 +    forward(s) 
 +    left(180 - 180/n) 
 +end_fill() 
 + 
 +</code>
  
  • lehrkraefte/sbtsnr/python/logic.1664554006.txt.gz
  • Last modified: 2022/09/30 18:06
  • by Karlheinz Schubert