Making a string that adapts itself to the text inside it - vb.net

I'm making a program to solve second grade equations (e.g. 2x²+2x+2) and I was trying to have the user input the whole equation in a single text box. The computer then stores what has been typed into the text box in a string, and then it parses the string to find the coefficents. For an equation like 2x²+2x+2, the coeficients are 2, 2 and 2 and they are stored in the string at locations 0, 4 and 7. The big problem is, what if it's a bigger equation like 32x²+32x+45 or 123x²+45x+6? My logic won't work in that case. Does anyone know how to do it?
Here's my code that only works for the small equations:
Public Class Form1
Dim i1 As Double
Dim i2 As Double
Dim i3 As Double
Dim delta As Double
Dim x1 As Double
Dim x2 As Double
Dim leters As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i1 = T1.Text
i2 = T2.Text
i3 = T3.Text
delta = (i2 * i2) - 4 * (i1 * i3)
If (delta < 0) Then
Ld.Text = delta
L1.Text = "Impossível"
L2.Text = "Impossível"
Else
x1 = (-i2 + Math.Sqrt(delta)) / (2 * i1)
x2 = (-i2 - Math.Sqrt(delta)) / (2 * i1)
Ld.Text = delta
L1.Text = x1
L2.Text = x2
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
GroupBox1.Text = "Equação"
GroupBox1.Width = 200
GroupBox1.Height = 58
T1.Width = 188
T3.Hide()
T2.Hide()
Label1.Hide()
Button1.Hide()
Button2.Show()
End Sub
Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
GroupBox1.Text = "Coeficientes"
GroupBox1.Width = 200
GroupBox1.Height = 143
T1.Width = 119
T3.Show()
T2.Show()
Label1.Show()
Button1.Show()
Button2.Hide()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
leters = T1.Text
leters.ToString()
End Sub
End Class

You could use RegEx to parse the string, which would be nice because you could change the RegEx expression as a setting without rebuilding your assembly. However, if the equation string format is always going to be the same, as appears to be the case here (because the rest of the code would fail if it weren't), then you could simply use String.Split to parse the string. For instance:
Dim equation As String = "32x²+32x+45"
Dim parts() As String = equation.Split(New Char() {"+"c, "x"c}, StringSplitOptions.RemoveEmptyEntries)
Dim coefficient1 As Integer = Integer.Parse(parts(0))
Dim coefficient2 As Integer = Integer.Parse(parts(2))
Dim coefficient3 As Integer = Integer.Parse(parts(3))

Related

Fibonacci Sequence Visual Basic

I have a quick question about another Visual Basic assignment I'm working on. I have all the code and everything has gone smoothly so far. The app is meant to display the first 100 Fibonacci numbers in a list box, adding the two previously displayed numbers to get the next in a loop. The only problem is that when I hit the button to display the code, the loop continues, and doesn't just stop at 100 numbers. Where did I go wrong?
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim dblA As Double = 0
Dim dblB As Double = 1
Dim dblC As Double
Dim intCounter As Integer
lstSequence.Items.Add(dblA.ToString)
lstSequence.Items.Add(dblB.ToString)
For intCounter = 1 To 100
dblC = dblA + dblB
dblA = dblB
dblB = dblC
lstSequence.Items.Add(dblC.ToString)
Next
End Sub
I just tried this. It works fine.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer
Dim userinput, i As Integer
userinput = InputBox("how many interations?")
i = userinput
ListView1.Items.Add(1)
Do
fib = a + b
a = b
b = fib
ListView1.Items.Add(fib)
i = i + 1
Loop While fib < i
End Sub
End Class

Get Character Of IndexOf

For an assigment my teacher is asking that we read from a file to find the characters of our name and place them at a label at the top of the form.
here is my code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
searchFile = File.OpenText("AcademicEthicsandIntegrityStatement.txt")
Dim s As String = searchFile.ReadToEnd
Dim b As String = s.IndexOf("b"c)
Dim r As Integer = s.IndexOf("r"c)
Dim i As Integer = s.IndexOf("i"c)
Dim a As Integer = s.IndexOf("a"c)
Dim n As Integer = s.IndexOf("n"c)
Dim ec As Integer = s.IndexOf("e"c)
Dim bRead = GetChar(s, b)
Dim rRead = GetChar(s, r)
Dim iRead = GetChar(s, i)
Dim aRead = GetChar(s, a)
Dim nRead = GetChar(s, n)
Dim ecRead = GetChar(s, ec)
lblName.Text = bRead + rRead + iRead + aRead + nRead + nRead + ecRead
End Sub
The text that is reading into my lbl is "gmcaad" instead of "brianne"
Im sure that I am missing something here or there is a much easier way to do this. Any help is appreciated.
IndexOf returns a zero-based index.
GetChar accepts a one-based index.
For consistency,
if you want to use IndexOf, then use direct indexing instead of GetChar:
Dim bRead = s(b)
Dim rRead = s(r)
if you want to use GetChar, then use InStr instead of IndexOf that also returns one-based values.
Short Answer...case sensitive:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With File.ReadAllText("AcademicEthicsandIntegrityStatement.txt")
For Each C As Char In "Brianne".ToCharArray
' Note this is case-sensitive because it uses a binary comparison
Dim Index As Integer = .IndexOf(C)
If Index >= 0 Then lblName.Text &= .Substring(Index, 1)
Next
End With
End Sub
... and non-case sensitive:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With File.ReadAllText("AcademicEthicsandIntegrityStatement.txt")
For Each C As Char In "Brianne".ToCharArray
' Note this is not case-sensitive
Dim Index As Integer = .IndexOf(C.ToString, StringComparison.InvariantCultureIgnoreCase)
If Index >= 0 Then lblName.Text &= .Substring(Index, 1)
Next
End With
End Sub

Make specific randomly generated codes with timer

I would like to make randomly generated codes when a timer is finished.
But I would like to use this code ABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789
on this format XXXX-XXXX-XXXX (replace X with a randomly generated number/letter)
And I would like to insert the code in this
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
TextBox1.Text = "replace textbox by code"
Timer1.Stop()
End If
End Sub
Here's a solution using LINQ.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim random = New Random()
Dim num1 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim num2 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim num3 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim result = num1 & "-" & num2 & "-" & num3
TextBox1.Text = result
Timer1.Stop()
End If
End Sub
With this, you'd just change your TextBox1.Text = to point to the getNewRandomString() function.
Private Function getNewRandomString() As String
Dim sb As New StringBuilder
For i = 1 To 3
For j = 1 To 4
sb.Append(getNewRandomLetter())
Next
sb.Append("-")
Next
Return sb.ToString.Trim("-")
End Function
Private Function getNewRandomLetter() As Char
Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Return (chars(GetRandom(0, chars.Length - 1)))
End Function
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Static Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max + 1)
End Function

VB.NET Help sectioning with distinct numbers

What I need is this: (A, B, C, D, etc.. are chars(name of the section))
A = 0-40
B = 41-80
C = 81-120
D = 121-160
and so on
I have a combobox containing numbers from 0 to 1040, when I select numbers such as 80, the section turns into C, which should be B because it goes inside 41-80 as seen above. As well as I choose 120, it turns into D, which should be C. etc etc. Only numbers from 1-40 works perfectly. Here is my code:
Dim counter As String
Dim mysection As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = cmbYearAdd.SelectedItem
Dim a As Char = "A"
Dim distinct1% = 1, distinct2% = 40
For x As Integer = counter To 1040
If x >= distinct1 And x <= distinct2 Then
mysection = a
Exit For
Else
a = Chr(Asc(a) + 1)
distinct1 += 40
distinct2 += 40
End If
Next
lblSectionAdd.Text = "Section: " & mysection
End Sub
I have a label, combobox, and button.
This is because when the limits are raised the value of x is incremented when the for loop starts over.
But please don't try to fix it - this method is so awkward you shouldn't put any effort in it anymore. Simply keep the counter value fixed and divide by your section size to directly compute which section the value belongs to:
Dim counter As String
Dim mysection As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = cmbYearAdd.SelectedItem
Dim a As Char = "A"
Dim sectionsize as Integer = 40
Dim sect as Integer
sect = Int((counter-1) / sectionsize)
mysection = Chr(Asc("A") + sect)
lblSectionAdd.Text = "Section: " & mysection
End Sub
The division result needs to be truncated to Integer, I'm using Int() for this.

Calculator for newbs

I'm working on a calculator in which I want to get some numbers on a circle.
Private Sub Button6_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button6.Click
Dim radius As Integer = TextBox13.Text
Dim diameter As Integer = TextBox14.Text
Dim length As Integer = TextBox15.Text
TextBox13.Text = diameter / 2
TextBox14.Text = radius * 2
TextBox15.Text = radius * 2 * Math.PI
TextBox15.Text = diameter * Math.PI
End Sub
That is the current code, but I'm experiencing a problem with "the number must be less than infinity".
Note: I'm a COMPLETE noob.
The error lies in the fact that you tried to assign an integer value of type string.
Use the method provided by struct TryParse integer, this will also run in any runtime exception FormatException bran and do not send the application.
Here's an example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim diameter As Integer = 0
Dim radius As Integer = 0
Dim lenght As Integer = 0
If Integer.TryParse(Me.TextBox13.Text, diameter) Then
'your code
End If
If Integer.TryParse(Me.TextBox14.Text, radius) Then
'your code
End If
If Integer.TryParse(Me.TextBox15.Text, lenght) Then
'your code
End If
End Sub
More information about TryParse at this link:
http://msdn.microsoft.com/it-it/library/f02979c7.aspx
Bye
See if this works:
Dim radius As Integer = Integer.Parse(TextBox13.Text)
Dim diameter As Integer = Integer.Parse(TextBox14.Text)
Dim length As Integer = Integer.Parse(TextBox15.Text)