# Problem with Strings and equations

• (2 Pages)
• 1
• 2

## 18 Replies - 3207 Views - Last Post: 18 July 2012 - 02:19 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=286178&amp;s=c0a5f54de15f8df0a163e2b1c6ec0816&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

# Problem with Strings and equations

Posted 18 July 2012 - 06:07 AM

Hi,Im making a program to solve second grade equation (2x²+2x+2 for example)and i was trying to make the user input the whole equation in a text box,the computers stores whats written on the textbox in a string and then for a equation like
2x²+2x+2 the coeficients are 2,2 and 2 and are stored in the string on locations 0,4 and 7.The big problem is what if its a bigger equation like 32x²+32x+45 or 123x²+45x+6 ?That way wont work,anyone knows how to do it?
The code for now:
```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

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

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 Sub
End Class

```

Is This A Good Question/Topic? 0

## Replies To: Problem with Strings and equations

### #2 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: Problem with Strings and equations

Posted 18 July 2012 - 06:58 AM

Raminator: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

See FAQ # 30 for calculator tutorial.
See FAQ # 3 for debugging tutorials.
(Click the SHOW button below)

What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated July 2012
Spoiler

Take special note of tips 2,3, 4 as your coding style would really benefit from them.

Some of my common tips (some may apply more than others to your specific style):
• You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

• Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

• You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. [b]You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.

• Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
Spoiler

• Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

Spoiler

.

### #3 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: Problem with Strings and equations

Posted 18 July 2012 - 07:03 AM

tlhIn`toq, on 18 July 2012 - 06:58 AM, said:

Raminator: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

See FAQ # 30 for calculator tutorial.
See FAQ # 3 for debugging tutorials.
(Click the SHOW button below)

What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated July 2012
Spoiler

Take special note of tips 2,3, 4 as your coding style would really benefit from them.

Some of my common tips (some may apply more than others to your specific style):
• You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

• Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

• You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. [b]You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.

• Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
Spoiler

• Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

Spoiler

.

My problem is not with debbuging my program or with the calculator code,they both work,my problem is to find a way to separate data on a textbox (thats on a string in this case) n a way that the way it separates depends on the text written on it.

### #4 Bort

• Ill-informed Mongoloid

Reputation: 451
• Posts: 3,114
• Joined: 18-September 06

## Re: Problem with Strings and equations

Posted 18 July 2012 - 07:22 AM

And people say Klingons aren't smart. Nice post tlhIn`toq

### #5 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: Problem with Strings and equations

Posted 18 July 2012 - 07:39 AM

Federation propaganda. Last I checked we had working cloaking technology decades ago. And warp technology almost a century before Earth. I'm thinking some smart Klingon engineers at work there.

```                                     _
_|_|_
^/ . ..\^
___[=========]___
___-==++""" .  /. . .  \ .  """++==-___
__-+"" __\   .. . .  | ..  . |  . .  .   /__ ""+-__
/\__+-""   `-----=====\_ <O> _/=====-----'   ""-+__/\
_/_/                      ""=""                      \_\_
/_/                                                     \_\
//                                                         \\
/")                                                         ("\
\o\                                                         /o/
\_)                                                       (_/

```

### #6 Bort

• Ill-informed Mongoloid

Reputation: 451
• Posts: 3,114
• Joined: 18-September 06

## Re: Problem with Strings and equations

Posted 18 July 2012 - 07:53 AM

Very impressive

### #7 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Problem with Strings and equations

Posted 18 July 2012 - 07:57 AM

Getting back on topic,

Quote

is to find a way to separate data on a textbox (thats on a string in this case) n a way that the way it separates depends on the text written on it.

What?

### #8 Bort

• Ill-informed Mongoloid

Reputation: 451
• Posts: 3,114
• Joined: 18-September 06

## Re: Problem with Strings and equations

Posted 18 July 2012 - 08:04 AM

Ok, staying on topic, what about breaking down any text in the textbox, checking each character against IsNumeric (and if it is, sticking them together)? I'm not entirely sure how you would go about coding this though.

Basically what should happen is, your function takes TextBox1 and pulls the equation 32x + 23y (for example) out of it and breaks it down into the characters 3,2,x,+,2,3,y. It will then check each individual character to see if it is a number, and if it finds multiple numbers together, it sticks them together (there is a fancy word beginning with 'C' for this, concantation, or something along those lines) in it's own variable.

So, continuing the above example, it would check the 3, work out that is numeric, and assign it to the variable No1. It then checks the second character, 2, realises that is a number and adds it to the end of No1, which then becomes 32 and voila, you now have your first variable for working out the equation.

### #9 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Problem with Strings and equations

Posted 18 July 2012 - 08:09 AM

The problem you are going to have doing that is with the powers (as the OP wants to use quadratics).

If you define a pre - decided rule on the specific syntax that the user will input, then it shouldn't be too hard. For example x^2 + 8x + 12. You should be able to extract coefficients out of that without too much hassle if it's given in that format every time.

If on the other hand you want to be able to insert 8x + x^2 + 12. Then you are going to need to look into more formal expression parsing techniques.

### #10 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: Problem with Strings and equations

Posted 18 July 2012 - 08:12 AM

I'm going to refer you back to post #2 above where I point you at a calculator tutorial (FAQ 30) that already covers all this logic. We're not going to rehash all of that each and every time a student builds a calculator for class: That's why people write tutorials.

Not to mention, if you google "C# algebraic equastions" there are a dozen good tutorials that come back.

http://www.codeproje...-the-Interprete

### #11 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Problem with Strings and equations

Posted 18 July 2012 - 08:13 AM

It's a bit more complicated than that. If I understand what the OP wants to do correctly, he wants the user to insert an equation as a string, and for the program to parse out the coefficients and work out the solutions. If this is indeed what they are talking about, that tutorial won't help.

Edit - However that second tutorial should help more.

This post has been edited by Ryano121: 18 July 2012 - 08:15 AM

### #12 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: Problem with Strings and equations

Posted 18 July 2012 - 08:54 AM

Ryano121, on 18 July 2012 - 08:13 AM, said:

It's a bit more complicated than that. If I understand what the OP wants to do correctly, he wants the user to insert an equation as a string, and for the program to parse out the coefficients and work out the solutions. If this is indeed what they are talking about, that tutorial won't help.

Edit - However that second tutorial should help more.

Thats exactly what i want Ryano,anyone knows how to do it?

### #13 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: Problem with Strings and equations

Posted 18 July 2012 - 09:43 AM

LOOK AT THE SECOND TUTORIAL THAT I GAVE YOU A LINK FOR.

Your unwillingness to look at the tutorials we are pointing you toward is quickly going to get this thread bounced as "gimme teh codez" if you don't put in some effort of your own.

### #14 Raminator

• D.I.C Regular

Reputation: 1
• Posts: 292
• Joined: 16-July 12

## Re: Problem with Strings and equations

Posted 18 July 2012 - 12:15 PM

tlhIn`toq, on 18 July 2012 - 09:43 AM, said:

LOOK AT THE SECOND TUTORIAL THAT I GAVE YOU A LINK FOR.

Your unwillingness to look at the tutorials we are pointing you toward is quickly going to get this thread bounced as "gimme teh codez" if you don't put in some effort of your own.

Dude the page is offline, i dont know i just cant acess it.

### #15 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6535
• Posts: 14,450
• Joined: 02-June 10

## Re: Problem with Strings and equations

Posted 18 July 2012 - 12:24 PM

Have you heard of Google or Bing?

Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

Its questions like this that caused me to have to write a HOW-To article for search engines:
How to do a good search that will get you targeted answers.

I wish you the best of luck on your project, but I'm unsubscribing because I won't spend more effort on a project than the person actually building the application.

This post has been edited by tlhIn`toq: 18 July 2012 - 12:24 PM