Solved

Convert Sentence into formula VB.NET C#

Posted on 2012-04-10
6
407 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 250 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 12

Assisted Solution

by:CmdoProg2
CmdoProg2 earned 250 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
Run time Error 4 35
Angular - break the dataset in chunks? 7 40
Windows Media Player 2 16
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now