IRCForumları - IRC ve mIRC Kullanıcılarının Buluşma Noktası

IRCForumları - IRC ve mIRC Kullanıcılarının Buluşma Noktası (https://www.ircforumlari.net/)
-   Visual Basic (https://www.ircforumlari.net/visual-basic/)
-   -   Hesap Makinesi (https://www.ircforumlari.net/visual-basic/384837-hesap-makinesi.html)

nitX 27 Şubat 2011 16:18

Hesap Makinesi
 
Kod:

' Forma eklenmesi gerekenler örnek dosyada bulabilirsiniz
'
'  Calculator.frm
'
'  By Herman Liu
'
'  A calculator. It is built on a sample of VB with the following additional features/
'  major enhancements:
'
'  1. Users can use both mouse and keyboard key to carry out entries for numerals,
'    operating signs (Enter key is made the same as =), etc.
'
'  2. Provide memory functions.
'
'  3. Allow copying calculation result to the clipboard.
'
'----------------------------------------------------------------------------------
'  Memo: frmCalculator.Operator(4).Setfocus after a numeric or operation key.
'  (Because "Enter" key ASCII code cannot always be detected in a Windows
'  environment, to get around, we let focus always stay on the button "=", so that
'  whenever user presses Enter key, it will effectively be as if pressing "=" key).
'----------------------------------------------------------------------------------

Option Explicit
Const Maxdigits = 16        ' After this, scientific notation
Dim Op1 As Variant          ' Prev input operand
Dim Op2 As Variant          ' Further prev input operand
Dim DecimalFlag As Integer  ' Decimal point present yet?
Dim NumOps As Integer      ' Numkey of operands, 0 to 2
Dim LastInput As String    ' Indicate type of last keypress event.
Dim OpFlag As String        ' Indicate pending operation.
Dim PrevReadout As String  ' For restore if "CE"
Dim MemoResult              ' Store result for memo keys
Dim XReadout As String
Dim XOp1 As Variant
Dim XOp2 As Variant
Dim XDecimalFlag As Integer
Dim XNumOps As Integer
Dim XLastInput As String
Dim XOpFlag As String
Dim XCaption As String
Dim XMemoResult



Private Sub Form_Load()
    ResetStatus
End Sub


Sub ResetStatus()
    Readout = Format(0, "0.")
    PrevReadout = Format(0, "0.")
    Op1 = 0
    Op2 = 0
    DecimalFlag = False
    NumOps = 0
    LastInput = "NONE"
    OpFlag = " "
    lblMemoFlag.Caption = " "
    MemoResult = 0
End Sub


Sub RestoreStatus()
    Readout = XReadout
    Op1 = XOp1
    Op2 = XOp2
    DecimalFlag = XDecimalFlag
    NumOps = XNumOps
    LastInput = XLastInput
    OpFlag = XOpFlag
    lblMemoFlag.Caption = XCaption
    MemoResult = XMemoResult
End Sub


Sub MarkStatus()
    XReadout = Readout
    XOp1 = Op1
    XOp2 = Op2
    XDecimalFlag = DecimalFlag
    XNumOps = NumOps
    XLastInput = LastInput
    XOpFlag = OpFlag
    XCaption = lblMemoFlag.Caption
    XMemoResult = MemoResult
End Sub


Private Function MaxReached()
    MaxReached = False
    If Len(Readout) >= Maxdigits Then      ' Not allow further Numkey
        MaxReached = True
    End If
End Function


Function HasDecimal(strToRead As String)
    HasDecimal = False
    Dim i As Integer
    For i = Len(strToRead) To 1 Step -1
        If InStr(i, strToRead, ".") Then
            HasDecimal = True
            Exit For
        End If
    Next
End Function


' Copy the "Label" Caption onto the Clipboard.
Private Sub CopyButton_Click()
    Clipboard.SetText Readout
End Sub


Private Sub Cancel_Click()
    ResetStatus
    Operator(4).SetFocus
End Sub


Private Sub CancelEntry_Click()
    RestoreStatus
    LastInput = "CE"
    Operator(4).SetFocus
End Sub




Private Sub Decimal_Click()
    If HasDecimal(Readout) Then            ' One is enough
        Exit Sub
    End If
    If LastInput = "NUMS" Or LastInput = "DIGI" Then
        If Len(Readout) = Maxdigits Then
            MsgBox "Maximum digits " & Str(Maxdigits - 1) + _
                vbCrLf & "Cannot add another digit"
                Operator(4).SetFocus
            Exit Sub
        End If
    End If
   
    Me.Decimal.SetFocus
    MarkStatus
   
    If LastInput = "NEG" Then
        If Abs(Val(Readout)) <> 0 Then
            Readout = Format(0, "-0.")
        End If
    ElseIf LastInput <> "NUMS" And LastInput <> "DIGI" Then
        Readout = Format(0, "0.")
    End If
   
    DecimalFlag = True
    LastInput = "DIGI"
   
    If MaxReached Then
        MsgBox "Maximum digits " & Str(Maxdigits - 1) + _
          vbCrLf & "Result overflowed"
        RestoreStatus
        Exit Sub
    End If
    Operator(4).SetFocus
End Sub



Private Sub Numkey_Click(Index As Integer)
    If LastInput = "NUMS" Or LastInput = "DIGI" Then
        If MaxReached Then
            MsgBox "Maximum digits " & Str(Maxdigits - 1) + _
              vbCrLf & "Cannot add another digit"
            Operator(4).SetFocus
            Exit Sub
        End If
    End If
   
    Me.NumKey(Index).SetFocus
    MarkStatus
    If LastInput <> "NUMS" And LastInput <> "DIGI" Then
        Readout = Format(0, ".")
        DecimalFlag = False
    End If
    If DecimalFlag Then
        Readout = Readout + NumKey(Index).Caption
    Else
        Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + NumKey(Index).Caption + Format(0, ".")
    End If
    If LastInput = "NEG" Then
        Readout = "-" & Readout
    End If
    LastInput = "NUMS"
 
    Operator(4).SetFocus
End Sub



Private Sub Operator_Click(Index As Integer)
    Me.Operator(Index).SetFocus
    MarkStatus
   
    Dim strTempreadout As String
    strTempreadout = Readout
   
    If LastInput = "NUMS" Or LastInput = "DIGI" Then
        NumOps = NumOps + 1
    End If
   
    Select Case NumOps
        Case 0
            If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
                If Abs(Val(Readout)) <> 0 Then
                    Readout = "-" & Readout
                    LastInput = "NEG"
                End If
            End If
        Case 1
            Op1 = Readout
            If Operator(Index).Caption = "-" And (LastInput <> "NUMS" _
                    And LastInput <> "DIGI") And OpFlag <> "=" Then
                If Abs(Val(Readout)) <> 0 Then
                    Readout = "-"
                    LastInput = "NEG"
                End If
            End If
        Case 2
            Op2 = strTempreadout
            Select Case OpFlag
                Case "+"
                    Op1 = CDbl(Op1) + CDbl(Op2)
                Case "-"
                    Op1 = CDbl(Op1) - CDbl(Op2)
                Case "*"
                    Op1 = CDbl(Op1) * CDbl(Op2)
                Case "/"
                    If Op2 = 0 Then
                      MsgBox "Can't divide by zero", 48, "Calculator"
                      RestoreStatus
                      Exit Sub
                    Else
                      Op1 = CDbl(Op1) / CDbl(Op2)
                    End If
              Case "="
                    Op1 = CDbl(Op2)
            End Select
            Readout = Op1
            NumOps = 1
           
    End Select
    If LastInput <> "NEG" Then
        LastInput = "OPS"
        OpFlag = Operator(Index).Caption
    End If
   
    ' Be consistent, since we always show a decimal point
    If Not HasDecimal(Readout) Then
        If Abs(Val(Readout)) = 0 Then
          Readout = "0."
        Else
          Readout = Readout + "."
        End If
    End If
   
    Operator(4).SetFocus
End Sub




Private Sub MemoKey_Click(Index As Integer)
    MarkStatus
    Select Case Index
      Case 0                    ' Memory Plus
            MemoResult = MemoResult + Val(Readout)
      Case 1                    ' Memory Minus
            MemoResult = MemoResult - Val(Readout)
      Case 2                    ' Memory Recall
            Dim s As String
            s = Str(MemoResult)
            If Not HasDecimal(Str(s)) Then
                s = s + "."
            End If
            Readout = s
      Case 3                    ' Memory Clear
            MemoResult = 0
    End Select
    ' Our system is, if MemoResult is not cleared, show "M"
    If MemoResult <> 0 Then
        lblMemoFlag.Caption = "M"
    Else
        lblMemoFlag.Caption = " "
    End If
   
    LastInput = "OPS"
    NumOps = 1
    Op1 = Readout
    Op2 = 0
    Operator(4).SetFocus
End Sub



' Detect keyboard key
Private Sub Form_KeyPress(keyascii As Integer)
    MarkStatus
    If keyascii < Asc("0") Or keyascii > Asc("9") Then
        If keyascii <> 46 And keyascii <> 43 And _
          keyascii <> 45 And keyascii <> 42 And _
          keyascii <> 47 And keyascii <> 61 And _
          keyascii <> 13 Then
              keyascii = 0
        Else
          Select Case keyascii
            Case 46                  ' "."
              Decimal_Click
            Case 43
              Operator_Click (0)      ' re Property "+"
            Case 45                  ' "-"
              Operator_Click (1)
            Case 42                  ' "*"
              Operator_Click (2)
            Case 47                  ' "/"
              Operator_Click (3)
            Case 61                  ' "="
              Operator_Click (4)
            Case 13                  ' As "=" (if Windows allows Enter)
              Operator_Click (4)
          End Select
        End If
    Else
        Numkey_Click (Val(Chr(keyascii)))
    End If
End Sub


Lui 27 Şubat 2011 21:43

Cevap: Hesap Makinesi
 
Bunun Java ile olanı lazım ;) Elinde varsa paylaşırsan sevinirim..

toXic 28 Şubat 2011 10:27

Cevap: Hesap Makinesi
 
Kod:

import java.awt.BorderLayout;
            import java.awt.Button;
            import java.awt.Color;
            import java.awt.Font;
            import java.awt.Frame;
            import java.awt.GridLayout;
            import java.awt.Label;
            import java.awt.Panel;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import java.awt.event.WindowAdapter;
            import java.awt.event.WindowEvent;
            //Programimiz için gerekli bazi paketleri ‘import’  ettik.
           
            public class HesapMakinesi extends  Frame implements ActionListener {
                Label    display;
                Button    onOff;
                Button[]  tuslar;
                Frame    parent;
                long oncekiSayi = 0;
                char operatie = '=';
                boolean  yeniGirdi=true;
                Font bigFont = new Font("Arial",Font.PLAIN,24);  //Tuslar ve ekranin görünümü için bir 'font'  tanimliyoruz
           
                public static void main(String[]  arg) {
                    new HesapMakinesi().setVisible(true);
                }
           
                public HesapMakinesi() {
                    super("Hesap makinesi"); //Window  (pencere)  basligi
                    olusturGUI();
                      startFlashing();
                }
           
                public void  olusturGUI() {
                    parent=this;
                    display = new Label(" ",Label.RIGHT);
                      display.setBackground(Color.yellow);
                      display.setFont(bigFont);
           
                    onOff = new Button("On");
                      onOff.addActionListener(this);
                      onOff.setFont(bigFont);
           
                    Panel tusPaneli = new  Panel(); //Bu  paneli tuslarimisi yerlestirmek için kullanacagiz.
                      tusPaneli.setLayout(new  GridLayout(4,4));
                    String[] isaretler = {"9","8","7""/",
                                        "6","5","4""*",
                                        "3","2","1""-",
                                        "0","C","=""+" };
                    tuslar = new Button[16];
                    //Önce 'isaretler' adinda bize lazim olan isaretleri  iceren bir String dizi olusturduk.
           
                    for (int b=0;  b<16; b++) {
                          tuslar[b]=new Button(isaretler[b]);
                        tuslar[b].setFont(bigFont);
                          tuslar[b].addActionListener(this);
                          tusPaneli.add(tuslar[b]);
                        }
                    /*Tuslarimizi (on/of hariç) daha önce hazirladigimiz  'tusPaneli'ne for döngüsü yardimi ile tek tek          yerlestiriyor
                      ve  hepsini ActionListener'e bildiriyoruz. ActionListener arayüzü sayesinde  tuslarimizin ne  yapmasi herektigini
                        belirleyebilecegiz*/
           
                    this.add(display,BorderLayout.NORTH);
                    this.add(tusPaneli,BorderLayout.CENTER);
                    this.add(onOff,BorderLayout.SOUTH);
                    /*Ekranimizi (display), küçük tuslarimizi yapistirdigimiz  tusPanel'imizi ve on/off tusumuzu container'e (this)
                        ekliyoruz */
                    this.setSize(250,250);
           
                    this.addWindowListener(new  WindowAdapter() {
                        @Override
                                public void  windowClosing(WindowEvent we){
                                      System.exit(0);
                                      }
                                }
                            );
                    // Bu kod parçasi ise penceremizi X butonu ile  kapatabilmemizi saglar.
           
                }
           
                public void  actionPerformed(ActionEvent evt) {
                    /* ActionListener arayüzünden 'implemente' ettigimiz ve  tuslarimizin hareketlerini denetleyip
                    *  verdigimiz kpmotlara göre ne yapilmasi gerektiginin anlatildigi sinifimiz. */
           
                    if ( onOff==evt.getSource() ) { //örnegin burada anlatilmak istenen onOff tusuna  basildiginda...
                          doOnOff();
                        return;
                        }
                    if (onOff.getLabel().equals("On")) /*onOff butonunun  Label'i "On" ise..
                                                                  *Dikkat! Java'da String ifadenin  karsilastirilmasi == ile degil
                                                                * equals(); yordami ile yapilir. */
                        return;
           
                    char input = evt.getActionCommand().charAt(0); 
                    System.out.println("input:"+input);
           
                    if (input>='0'  & input<='9' ) {
                        if (display.equals("0")  || yeniGirdi)
                            display.setText(input+"");
                        else
                              display.setText(display.getText()+input);
                        return;
                        }
           
                    if (input=='C')  {
                          oncekiSayi=0;
                        operatie='=';
                          yeniGirdi=true;
                          display.setText("0");
                        return;
                        } 
           
                    String tekst="0"+display.getText().trim();
                    long sayi = Long.parseLong(tekst);
           
                      hesapla(input,sayi);
                      display.setText(oncekiSayi+"");
                    }
           
                public void  hesapla(char input, long  sayi) {
                    System.out.println("hesapla:"+input+"|"+sayi);
                    switch (operatie) {
                        case '=' :  oncekiSayi= sayi; break;
                        case '+' :  oncekiSayi+=sayi; break;
                        case '-' :  oncekiSayi-=sayi; break;
                        case '*' :  oncekiSayi*=sayi; break;
                        case '/' :  oncekiSayi/=sayi; break;
                        }         
                      operatie=input;
                    yeniGirdi=true;
                    }
           
           
                public void  doOnOff() {
                    yeniGirdi=true;
                    if ( onOff.getLabel().equals("On") ) {
                          onOff.setLabel("Off");
                          display.setBackground(Color.yellow);
                          display.setText("0");
                        return;
                        }
                      onOff.setLabel("On");
                      display.setText(" ");
                      startFlashing();
                    }
           
            public void startFlashing() {
                Runnable flash = new Runnable() {
                    public void run() {
                        boolean  yellow=true;
                        while(onOff.getLabel().equals("On")) {
                            if  (yellow) display.setBackground(Color.green);
                                    else  display.setBackground(Color.yellow);
                              yellow=!yellow;
                            try { Thread.sleep(600); } catch  (Exception ex) { } 
                            } // end while                 
                        } // end run
                    }; // end Runnable
                  new Thread(flash).start();
                }
           
                }




[Üye Olmadan Linkleri Göremezsiniz. Üye Olmak için TIKLAYIN...]


Tüm Zamanlar GMT +3 Olarak Ayarlanmış. Şuanki Zaman: 03:14.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO
Copyright ©2004 - 2025 IRCForumlari.Net Sparhawk