C# Windows窗体计算器算法。排序和最高/最低数字

C# Windows窗体计算器算法。排序和最高/最低数字,c#,algorithm,sorting,C#,Algorithm,Sorting,我目前正在使用计算器,但我的两个算法工作不正常 在我的历史记录(列表框)中,我从计算器中获取数字,我有一个底部,可以找到最小的数字。我的代码出错了 我希望有一个[排序]底部,对数字进行升序或降序排序。我尝试过listbox1.sorted,但我只能按字母顺序进行排序 如果你知道我在nr.1上做错了什么,或者知道如何修复排序算法,请让我知道 int count = 0; int tal = 0; double Mtal = 999999999999999999; bool hit; in

我目前正在使用计算器,但我的两个算法工作不正常

  • 在我的历史记录(列表框)中,我从计算器中获取数字,我有一个底部,可以找到最小的数字。我的代码出错了

  • 我希望有一个[排序]底部,对数字进行升序或降序排序。我尝试过listbox1.sorted,但我只能按字母顺序进行排序

  • 如果你知道我在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;
    
        }
    
    int count=0;
    int-tal=0;
    双Mtal=99999999999;
    布尔击中;
    int count1=0;
    私有无效按钮26_单击(对象发送者,事件参数e)
    {
    同时(计数<100)
    {
    foreach(listBox1.Items中的变量Listboxitem)
    {
    命中=错误;
    if(Convert.ToDouble(Listboxitem)
    列表框中的值是整数。因此,将变量
    Mtal
    的声明从
    double
    更改为
    int
    。然后,不要使用
    Convert.ToDouble()
    而是使用
    int.Parse()
    ,因为需要整数与现有的最小值进行比较。 像这样的东西应该适合你:

     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;
    
     }
    
    int count=0;
    int-tal=0;
    int Mtal=int.MaxValue;
    布尔击中;
    int count1=0;
    私有无效按钮26_单击(对象发送者,事件参数e)
    {
    同时(计数<100)
    {
    foreach(listBox1.Items中的变量Listboxitem)
    {
    命中=错误;
    if(int.Parse(Listboxitem.ToString())
    然后为了订购,我建议您在
    列表中使用LINQ。例如:

    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);
            }
    
     }
    
    private void按钮\u OrderByDescencing\u单击(对象发送方,事件参数e)
    {
    列表项=新列表();
    foreach(在磅项目中列出项目a)
    {
    增加(a)项;
    }
    items=items.OrderByDescending(a=>int.Parse(a.Value)).ToList();
    foreach(在项目中列出项目a)
    {
    列表框1.项目。添加(a);
    }
    }
    
    对于提升:

     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);
            }
    
     }
    
    private void按钮\u orderby升序\u单击(对象发送者,事件参数e)
    {
    列表项=新列表();
    foreach(在磅项目中列出项目a)
    {
    增加(a)项;
    }
    items=items.OrderBy(a=>int.Parse(a.Value)).ToList();
    foreach(在项目中列出项目a)
    {
    列表框1.项目。添加(a);
    }
    }
    
    列表框中的值是整数。因此,将变量
    Mtal
    的声明从
    double
    更改为
    int
    。然后,不要使用
    Convert.ToDouble()
    而是使用
    int.Parse()
    ,因为需要整数与现有的最小值进行比较。 像这样的东西应该适合你:

     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;
    
     }
    
    int count=0;
    int-tal=0;
    int Mtal=int.MaxValue;
    布尔击中;
    int count1=0;
    私有无效按钮26_单击(对象发送者,事件参数e)
    {
    同时(计数<100)
    {
    foreach(listBox1.Items中的变量Listboxitem)
    {
    命中=错误;
    if(int.Parse(Listboxitem.ToString())
    然后为了订购,我建议您在
    列表中使用LINQ。例如:

    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);
            }
    
     }
    
    private void按钮\u OrderByDescencing\u单击(对象发送方,事件参数e)
    {
    列表项=新列表();
    foreach(在磅项目中列出项目a)
    {
    增加(a)项;
    }
    items=items.OrderByDescending(a=>int.Parse(a.Value)).ToList();
    foreach(在项目中列出项目a)
    {
    列表框1.项目。添加(a);
    }
    }
    
    对于提升:

     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);
            }
    
     }
    
    private void按钮\u orderby升序\u单击(对象发送者,事件参数e)
    {
    列表项=新列表();
    foreach(在磅项目中列出项目a)
    {
    增加(a)项;
    }
    items=items.OrderBy(a=>int.Parse(a.Value)).ToList();
    foreach(在项目中列出项目a)
    {
    列表框1.项目。添加(a);
    }
    }
    
    无法将类型“double”隐式转换为“int”。存在显式转换(是否缺少强制转换?是我得到的错误btw@EmilSjödin运行您在此处发布的代码,我无法重现您的问题,因此..无法将类型“double”隐式转换为“int”。存在显式转换(你错过演员阵容了吗?是我犯的错误吗btw@EmilSjödin运行您在此处发布的代码,我无法重现您的问题,因此..谢谢您的快速回答@Tadej 1我从您代码中得到的问题是它想要从对象转换为字符串。(参数1:无法从“对象”转换为“字符串”)@EmilSjödin看到了我所做的编辑。现在这两个问题都应该解决了。谢谢你的快速回答@Tadej 1。我从你的代码中得到的问题是,它想要从对象转换为字符串。(参数1:无法从“对象”转换为“字符串”)@EmilSjödin看到了我所做的编辑。现在这两个问题都应该解决了。