Schülerarbeit: Taschenrechner

Turtle Graphics

Das Programm von Dilana Kunz und Linus Egli bildet einen Taschenrechner mit den verschiedenen Funktionen nach. Die Simulation geht so weit, dass man den Rechner zuerst oben links einschalten muss. Aus Zeitgründen konnten nicht ganz alle Operationen implementiert werden: Dieses Problem wurde aber kreativ gelöst.

Der Programm-Code

#
# (c) 2014, Linus Egli & Dilana Kunz
#
from gturtle import *
from math import *
from random import *

# Bildschirm leeren:
def leeren():
    setPenColor("white")
    heading(90)
    setPos(-128,150)
    repeat 33:
        forward(228)
        left(90)
        forward(1)
        left(90)
        forward(228)
        right(90)
        forward(1)
        right(90)
        
def leeren2():
    setPenColor("white")
    heading(90)   
    setPos(38,216)
    repeat 10:   
        forward(49)
        left(90)
        forward(1)
        left(90)
        forward(49)
        right(90)
        forward(1)
        right(90)
        
zahl1=0
zahl0=0
operator=0
mass=0
zahl3=0
anzahl=0
q="off"

def error():
    global zahl1
    global zahl0
    if zahl1>=100000000000000 or zahl0>=100000000000000:
        leeren()
        setPenColor("red")
        zahl1="ERROR"
        setPos(-55,175)
        label(str(zahl1))
    if zahl1<=-100000000000000 or zahl0<=-100000000000000:
        leeren()
        setPenColor("red")
        zahl1="ERROR"
        setPos(-55,175)
        label(str(zahl1))    
    setPos(-125,175)

# Operationen:
def onClick(x,y):
    global zahl1
    global zahl0
    global operator
    global mass
    global zahl3
    global anzahl
    global q
    if x>-130 and y>80 and x<-80 and y<110:
        if q=="off":
            q="on"
            setPenColor("black")
            setPos(-125,175)
            label(str(0))
        elif q=="on":
            q="off"
    if q=="on":     
        if x>50 and y>80 and x<=100 and y<=110:
            ziffer="affe"
            zahl1=0
            zahl0=0
            zahl3=0
            anzahl=0
            leeren()
            setPenColor("black")
            setPos(-125,175)
            label(str(0))
        elif x>=-130 and y>=-240 and x<=-80 and y<=-210:
            ziffer=0
        elif x>=-130 and y>=-200 and x<=-80 and y<=-170:
            ziffer=1
        elif x>=-70 and y>-200 and x<=-20 and y<=-170:
            ziffer=2
        elif x>=-10 and y>-200 and x<=40 and y<=-170:
            ziffer=3
        elif x>=-130 and y>-160 and x<=-80 and y<=-130:
            ziffer=4
        elif x>=-70 and y>-160 and x<=-20 and y<=-130:
            ziffer=5
        elif x>=-10 and y>-160 and x<=40 and y<=-130:
            ziffer=6
        elif x>=-130 and y>-120 and x<=-80 and y<=-90:
            ziffer=7
        elif x>=-70 and y>-120 and x<=-20 and y<=-90:
            ziffer=8
        elif x>=-10 and y>-120 and x<=40 and y<=-90:
            ziffer=9
        elif x>=-130 and y>=-40 and x<=-80 and y<=0:
            ziffer=pi          
        elif x>=-130 and y>=-80 and x<=-80 and y<=-50:
            ziffer=e     
        else:
            ziffer="affe"
        if ziffer !="affe":
            zahl1=zahl1*10+ziffer
            leeren()
            setPenColor("black")
            setPos(-125,175)
            label(str(zahl1))
        # +    
        if x>=50 and y>=-184 and x<=100 and y<=-138:
            zahl0=zahl1
            zahl1=0
            anzahl=0 
            operator="+"         
        # -    
        if x>=50 and y>=-128 and x<=100 and y<=-82:
            zahl0=zahl1
            zahl1=0
            anzahl=0
            operator="-"
        # *
        if x>=50 and y>=-72 and x<=100 and y<=-26:
            zahl0=zahl1
            zahl1=0
            anzahl=0
            operator="*"	
        # /    
        if x>=50 and y>=-16 and x<=100 and y<=20:
            zahl0=zahl1
            zahl1=0
            anzahl=0
            operator="/"	
        # x^y
        if x>=-10 and y>=-40 and x<=40 and y<=-10:
            zahl0=zahl1
            zahl1=0          
            operator="x^y"
        # sqrt
        if x>=-70 and y>=-40 and x<=-20 and y<=-10:
            if zahl1<0:
                leeren()
                setPenColor("red")
                zahl1=0
                setPos(-55,175)
                label("ERROR")
            else:
                zahl1=sqrt(zahl1)
                leeren()
                setPenColor("black")
                setPos(-125,175)  
                label(str(zahl1))
        # =       
        if x>=50 and y>=-240 and x<=100 and y<=-194:
            if operator=="+":
                leeren()
                setPenColor("black")
                if anzahl!=0:
                    zahl0=zahl3
                    zahl1=zahl0+zahl1
                else:
                    zahl1=zahl0+zahl1
                    zahl3=zahl1-zahl0
                error()
                label(str(zahl1))
                anzahl+=1
            if operator=="-":
                leeren()
                setPenColor("black")        
                if anzahl!=0:
                    zahl0=zahl3
                    zahl1=zahl1-zahl0
                else:
                    zahl1=zahl0-zahl1
                zahl3=zahl0-zahl1
                error()
                label(str(zahl1))
                anzahl+=1
            if operator=="*":
                if zahl1==0 or zahl0==0:
                    leeren()
                    setPos(-125,175)
                    setPenColor("black")
                    label(str(0))
                else:
                    leeren()
                    setPos(-125,175)
                    setPenColor("black")
                    if anzahl!=0:
                        zahl0=zahl3
                        zahl1=zahl0*zahl1
                    else:
                        zahl1=zahl0*zahl1
                        zahl3=zahl1/zahl0
                    error()
                    label(str(zahl1))
                    anzahl+=1
            if operator=="/":
                leeren()
                setPos(-125,175)
                setPenColor("black")
                if zahl1==0:
                    leeren()
                    setPenColor("red")
                    setPos(-55,175)
                    label("ERROR")                        
                else:
                    if anzahl!=0:
                        zahl0=zahl3
                        zahl1=zahl1/zahl0
                    else:
                        zahl1=zahl0/zahl1
                        zahl3=zahl0/zahl1
                    error()
                    label(str(zahl1))
                    anzahl+=1
            if operator=="x^y":
                leeren()
                setPos(-125,175)
                setPenColor("black")
                zahl1=zahl0**zahl1
                error()
                label(str(zahl1))
        # +/-
        if x>=-10 and y>=-240 and x<=40 and y<=-210:
            zahl1*=-1
            leeren()
            setPenColor("black")
            setPos(-125,175)
            label(str(zahl1))    
        # Grad oder Bogenmass
        if x>=-70 and y>=80 and x<=-20 and y<=110:
            leeren2()
            setPenColor("black")
            setPos(36,216)
            label(str("DEG"))
            mass="grad"
        if x>=-10 and y>=80 and x<=40 and y<=110:
            leeren2()
            setPenColor("black")
            setPos(38,216)
            label(str("RAD"))
            mass="radiend"           
        if mass=="grad" or mass==0:
            #sin
            if x>=-130 and y>=10 and x<=-80 and y<=30:
                zahl1=zahl1*(pi/180)
                zahl1=sin(zahl1)
                leeren()
                setPenColor("black")
                setPos(-125,175)
                label(str(round((zahl1),15)))        
            # cos
            if x>=-70 and y>=10 and x<=-20 and y<=30:
                zahl1=zahl1*(pi/180)
                zahl1=cos(zahl1)
                leeren()
                setPenColor("black")
                setPos(-125,175)
                label(str(round((zahl1),15)))
            # tan
            if x>=-10 and y>=10 and x<=40 and y<=30:
                zahl1=zahl1*(pi/180)
                zahl1=tan(zahl1)
                leeren()
                setPenColor("black")
                setPos(-125,175)
                label(str(round((zahl1),15)))   
        if mass=="radiend":
            #sin
            if x>=-130 and y>=10 and x<=-80 and y<=30:
                zahl1=sin(zahl1)
                leeren()
                setPenColor("black")
                setPos(-125,175)
                label(str(zahl1)) 
            # cos
            if x>=-70 and y>=10 and x<=-20 and y<=30:
                zahl1=cos(zahl1)
                leeren()
                setPenColor("black")
                setPos(-125,175)
                label(str(zahl1))
            # tan
            if x>=-10 and y>=10 and x<=40 and y<=30:
                zahl1=tan(zahl1)
                print zahl1
                leeren()
                setPenColor("black")
                setPos(-125,175)
                label(str(zahl1))
        # log
        if x>=-130 and y>=40 and x<=-80 and y<=70:
            if zahl1==0:
                leeren()
                setPenColor("red")
                zahl1=0
                setPos(-55,175)
                label("ERROR")
            else:
                zahl1=log10(zahl1)
                leeren()
                setPenColor("black")
                setPos(-125,175)
                label(str(zahl1))
        # ln
        if x>=-70 and y>=40 and x<=-20 and y<=70:
            if zahl1==0:
                leeren()
                setPenColor("red")
                zahl1=0
                setPos(-55,175)
                label("ERROR")
            else:
                zahl1=log(zahl1)
                leeren()
                setPenColor("black")
                setPos(-125,175)
                label(str(zahl1))
        error()
        #farben    
        if x>=-70 and x<=-20 and y>=-240 and y<=-210 or x>=-70 and \
           y>=-80 and x<=-20 and y<=-50 or x>=-10 and y>=-80 and \
           x<=40 and y<=-50 or x>=-10 and y>=40 and x<=40 and y<=70 or \
           x>=50 and y>=40 and x<=100 and y<=70:
            zahl=randint(0,19)
            farbe=colorlist[zahl]
            setFillColor(farbe)
            setPos(-144,-200)
            fill()
            leeren()
            setPenColor(farbe)
            setPos(-100,175)
            label("We're so sorry :-)")
            
makeTurtle(mouseHit=onClick)
hideTurtle()
setPenColor("black")
setLineWidth(5)

# Taschenrechner zeichnen (Breite: 305, Länge: 550)
setPos(-150,-275)
penUp()
forward(45)
penDown()
repeat 2:
    forward(460)
    setLineWidth(4)
    repeat 4500:
        forward(0.01)
        right(0.02) 
    setLineWidth(5)    
    forward(215)
    setLineWidth(4)
    repeat 4500:
        forward(0.01)
        right(0.02)
        
# Taste definieren:
setLineWidth(1)
def taste(x,y):
    heading(90)
    penUp()
    forward(3)
    penDown()    
    repeat 2:
        forward(x-3)
        repeat 3:
            forward(1)
            right(30)
        forward(y-6)
        repeat 3:
            forward(1)
            right(30)

# Tasten zeichnen (Breite: 50, Höhe: 30)
setPos(-130,-210)
z=60
a=40
repeat 7:
    repeat 2:
        taste(50,30)
        penUp()
        back(3)
        heading(90)
        forward(z)
        heading(0)
        penDown()
    taste(50,30)
    penUp()
    setPos(-127,-210)
    back(3)
    heading(0)
    forward(a)
    a+=40
    penDown()
repeat 2:
    repeat 3:
        taste(50,30)
        penUp()
        back(3)
        heading(90)
        forward(z)
        heading(0)
        penDown()
    taste(50,30)
    penUp()
    setPos(-127,-210)
    back(3)
    heading(0)
    forward(a)
    a+=40
    penDown()

# Grundoperations-Tasten zeichnen (Breite: 50, Höhe: 46)
setPos(50,-194)
repeat 5:
    taste(50,46)
    penUp()
    heading(0)
    forward(56)
    left(90)
    forward(3)
    heading(0)
    penDown()

# Tasten beschriften
penUp()
setPos(-110,85)
setFillColor("red")
fill()
labellist=[
    (-110,-234,"0"),(-45,-234,"."),(3,-234,"+/-"),(-110,-194,"1"),
    (-49,-194,"2"),(10,-194,"3"),(-110,-154,"4"),(-49,-154,"5"),
    (10,-154,"6"),(-110,-114,"7"),(-49,-114,"8"),(10,-114,"9"),
    (-110,-74,"e"),(-49,-72,"("),(15,-72,")"),(-112,-34,"pi"),
    (-64,-34,"sqrt"),(0,-34,"x^y"),(-120,6,"sin"),(-60,6,"cos"),
    (2,6,"tan"),(-120,46,"log"),(-55,46,"ln"),(12,47,"x"),(-110,86,""),
    (-63,86,"Deg"),(-4,86,"Rad"),(70,-225,"="),(70,-169,"+"),
    (75,-113,"-"),(72,-62,"*"),(73,0,"/"),(60,47,"f(x)"),(70,86,"C"),
    (-120,86,"ON")
  ]
colorlist=[
    "yellow", "gold", "orange", "maroon", "violet", 
    "pink", "magenta", "purple", "navy", "blue", 
    "sky blue", "cyan", "turquoise", "lightgreen", 
    "green", "darkgreen", "chocolate", "brown", 
    "red", "gray"
  ]
def labels():
    for w,q,text in labellist:
        moveTo(w,q)
        label(text)
labels()
heading(0)

# Bildschirm zeichnen (Breite: 265, Höhe: 145)
setPos(-130, 110)
penUp()
forward(45)
penDown()
repeat 2:
    forward (55)
    repeat 4500:
        forward(0.01)
        right(0.02)
    forward(175)
    repeat 4500:
        forward(0.01)
        right(0.02)
setPos(-146,-200)
setFillColor("gray")
fill()