带有小数的已排序列表框 [英] Sorted listbox with fractional numbers

查看:128
本文介绍了带有小数的已排序列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Basic 2008

用户选择要放在列表中的十进制数字(例如:4; 0,3333; 0,75; 2,125等) 。

十进制数列表必须在列表框中排列,从最小到最大(0,3333; 0,75; 2,125; 4;等)。

我做了这个代码,它对我很有用。





Visual Basic 2008
The user selects the decimal numbers to be put on the list ( for example: 4; 0,3333; 0,75; 2,125 etc).
List of decimal numbers must be arranged in listbox from smallest to bigest( 0,3333; 0,75; 2,125; 4; etc).
I have made this code and it works great for me.


Option Strict On
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text <> "" Then
            ListBox1.Items.Add(TextBox1.Text)
        Else
            MsgBox("Empty field")
           End If
        TextBox1.Text = ""
      
    End Sub
  TextBox1.Select()


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Clear()
        TextBox1.Select()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim List As New List(Of Decimal)
        For Each ListBoxItem In ListBox1.Items
            List.Add(CDec(ListBoxItem))
        Next

        List.Sort()
        ListBox1.Items.Clear()

        For Each ListItem In List
            If Not ListBox1.Items.Contains(ListItem) Then 
                ListBox1.Items.Add(ListItem)
            Else
                MsgBox("Already exist ")
            End If
        Next

    End Sub
End Class



但是,如果用户以小数形式输入数字,即4,则此特定应用更合适; 1/3; 3/4; 17/8等;

此列表也必须从最小到最大的数字排序,即1/3; 3/4; 17/8; 4等。

怎么做?。


But for this specific application is more appropriate if user enters the numbers in fractional form, ie 4; 1/3; 3/4; 17/8 etc ;
This list must also be sorted from the smallest to the largest number ie 1/3; 3/4;17/8; 4 etc .
How to do it?.

推荐答案

这叫做有理数:http://en.wikipedia.org/wiki/Rational_number [ ^ ]。



您需要了解一下他们的理论(他们的基础知识应该很多)在小学教,并发展他们的算术。为了表示它们,您可以使用结构或具有两个整数成员的类:分子和分母。您需要能够将每个分解成为素数的乘积。您将需要它来简化(减少)有理数,例如:6/8 = 3/4。如果你的整数成员不是太大的数字,它将是一个非常快的操作,但对于大数字它将是非常慢的。



因此,你可以使用不同的方法,使用一些冗余的方法来提高性能。



您可以首先使用素数。您的分子和分母可以表示为代表其产品的素数列表, System.Collection.Generic.List< int> ,其中每个列表成员都是素数。如果你将一个有理数乘以一个整数,你应该首先将这个整数分解为素数的乘积,然后你将所有素因子加到你的列表中,然后你立即通过删除来减少你的有理数来自两个列表的相同的素因子,来自分子和分母。我希望这很清楚。分裂是以相同的方式进行的,但分子和分母的角色是颠倒的。完成这些带整数的运算后,您可以定义有理数的乘法和除法。我希望你已经明白了。



现在,使用公分母的方法实现加法和减法。记住小学。



请参阅:

http://en.wikipedia.org/wiki/Factorization [ ^ ],

http://en.wikipedia.org/wiki/Prime_number [ ^ ],

http://en.wikipedia.org/wiki/Redundancy_%28information_theory%29 [ ^ ]。



现在,使用 ComboBox 列表不存储字符串。存储某些结构或类的对象,这些对象可帮助您使用列表元素。例如,它可以是实现上述有理数运算的结构。 如今,初学者最大的谬误之一就是使用代表数据的字符串代替数据本身的趋势。不要犯这个错误。



唯一的问题是:当你添加这样的列表元素时,UI中会显示什么?答案很简单:无论方法 ToString()返回。因此,在您的类或结构中,还要覆盖 System.Object.ToString(),以根据您的分子/分母值显示3/4,而不是结构名称。







有关此技术的更多详情,请参阅我过去的答案:

从列表中显示图像框 [ ^ ],

combobox.selectedvalue在winform中显示{} [ ^ ]。



-SA
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.



For more detail on this technique, please see my past answers:
Displaying an image from a list box[^],
combobox.selectedvalue shows {} in winform[^].

—SA


这篇关于带有小数的已排序列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆