Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Long Division Program
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
peterggmss




PostPosted: Mon Jan 08, 2007 1:26 pm   Post subject: Long Division Program

See Attached

code:
'Long Division, v.1.0, (c) 2006 Peter Browne
'PPPPPP  RRRRRR  BBBBB
'PP   PP RR   RR BB   B
'PPPPPP  RRRRRR  BBBBBB
'PP      RR  RR  BB   BB
'PP      RR   RR BBBBBB
'License: GPL (GNU General Public License), v.2
'http://www.gnu.org/licenses/gpl.txt
   
   
    'Declare the Variables
    Dim Dividend, Divisor, StepOneAnswer, StepTwoAnswer, StepThreeAnswer As Single
    Dim StepCounter As Integer
    Dim FinalAnswer As Double

Function ResetOrOpen()

    'Hide the Divisor and Dividend
    lblDivisor.Visible = False
    lblDividend.Visible = False

    'Show the Text/Input Boxes
    txtDivisor.Visible = True
    txtDividend.Visible = True

    'Enable the user to Start or Clear the Boxes
    cmdStart.Enabled = True
    cmdClear.Enabled = True
    cmdRestart.Enabled = False
   
    'Show the buttons to Start or Clear
    cmdStart.Visible = True
    cmdClear.Visible = True
    cmdRestart.Enabled = False

    'cmdStart.Left = 720
    'cmdStart.Top = 6600
    'cmdClear.Left = 2160
    'cmdClear.Top = 6600

End Function

Private Sub form_load()
   
    ResetOrOpen
   
    cmdNext.Enabled = False
   
End Sub

Function GenerateQuestion()

    'If the Divisor is blank is or 0 then give an error
    If txtDivisor.Text = "" Or txtDivisor.Text = "0" Then
        MsgBox ("You need to enter a Divisor, it cannot be 0.")
    End If

    'If there is no dividend, give an error
    If txtDividend.Text = "" Then
        MsgBox ("You need to enter a Dividend")
    End If

    'Don't let the user click Start or Clear, and hide the buttons
    cmdStart.Visible = False
    cmdClear.Visible = False
    cmdStart.Enabled = False
    cmdClear.Enabled = False
   
    'cmdStart.Top = 0
    'cmdStart.Left = 0
    'cmdClear.Top = 0
    'cmdClear.Left = 0
   
    'Set the Divisor and Dividend to the corresponding variables
    Divisor = txtDivisor.Text
    Dividend = txtDividend.Text

    'We should hide the text/input boxes, but show the labels
    txtDivisor.Visible = False
    txtDividend.Visible = False
    lblDivisor.Visible = True
    lblDividend.Visible = True

    'Show the Divisor and Dividend in the correspoding boxes
    lblDivisor.Caption = Divisor
    lblDividend.Caption = Dividend
   
    StepCounter = 1
   
    cmdNext.Enabled = True

End Function


Private Sub txtDividend_KeyDown(KeyCode As Integer, Shift As Integer) 'When something is entered into txtDividend

    'If the input is not a number, clear it and display an error
    If KeyCode > 57 And KeyCode < 96 Then
        MsgBox ("Numbers Only Please!")
        KeyCode = 0
        txtDividend.Text = ""
    End If
    If KeyCode > 105 Then
        MsgBox ("Numbers Only Please!")
        KeyCode = 0
        txtDividend.Text = ""
    End If

End Sub

Private Sub txtDivisor_KeyDown(KeyCode As Integer, Shift As Integer) 'When something is entered into txtDivisor
   
    'If the input is not a number, clear it and display an error
    If KeyCode > 57 And KeyCode < 96 Then
        MsgBox ("Numbers Only Please!")
        KeyCode = 0
        txtDivisor.Text = ""
    End If
    If KeyCode > 105 Then
        MsgBox ("Numbers Only Please!")
        KeyCode = 0
    txtDivisor.Text = ""
    End If

End Sub

Private Sub cmdStart_Click()

    GenerateQuestion
    cmdRestart.Enabled = True
    cmdRestart.Visible = True

End Sub

Private Sub cmdClear_Click()

    'Let's Clear the Text Boxes when the user clicks Clear
    txtDividend.Text = ""
    txtDivisor.Text = ""

End Sub

Private Sub cmdRestart_Click()

    ResetOrOpen
    lblAnswer.Caption = ""
    lblStepOne1.Caption = ""
    lblStepOne2.Caption = ""
    lblStepTwo1.Caption = ""
    lblStepTwo2.Caption = ""
    lblStepThree1.Caption = ""
    lblStepThree2.Caption = ""
    txtDivisor.Text = ""
    txtDividend.Text = ""

End Sub

Private Sub cmdDone_Click()
   
    'Let's close the program
    Unload frmDivision

End Sub

Private Sub cmdNext_Click()
   
    'When the user clicks on cmdNext, calculate FinalAnswer then proceed to the next Step
    'FinalAnswer = Dividend / Divisor

    'If we're at Step One, then follow do StepOne, and increase the StepCounter
    If StepCounter = 1 Then
       
        StepOne
        StepCounter = StepCounter + 1
        Exit Sub
       
    End If
       
    'Step Two
    If StepCounter = 2 Then
       
        StepTwo
        StepCounter = StepCounter + 1
        Exit Sub
       
    End If
   
    'Step Three
    If StepCounter = 3 Then
       
        StepThree
        StepCounter = StepCounter + 1
        Exit Sub
       
    End If
   
    If StepCounter = 4 Then
       
        StepFour
        Exit Sub
       
    End If

End Sub

Function StepOne()

    'Show the Step One Line
    lineStepOne.Visible = True

    'Divide the Dividend by the Divisor
    'Lets try a different way
    lblStepOne2.Caption = Dividend Mod Divisor
    lblAnswer.Caption = (Dividend - lblStepOne2.Caption) / Divisor
    lblStepOne1.Caption = "-" & Dividend - lblStepOne2.Caption
    StepOneAnswer = Dividend / Divisor

End Function

Public Sub StepTwo()

    If StepOneAnswer = 0 Then
       
        Call Fin
        lblStepTwo1.Caption = "Finished!" 'Show "Done" in the Step Two label
        Exit Sub

    ElseIf Divisor > StepOneAnswer Then
       
        lineStepTwo.Visible = True 'Show the Step Two Line
        lblAnswer.Caption = lblAnswer.Caption & "."
        lblDividend.Caption = Dividend & ".0"
        StepOneAnswer = Val(lblStepOne2.Caption & "0")
        lblStepOne2.Caption = StepOneAnswer
        lblStepTwo1.Caption = "-" & ((StepOneAnswer \ Divisor) * Divisor)
        lblStepTwo2.Caption = StepOneAnswer - ((StepOneAnswer \ Divisor) * Divisor)
        lblAnswer.Caption = lblAnswer.Caption & (StepOneAnswer \ Divisor)
        StepTwoAnswer = Val(lblStepTwo2.Caption)
   
    End If

End Sub

Public Sub StepThree()

    If StepTwoAnswer = 0 Then
       
        Fin
        lblStepThree1.Caption = "Finished!"
        Exit Sub

    ElseIf Divisor > StepTwoAnswer Then
        StepTwoAnswer = Val(lblStepTwo2.Caption & "0")
        lblStepTwo2.Caption = StepTwoAnswer
        lblStepThree1.Caption = "-" & ((StepTwoAnswer \ Divisor) * Divisor)
        lineStepThree.Visible = True
        lblStepThree2.Caption = StepTwoAnswer - ((StepTwoAnswer \ Divisor) * Divisor)
        lblAnswer.Caption = lblAnswer.Caption & (StepTwoAnswer \ Divisor)
        StepThreeAnswer = Val(lblStepThree2.Caption)
       
    End If

End Sub

Public Sub StepFour()

    'If the Answer from Step Three is Zero, then display a Finished Message Box
    'Otherwise, show the answer.
    If StepThreeAnswer = 0 Then
       
        Fin
        Exit Sub
       
    Else
       
        MsgBox ("This question has more than three steps, and I can only calculate the first three steps." & vbNewLine & vbNewLine & "But, I do know the final answer," & vbNewLine & "Which is: " & FinalAnswer)
        Fin
        lblAnswer.Caption = FinalAnswer
       
    End If
   
    'Disable the Next Step button
    cmdNext.Enabled = False
   
End Sub

Function Fin()
       
        'Display a Message Box to tell the user that the question is done, and that they can do another question
        MsgBox ("Finshed!" & vbNewLine & "Click the Restart Button to do another question!")
       
       
End Function



frmDivision.zip
 Description:

Download
 Filename:  frmDivision.zip
 Filesize:  2.84 KB
 Downloaded:  442 Time(s)


Long Division.zip
 Description:

Download
 Filename:  Long Division.zip
 Filesize:  7.77 KB
 Downloaded:  487 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Wed Jan 10, 2007 3:21 pm   Post subject: Re: Long Division Program

nice. has a few glitches but overall nicely done. Too bad it only does 3 steps. the major glitch i found was that if it has more than 3 steps it will say that it can only do 3 steps but the answer is 0 which is obviously not.
cool dude




PostPosted: Wed Jan 10, 2007 3:24 pm   Post subject: Re: Long Division Program

just found out why its showing 0 as the answer. you commented the following line and forgot to take that comment out.

code:

 FinalAnswer = Dividend / Divisor
Silent Avenger




PostPosted: Wed Jan 10, 2007 3:29 pm   Post subject: Re: Long Division Program

Good job, same problem as cool dude has mentioned. I also like how you use indenting to organize your code.
cool dude




PostPosted: Wed Jan 10, 2007 4:21 pm   Post subject: Re: Long Division Program

Silent Avenger @ Wed Jan 10, 2007 4:29 pm wrote:
Good job, same problem as cool dude has mentioned. I also like how you use indenting to organize your code.


Yes and that makes it so much easier to read and debug his code. That is how i pinpointed the problem right away. If someone new to visual basic is reading this take example from the indenting and the good use of variable names.
Silent Avenger




PostPosted: Wed Jan 10, 2007 11:12 pm   Post subject: Re: Long Division Program

Also the use of the comment statements really helps making the code understandable. Although most new people write the code then put in comment statements which I think is harder than putting it in right away.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: