Taschenrechner

Eigene oder Gemeinschaftsprojekte, Klassen, Module, Codeschnipsel, HowTos, ...
Antworten
gripslund
Foriker
Beiträge: 34
Registriert: Sa 23. Jun 2007, 22:24
Wohnort: Hannover
Kontaktdaten:

Taschenrechner

Beitrag von gripslund » Di 18. Mär 2008, 23:26

Ja ja,
es gibt mit 100%-iger Sicherheit bessere Rechner, aber: mühsam ernährt sich das Eichhörnchen!
Habe zu diesem Thema HIER nichts passendes gefunden.
Den Code lege ich bei, aber es geht wahrscheinlich kürzer. Ich muss im Augenblick jede Taste (Button) einzeln mit 'ner Klick-Routine abfragen. Ist es möglich, die Ziffern über einen Index (0-9) abzufragen?

Code: Alles auswählen

' Gambas class file , letzte Aenderung am 18. 03. 2008

  Index AS Integer
  CalcMode AS Integer
  Num1 AS Float
  Num2 AS Float
  Erase AS Integer

PUBLIC SUB Form_Open()
  TextBox1.text = ""
  Index = 0
  CalcMode = 0
  Num1 = 0
  Num2 = 0
END
'---------------------------------------------------------------------------------------------------------------------------
PUBLIC SUB Button0_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 0
  Erase = 0
END

PUBLIC SUB Button1_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 1
  Erase = 0
END

PUBLIC SUB Button2_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 2
  Erase = 0
END

PUBLIC SUB Button3_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 3
  Erase = 0
END

PUBLIC SUB Button4_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 4
  Erase = 0
END

PUBLIC SUB Button5_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 5
  Erase = 0
END

PUBLIC SUB Button6_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 6
  Erase = 0
END

PUBLIC SUB Button7_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 7
  Erase = 0
END

PUBLIC SUB Button8_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 8
  Erase = 0
END

PUBLIC SUB Button9_Click()
  IF Erase = 1 THEN TextBox1.text = ""
  IF Index = 1 THEN
    Index = 2
    TextBox1.text = ""
  END IF
  TextBox1.text = TextBox1.text & 9
  Erase = 0
END
'============================================================================================================================
PUBLIC SUB Button10_Click() ' Komma (Zahlenfeld-Punkt)
  IF Erase = 1 THEN TextBox1.text = ""
  IF InStr(TextBox1.text, ".") = 0 THEN
    TextBox1.text = TextBox1.text & "."
    Erase = 0
  END IF
END

PUBLIC SUB Button11_Click() ' EE -> Expotential
END

PUBLIC SUB Button12_Click() ' BackSpace
  TextBox1.text = Left$(TextBox1.text, Len(TextBox1.text) - 1)
END

PUBLIC SUB Button13_Click() ' Break
  TextBox1.Clear
  Index = 0
  CalcMode = 0
  Num1 = 0
  Num2 = 0
END

PUBLIC SUB Button14_Click() ' Multiplikation
  IF CalcMode = 0 THEN
    CalcMode = 1
    Num1 = Val(TextBox1.text)
    Index = 1
  END IF
END

PUBLIC SUB Button15_Click() ' Division
  IF CalcMode = 0 THEN
    CalcMode = 2
    Num1 = Val(TextBox1.text)
    Index = 1
  END IF
END

PUBLIC SUB Button16_Click() ' Addition
  IF CalcMode = 0 THEN
    CalcMode = 3
    Num1 = Val(TextBox1.text)
    Index = 1
  END IF
END

PUBLIC SUB Button17_Click() ' Subtraktion
  IF CalcMode = 0 THEN
    CalcMode = 4
    Num1 = Val(TextBox1.text)
    Index = 1
  END IF
END

PUBLIC SUB Button18_Click() ' Execute! (=)
  IF Index = 2 THEN Num2 = TextBox1.text
  IF CalcMode = 1 THEN
    Num1 = Num1 * Num2
  ELSE IF CalcMode = 2 THEN
    Num1 = Num1 / Num2
  ELSE IF CalcMode = 3 THEN
    Num1 = Num1 + Num2
  ELSE IF CalcMode = 4 THEN
    Num1 = Num1 - Num2
  ENDIF
  TextBox1.text = Str$(Num1)
  Index = 0
  CalcMode = 0
  Erase = 1
END


' Das ist ein sehr einfacher Taschenrechner, welcher auch nicht besonders elegant programmiert ist. Aber er funktioniert
' wenigstens rudimentär!


Ich hoffe, dass es wenigstens etwas verständlich ist. Weshalb der Index=2 werden soll, weiß ich im Augenblick auch nicht mehr ?! :?

Gute Nacht wünscht

gripslund

gripslund
Foriker
Beiträge: 34
Registriert: Sa 23. Jun 2007, 22:24
Wohnort: Hannover
Kontaktdaten:

Beitrag von gripslund » Do 20. Mär 2008, 22:15

Falls es interessiert, bei WikiBooks gibt es was mit 'nem Zahlenrate-Prg.. Da werden 27 Buttons mit einer einzigen Click-Routine abgefragt. Bestimmt kann ich damit was anfangen! Wenn ich was erreicht habe, dann werde ich es hier vermelden. Wird aber ein paar Tage dauern, bin über Ostern nicht am Rechner.
Frohes Eiersuchen allen!

gripslund

gripslund
Foriker
Beiträge: 34
Registriert: Sa 23. Jun 2007, 22:24
Wohnort: Hannover
Kontaktdaten:

Beitrag von gripslund » Do 27. Mär 2008, 22:37

Ja das ist er wieder!
Also: es funktioniert tatsächlich mit EINER Click-Routine. Im Anhang der bisherige Stand der Dinge. Evtl. kann ja mal jmd. 'nen Kommentar dazu abgeben, was ich anders/besser machen sollte?!

Code: Alles auswählen

' Gambas class file

  b[51] AS Button
  n AS Integer
  c AS Integer
  d AS Integer
  r AS Integer
 
  TextBox1 AS TextBox
  TextBox2 AS TextBox

  Index AS Integer
  CalcMode AS Integer
  Num1 AS Float
  Num2 AS Float
  Erase AS Integer


PUBLIC SUB Form_Open()

  DIM x, y AS Integer

  ME.Text = "Taschenrechner"
  ME.Border = Window.Resizable
  ME.W = 630
  ME.H = 385
  ME.Border = Window.Fixed
  ME.Center

  TextBox1 = NEW TextBox(ME) AS "Text"
  TextBox1.MoveScaled(2, 1, 52, 5)
  TextBox1.Font = Font["Nimbus Mono L,Bold,+1"]

  TextBox2 = NEW TextBox(ME) AS "Text"
  TextBox2.MoveScaled(2, 12, 47, 3)
  TextBox2.Text = "gripslund 2008-box not used yet!"
  TextBox2.Font = Font["Nimbus Mono L,Bold"]

  n = 0
  FOR y = 1 TO 3              ' die Tasten 1 bis 9
    FOR x = 1 TO 3
      INC n
      b[n] = NEW Button(ME) AS "btn"
      b[n].Tag = n
      b[n].Caption = n
      b[n].MoveScaled(53 + x * 6, 25 - y * 4, 5, 3)
      b[n].Font = Font["Bold"]
      b[n].Background = Color.Foreground
      b[n].Foreground = Color.SelectedForeground
      b[n].Text = Str(n)
    NEXT
  NEXT

  n = 0                      ' Taste 0 Null
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(59, 25, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = Color.Foreground
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = Str(n)

  n = 10                     ' das Komma
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(65, 25, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = Color.Foreground
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "."

  n = 11                     ' die Exp-Taste (noch nicht verwendet)
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(71, 25, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = Color.Foreground
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "EE"

  n = 12                     ' BackSpace
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(77, 13, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = &HFF0000&
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "BS"

  n = 13                     ' Break
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(83, 13, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = &HFF0000&
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "BRK"

  n = 14                     ' Multiplikation
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(77, 17, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = Color.Foreground
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "*"

  n = 15                     ' Division   
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(83, 17, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = Color.Foreground
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "/"

  n = 16                     ' Addition
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(77, 21, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = Color.Foreground
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "+"

  n = 17                     ' Subtraktion
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(83, 21, 5, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = Color.Foreground
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "-"

  n = 18                     ' Execute -> "="
    b[n] = NEW Button(ME) AS "btn"
    b[n].Tag = n
    b[n].Caption = n
    b[n].MoveScaled(77, 25, 11, 3)
    b[n].Font = Font["Bold"]
    b[n].Background = Color.Foreground
    b[n].Foreground = Color.SelectedForeground
    b[n].Text = "EXE"

  TextBox1.text = "0"
  Index = 0
  CalcMode = 0
  Num1 = 0
  Num2 = 0
  Erase = 0

END
'---------------------------------------------------------------------------------------------------------------------------
PUBLIC SUB btn_Click()
  r = LAST.Tag
  IF r < 10 THEN
    IF TextBox1.text = "0" THEN TextBox1.text = ""
    IF Erase = 1 THEN TextBox1.text = ""      ' Zuvor EXE gedrückt
    IF Index = 1 THEN                         ' Rechenoperation ausgewählt, neue Eingabe
      Index = 2
      TextBox1.text = ""
    END IF
    TextBox1.text = TextBox1.text & Str(r)
    Erase = 0
  ENDIF
  IF r = 10 THEN
    IF Erase = 1 THEN TextBox1.text = ""
    IF InStr(TextBox1.text, ".") = 0 THEN
      TextBox1.text = TextBox1.text & ","
      Erase = 0
    END IF
  ENDIF
  IF r = 11 THEN
  ENDIF
  IF r = 12 THEN
    TextBox1.text = Left$(TextBox1.text, Len(TextBox1.text) - 1)
  ENDIF
  IF r = 13 THEN
    TextBox1.text = "0"
    Index = 0
    CalcMode = 0
    Num1 = 0
    Num2 = 0
  ENDIF
  IF r = 14 THEN
    IF CalcMode = 0 THEN
      CalcMode = 1
      IF TextBox1.text = "" THEN TextBox1.text = "0"
      Num1 = Val(TextBox1.text)
      Index = 1
    END IF
  ENDIF
  IF r = 15 THEN
    IF CalcMode = 0 THEN
      CalcMode = 2
      IF TextBox1.text = "" THEN TextBox1.text = "0"
      Num1 = Val(TextBox1.text)
      Index = 1
    END IF
  ENDIF
  IF r = 16 THEN
    IF CalcMode = 0 THEN
      CalcMode = 3
      IF TextBox1.text = "" THEN TextBox1.text = "0"
      Num1 = Val(TextBox1.text)
      Index = 1
    END IF
  ENDIF
  IF r = 17 THEN
    IF CalcMode = 0 THEN
      CalcMode = 4
      IF TextBox1.text = "" THEN TextBox1.text = "0"
      Num1 = Val(TextBox1.text)
      Index = 1
    END IF
  ENDIF
  IF r = 18 THEN
    IF Index = 2 THEN
      Num2 = Val(TextBox1.text)
    END IF
    SELECT CASE CalcMode
      CASE 1  ' Multiplikation
        Num1 = Num1 * Num2
      CASE 2  ' Division
        Num1 = Num1 / Num2
      CASE 3  ' Addition
        Num1 = Num1 + Num2
      CASE 4  ' Subtraktion
        Num1 = Num1 - Num2
    END SELECT
    TextBox1.text = Str$(Num1)
    Index = 0
    CalcMode = 0
    Erase = 1
  ENDIF
END
'============================================================================================================================

'19.03.2008
'- Rechenoperation wird jetzt mit SelectCase ermittelt, IfThen zu unübersichtlich
'- gleichzeitige Anzeige von Dec / Hex / Bin

'27.03.2008
'- alles ein wenig umgestrickt


Danke für's Ansehen!

gripslund

gripslund
Foriker
Beiträge: 34
Registriert: Sa 23. Jun 2007, 22:24
Wohnort: Hannover
Kontaktdaten:

Beitrag von gripslund » Mo 14. Apr 2008, 13:29

Hallo,
ich habe den Rechner nun soweit, dass er auch Punkt- vor Strich-Rechnung kann. Die Eingabe erfolgt per Mausklick auf die jew. Tasten in der oberen Zeile, das Ergebnis escheint in der unteren Zeile.
Bevor ich jetzt jedoch wieder den Quelltext hinterlasse: will's überhaupt jmd. wissen? Sonst lass ich's eben und erspare euch meine geistigen Ergüsse ;-)

gripslund

Antworten

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 0 Gäste