Sub EingabeDezimalAusgabeGraycode()
->hier kann die Breite der Gray-Codes eingestellt werden
Dim x As Long, s_Dezimal As String, l_Dezimal As Long
Dim l_2erPotenz As Long, s As String, d_tmp As Long
Dim f() As Integer, diggit As Long
s_Dezimal =
ZahlenEingabe:
s_Dezimal = InputBox( _
Bitte geben Sie eine Zahl ein., _
Umrechnung Dezimalzahl in Gray-Code, _
)
->Eingabe pruefen
If Trim(s_Dezimal) = Then Exit Sub
For x = 1 To Len(s_Dezimal)
s = Mid(s_Dezimal, x, 1)
Select Case s
Case 0 To 9
Case Else
MsgBox (x & Zeichen unzulässig.)
GoTo ZahlenEingabe
End Select
Next
l_Dezimal = s_Dezimal
->Graycode-Breite bestimmen
l_tmp = l_Dezimal
l_2erPotenz = 0
Do
l_2erPotenz = l_2erPotenz + 1
l_tmp = l_tmp \ 2
If l_tmp = 0 Then Exit Do
Loop
->Integerfeld für die Aufnahme der einzelnen diggits des Gray-Code
->diggit 0 ist das Zeichen rechts
ReDim f(0 To l_2erPotenz - 1)
->Bildungsgesetz für jedes Digget
->Diggits werden von rechts nach links betrachtet
->0 ist das erste diggit rechts
For diggit = LBound(f()) To UBound(f())
If ((l_Dezimal + (2 ^ diggit)) Mod (2 ^ (2 + diggit))) < (2 ^ (diggit + 1)) Then
f(diggit) = 0
Else
f(diggit) = 1
End If
Next
->Ergebnis ausgeben (Dezimal / Graycode)
MsgBox ( _
Umwandlung Dezimal <-> Gray-Code & vbLf & vbLf & _
Dezimal : & l_Dezimal & vbLf & _
Gray-Code: & GraycodeAlsString(f()))
End Sub
'**********************************************
Sub GraycodeTablleErstellen()
->hier kann die Breite der Gray-Codes eingestellt werden
Const c_BreiteGraycode As Long = 5
Const c_SpalteZahl = 1
Const c_SpalteGraycode = 2
Dim wb As Workbook, ws As Worksheet
Dim l_zeile As Long, x As Long, n As Long, pos1 As Long, pos0 As Long
->Integerfeld für die Aufnahme der einzelnen diggits
->diggit 0 ist das Zeichen rechts
Dim f(0 To c_BreiteGraycode - 1) As Integer, diggit As Long
->neue Mappe anlegen für Ergebnis
Set wb = Workbooks.Add
Set ws = wb.Worksheets(1)
->Foramtierung des benötigten Bereiches als Text
ws.Range(ws.Cells(1, 1), _
ws.Cells(2 + (2 ^ c_BreiteGraycode), c_SpalteGraycode)).NumberFormat = @
->Zeile für Ausgabe setzen
l_zeile = 3
->für alle Kombinationen - Anfangskombination
For x = 0 To (2 ^ c_BreiteGraycode) - 1
->Bildungsgesetz für jedes Digget
->Diggits werden von rechts nach links betrachtet
->0 ist das erste diggit rechts
For diggit = LBound(f()) To UBound(f())
If ((x + (2 ^ diggit)) Mod (2 ^ (2 + diggit))) < (2 ^ (diggit + 1)) Then
f(diggit) = 0
Else
f(diggit) = 1
End If
Next
->Zeile ausgeben (Dezimal / Graycode)
ws.Cells(l_zeile, c_SpalteZahl).Value = x
ws.Cells(l_zeile, c_SpalteGraycode).Value = GraycodeAlsString(f())
l_zeile = l_zeile + 1
Next
->Überschrift schreiben, Spalten ausrichten
ws.Cells(2, c_SpalteZahl).Value = Dezimal
ws.Cells(2, c_SpalteZahl).Font.Bold = True
ws.Columns(c_SpalteZahl).AutoFit
ws.Columns(c_SpalteZahl).HorizontalAlignment = xlCenter
ws.Cells(2, c_SpalteGraycode).Value = Gray-Code
ws.Cells(2, c_SpalteGraycode).Font.Bold = True
ws.Columns(c_SpalteGraycode).AutoFit
ws.Columns(c_SpalteGraycode).HorizontalAlignment = xlCenter
ws.Cells(1, 1).Value = Gray-Code Breite= & c_BreiteGraycode
ws.Cells(1, 1).Font.Bold = True
Set ws = Nothing: Set wb = Nothing
End Sub
'****************************************************
Function GraycodeAlsString(f() As Integer) As String
Dim x As Long
GraycodeAlsString =
For x = UBound(f()) To LBound(f()) Step -1
GraycodeAlsString = GraycodeAlsString & f(x)
Next
End Function
'**********************************************
Sub GraycodeTablleErstellenMitBerechnungZwischenergebnissen()
->hier kann die Breite der Gray-Codes eingestellt werden
Const c_BreiteGraycode As Long = 5
Const c_SpalteZahl = 1
Const c_SpalteGraycode = 2
->xxx mod yyy < zzz
Const c_Spalte_x = 3
Const c_Spalte_digit = 4
Const c_Spalte_digitWert = 5
Const c_Spalte_xxx = 6
Const c_Spalte_yyy = 7
Const c_Spalte_xxxmodyyy = 8
Const c_Spalte_zzz = 9
Dim wb As Workbook, ws As Worksheet
Dim l_zeile As Long, x As Long, n As Long, pos1 As Long, pos0 As Long
->Integerfeld für die Aufnahme der einzelnen diggits
->diggit 0 ist das Zeichen rechts
Dim f(0 To c_BreiteGraycode - 1) As Integer, diggit As Long
->neue Mappe anlegen für Ergebnis
Set wb = Workbooks.Add
Set ws = wb.Worksheets(1)
->Foramtierung des benötigten Bereiches als Text
ws.Range(ws.Cells(1, 1), _
ws.Cells(2 + (2 ^ c_BreiteGraycode) * c_BreiteGraycode, _
c_Spalte_zzz)).NumberFormat = @
->Zeile für Ausgabe setzen
l_zeile = 3
->für alle Kombinationen - Anfangskombination
For x = 0 To (2 ^ c_BreiteGraycode) - 1
->Bildungsgesetz für jedes Digget
->Diggits werden von rechts nach links betrachtet
->0 ist das erste diggit rechts
For diggit = LBound(f()) To UBound(f())
If ((x + (2 ^ diggit)) Mod (2 ^ (2 + diggit))) < (2 ^ (diggit + 1)) Then
f(diggit) = 0
Else
f(diggit) = 1
End If
->Ausgabe der einzelnen Bestandteile der Berechnungsformel
ws.Cells(l_zeile + diggit, c_Spalte_x).Value = x
ws.Cells(l_zeile + diggit, c_Spalte_digit).Value = diggit
ws.Cells(l_zeile + diggit, c_Spalte_digitWert).Value = f(diggit)
ws.Cells(l_zeile + diggit, c_Spalte_xxx).Value = x + (2 ^ diggit)
ws.Cells(l_zeile + diggit, c_Spalte_yyy).Value = 2 ^ (2 + diggit)
ws.Cells(l_zeile + diggit, c_Spalte_xxxmodyyy).Value = _
(x + (2 ^ diggit)) Mod (2 ^ (2 + diggit))
ws.Cells(l_zeile + diggit, c_Spalte_zzz).Value = (2 ^ (diggit + 1))
Next
->Zeile ausgeben (Dezimal / Graycode)
ws.Cells(l_zeile, c_SpalteZahl).Value = x
ws.Cells(l_zeile, c_SpalteGraycode).Value = GraycodeAlsString(f())
l_zeile = l_zeile + c_BreiteGraycode
Next
->Überschrift schreiben, Spalten ausrichten
ws.Cells(2, c_SpalteZahl).Value = Dezimal
ws.Cells(2, c_SpalteZahl).Font.Bold = True
ws.Columns(c_SpalteZahl).AutoFit
ws.Columns(c_SpalteZahl).HorizontalAlignment = xlCenter
ws.Cells(2, c_SpalteGraycode).Value = Gray-Code
ws.Cells(2, c_SpalteGraycode).Font.Bold = True
ws.Columns(c_SpalteGraycode).AutoFit
ws.Columns(c_SpalteGraycode).HorizontalAlignment = xlCenter
ws.Cells(2, c_Spalte_x).Value = x
ws.Cells(2, c_Spalte_x).Font.Bold = True
ws.Columns(c_Spalte_x).AutoFit
ws.Columns(c_Spalte_x).HorizontalAlignment = xlCenter
ws.Cells(2, c_Spalte_digit).Value = diggit
ws.Cells(2, c_Spalte_digit).Font.Bold = True
ws.Columns(c_Spalte_digit).AutoFit
ws.Columns(c_Spalte_digit).HorizontalAlignment = xlCenter
ws.Cells(2, c_Spalte_digitWert).Value = diggit & vbLf & Wert
ws.Cells(2, c_Spalte_digitWert).Font.Bold = True
ws.Columns(c_Spalte_digitWert).AutoFit
ws.Columns(c_Spalte_digitWert).HorizontalAlignment = xlCenter
ws.Cells(2, c_Spalte_xxx).Value = x+(2^diggit)
ws.Cells(2, c_Spalte_xxx).Font.Bold = True
ws.Columns(c_Spalte_xxx).ColumnWidth = 12
ws.Columns(c_Spalte_xxx).HorizontalAlignment = xlRight
ws.Cells(2, c_Spalte_yyy).Value = 2^(2+diggit)
ws.Cells(2, c_Spalte_yyy).Font.Bold = True
ws.Columns(c_Spalte_yyy).ColumnWidth = 12
ws.Columns(c_Spalte_yyy).HorizontalAlignment = xlRight
ws.Cells(2, c_Spalte_xxxmodyyy).Value = (x+(2^diggit))Mod (2^(2+diggit)))
ws.Cells(2, c_Spalte_xxxmodyyy).WrapText = True
ws.Cells(2, c_Spalte_xxxmodyyy).Font.Bold = True
ws.Columns(c_Spalte_xxxmodyyy).ColumnWidth = 30
ws.Columns(c_Spalte_xxxmodyyy).HorizontalAlignment = xlRight
ws.Cells(2, c_Spalte_zzz).Value = 2^(diggit+1)
ws.Cells(2, c_Spalte_zzz).Font.Bold = True
ws.Columns(c_Spalte_zzz).ColumnWidth = 12
ws.Columns(c_Spalte_zzz).HorizontalAlignment = xlRight
ws.Cells(1, c_Spalte_x).Value = ((x+(2^diggit)) Mod (2^(2+diggit))) < (2^(diggit+1))
ws.Cells(1, c_Spalte_x).Font.Bold = True
ws.Cells(1, c_Spalte_x).HorizontalAlignment = xlLeft
ws.Cells(1, 1).Value = Gray-Code Breite= & c_BreiteGraycode
ws.Cells(1, 1).Font.Bold = True
Set ws = Nothing: Set wb = Nothing
End Sub