Share via

How to count correctly

MiPakTeh 1,476 Reputation points
2023-04-28T13:41:54.4166667+00:00

In this code min count correct but max count is wrong.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bat_2
{
    public partial class Form1 : Form
    {
        List<string> Data1 = new List<string>();
        List<string> Data2 = new List<string>();

        List<ItemInfo> Items = new List<ItemInfo>();

        public static List<string> list = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //040792,19920506,0019,1124,0592,0950,2479,7139,3114,4609,7836,8981,4465,6114,5301,5311,1949,1606,3775,6226,1271,7455,7227,9258,0407
            //040792,19920506,0019,1124,0592,0950,2479,7139,3114,4609,7836,8981,4465,6114,5301,5311,1949,1606,3775,6226,1271,7455,7227,9258,0407
            //040992,19920510,4162,5766,9514,9309,9344,1573,5077,1037,6498,8325,7203,4754,7849,6908,4809,0464,1932,8240,2725,6294,9991,5707,6754
            //040992,19920510,4162,5766,9514,9309,9344,1573,5077,1037,6498,8325,7203,4754,7849,6908,4809,0464,1932,8240,2725,6294,9991,5707,6754
            //041192,19920514,5371,5472,3489,6872,2455,0600,4590,7261,3384,8670,7869,5664,4100,1683,6817,4710,6375,4702,9716,6082,9674,8899,7760
            //041192,19920514,5371,5472,3489,6872,2455,0600,4590,7261,3384,8670,7869,5664,4100,1683,6817,4710,6375,4702,9716,6082,9674,8899,7760
            foreach (var s in File.ReadLines(@"D:\Ori.txt")
    .Skip(1)
    .Select(s => s.Split(new[] { ',' }, 3)[2]))
            {
                listBox1.Items.Add(s);
                Data1.Add(s);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var item in Data1)
            {
                string t = item.Replace(",", "").Trim();
                Data2.Add(t);
            }
            foreach (var item in Data2)
            {
                listBox2.Items.Add(item);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (var item in Data2)
            {
                var ItemInfos = new List<ItemInfo>();

                int min = int.MaxValue;
                int max = int.MinValue;

                for (int j = 0; j <= 9; j++)
                {
                    String Number_ = j.ToString();
                    int NumberCount_ = item.ToCharArray().Count(c => c.ToString() == Number_);
                    ItemInfos.Add(new ItemInfo() { Index = Convert.ToInt32(Number_.ToString()), Value = NumberCount_ });
                    listBox3.Items.Add(j.ToString() + "-" + NumberCount_);
                }

                foreach (ItemInfo ITM in ItemInfos)
                {
                    if (ITM.Value < min)
                    {
                        min = ITM.Value;
                    }

                    if (ITM.Value > max)
                    {
                        max = ITM.Value;
                    }
                }

                string result = string.Empty;
                int Min_count = 0;

                foreach (ItemInfo ITM in ItemInfos)
                {
                    if (ITM.Value == min)
                    {
                        result += "[Min = " + min.ToString() + "=>" + "Min Item = " + ITM.Index.ToString() + "]" + "   ";
                        Min_count += min.ToString().Count();
                    }
                }
                if (result != string.Empty)
                    listBox3.Items.Add(result);

                listBox3.Items.Add("Count_Min=" + Min_count);

                string result_ = string.Empty;
                int Max_count = 0;

                foreach (ItemInfo ITM in ItemInfos)
                {
                    if (ITM.Value == max)
                    {
                        result_ += "[Max = " + max.ToString() + "=>" + "Max Item = " + ITM.Index.ToString() + "]" + "   ";
                        Max_count += max.ToString().Count();
                    }
                }
                if (result_ != string.Empty)
                    listBox3.Items.Add(result_);
                listBox3.Items.Add("Count_Max=" + Max_count);
                listBox3.Items.Add("========================");

            }

        }

    }
}
public partial class ItemInfo
{
    public int Index;
    public int Value;

    public override string ToString()
    {
        return Index.ToString() + " => " + Value.ToString();
    }
}


Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2023-04-28T15:33:45.1366667+00:00

Try to replace Max_count += max.ToString().Count() with ++Max_count.

A similar modification can be made for Min_count.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.