Лабораторная работа: Сложение и вычитание целых неотрицательных чисел в двоичном коде

Министерство образования Республики Таджикистан

Таджикский Технический Университет им. ак. М. С. Осими

кафедра АСОИиУ

Лабораторная работа №2

«Сложение и вычитание целых неотрицательных чисел в двоичном коде»

Выполнил:

Принял:

 

-Душанбе 2009-


Программа «Plusdouble»

 

Option Explicit

Public sel As Integer

Public i, x, j, x0 As Double

Public c As Integer


                                                                        Окно приложения в исходном состоянии.

Private Sub Command1_Click()

fir.Text = ""

sec.Text = ""

res.Text = ""

x = 0

j = 0                                                                   Процедура очистки полей формы

x0 = 0

i = 0

End Sub


Private Sub ext_Click()

End                                                   Процедура выхода

End Sub

Private Sub fir_Change()

If val(fir.Text) = 0 Or val(fir.Text) = 1 Then

lb1.Caption = fir.Text

Exit Sub

Else

i = (Len(fir.Text))

x = 0

j = 0

Do                                                                                            Процедура для перевода

x = x + (val(Mid(fir.Text, i, 1)) * (2 ^ j))                                   введённого значения

i = i – 1                                                                                        в десятичный код

j = j + 1

Loop Until i = 0

lb1.Caption = x

End If

End Sub

Private Sub fir_GotFocus()

sel = 1

End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

On Error GoTo err:

If (KeyCode = vbKeyBack) Then

Select Case sel

Case 1

fir.Text = Left(fir.Text, Len(fir.Text) - 1)

Case 2

sec.Text = Left(sec.Text, Len(sec.Text) - 1)

End Select

Else

End If

Exit Sub

err:

Beep

End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)

Dim val As String

val = Chr(KeyAscii)

If (val >= "0") And (val <= "9") Then

If sel = 1 Then

Select Case val

Case "0"

fir.Text = fir.Text & "0"

Case "1"                                                                                                                    Процедуры

fir.Text = fir.Text & "1"                                                                                            контроля за

End Select                                                                                                                    вводом

ElseIf sel = 2 Then

Select Case val

Case "0"

sec.Text = sec.Text & "0"

Case "1"

sec.Text = sec.Text & "1"

End Select

Else

End If

Else

End If

End Sub

Private Sub Form_Load()

plus.Value = True

c = 1

End Sub



Форма с введёнными значениями

Private Sub res_Change()

If val(res.Text) = 0 Or val(res.Text) = 1 Then

lb3.Caption = res.Text

Exit Sub

Else

i = (Len(res.Text))

x = 0

j = 0

Do

x = x + (val(Mid(res.Text, i, 1)) * (2 ^ j))

i = i - 1

j = j + 1

Loop Until i = 0

lb3.Caption = x

End If

End Sub

Private Sub res_GotFocus()

sel = 3

End Sub

Private Sub run_Click()

Dim d(90) As Integer

If fir.Text = "" Or sec.Text = "" Then

MsgBox "Incorrect input. Please input numbers again", , "=VaMp1r3=™"

Exit Sub

Else

Select Case plus.Value

Case True

x0 = ((CInt(lb1.Caption)) + (CInt(lb2.Caption)))

x = 0

c = 1

Do

d(c) = x0 Mod 2

x = Round((CDbl(x0) / 2) - 0.3, 0)

c = c + 1

x0 = x

Loop Until x = 1

d(c) = x

res.Text = ""

Do

res.Text = res.Text & d(c)

c = c - 1

Loop Until c = 0

Case False

If val(lb2.Caption) > val(lb1.Caption) Then

MsgBox "Так нельзя! Первое слагаемое должно быть больше второго. , "=VaMP1r3=™"

Exit Sub

Else

x0 = ((CInt(lb1.Caption)) - (CInt(lb2.Caption)))

x = 0

c = 1

Do

If x0 = 0 Then Exit Do

d(c) = x0 Mod 2

x = Round((CDbl(x0) / 2) - 0.3, 0)

c = c + 1

x0 = x

Loop Until x = 1

d(c) = x

res.Text = ""

Do

res.Text = res.Text & d(c)

c = c - 1

Loop Until c = 0

End If

End Select

End If

End Sub


Форма с вычисленными данными

Private Sub sec_Change()

If val(sec.Text) = 0 Or val(sec.Text) = 1 Then

lb2.Caption = sec.Text

Exit Sub

Else

i = (Len(sec.Text))

x = 0

j = 0

Do

x = x + (val(Mid(sec.Text, i, 1)) * (2 ^ j))

i = i - 1

j = j + 1

Loop Until i = 0

lb2.Caption = x

End If

End Sub

Private Sub sec_GotFocus()

sel = 2

End Sub