Solved

Convert Sentence into formula VB.NET C#

Posted on 2012-04-10
6
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Six Sigma Control Plans
Introduction to Processes

734 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