2

I'm making a program that will give you the result of a sum when the input is unstructured. Please see the following:

Input:

whats 20 + 26

Output:

46

I already know how to search for text in a string (If text.IndexOf("hello") >= 0 Then)...

I have no idea how to use the two integers for an addition equation.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Naqeeb
  • 49
  • 7
  • 1
    What's the purpose for using an unstructured process of inputting data? I.e: Why the added complication of needing to parse through input as opposed to a requested value 1, value 2, value n? – Hopeful Llama Jan 04 '14 at 02:39
  • 2
    This is actually a rather difficult problem. It is not a specific programming question, it is a broad programming problem. To solve it you need to apply a very broad set of programming tools, especially if you have no restrictions on the format of the input string. It's clear you haven't even thought through all of the sub-problems that are required to put this together. What if a user types `what is 20 + 26` or `I have 20 apples and 26 fingers` or `whatever you do, don't tell me the sum of 20 + 26`, or `What are 26.9+7 things % 133t k0derz can d0?` – J... Jan 04 '14 at 02:57
  • 1
    In brief, consider the complexity of what you are asking and try to break the problem down into simpler subsets. As it stands, this question is asking how to write a natural language parser - this is not a trivial problem. If you're just learning how to program, I suggest you start with the basics first. – J... Jan 04 '14 at 02:58
  • You might want to start looking at regular expressions - questions like this might help : http://stackoverflow.com/q/3373885/327083 Mind you, regular expressions are only going to provide a limited and specific type of solution to this problem, but it's at least a start. – J... Jan 04 '14 at 03:01
  • just add restrictions to your `textbox` - like no alphabets allowed. that will simplify your homework :) also, what kind of a person will type "what's 20+20" rather than "20+20" `clicks button` – AdorableVB Jan 04 '14 at 03:14
  • You could write VB code which uses a `Regular Expression` to pick out two numeric values from a string and store those values in variables. Then write another `REGEX` to pick out the operator. And finally, perform arithmetic on the numbers. – laylarenee Jan 04 '14 at 03:23
  • As pointed out in J's examples, your test might have **three** numbers in your string `what is 20 + 26 + 1`, which adds another level of complexity. – laylarenee Jan 04 '14 at 03:28
  • Im sorry but i dont have any idea what everyone is saying, like i said i dont understand these advanced things. – Naqeeb Jan 04 '14 at 19:44
  • By the way, This is NOT for homework, just for an AI im making. – Naqeeb Jan 04 '14 at 19:44

2 Answers2

0

Here's a quick example of how to start doing this with regular expressions. This won't be bulletproof against anything you throw at it, but it should make for a good start.

Be sure to add : Imports System.Text.RegularExpressions to your .vb file

    'This string is an example input.  It demonstrates that the method below 
    'will find the sum "2345+ 3256236" but will skip over things like 
    '  if + .04g
    '  1.23 + 4
    ' etc...
    Dim input As String = _   
       "aoe%rdes 2345+ 3256236 if + .04g  rcfo 8 3 . 1.23 + 4 the#r whuts"

    Dim pattern As String = "\s\d+\s*\+\s*\d+(?=\s|$)"
    For Each _match As Match In Regex.Matches(input, pattern)
        Dim a = _match.Value.Split("+"c)  'Match extracts "2345+ 3256325"
        Dim x As Integer                
        Dim y As Integer
        If Integer.TryParse(a(0), x) AndAlso Integer.TryParse(a(1), y) Then
            Console.WriteLine("Found the Sum : " & x & " + " & y)
            Console.WriteLine("Sum is : " & x + y)
        Else
            Console.WriteLine("Match failed to parse")
        End If
    Next

The regular expression can be broken down as

\s        '(definitely) one whitespace
\d+       'followed by any number of integer digits
\s*       'followed by (possibly) a whitespace
\+        'followed by (definitely) a "+"
\s*       'followed by (possibly) a whitespace
\d+       'followed by any number of integer digits
(?=\s|$)  'followed by (definitely) either a whitespace or EOL

Read more here :

Regular Expression Language - Quick Reference

.NET Framework Regular Expressions

Regular Expressions Reference

J...
  • 30,968
  • 6
  • 66
  • 143
  • Sorry I was busy with other code.I just came back to my question. Ok either i really suck at vb.net or you did it wrong. I tried your code (i did not really understand it) and it did not work. I typed in `whats 6 + 6` and it gave me a long string of integers 3258581 which im pretty sure is not the answer to 6 + 6. SO YEAH I PROBABLY DID SOMETHING WRONG. – Naqeeb Feb 11 '14 at 03:15
  • @Naqeeb 3258581 is the answer to the example string `input` in my answer. The idea is that you are supposed to replace `input` with your input. You can't just close your eyes, press `ctrl+c`, `ctrl+v` and hope that things are going to work. This is programming, son. You've got to read and pay attention. Did you not notice the code also output `Found the Sum : 2345 + 3256236` - which is the same sum that is in the string `input`? – J... Feb 11 '14 at 10:22
  • Ok sorry ill try it again, like I said i did not really understand it :( – Naqeeb Feb 11 '14 at 21:11
  • @Naqeeb did you read the links I posted? To understand the answer you have to understand what regular expressions are and how they work. The links at the end should give you a good introduction - the rest should make more sense after that. – J... Feb 11 '14 at 22:05
-1

Parse the two integers into two integer variables - we'll call them x and y. Then add their results.

Dim x as Integer
Dim y as Integer
Dim result as Integer

'parse your integers- you indicate you already know how to do this

'add the results
result = x + y

'now print out the result
STLDev
  • 5,950
  • 25
  • 36
  • He did not say he already knew how to parse the integers - he said he knew how to find specific text, not general things like an unspecified integer of arbitrary size. – J... Jan 04 '14 at 03:03
  • @J... Please note that the OP made two separate statements that indicate that he can most likely parse the integers: 1) "I already know how to search for text in a string"; and 2) "I have no idea how to use the two integers for an addition equation". How can he have two integers "for an addition equation" if they haven't been parsed? I answered his question of "I have no idea how to use the two integers for an addition equation". Your down-votes of my and Dan Verdolino 's responses are unmerited. I hope you have a good day. – STLDev Jan 04 '14 at 06:58
  • Read the post title - it asks *How to search for an integer in a string*. The statement *"I know how to search for text"* does not mean *"I know how to search for integers"*. English is obviously not OP's native language - statement 2 most logically is to be read as *"I have no idea how to use the two integers [in the string] for an addition equation"*. Surely OP is not stumped at the syntax for assignment and addition. Think about it and look at what actual content you've provided. – J... Jan 04 '14 at 13:30
  • thanks for giving an anwer but i need to find 2 integers in the text from the textbox. – Naqeeb Jan 04 '14 at 19:48
  • @J... I honestly have no idea what the OP's native language is. I was simply trying to help her/him. How you can assume that he well knows one thing and not another is beyond me. Clairvoyance, perhaps. Naqeeb, as others have said, parsing of two separate integers from a string can be difficult without placing stringent rules on the format of that string. You may want to look into the subject of lexical analysis and parsing. – STLDev Jan 04 '14 at 23:10