C# - Textbox Addition to String Values & Duplicate Checking/Concatting
I have a text document that looks similar to this:
R.D. P.N. X Y Rot Pkg
L5 120910 64.770 98.425 180 SOP8
L4 120911 -69.850 98.425 180 SOIC12
L10 120911 -19.685 83.820 180 SOIC10
L9 120911 25.400 83.820 180 0603
L5 120910 62.484 98.425 180 SOP8
.. ...... ...... ...... .. .......
I would like to have two text boxes (labeled X and Y) that get input from the user (only numbers that are positive or negative and can be decimals. ex. -124.893)
With these text boxes I would like to find the the values under the 3rd and 4th spots in the string after the line.Split(" ");
is used. This meaning for the dataset above the values I would want are:
64.770 98.425
-69.850 98.425
-19.685 83.820
25.400 83.820
62.484 98.425
...... ......
With these values I would like to take the input from the X and Y textboxes and add it to these values. So if the user entered "10.552" into the X textbox and "-140.123" into the Y textbox the new values would be:
75.322 -41.698
-59.298 -41.698
-9.133 -56.303
35.952 -56.303
73.036 -41.698
Another problem I would like to solve has to do with if the line has a duplicate "R.D." value. So in the example above there are two lines that beging with "L5" that need to be changed. When the second value is found, it changes the first found value to L5-1 and the second value to L5-2. This is not restricted to just 2 values but rather an infinite amount of "-#".
So the final text would look like this (with the addition and concatting together):
开发者_Go百科R.D. P.N. X Y Rot Pkg
L5-1 120910 75.322 -41.698 180 SOP8
L4 120911 -59.298 -41.698 180 SOIC12
L10 120911 -9.133 -56.303 180 SOIC10
L9 120911 35.952 -56.303 180 0603
L5-2 120910 73.036 -41.698 180 SOP8
.. ...... ...... ...... .. .......
QUESTIONS:
- How do I get the input from a textbox (negative, positive, decimal, numbers only) and add it to a value in a string and then place that new value back into the same spot in the string?
- How do I find a line that already started with the same value?
- When this line is found, how do I concat a "-1", "-2", "-3", etc. to the values?
Instead of dealing with text data and always converting datatypes, you can have an object that has strongly-typed numeric data parsed from the text document, do all calculations in-memory using numeric datatypes, and then convert them into text again to display the values.
For numbering duplicate values, you can have a Dictionary<string,int>
with the RD value as the key and the value as the number of times it appears in the RD column. You can have another dictionary of the same type, except it keeps track of the last number assigned. When you go through your list of items to display them you can check the dictionary, subtract one from the value in the first dictionary, add one to the value in the second dictionary, and concatenate the result to the RD value (item.RD += "-"+number
).
In addition to @marks answer I would say that in your specific case, you can have a datastucture, but you should also pay very attention on floating point numbers. I don't have any idea how strong accuracy you need, but for example in file you can store them like an integers, so your current values multiplied by 1000. And when user inserts a floating point number multiply it by 1000 and after add/substract or whatever. When you're going to show to the user devide it by 1000.
Hope this helps.
Regards.
精彩评论