Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert Sentence into formula VB.NET C#

Posted on 2012-04-10
6
Medium Priority
?
420 Views
Last Modified: 2012-04-16
Okay, this is not homework. But, I am looking for a method to input a string in and then calculate the resultant value.
I have a string such as "Eight Furlongs On The Dirt Track"
Now, I can pull out the "On The Dirt Track" part using a simple string.remove().

There are two formats to this string, and they are as follows:
1:     [number] [distance] On The [surface] <-Surface is irrelevent but dynamic as well
             example - "Eight Furlongs On The Dirt Track"
2:     [number] And [number] [distance] On The [surface]
             example - "One And One Sixteenth Miles On The Turf"

Any suggestions as to how to do this, VB.NET or C# will work.
0
Comment
Question by:Thomas_Hawkins
  • 3
  • 2
6 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 1000 total points
ID: 37831112
0
 

Author Comment

by:Thomas_Hawkins
ID: 37831307
Dhaest - that converts numbers to text, I need to convert text to numbers.

For example, I have "Eight furlongs on the dirt". I remove "on the dirt" and need to convert string "Eight furlongs" into 8*660 or 5280. Of course sometimes it can be different, some times it could be something like "One and One Quarter Mile on the dirt" in which case I would need something akin to (1*5280) + (.25*5280).

In the above "660" replaces furlong and "5280" replaces mile. Not that you guys wouldn't know that, just being overly explanatory.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 37831436
WordStringToNumber.txtHree you find already the part to convert a word-number to a number

long myNumber = CreateNumber("Nine Thousand Two Hundred Seventy Three");
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Assisted Solution

by:CmdoProg2
CmdoProg2 earned 1000 total points
ID: 37832668
You want to split the original string into whole number, fractional part, and distance after removing the surface part.  The uses the method of Dhaest's post.  You'll need to handle case sensitivity and pural forms.
StringSplitCSharp.txt
StringSplitVB.txt
0
 

Accepted Solution

by:
Thomas_Hawkins earned 0 total points
ID: 37835848
To anyone in this situation, here is the code I finally used. The experts where right in that there was no built-in way to do it. I was hoping to avoid the tedious code - but after several revisions I came up with this and it works marvelously, it tackles any situation I might have using furlongs, miles,  yards, and even feet to a degree of precision of sixteenths.

I only attach this code to help anyone looking for a roll your own situation - I know how frustrating it can be.
Public conversionWhole(,) As String = {{"sixteenth", "0.0625"}, {"eighth", "0.0125"}, {"quarter", "0.25"}, {"half", "0.5"},
                                   {"tenth", "0.1"}, {"one", "1"}, {"two", "2"}, {"three", "3"}, {"four", "4"}, {"five", "5"}, {"six", "6"}, {"seven", "7"}, {"eight", "8"}, {"nine", "9"}, {"ten", "10"},
                                   {"eleven", "11"}, {"twelve", "12"}, {"thirteen", "13"}, {"fourteen", "14"}, {"fifteen", "15"}, {"sixteen", "16"}, {"seventeen", "17"}, {"eighteen", "18"},
                                   {"nineteen", "19"}, {"twenty", "20"}, {"twenty-five", "25"}, {"thirty", "30"}, {"thirty-five", "35"}, {"forty", "40"}, {"forty-five", "45"}, {"fifty", "50"},
                                   {"fifty-five", "55"}, {"sixty", "60"}, {"sixty-five", "65"}, {"seventy", "70"}, {"seventy-five", "75"}, {"eighty", "80"}, {"eighty-five", "85"},
                                   {"ninety", "90"}, {"ninety-five", "95"}, {"hundred", "100"}}

Public Sub getRaceLengthType(ByVal str As String)
        str = str.ToLower
        'dMod holds the different lengths one might find in race lengths
        Dim dMod As Double = 0.0  ' This helps differentiate if these mods are used

        'num holds the different lengths one might find in race lengths
        Dim num As Double = 0.0

        Dim distance As Double = 0.0
        Dim r As Integer = 0

        'Place type of race inside raceType and trim it (Dirt, Turf, All Weather Track, etc)
        Dim raceType As String = str.Substring(str.IndexOf(" on ")).Trim
        raceType = raceType.Remove(0, 7).Trim

        'Here we would do something with the value in raceType - in my code it is sent to a public array. The variable raceType is reused later in the code.

        str = str.Substring(0, str.IndexOf(" on ")).Trim
        
        Dim pos As Integer

        Do Until str.Length = 0
            If str.Contains(" ") = False Then
                raceType = str.Substring(0)
            Else
                pos = str.IndexOf(" ")
                raceType = str.Substring(0, pos).Trim
            End If

            If raceType = "mile" Or raceType = "miles" Then
                dMod = 5280
                distance = distance + (num * dMod)
                num = 0
                If raceType = "miles" Then
                    pos = str.IndexOf("miles")
                    str = str.Remove(pos, 5).Trim
                ElseIf raceType = "mile" Then
                    pos = str.IndexOf("mile")
                    str = str.Remove(pos, 4).Trim
                End If
            ElseIf raceType = "furlong" Or raceType = "furlongs" Then
                dMod = 660
                distance = distance + (num * dMod)
                num = 0
                If raceType = "furlongs" Then
                    pos = str.IndexOf("furlongs")
                    str = str.Remove(pos, 8).Trim
                ElseIf raceType = "furlong" Then
                    pos = str.IndexOf("furlong")
                    str = str.Remove(pos, 7).Trim
                End If
            ElseIf raceType = "yard" Or raceType = "yards" Then
                dMod = 3
                distance = distance + (num * dMod)
                num = 0
                If raceType = "yards" Then
                    pos = str.IndexOf("yards")
                    str = str.Remove(pos, 5).Trim
                ElseIf raceType = "yard" Then
                    pos = str.IndexOf("yard")
                    str = str.Remove(pos, 4).Trim
                End If
            ElseIf raceType = "feet" Or raceType = "foot" Then
                dMod = 1
                distance = distance + (num * dMod)
                num = 0
                If raceType = "foot" Then
                    pos = str.IndexOf("foot")
                    str = str.Remove(pos, 4).Trim
                ElseIf raceType = "feet" Then
                    pos = str.IndexOf("feet")
                    str = str.Remove(pos, 4).Trim
                End If
            ElseIf raceType = "and" Then
                str = str.Replace(raceType, "").Trim
            ElseIf raceType = "hundred" Then
                pos = str.IndexOf("hundred")
                num = num * 100
                str = str.Remove(0, 7).Trim
            Else
                r = 0
                Do Until r = 41
                    If raceType = conversionWhole(r, 0) Or raceType = conversionWhole(r, 0) & "s" Then
                        pos = str.IndexOf(raceType)
                        If CType(conversionWhole(r, 1), Double) < 1 Then
                            num = num * CType(conversionWhole(r, 1), Double)
                        Else
                            num = num + CType(conversionWhole(r, 1), Double)
                        End If
                        str = str.Remove(0, raceType.Length).Trim
                    End If
                    r += 1
                Loop
            End If

        Loop

        testlbl.Text = distance.ToString

Open in new window

0
 

Author Closing Comment

by:Thomas_Hawkins
ID: 37850402
I only included my solution as best for people looking for this very thing. I wanted to provide them with a straight code solution. The two experts given points where right on the money!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Screencast - Getting to Know the Pipeline

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question