Answered by:
Arrays, Specifically Char Arrays

Question
-
private void Benter_Click(object sender, EventArgs e) { string mainString = INmain.Text; string chars = "+-x/"; char [] charArray = chars.ToCharArray(); INmain.Text = charArray.ToString(); for (int i = 0; i >= 4; i++) { int tempInt = mainString.IndexOfAny(charArray); INmain.Text = tempInt.ToString(); } }
Benter is my 'Enter' button.
INmain is my inputbox.
The problem is the output is always System.Char[], regardless of the input.
I can do the same thing without a Char Array -and it works, but it increases my code threefold.
If you can't tell I am horrible with arrays.Tuesday, January 19, 2010 6:32 PM
Answers
-
If you want to have the indices of all the operators, you need to tell IndexOfAny to not search from the start every time.
List<int> getOperators(string equation) { char[] operators = { '+', '-', '*', '/' }; List<int> indices = new List<int>(); int i = equation.IndexOfAny(operators); while(i != -1) { indices.Add(i); i = equation.IndexOfAny(operators, i); } return indices; }
- Marked as answer by Alex Liang Tuesday, January 26, 2010 12:48 PM
Wednesday, January 20, 2010 1:31 PM
All replies
-
Sorry for the double post, but I also tried this (it still gives me the same problem):
private void Benter_Click(object sender, EventArgs e) { string mainString = INmain.Text; // string chars = "+-x/"; char [] charArray = {'+','-','x','/'}; // INmain.Text = charArray.ToString(); for (int i = 0; i >= 4; i++) { int tempInt = mainString.IndexOfAny(charArray); INmain.Text = INmain.Text + tempInt.ToString(); } }
Tuesday, January 19, 2010 6:34 PM -
You need to use the i in the for loop. Try changing (charArray) to (charArray[i]).
EDIT:
Actually...what are you trying to accomplish. Also, you might want to change i >= 4 to i < 4 since there are only 4 items in your array that I'm assuming you want to loop through?- Edited by HMote Tuesday, January 19, 2010 6:52 PM
Tuesday, January 19, 2010 6:37 PM -
I was making a calculator.
The whole point was to make it 'fancy.'
Where you could input almost any equation and it solves it for you.
Such as the quadratic equation:
x=(-b +/- √(b^2 -4a*c)) / (2*a)
Well lets use a simpler example
(-9)+87*52
It would get the index of each operator, save it to an int, and save the value between
each index as a number. Then get the value of the index of the operator and save it to a
char. If I was to do it without using a char array I would have to do .LastIndexOf, for each
possible operation. Then check to see the placement of the index. Whereas with the Char
Array I can use the .LastIndexAny witch would automatically give me the last index of the
last operator. Then I can set the start value of the .LastIndexOfAny (which would be in a loop)
to the value of the last found operator+1. And that could be repeated for all of the operators.
So:
( = char
- = char
9 = int
) = char
+ = char
87 = int
* = char
52 = int
Then it would do the operation between each number.
P.S. It would multiply the 9 by -1.Tuesday, January 19, 2010 6:48 PM -
If you want to have the indices of all the operators, you need to tell IndexOfAny to not search from the start every time.
List<int> getOperators(string equation) { char[] operators = { '+', '-', '*', '/' }; List<int> indices = new List<int>(); int i = equation.IndexOfAny(operators); while(i != -1) { indices.Add(i); i = equation.IndexOfAny(operators, i); } return indices; }
- Marked as answer by Alex Liang Tuesday, January 26, 2010 12:48 PM
Wednesday, January 20, 2010 1:31 PM