第18講 n進数演算−−−加法
第6話 足し算マクロ例
03154
を実現するプログラム例
Private Sub CommandButton1_Click()

  CommandButton2_Click
  Dim a(100) As Integer, b(100) As Integer, c(100) As Integer, n As Integer
  n = Cells(3, 10)
  Call f(a(), n) 'データ発生
  Call f(b(), n) 'データ発生
  Call g(a(), b(), n) '表示
  Call s(a(), b(), c(), n) '加法と結果表示
    
End Sub

Sub f(a() As Integer, n As Integer)
  
  Dim i As Integer, o As Integer
  Do While 1
    o = Int(10 * Rnd)
    If o > 2 Then Exit Do
  Loop
  For i = 0 To o - 1
    a(i) = Int(n * Rnd)
  Next
  Do While 1
    a(o) = Int(n * Rnd)
    If a(o) > 0 Then
      Exit Do
    End If
  Loop
  a(o + 1) = n
  
End Sub

Sub g(a() As Integer, b() As Integer, n As Integer)
  
  Dim i As Integer, ik1 As Integer, ik2 As Integer, mx As Integer
  
  i = 0
  Do While 1
    If a(i) = n Then
      ik1 = i
      Exit Do
    End If
    i = i + 1
  Loop
  i = 0
  Do While 1
    If b(i) = n Then
      ik2 = i
      Exit Do
    End If
    i = i + 1
  Loop
  mx = ik1
  If mx < ik2 Then mx = ik2
  For i = 0 To ik1 - 1
    Cells(4, 1 + mx - i) = a(i)
  Next
  For i = 0 To ik2 - 1
    Cells(5, 1 + mx - i) = b(i)
  Next

End Sub

Sub s(a() As Integer, b() As Integer, c() As Integer, n As Integer)
  
  Dim i As Integer, ik1 As Integer, ik2 As Integer, mx As Integer
  
  i = 0
  Do While 1
    If a(i) = n Then
      a(i) = 0
      ik1 = i
      Exit Do
    End If
    i = i + 1
  Loop
  i = 0
  Do While 1
    If b(i) = n Then
      b(i) = 0
      ik2 = i
      Exit Do
    End If
    i = i + 1
  Loop
  mx = ik1
  If mx < ik2 Then mx = ik2
  For i = 0 To mx - 1
    c(i) = c(i) + a(i) + b(i)
    c(i + 1) = c(i + 1) + Int(c(i) / n)
    c(i) = c(i) Mod n
  Next
  If c(mx) = 0 Then
    c(mx) = n
  Else
    c(mx + 1) = n
  End If

  For i = 0 To mx
    If c(i) < n Then Cells(6, 1 + mx - i) = c(i)
  Next
  a(ik1) = n
  b(ik2) = n

End Sub

Private Sub CommandButton2_Click()
  
  Rows("4:20000").Select
  Selection.ClearContents
  Cells(1, 1).Select
  
End Sub
参考ダウンロード添付ファイル

当然皆さんの頭には??????と?の嵐ですよね。
次話で詳しく解説しましょう。



第5話へ 第7話へ
004

eclipse c++ 入門
魔方陣 数独で学ぶ VBA 入門
数独のシンプルな解き方・簡単な解法の研究
vc++講義へ
excel 2013 2010 2007 vba入門へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座へ
初心者のための世界で一番わかりやすいVisual Basic入門基礎講座へ
専門用語なしの C言語 C++ 入門(Visual C++ 2010で学ぶ C言語 C++ 入門)
専門用語なしの excel vba マクロ 入門 2013 2010 2007 対応講義 第1部
eclipse java 入門へ
excel 2016 vba 入門へ
小学生からエンジニアまでのRuby入門へ
本サイトトップへ