|
Public Class Tester
Public Shared Sub Main Dim result As New System.Text.StringBuilder
Dim testDouble As Double = Math.PI result.Append("Double ").AppendLine(testDouble) testDouble += Math.PI result.Append("+= ").AppendLine(testDouble) testDouble *= Math.PI result.Append("*= ").AppendLine(testDouble) testDouble -= Math.PI result.Append("-= ").AppendLine(testDouble) testDouble /= Math.PI result.Append("/= ").AppendLine(testDouble) testDouble ^= Math.PI result.Append("^= ").AppendLine(testDouble) result.AppendLine()
Dim testInteger As Integer = 17 result.Append("Integer ").AppendLine(testInteger) testInteger \= 2 result.Append("\= 2 ... ").AppendLine(testInteger) testInteger += 1 result.Append("+= 1 ... ").AppendLine(testInteger) testInteger <<= 1 result.Append("<<= 1 ... ").AppendLine(testInteger) testInteger >>= 3 result.Append(">>= 3 ... ").AppendLine(testInteger) result.AppendLine()
Dim testString As String = "Abcdef" result.Append("String ").AppendLine(testString) testString &= "ghi" result.Append("&= ghi ... ").AppendLine(testString) testString += "jkl" result.Append("+= jkl ... ").AppendLine(testString)
Console.WriteLine(result.ToString())
End Sub End Class
|
3.1.2.Using an assignment operator to calculate a power of 2
|
Module Tester
Sub Main() Dim exponent As Integer ' power input by user Dim result As Integer = 2 ' number to raise to a power
exponent = 12
result ^= exponent ' same as result = result ^ exponent Console.WriteLine("result = exponent: {0}", result)
result = 2 ' reset result to 2 result = result ^ exponent Console.WriteLine("result = result exponent: {0}", result)
End Sub
End Module
|
result = result ^ exponent: 4096
|
' Quote from 'Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers 'by Tim Patrick (Author), John Craig (Author) '# Publisher: O'Reilly Media, Inc. (September 21, 2006) '# Language: English '# ISBN-10: 0596101775 '# ISBN-13: 978-0596101770
Public Class Tester
Public Shared Sub Main Console.WriteLine(FindPi(500))
End Sub Private Shared NumberDigits As Integer
Public Shared Function FindPi(ByVal digits As Integer) As String ' ----- Calculate Pi to the specified number of digits, ' based on the formula: ' Pi/4 = arctan(1/2) + arctan(1/3) Dim result As New System.Text.StringBuilder("PI=3.") Dim digitIndex As Integer Dim divFactor As Integer
' ----- Build an array that will hold manual calculations. NumberDigits = digits + 2 Dim targetValue(NumberDigits) As Integer Dim sourceValue(NumberDigits) As Integer
' ---- Perform the calculation. divFactor = 2 ArcTangent(targetValue, sourceValue, divFactor) divFactor = 3 ArcTangent(targetValue, sourceValue, divFactor) ArrayMult(targetValue, 4)
' ----- Return a string version of the calculation. For digitIndex = 1 To NumberDigits - 3 result.Append(Chr(targetValue(digitIndex) + Asc("0"c))) Next digitIndex Return result.ToString End Function
Private Shared Sub ArrayMult(ByRef baseNumber() As Integer, _ ByRef multiplier As Integer) ' ----- Multiply an array number by another number by hand. ' The product remains in the array number. Dim carry As Integer Dim position As Integer Dim holdDigit As Integer
' ----- Multiple each base digit, from right to left. For position = NumberDigits To 0 Step -1 ' ----- If the multiplication went past 9, carry the ' tens value to the next column. holdDigit = (baseNumber(position) * multiplier) + carry carry = holdDigit \ 10 baseNumber(position) = holdDigit Mod 10 Next position End Sub
Private Shared Sub ArrayDivide(ByRef dividend() As Integer, ByRef divisor As Integer) ' ----- Divide an array number by another number by hand. ' The quotient remains in the array number. Dim borrow As Integer Dim position As Integer Dim holdDigit As Integer
' ----- Process division for each digit. For position = 0 To NumberDigits ' ----- If the division can't happen directly, borrow from ' the previous position. holdDigit = dividend(position) + borrow * 10 dividend(position) = holdDigit \ divisor borrow = holdDigit Mod divisor Next position End Sub
Private Shared Sub ArrayAdd(ByRef baseNumber() As Integer, ByRef addend() As Integer) ' ----- Add two array numbers together. ' The sum remains in the first array number. Dim carry As Integer Dim position As Integer Dim holdDigit As Integer
' ----- Add each digit from right to left. For position = NumberDigits To 0 Step -1 ' ----- If the sum goes beyond 9, carry the tens ' value to the next column. holdDigit = baseNumber(position) + addend(position) + carry carry = holdDigit \ 10 baseNumber(position) = holdDigit Mod 10 Next position End Sub
Private Shared Sub ArraySub(ByRef minuend() As Integer, ByRef subtrahend() As Integer) ' ----- Subtract one array number from another. ' The difference remains in the first array number. Dim borrow As Integer Dim position As Integer Dim holdDigit As Integer
' ---- Subtract the digits from right to left. For position = NumberDigits To 0 Step -1 ' ----- If the subtraction would give a negative value ' for a column, we will have to borrow. holdDigit = minuend(position) - subtrahend(position) + 10 borrow = holdDigit \ 10 minuend(position) = holdDigit Mod 10 If (borrow = 0) Then minuend(position - 1) -= 1 Next position End Sub
Private Shared Function ArrayZero(ByRef baseNumber() As Integer) As Boolean ' ----- Report whether an array number is all zero. Dim position As Integer
' ----- Examine each digit. For position = 0 To NumberDigits If (baseNumber(position) <> 0) Then ' ----- The number is nonzero. Return False End If Next position
' ----- The number is zero. Return True End Function
Private Shared Sub ArcTangent(ByRef targetValue() As Integer, _ ByRef sourceValue() As Integer, _ ByVal divFactor As Integer) ' ----- Calculate an arctangent of a fraction, 1/divFactor. ' This routine performs a modified Maclaurin series to ' calculate the arctangent. The base formula is: ' arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - ... ' where -1 < x < 1 (it's 1/divFactor in this case). Dim workingFactor As Integer Dim incremental As Integer
' ----- Figure out the "x" part, 1/divFactor. sourceValue(0) = 1 incremental = 1 workingFactor = divFactor ArrayDivide(sourceValue, workingFactor)
' ----- Add "x" to the total. ArrayAdd(targetValue, sourceValue) Do ' ----- Perform the "- (xy)/y" part. ArrayMult(sourceValue, incremental) workingFactor = divFactor * divFactor ArrayDivide(sourceValue, workingFactor) incremental += 2 workingFactor = incremental ArrayDivide(sourceValue, workingFactor) ArraySub(targetValue, sourceValue)
' ----- Perform the "+ (xy)/y" part. ArrayMult(sourceValue, incremental) workingFactor = divFactor * divFactor ArrayDivide(sourceValue, workingFactor) incremental += 2 workingFactor = incremental ArrayDivide(sourceValue, workingFactor) ArrayAdd(targetValue, sourceValue) Loop Until ArrayZero(sourceValue) End Sub End Class
|
PI=3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211
7067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930
3819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491
4127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151
1609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011
3.1.4.Prime numbers using the Sieve of Eratosthenes
|
Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Collections Public Class Tester Public Shared Sub Main Dim needBreak As Boolean = True
Console.WriteLine("Prime numbers using the ""Sieve of Eratosthenes""")
Dim index As Integer = 1 Dim counter As Integer
Do While (index < (MaxNumber - 1)) index += 1 If (PrimeStorage(index) = True) Then For counter = index * 2 To MaxNumber - 1 Step index PrimeStorage(counter) = False Next counter End If Loop
For counter = 2 To 7999999 If (GetBit(counter) = 1) Then If (counter < 50) Or (counter > 7999800) Then Console.WriteLine(counter) ElseIf (needBreak = True) Then Console.WriteLine("...") needBreak = False End If End If Next counter End Sub Private Const MaxNumber As Integer = 8000000 Private Shared PrimeStorage As New BitArray(MaxNumber, True)
Public Shared Function GetBit(ByVal index As Integer) As Integer If (PrimeStorage(index) = True) Then Return 1 Else Return 0 End Function
End Class
|
Prime numbers using the "Sieve of Eratosthenes
3.2.1.Shortcut assignment operators
|
Module Tester
Sub Main() Dim A As Integer
A = 0 A += 10 Console.WriteLine("A += 10 yields " & A)
A -= 5 Console.WriteLine("A -=5 yields " & A)
A *= 3 Console.WriteLine("A *= 3 yields " & A)
A /= 5 Console.WriteLine("A /= 5 yields " & A)
A ^= 2 Console.WriteLine("A = 2 yields " & A)
End Sub
End Module
|
|
Option Strict On Imports System Module Module1
Sub Main( )
Dim value As Integer = 5 Dim power As Integer = 4
Console.WriteLine("{0} to the {1}th power is {2}", _ value, power, value ^ power)
End Sub
End Module
|
5 to the 4th power is 625
3.2.3.Boolean value with And and Or
|
Option Strict On Imports System Module Module1
Sub Main( )
Dim x As Integer = 5 Dim y As Integer = 7
Dim andValue As Boolean Dim orValue As Boolean Dim xorValue As Boolean Dim notValue As Boolean
andValue = x = 3 And y = 7 orValue = x = 3 Or y = 7 xorValue = x = 3 Xor y = 7 notValue = Not x = 3
Console.WriteLine("x = 3 And y = 7. {0}", andValue) Console.WriteLine("x = 3 Or y = 7. {0}", orValue) Console.WriteLine("x = 3 Xor y = 7. {0}", xorValue) Console.WriteLine("Not x = 3. {0}", notValue)
End Sub 'Main
End Module
|
3.3.1.Logical operator: AndAlso
|
Module Tester
Sub Main() Console.WriteLine("AndAlso" & vbCrLf & vbCrLf & _ "False AndAlso False: " & (False AndAlso False) & _ vbCrLf & "False AndAlso True: " & _ (False AndAlso True) & vbCrLf & _ "True AndAlso False: " & (True AndAlso False) & _ vbCrLf & "True AndAlso True: " & (True AndAlso True))
End Sub
End Module
|
False AndAlso False: False
False AndAlso True: False
True AndAlso False: False
3.3.2.Logical operator: OrElse
|
Module Tester
Sub Main() Console.WriteLine("OrElse" & vbCrLf & vbCrLf & _ "False OrElse False: " & (False OrElse False) & _ vbCrLf & "False OrElse True: " & (False OrElse True) & _ vbCrLf & "True OrElse False: " & (True OrElse False) & _ vbCrLf & "True OrElse True: " & (True OrElse True))
End Sub
End Module
|
False OrElse False: False
3.3.3.Logical operator: And
|
Module Tester
Sub Main() Console.WriteLine("And" & vbCrLf & vbCrLf & _ "False And False: " & (False And False) & vbCrLf & _ "False And True: " & (False And True) & vbCrLf & _ "True And False: " & (True And False) & vbCrLf & _ "True And True: " & (True And True)) End Sub
End Module
|
3.3.4.Logical operator: Or
|
Module Tester
Sub Main() Console.WriteLine("Or" & vbCrLf & _ vbCrLf & "False Or False: " & (False Or False) & _ vbCrLf & "False Or True: " & (False Or True) & _ vbCrLf & "True Or False: " & (True Or False) & _ vbCrLf & "True Or True: " & (True Or True)) End Sub
End Module
|
Module Tester
Sub Main() Console.WriteLine("Xor" & vbCrLf & _ vbCrLf & "False Xor False: " & (False Xor False) & _ vbCrLf & "False Xor True: " & (False Xor True) & _ vbCrLf & "True Xor False: " & (True Xor False) & _ vbCrLf & "True Xor True: " & (True Xor True))
End Sub
End Module
|
3.3.6.Logical operator: Not
|
Module Tester
Sub Main() Console.WriteLine("Not" & vbCrLf & vbCrLf & _ "Not False: " & (Not False) & vbCrLf & "Not True: " & _ (Not True)) End Sub
End Module
|
3.3.7.Use Logic operator Or to link two functions
|
Module LogicTest
Sub Main() Dim x As Integer = 1 Dim y As Integer = 1 If A(x) Or B(y) Then Console.WriteLine("x= " & CStr(x) & ", y = " & CStr(y)) End If If A(x) OrElse B(y) Then Console.WriteLine("x= " & CStr(x) & ", y = " & CStr(y)) End If End Sub
Function A(ByVal v1 As Integer) As Boolean Return True End Function
Function B(ByVal v1 As Integer) As Boolean Return True End Function
End Module
|
3.3.8.Use logic operators in If statement: Not, And, Or
|
Module Module1
Sub Main() Dim A As Boolean = False Dim B As Boolean = True Dim C As Boolean = True
If (Not A) Then Console.WriteLine("Not A") End If
If (B Or C) Then Console.WriteLine("B Or C") End If
If (B And C) Then Console.WriteLine("B And C") End If
End Sub
End Module
|
3.4.1.Use Mod to fix degree
|
Public Class Tester Public Shared Function FixRange(ByVal origValue As Double, _ ByVal rangeMin As Double, ByVal rangeMax As Double) _ As Double
Dim shiftedValue As Double Dim delta As Double
shiftedValue = origValue - rangeMin delta = rangeMax - rangeMin Return (((shiftedValue Mod delta) + delta) Mod delta) + _ rangeMin End Function
Public Shared Sub Main Dim result As New System.Text.StringBuilder Dim formatDegrees As String = "Degrees: {0} Range: {1},{2} Value: {3}" Dim formatRadians As String = "Radians: {0} Range: {1},{2} Value: {3}" Dim degrees As Double Dim radians As Double Dim ranged As Double
' ----- Degrees over the range. degrees = 367.75 ranged = FixRange(degrees, 0, 360) result.AppendLine(String.Format(formatDegrees,degrees, 0, 360, ranged))
' ----- Radians over the range. radians = Math.PI * 3.33 ranged = FixRange(radians, -Math.PI, Math.PI) result.AppendLine(String.Format(formatRadians, _ radians, -Math.PI, Math.PI, ranged))
Console.WriteLine(result.ToString()) End Sub End Class
|
|
Public Structure Diamond Private weight As Single Private price As Decimal Private fWeight As Boolean
Public Sub New(oz As Single, pr As Decimal) weight = oz price = pr fWeight = True End Sub
Public Property ByWeight() As Boolean Get Return fWeight End Get Set fWeight = Value End Set End Property
Public ReadOnly Property Size() As Single Get Return weight End Get End Property
Public Shared Operator > (operand1 As Diamond, operand2 As Diamond) As Boolean If operand1.Price / operand1.Size > operand2.Price / operand2.Size Then Return True Else Return False End If End Operator
Public Shared Operator < (operand1 As Diamond, operand2 As Diamond) As Boolean If operand1.Price / operand1.Size < operand2.Price / operand2.Size Then Return True Else Return False End If End Operator
Public Shared Operator Or (op1 As Diamond, op2 As Diamond) As Diamond If op1.ByWeight And op2.ByWeight If op1 < op2 Then Return op1 Else Return op2 End If Else If op1.ByWeight Then Return op1 ElseIf op2.ByWeight Then Return op2 Else Return Nothing End If End If End Operator
Public Shared Operator IsTrue(Byval op1 As Diamond) As Boolean If op1.ByWeight Then Return True Else Return False End If End Operator
Public Shared Operator IsFalse(ByVal op1 As Diamond) As Boolean If op1.ByWeight Then Return False Else Return True End If End Operator End Structure
Public Module modTest Public Sub Main() Dim a As Diamond = New Diamond(1, .3d) Dim b As Diamond = New Diamond(2, .5d)
a.ByWeight = False b.ByWeight = True
If a Or b Then Console.WRiteLine(a.Size) If a OrElse b Then Console.WriteLine(a.Size)
Console.WriteLine(b < a) End Sub End Module
|
3.6.1.Operator Precedence
|
Module Module1
Sub Main() Dim Expression1 As Double Dim Expression2 As Double Dim Expression3 As Double Dim Expression4 As Double
Expression1 = 5 ^ 2 + 1 * 3 - 4 Expression2 = 5 ^ (2 + 1) * 3 - 4 Expression3 = 5 ^ (2 + 1) * (3 - 4) Expression4 = 5 ^ ((2 + 1) * (3 - 4))
Console.WriteLine(Expression1) Console.WriteLine(Expression2) Console.WriteLine(Expression3) Console.WriteLine(Expression4) End Sub
End Module
|
3.7.1.Using equality and relational operators.
|
Module Tester
Sub Main() Dim number1, number2 As Integer
number1 = 10
number2 = 20
If number1 = number2 Then Console.WriteLine("{0} = {1}", number1, number2) End If
If number1 <> number2 Then Console.WriteLine("{0} <> {1}", number1, number2) End If
If number1 < number2 Then Console.WriteLine("{0} < {1}", number1, number2) End If
If number1 > number2 Then Console.WriteLine("{0} > {1}", number1, number2) End If
If number1 <= number2 Then Console.WriteLine("{0} <= {1}", number1, number2) End If
If number1 >= number2 Then Console.WriteLine("{0} >= {1}", number1, number2) End If
End Sub
End Module
|
3.8.1.String is Like 'regular expressions *'
|
public class Test public Shared Sub Main Dim s As String s = "VB.Net" If s Like "*VB.Net" Then Console.WriteLine("'VB.Net' is Like '*VB.Net'")
End Sub End class
|
'VB.Net' is Like '?B.Net'
3.8.2.String is Like 'regular expressions ?' (question mark)
|
public class Test public Shared Sub Main Dim s As String s = "VB.Net" If s Like "?B.Net" Then Console.WriteLine("'VB.Net' is Like '?B.Net'")
End Sub End class
|
'VB.Net' is Like '?B.Net'
3.9.1.Demonstrate 'is a' relationship
|
Class Tester
Shared Sub Main() Dim point1, point2 As Point Dim circle1, circle2 As Circle
point1 = New Point(30, 50) circle1 = New Circle(120, 89, 2.7)
Console.WriteLine("Point point1: " & point1.ToString() & _ vbCrLf & "Circle circle1: " & circle1.ToString())
point2 = circle1
Console.WriteLine("Circle circle1 (via point2): " & point2.ToString())
circle2 = CType(point2, Circle) ' allowed only via cast
Console.WriteLine("Circle circle1 (via circle2): " & circle2.ToString())
If (TypeOf point1 Is Circle) Then circle2 = CType(point1, Circle) Console.WriteLine("cast successful") Else Console.WriteLine("point1 does not refer to a Circle") End If End Sub
End Class
Public Class Point Private mX, mY As Integer
Public Sub New() End Sub ' New
Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer) End Sub ' New Public Overrides Function ToString() As String Return "[" & mX & ", " & mY & "]" End Function ' ToString
End Class
Public Class Circle Inherits Point
Private mRadius As Double
Public Sub New() End Sub ' New
Public Sub New(ByVal xValue As Integer, _ ByVal yValue As Integer, ByVal radiusValue As Double)
MyBase.New(xValue, yValue) End Sub ' New
Public Overrides Function ToString() As String Return "Center= " & MyBase.ToString() & _ "; Radius = " & mRadius End Function ' ToString
End Class
|
Circle circle1: Center= [0, 0]; Radius = 0
Circle circle1 (via point2): Center= [0, 0]; Radius = 0
Circle circle1 (via circle2): Center= [0, 0]; Radius = 0
point1 does not refer to a Circle
3.9.2.Use Is and As to convert a class to its implenented Interface
|
Imports System
Interface Printable Sub Read( ) Sub Write(ByVal obj As Object)
Property Status( ) As Integer End Interface
Interface Zippable Sub Zip( ) Sub Unzip( ) End Interface
Public Class Document Implements Printable
Public Sub New(ByVal s As String) Console.WriteLine("Creating document with: {0}", s) End Sub
Public Sub Read( ) Implements Printable.Read Console.WriteLine("Implementing the Read Method for Printable") End Sub
Public Sub Write(ByVal o As Object) Implements Printable.Write Console.WriteLine("Implementing the Write Method for Printable") End Sub 'Write
Public Property Status( ) As Integer Implements Printable.Status Get Return Status End Get Set(ByVal Value As Integer) Status = Value End Set End Property Private myStatus As Integer = 0 End Class 'Document
Class Tester Public Sub Run( ) End Sub 'Run
Shared Sub Main( ) Dim doc As New Document("Test Document")
If TypeOf doc Is Printable Then Dim isDoc As Printable = doc isDoc.Read( ) Else Console.WriteLine("Could not cast to Printable") End If
If TypeOf doc Is Zippable Then Dim icDoc As Zippable = doc icDoc.Zip( ) Else Console.WriteLine("Could not cast to Zippable") End If End Sub 'Main
End Class
|
Creating document with: Test Document
Implementing the Read Method for Printable
Could not cast to Zippable
Wyszukiwarka
Podobne podstrony:
kurs vba Statements 4 kolumnykurs vba Operator 3kurs vba Language Basics 1 kolumnykurs vba Data Type 2 kolumnykurs vba Date Time 5 kolumnyPytanie Nr 3, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pitPYTANIE NR 5, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pitPYTANIE NR 1, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pitEXCEL 2003 Kurs VBAPYTANIE NR 8, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pitpytanie NR 2, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pitWniosek który wypełnia operator, Kurs na operatora maszyn budowlanychPYTANIE NR7, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pituPYTANIE NR 6, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pitPYTANIE NR10, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pitPYTANIE NR 4, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pitPYTANIE NR9, Konspekty Instruktorskie, Instruktor kat C+E, KURS NA OPERATORA KOPARKO-ŁADOWARKI (pituwięcej podobnych podstron