简体   繁体   English

Windows表单计算器算法。 排序和最高/最低数字

[英]Windows forms calculator algorithms. Sorting and highest/lowest numbers

I'm currently working on a calculator but 2 of my algorithms are not working correctly. 我目前正在使用计算器,但是我的2种算法无法正常工作。

  1. In my history (listbox) I get the numbers from the calculator and I have a bottom that finds the smallest number. 在我的历史记录(列表框)中,我从计算器中获得数字,并且底部有一个最小的数字。 I get an error with my code. 我的代码出现错误。

  2. I want to have a [sorting] bottom that sorts the numbers ascending or descending. 我想要一个[排序]底部,对数字进行升序或降序排序。 I've tried things as listbox1.sorted but I only get it to work as alphabetic. 我已经尝试过将listbox1.sorted用作东西,但是我只能让它按字母顺序工作。

If you know what I'm doing wrong with nr.1 or know how to fix a sorting algorithm please let me know. 如果您知道我在使用nr.1时出错了,或者知道如何解决排序算法,请告诉我。

 int count = 0;
 int tal = 0;
 double Mtal = 999999999999999999;
 bool hit;
 int count1 = 0;
 private void button26_Click(object sender, EventArgs e)
    {
        while (count < 100)
        {
            foreach (var Listboxitem in listBox1.Items)
            {
                hit = false;
                if (Convert.ToDouble(Listboxitem) < Mtal)
                {

                    Mtal = Convert.ToDouble(Listboxitem);
                    hit = true;
                }
                count = count + 1;
                if (hit)
                {
                    count1 = count;
                }
            }
        }
        this.listBox1.SelectedIndex = count1 - 1;

    }

Your values in ListBox are Integers. 您在ListBox中的值为Integers。 So change the declaration of your variable Mtal from double to int . 因此,将变量Mtal的声明从double更改为int And then, instead of Convert.ToDouble() use int.Parse() , because you need integers for comparing to existing min value. 然后,使用int.Parse()代替Convert.ToDouble() ,因为您需要整数来与现有的最小值进行比较。 Something like this should work for you: 这样的事情应该为您工作:

 int count = 0;
 int tal = 0;
 int Mtal = int.MaxValue;
 bool hit;
 int count1 = 0;
 private void button26_Click(object sender, EventArgs e)
 {
     while (count < 100)
     {
          foreach (var Listboxitem in listBox1.Items)
          {
             hit = false;
             if (int.Parse(Listboxitem.ToString()) < Mtal)
             {

                 Mtal = int.Parse(Listboxitem.ToString());
                 hit = true;
             }
             count = count + 1;
             if (hit)
             {
                count1 = count;
             }
        }
    }
    this.listBox1.SelectedIndex = count1 - 1;

 }

And then for Ordering I would suggest you to use LINQ on List<ListItem> . 然后对于订购,我建议您在List<ListItem>上使用LINQ。 For your example: 例如:

private void button_OrderByDescencing_Click(object sender, EventArgs e)
 {
        List<ListItem> items= new List<ListItem>();
        foreach (ListItem a in lb.Items)
        {
            items.Add(a);
        }
        items=items.OrderByDescending(a => int.Parse(a.Value)).ToList();
        foreach (ListItem a in items)
        {
             listBox1.Items.Add(a);
        }

 }

And for ascending: 对于升序:

 private void button_OrderByAscending_Click(object sender, EventArgs e)
 {
        List<ListItem> items= new List<ListItem>();
        foreach (ListItem a in lb.Items)
        {
            items.Add(a);
        }
        items= items.OrderBy(a => int.Parse(a.Value)).ToList();
        foreach (ListItem a in items)
        {
             listBox1.Items.Add(a);
        }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM