This is called "rational numbers":
http://en.wikipedia.org/wiki/Rational_number[
^].
You need to learn their theory a bit (a big deal of their basics should be taught in elementary school) and develop their arithmetic. For representation of them, you can use the structure or a class with two integer member: numerator and denominator. You need to be able to
factorize each of them into the product of
prime numbers. You will need it to simplify (reduce) rational numbers such as: 6/8 = 3/4. If your integer member are not too big numbers, it will be a quite fast operation, but it will be extremely slow for big numbers.
Therefore, you can use a different approach, the approach with some
redundancy use to improve performance.
You can work with prime numbers in first place. Your numerator and denominator could be presented as a list of prime number representing their product,
System.Collection.Generic.List<int>
, where each list member is a prime number. If you, say, multiply a rational number by an integer, you should factorize this integer into a product of prime numbers in first place, then you add all prime factors to your list, and then you reduce your rational number, immediately, by removing identical prime factors from both lists by pairs, from numerator and denominator. I hope this is clear. Division is made in the same fashion, but numerator and denominator roles are inverted. When these operations with integers are done, you can define multiplication and division of rational numbers. I hope you already understand how.
Now, addition and subtraction are implemented using the method of the
common denominator. Remember the elementary school.
Please see:
http://en.wikipedia.org/wiki/Factorization[
^],
http://en.wikipedia.org/wiki/Prime_number[
^],
http://en.wikipedia.org/wiki/Redundancy_%28information_theory%29[
^].
Now, working with the list of
ComboBox
don't store strings. Store the objects of some structures or classes which helps you to work with list elements. For example, it could be the structures implementing the rational number arithmetic I described above.
One of the biggest fallacies of the beginners these days is the trend to work with strings representing data instead of data itself. Don't do this mistake.
The only problem is: what will be shown in UI when you add such list element? The answer is simple: whatever the method
ToString()
returns. So, in your class or structure, also override
System.Object.ToString()
, to show "3/4" based on your numerator/denominator value, not structure name.
[EDIT]
For more detail on this technique, please see my past answers:
Displaying an image from a list box[
^],
combobox.selectedvalue shows {} in winform[
^].
—SA