2010年3月29日 星期一

大數除法

'大數除法
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a = "1234567890123456788999999"
        Dim b = "12345678901234567890"
        Dim q = 0
        While a.Length > b.Length Or a.Length = b.Length And a >= b
            q = q + 1
            a = bigSub(a, b)
        End While
        MsgBox(q)
        End
    End Sub
    Function bigSub(ByVal n1, ByVal n2)
        Dim a(100)
        Dim b(100)
        Dim d(100)

        Dim i1 = 0
        For i = n1.Length - 1 To 0 Step -1
            a(i1) = Val(n1.Chars(i))
            i1 = i1 + 1
        Next

        Dim i2 = 0
        For i = n2.Length - 1 To 0 Step -1
            b(i2) = Val(n2.Chars(i))
            i2 = i2 + 1
        Next

        Dim imax = IIf(i1 > i2, i1, i2)

        Dim c = 0
        For i = 0 To imax
            If a(i) - c >= b(i) Then
                d(i) = a(i) - b(i) - c
                c = 0
            Else
                d(i) = a(i) - b(i) - c + 10
                c = 1
            End If
        Next

        Dim str = ""
        For i = 0 To imax
            str = d(i) & str
        Next

        While Microsoft.VisualBasic.Left(str, 1) = "0"
            str = Microsoft.VisualBasic.Right(str, str.Length - 1)
        End While
        Return (str)
    End Function
End Class


大數乘法

'大數乘法
Public Class Form1
    '12345*45678=563894910
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a(300)
        Dim b(300)
        Dim c(600)
        Dim aStr = "12345"
        Dim bStr = "45678"
        Dim i
        Dim ia = 0
        For i = aStr.Length - 1 To 0 Step -1
            a(ia) = aStr.Chars(i)
            ia = ia + 1
        Next
        Dim ib = 0
        For i = bStr.Length - 1 To 0 Step -1
            b(ib) = bStr.Chars(i)
            ib = ib + 1
        Next

        Dim j
        For i = 0 To 300
            For j = 0 To 300
                c(i + j) = c(i + j) + Val(a(i)) * Val(b(j))
            Next
        Next
        For i = 0 To 300 - 1
            If c(i) >= 10 Then
                c(i + 1) = c(i + 1) + c(i) \ 10
                c(i) = c(i) Mod 10
            End If
        Next

        Dim str = ""
        For i = 0 To 300
            str = c(i) & str
        Next

        While Microsoft.VisualBasic.Left(str, 1) = "0"
            str = Microsoft.VisualBasic.Right(str, str.ToString.Length - 1)
        End While
        MsgBox(str)
    End Sub
End Class


大數相減

'大數相減
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim n1 = "10000000000000001"
        Dim n2 = "100000000000000000000"
        Dim isNeg = False
        If n1.Length < n2.Length Or n1.Length = n2.Length And Val(Microsoft.VisualBasic.Left(n1, 1)) < Val(Microsoft.VisualBasic.Left(n2, 1)) Then
            isNeg = True
            Dim tem = n1
            n1 = n2
            n2 = tem
        End If

        Dim a(100)
        Dim b(100)
        Dim d(100)

        Dim i1 = 0
        For i = n1.Length - 1 To 0 Step -1
            a(i1) = Val(n1.Chars(i))
            i1 = i1 + 1
        Next

        i1 = 0
        For i = n2.Length - 1 To 0 Step -1
            b(i1) = Val(n2.Chars(i))
            i1 = i1 + 1
        Next

        Dim c = 0
        For i = 0 To 100
            If a(i) - c >= b(i) Then
                d(i) = a(i) - b(i) - c
                c = 0
            Else
                d(i) = a(i) - b(i) - c + 10
                c = 1
            End If
        Next

        Dim str = ""
        For i = 0 To 100
            str = d(i) & str
        Next

        While Microsoft.VisualBasic.Left(str, 1) = "0"
            str = Microsoft.VisualBasic.Right(str, str.Length - 1)
        End While
        If isNeg = True Then str = "-" & str
        MsgBox(str)
        End
    End Sub
End Class

大數相加

'大數相加
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim n1 = "1234567890"
        Dim n2 = "12345678901200000"
        Dim a(100)
        Dim b(100)
        Dim d(100)

        Dim i1 = 0
        For i = n1.Length - 1 To 0 Step -1
            a(i1) = Val(n1.Chars(i))
            i1 = i1 + 1
        Next

        i1 = 0
        For i = n2.Length - 1 To 0 Step -1
            b(i1) = Val(n2.Chars(i))
            i1 = i1 + 1
        Next

        Dim c = 0
        For i = 0 To 100
            d(i) = a(i) + b(i) + c
            If d(i) >= 10 Then
                c = 1
                d(i) = d(i) - 10
            Else
                c = 0
            End If
        Next

        Dim str = ""
        For i = 0 To 100
            str = d(i) & str
        Next

        While Microsoft.VisualBasic.Left(str, 1) = "0"
            str = Microsoft.VisualBasic.Right(str, str.Length - 1)
        End While
        MsgBox(str)
        'MsgBox(str & " -- " & (Val(n1) + Val(n2)) & " -- " & Val(str) - (Val(n1) + Val(n2)))
        End
    End Sub
End Class


2010年3月10日 星期三

累計不連續上網秒數自動關瀏覽器

imageimage
Import s System.Text
Imports System.IO.Ports
Public Class Form1
    Dim proc As System.Diagnostics.Process
    Dim accSec As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer2.Enabled = True
        Timer2.Interval = 1000
        Me.Text = "累計不連續上網秒數自動關瀏覽器"
        Label1.Text = "打開IE瀏覽器後,計時器會自動起動"
    End Sub

2010年3月9日 星期二

程式管理

image

Public Class Form1
    Dim proc As System.Diagnostics.Process
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Interval = 5000
        Timer1.Enabled = True
        Me.Hide()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If CheckBox1.Checked = True Then
            For Each proc In System.Diagnostics.Process.GetProcesses
                If proc.ProcessName Like "*iexplore*" Then proc.Kill()
            Next
        End If
        If CheckBox2.Checked = True Then
            For Each proc In System.Diagnostics.Process.GetProcesses
                If proc.ProcessName Like "*mspaint*" Then proc.Kill()
            Next
        End If
        If CheckBox3.Checked = True Then
            For Each proc In System.Diagnostics.Process.GetProcesses
                If proc.ProcessName Like "*calc*" Then proc.Kill()
            Next
        End If

        If TextBox1.Text <> "" Then
            Dim proc1 = "*" & TextBox1.Text & "*"
            For Each proc In System.Diagnostics.Process.GetProcesses
                If proc.ProcessName Like proc1 Then proc.Kill()
            Next
        End If

        If TextBox2.Text <> "" Then
            Dim proc1 = "*" & TextBox2.Text & "*"
            For Each proc In System.Diagnostics.Process.GetProcesses
                If proc.ProcessName Like proc1 Then proc.Kill()
            Next
        End If

        If TextBox3.Text <> "" Then
            Dim proc1 = "*" & TextBox3.Text & "*"
            For Each proc In System.Diagnostics.Process.GetProcesses
                If proc.ProcessName Like proc1 Then proc.Kill()
            Next
        End If
    End Sub
End Class

lotto

image

Public Class Form1
    Dim cn(42)
    Dim rn(42)
    Dim aM
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i
        ReDim cn(42)
        For i = 1 To 42
            cn(i) = i
        Next
        For i = 1 To 6
            Dim s = Int(Rnd() * 42 + 1)
            Dim t = cn(i)
            cn(i) = cn(s)
            cn(s) = t
        Next

        ReDim Preserve cn(6)
        Array.Sort(cn)

        TextBox1.Text = cn(1)
        TextBox2.Text = cn(2)
        TextBox3.Text = cn(3)
        TextBox4.Text = cn(4)
        TextBox5.Text = cn(5)
        TextBox6.Text = cn(6)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i
        ReDim rn(42)
        For i = 1 To 42
            rn(i) = i
        Next
        For i = 1 To 6
            Dim s = Int(Rnd() * 42 + 1)
            Dim t = rn(i)
            rn(i) = rn(s)
            rn(s) = t
        Next

        ReDim Preserve rn(6)
        Array.Sort(rn)

        TextBox7.Text = rn(1)
        TextBox8.Text = rn(2)
        TextBox9.Text = rn(3)
        TextBox10.Text = rn(4)
        TextBox11.Text = rn(5)
        TextBox12.Text = rn(6)
        '
        Dim c = 0, j
        Dim s1 = ""
        For i = 1 To 6
            For j = 1 To 6
                If cn(i) = rn(j) Then
                    c = c + 1
                    s1 = s1 & cn(i) & ". "
                End If
            Next
        Next
        Label2.Text = "中" & c & "號"
        Label1.Text = "中的號碼:" & s1
        Dim m() = {0, 0, 0, 200, 2000, 20000, 2000000}
        Label5.Text = "獎金:" & Format(m(c), "#,##0")
        aM = aM - 50 + m(c)
        label6.text = "累積輸贏: $" & Format(aM, "#,##0")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim i
        For i = 1 To 100
            Button1_Click(sender, e)
        Next

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        aM = 0
    End Sub
End Class

2010年3月7日 星期日

因數分解 - 副程式、函數、字串應用綜合演練版

image

'因數分解 - 副程式、函數、字串應用綜合演練版
Public Class Form1
    Dim bqn
    Dim a(20)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str2 = ""
        ' Dim qn = 720
        For qn = 1 To 50
            bqn = qn
            factorize(qn)
            str2 = str2 & toexpType(a) & vbNewLine
        Next
        MsgBox(str2)
        End
    End Sub

    Sub factorize(ByVal qn)
        ReDim a(20)
        Dim j = 0
        For k = 2 To qn
            While qn Mod k = 0
                qn = qn / k
                a(j) = k
                j = j + 1
            End While
        Next
        j = j - 1
        ReDim Preserve a(j)
    End Sub

    Function toexpType(ByVal a) As String
        Dim str1 = ""
        Dim p = 0
        Dim n = 1
        For i = 0 To a.Length - 1
            Dim c = a(i)
            If c <> p Then
                If str1 = "" Then
                    str1 = str1 & c & "^"
                Else
                    str1 = str1 & n & "*" & c & "^"
                End If
                n = 1
            Else
                n = n + 1
            End If
            p = c
        Next
        str1 = str1 & n
        str1 = Replace(str1, "^1", "")
        str1 = bqn & "=" & str1
        Return str1
    End Function
End Class

因數的次方表達處理 720=2^4*3^2*5

image 

'因數的次方表達處理2
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a(20)
        Dim qn = 720
        Dim bqn = qn
        Dim j = 0
        For k = 2 To qn
            While qn Mod k = 0
                qn = qn / k
                a(j) = k
                j = j + 1
            End While
        Next
        j = j - 1
        ReDim Preserve a(j)

        Dim str1 = ""
        Dim p = 0
        Dim n = 1
        For i = 0 To a.Length - 1
            Dim c = a(i)
            If c <> p Then
                If str1 = "" Then
                    str1 = str1 & c & "^"
                Else
                    str1 = str1 & n & "*" & c & "^"
                End If
                n = 1
            Else
                n = n + 1
            End If
            p = c
        Next
        str1 = str1 & n
        str1 = Replace(str1, "^1", "")
        str1 = bqn & "=" & str1
        MsgBox(str1)
        End
    End Sub
End Class

因數的次方表達處理

image

'因數的次方表達處理
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a() = {2, 2, 2, 3, 3, 5, 7, 7}
        Dim str1 = ""
        Dim p = 0
        Dim n = 1
        For i = 0 To a.Length - 1
            Dim c = a(i)
            If c <> p Then
                If str1 = "" Then
                    str1 = str1 & c & "^"
                Else
                    str1 = str1 & n & "*" & c & "^"
                End If
                n = 1
            Else
                n = n + 1
            End If
            p = c
        Next
        str1 = str1 & n
        str1 = Replace(str1, "^1", "")
        MsgBox(str1)
    End Sub
End Class

字串處理練習

2010-3-7 下午 12-35-01 

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim a = "22233577"
        Dim str1 = ""
        Dim p = ""
        Dim n = 1
        For i = 1 To a.Length
            Dim c = Mid(a, i, 1)
            If c <> p Then
                If str1 = "" Then
                    str1 = str1 & c & "^ "
                Else
                    str1 = str1 & n & "*" & c & "^"
                End If
                n = 1
            Else
                n = n + 1
            End If
            p = c
        Next
        str1 = str1 & n
        str1 = Replace(str1, "^1", "")
        MsgBox(str1)
    End Sub
End Class