Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1. Negitifying Binary Number!

FINITE STATE MACHINE:!

CODE:!

!
Module Module1!
!
Sub Main()!
!
!
!
!

Dim input As String!


Dim answer As String!
Dim state As Boolean = False!
input = Console.ReadLine!
For x = 0 To input.Length - 1!
If state = False Then!
S0(input, (input.Length) - x, answer, state)!
Else!
S1(input, (input.Length) - x, answer, state)!
End If!
Next!
Console.WriteLine(answer)!
Console.ReadLine()!

!
End Sub!
!
Sub S0(ByVal input, ByVal position, ByRef answer, ByRef state)!
!
If Mid(input, position, 1) = 0 Then!
!
answer = "0" + answer!
!
ElseIf Mid(input, position, 1) = 1 Then!
!
!
!
!

answer = "1" + answer!


state = True!

End If!

Atharva

End Sub!

!
Sub S1(ByVal input, ByVal position, ByRef answer, ByVal state)!
!
If Mid(input, position, 1) = 0 Then!
!
answer = "1" + answer!
!
ElseIf Mid(input, position, 1) = 1 Then!
!
answer = "0" + answer!
!
End If!
!
End Sub!
!
!
End Module!
!
!
TESTING:!

Input!

Output

00010011

11101101

00010100

11101100

00010010

11101110

!
2. Vending Machine!
!
FINITE STATE MACHINE:!

Atharva

CODE:!

!
Module Module1!
!
Sub Main()!
!
!
!
!

Dim input As Integer!


Dim total As Integer = 0!
Dim state As Integer = 0!
Console.WriteLine("The drink costs 50 pence, enter 10, 20 or 50")!
Do!
input = Console.ReadLine()!
validate(input)!
choosestate(input, total, state)!

!
Loop Until state > 49!
!
!
Console.ReadLine()!
!
End Sub!
!
Sub validate(ByRef input)!
!

Do While input <> 10 And input <> 20 And input <> 50!
Console.WriteLine("Please enter a 10, 20 or 50 coin")!
input = Console.ReadLine()!

!
Loop!
!
End Sub!
!
Sub choosestate(ByVal input, ByRef total, ByRef state)!
!
!

total = total + input!


state = total!

Select Case state!


Case 0!
S0(input, total, state)!
Case 10!
S10(input, total, state)!
Case 20!
S20(input, total, state)!
Case 30!
S30(input, total, state)!
Case 40!
S40(input, total, state)!
Case 50!
Console.WriteLine("You have received your chosen beverage")!
Case Else!
Console.WriteLine("Reject")!
End Select!

Atharva

!
!
!
!
End Sub!
!
!
!

Sub S0(ByVal input, ByRef total, ByRef state)!


Console.WriteLine("Your total is 0 coins")!
End Sub!
Sub S10(ByVal input, ByRef total, ByRef state)!
Console.WriteLine("Your total is 10 coins")!

!
End Sub!
!

Sub S20(ByVal input, ByRef total, ByRef state)!


Console.WriteLine("Your total is 20 coins")!

!
End Sub!
!

Sub S30(ByVal input, ByRef total, ByRef state)!


Console.WriteLine("Your total is 30 coins")!

!
End Sub!
!

Sub S40(ByVal input, ByRef total, ByRef state)!


Console.WriteLine("Your total is 40 coins")!

!
End Sub!
!
End Module!
!!
!
!!
TESTING:!

!!
!
!
!
!

Input!

Output

10,10,10,10,10

You have received


your beverage

20,20,50

Reject

50

You have received


your beverage

Atharva

3. Parity Bit!

FINITE STATE MACHINE:!

!
!
Module Module1!
!
Sub Main()!
!
CODE:!

!
!
!
!
!
!
!

Dim input As String!


Dim parity As Char!
Dim odd As Boolean = False!
input = Console.ReadLine!
For x = 1 To input.Length!
If odd = False Then!
S0(input, x, odd)!
Else!
S1(input, x, odd)!
End If!
Next!
If odd = True Then!
parity = "1"!
Else!
parity = "0"!
End If!
Console.WriteLine(input + parity)!
Console.ReadLine()!

Atharva

End Sub!

!
Sub S0(ByVal input, ByVal position, ByRef odd)!
!
If Mid(input, position, 1) = "0" Then!
odd = False!
ElseIf Mid(input, position, 1) = "1" Then!
odd = True!
End If!

!
End Sub!
!
Sub S1(ByRef input, ByVal position, ByRef odd)!
!
If Mid(input, position, 1) = "0" Then!
odd = True!
ElseIf Mid(input, position, 1) = "1" Then!
odd = False!
End If!

!
!
End Sub!
!
End Module!
!!
!
TESTING!

Input!

Output

1001

10010

0000

00000

1011

10111

Atharva

You might also like