I'm having trouble with my binary to decimal converter - vb.net

When I put in a binary number it gives me more than one result in messagebox.show.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String
Dim a, b, c As Long
a = CLng(Val(TxtBoxInput.Text))
s = TxtBoxInput.Text
TxtBoxInput.Clear()
For i = 1 To Len(s) Step 1
b = CLng(Mid(s, i, 1))
c = Len(s) - i
b = CLng(Val(b) * (2 ^ c))
TxtBoxInput.Text = CStr(MessageBox.Show(CStr(Val(TxtBoxInput.Text) + b)))
Next i
End Sub

You would use the MsgBox Method. It is uncertain from your example where the result is that you are wanting to show.
MsgBox("Information Here", MsgBoxStyle.Information, "MyTitle")
Edit based on Comments
Put your MessageBox outside of your loop something like this:
Dim s, temp As String
Dim a, b, c As Long
a = CLng(Val(TxtBoxInput.Text))
s = TxtBoxInput.Text
TxtBoxInput.Clear()
For i = 1 To Len(s) Step 1
b = CLng(Mid(s, i, 1))
c = Len(s) - i
b = CLng(Val(b) * (2 ^ c))
temp = CStr(Val(temp) + b)
Next i
MessageBox.Show(temp)

Dim s As String
Dim a, b, c As Long
a = CLng(Val(TxtBoxInput.Text))
s = TxtBoxInput.Text
TxtBoxInput.Clear()
For i = 1 To Len(s) Step 1
b = CLng(Mid(s, i, 1))
c = Len(s) - i
b = CLng(Val(b) * (2 ^ c))
TxtBoxInput.Text = CStr(Val(TxtBoxInput.Text) + b)
Next i
MSGBOX(TXTBOXINPUT)

Related

Items doesn't display in list box when I run the program

I was doing a program about Bisection Method. I haven't encountered any errors while doing the program, but when I run it, input the data, and press the compute button, no answer is displayed, but a scroll bar appears and freezes the program.
I don't know what is wrong but I guess it has something to do with logical error.
Public Class Form1
Dim a As Double
Dim b As Double
Dim c As Double
Dim fa As Double
Dim fb As Double
Dim fc As Double
Dim err As Double
Dim n As Integer = 1
Dim x As Double
Dim diserr As String
Dim fas As String
Dim fbs As String
Dim fcs As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = TextBox1.Text
b = TextBox2.Text
err = TextBox3.Text
fa = a ^ 3 - a - 3
fb = b ^ 3 - b - 3
c = (a + b) / 2
fc = c ^ 3 - c - 3
x = Math.Abs(a - b)
Do While x > err
fa = a ^ 3 - a - 3
fb = b ^ 3 - b - 3
c = (a + b) / 2
fc = c ^ 3 - c - 3
x = Math.Abs(a - b)
If err < x Then
diserr = "No"
Else
diserr = "Yes"
End If
If fa > 0 Then
fas = "+"
Else
fas = "-"
End If
If fb > 0 Then
fbs = "+"
Else
fbs = "-"
End If
If fc > 0 Then
fcs = "+"
Else
fcs = "-"
End If
If diserr = "Yes" Then
fcs = Str(x)
End If
ListBox1.Items.Add(Str(n))
ListBox2.Items.Add(Str(a))
ListBox3.Items.Add(Str(b))
ListBox4.Items.Add(Str(c))
ListBox5.Items.Add(Str(x))
ListBox6.Items.Add(diserr)
ListBox7.Items.Add(fas)
ListBox8.Items.Add(fbs)
ListBox9.Items.Add(fcs)
n = n + 1
If fas = "-" And fcs = "+" Then
b = c
End If
If fbs = "-" And fcs = "+" Then
a = b
b = c
End If
Loop
End Sub
End Class
You need to consider the cases where fcs is "-".
If I'm not misunderstanding the method you can just replace the last two ifs with
If fas <> fcs Then
b = c
Else
a = b
b = c
End If

Increment decimal place by 0.05 in Word VBA

I'll start off by saying i have jut started teaching myself VBA about a week ago, so I may not be asking the right question, but...
I am attempting to write a loop in Word VBA that will increment a number calculated partially from text retrieved from bookmarks. I want it to round up to the nearest .05, so .87 becomes .90 and .21 becomes .25.
The module that I have written follows:
A = ActiveDocument.Bookmarks("SRebateIncome").Range.Text
B = ActiveDocument.Bookmarks("RebateDefault").Range.Text
C = ((A - 6000) * 0.15)
D = B - C
E = B + D
F = (18200 + ((445 + E) / 0.19)) + 1
G = (0.19 * 18200) + 445 + E + (37000 * (0.015 + 0.325 - 0.19))
H = (G / (0.015 + 0.325)) + 1
I = ActiveDocument.Bookmarks("TRebateIncome").Range.Text
If F < 37000 = True Then
J = (0.125 * (I - F))
Else
J = (0.125 * (I - H))
End If
K = E - J
K = Format(Round(K, 2), "###,##0.00")
'round K up to the nearest .00 or .05
If K <> "###,###.#0" = False or K <> "###,###.#5") = False Then
Do
K = K + 0.01
Loop Until K = "###,###.#0" = True or K <> "###,###.#5") = True
End If
Set RebateOutput = ActiveDocument.Bookmarks("RebateOutput").Range
RebateOutput.Text = K
Now assuming that the value input for bookmarks "SRebateIncome", "RebateDefault" and "TRebateIncome" are 10175, 1602 and 43046 respectively, I expected the output to be 1460.80, but instead "K" is returned as 1460.78.
At this stage I don't know anything about using Excel within word (except copy/paste a spreadsheet into a document and I don't want to do that with this).
Any help would be appreciated
Thanks!
You can do it with an excel object and the Ceiling function
Option Explicit
Sub RoundText()
Dim dblSRebateIncome As Double
Dim dblRebateDefault As Double
Dim dblTRebateIncome As Double
Dim dblFinal As Double
Dim rngOutput As Range
Dim oExcel As Object
' Load the variables
Set oExcel = CreateObject("Excel.Application")
Set rngOutput = ActiveDocument.Bookmarks("RebateOutput").Range
dblSRebateIncome = CDbl(ActiveDocument.Bookmarks("SRebateIncome").Range.Text)
dblRebateDefault = CDbl(ActiveDocument.Bookmarks("RebateDefault").Range.Text)
dblSRebateIncome = CDbl(ActiveDocument.Bookmarks("TRebateIncome").Range.Text)
dblFinal = GetCalculatedValue(dblSRebateIncome, dblRebateDefault, dblTRebateIncome)
dblFinal = oExcel.worksheetfunction.Ceiling(dblFinal, 0.05)
rngOutput.Text = Format$(dblFinal, "###,##0.00")
End Sub
Function GetCalculatedValue(ByVal dblSIncome As Double, _
ByVal dblDefault As Double, _
ByVal dblTIncome) As Double
' Declare all the intermediate variables.
Dim c As Double, d As Double, e As Double
Dim f As Double, g As Double, h As Double
Dim j As Double, ret As Double
' Perform the complicated calculation
c = ((dblSIncome - 6000) * 0.15)
d = dblDefault - c
e = dblDefault + d
f = (18200 + ((445 + e) / 0.19)) + 1
g = (0.19 * 18200) + 445 + e + (37000 * (0.015 + 0.325 - 0.19))
h = (g / (0.015 + 0.325)) + 1
If f < 37000 Then
j = (0.125 * (dblTIncome - f))
Else
j = (0.125 * (dblTIncome - h))
End If
ret = e - j
' Return the value of the fucntion
GetCalculatedValue = ret
End Function
Hope this helps. :)
Dim x As Double
x = 1.111 'E.g.
Debug.Print Round(x * 20, 0)/20 '>> 1.10

Randomly ordering variables

I have 4 strings in variables a,b,c and d. I need to randomly order these variables in such a way so that I can input them into 4 different text boxes but not the same ones every time the program is ran.
I've tried to simplify it for myself by putting the strings into an array. Tell me what I'm doing wrong or if there's a way I could do it much easier.
Private Sub Random()
For i = 1 To 4
If a = 0 Then
a = r.Next(2, 5)
ElseIf b = 0 Then
Do Until b <> a
b = r.Next(2, 5)
Loop
ElseIf c = 0 Then
Do Until c <> a Or c <> b
c = r.Next(2, 5)
Loop
ElseIf d = 0 Then
Do Until d <> a Or d <> b Or d <> c
d = r.Next(2, 5)
Loop
End If
Next
End Sub
Here is one way to do it:
Dim a As String = "a"
Dim b As String = "b"
Dim c As String = "c"
Dim d As String = "d"
Dim all As String() = {a, b, c, d}
Dim random As New Random
Dim allRandom As String() = all.OrderBy(Function() random.Next).ToArray

How to generate Fibonacci series?

I am a beginner for programming and dealing with concept of visual basic 2010 to generate the following Fibonacci series output that will get the next number by adding two consecutive numbers
I have tried that by creating variables like bellow
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer = 0
Do
fib = a + b
a = b
b = fib
Label1.Text = Label1.Text + fib.ToString & ControlChars.NewLine
Loop While fib < 55
End Sub
End Class
This should work
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer
Do
Label1.Text += a.ToString & ControlChars.NewLine
fib = a + b
a = b
b = fib
Loop While a <= 55
Private Sub Command1_Click()
Dim x, g, n, i, sum As Integer
n = Val(Text1.Text)
x = 0
y = 1
Print x
Print y
For i = 3 To n
sum = x + y
Print sum
x = y
y = sum
y = sum
Next i
End Sub
Here the solution:
Dim num1 As Integer = 1
Dim num2 As Integer = 1
Dim aux As Integer
Console.Write(num1)
Console.Write(", ")
Console.Write(num2)
Console.Write(", ")
While num1 < 100
aux = num1 + num2
Console.Write(aux)
Console.Write(", ")
num1 = num2
num2 = aux
End While
This print Fibonacci series till number 100
I hope this help you!

Pascal's Triangle - VB.NET

I've already created a program that will display x number of rows and repeat that :
i.e.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
Now I want to make Pascal's Triangle
Maybe something like this:
Dim arr As Integer(,) = New Integer(7, 7) {}
For i As Integer = 0 To 7
For k As Integer = 7 To i + 1 Step -1
'print spaces
Console.Write(" ")
Next
For j As Integer = 0 To i - 1
If j = 0 OrElse i = j Then
arr(i, j) = 1
Else
arr(i, j) = arr(i - 1, j) + arr(i - 1, j - 1)
End If
Console.Write(arr(i, j) & " ")
Next
Console.WriteLine()
Next
Console-output:
Another approach, only keeps previous and current iterations in memory:
Dim oldList As New List(Of Integer)({0, 1})
For line = 1 To 7
Dim newList As New List(Of Integer)
For i = 1 To oldList.Count - 1
newList.Add(oldList(i - 1) + oldList(i))
Next
Debug.Print(String.Join(" ", newList))
oldList.Clear()
oldList.Add(0)
oldList.AddRange(newList)
oldList.Add(0)
Next
to do that using a windows form, you would need a textbox,multi-line textbox and a button on the design interface
here is the code you need to generate it
Imports System.Numerics 'this allows you to use big integer
Public Class pascal_triangle
Private Function factorial(ByVal k As Integer) As BigInteger
'big integer allows your proram compute for inputs of more than 22
If k = 0 Or k = 1 Then
Return 1
Else
Return k * factorial(k - 1)
End If
End Function
Private Sub BtnGen_Click(sender As Object, e As EventArgs) Handles BtnGen.Click
Dim nCr As Double
Dim i, j, k As Integer
Dim output As String
output = ""
j = Val(TxtColumn.Text)
For k = 0 To j
For i = 0 To k
Dim fact, fact1, fact2 As BigInteger
fact = factorial(k)
fact1 = factorial(k - i)
fact2 = factorial(i)
nCr = fact / (fact1 * fact2)
TxtOutput.Text += Str(nCr) & output
Next
TxtOutput.Text += vbCrLf
Next
End Sub
Private Sub pascal_triangle_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class