zliczanie wystąpień wszystkich elementów tablicy

0

aplikacja ma za zadanie pobrać wartości atrybutów StahlLaenge i StahlNr z pliku xml i zliczyć ile razy każda para powtarza się. Mam problem ze zliczeniem wszystkich możliwości.
kod w tej postaci:

   //ZLICZANIE

                int suma = 0;
                for (int a = 0; a < ile; ++a)
                {
                    for (int b = 1; b < ile; ++b)
                    {
                        if ((tablica[a, 1] == tablica[b, 1]) && (tablica[a, 0] == tablica[b, 0])) ++suma;
      
                    }
                    textBox1.Text = tablica[a, 0] + "   " + tablica[a, 1] + " - " + suma + " szt";
                    textBox1.Text += "\r\n";
                    suma = 0;

                } 

wyświetla mi tylko jedna wartość. Co trzeba zrobić w textboxie wyświetlić wszystkie możliwości bez powtarzania ?

Cały kod :

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


namespace liczek
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.ShowDialog();
            dialog.Filter = "XML Files(*.xml) | *.xml";
            if (dialog.FileName != "")
            {

                textBox1.Clear();
                string fileName = dialog.FileName;

                XmlDocument XmlDoc = new XmlDocument();
                XmlDoc.Load(fileName);
                int ile = XmlDoc.GetElementsByTagName("Teiledaten").Count;
                label1.Text = ile + " patyków do naszykowania :-D";


                XPathDocument doc = new XPathDocument(fileName);
                XPathNavigator nav = doc.CreateNavigator();


                XPathExpression stl;
                stl = nav.Compile("//@StahlLaenge");
                XPathNodeIterator StahlLaenge = nav.Select(stl);


                XPathExpression stnr;
                stnr = nav.Compile("//@StahlNr");
                XPathNodeIterator StahlNr = nav.Select(stnr);

                int[,] tablica = new int[ile, 2];

                int i = 0;
                int j = 0;


                //wczytywanie dl stali do tablicy
                try
                {
                    while (StahlLaenge.MoveNext())
                    {

                        XPathNavigator nav1 = StahlLaenge.Current.Clone();
                        int nav3 = Int32.Parse(nav1.Value);
                        tablica[i, 1] = nav3;
                        ++i;
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


                //wczytywanie nr stali do tablicy
                try
                {

                    while (StahlNr.MoveNext())
                    {

                        XPathNavigator nav2 = StahlNr.Current.Clone();
                        int nav4 = Int32.Parse(nav2.Value);
                        tablica[j, 0] = nav4;
                        ++j;
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                //Sortowanie

                Boolean zm = true;
                while (zm == true)
                {
                    zm = false;
                    for (int a = 0; a < ile - 1; a++)
                    {
                        if (tablica[a, 1] > tablica[a + 1, 1])
                        {
                            int zmiana1 = tablica[a, 1];
                            int zmiana2 = tablica[a, 0];
                            tablica[a, 1] = tablica[a + 1, 1];
                            tablica[a, 0] = tablica[a + 1, 0];
                            tablica[a + 1, 1] = zmiana1;
                            tablica[a + 1, 0] = zmiana2;
                            zm = true;
                        }
                    }


                }

                //ZLICZANIE

                int suma = 0;
                for (int a = 0; a < ile; ++a)
                {
                    for (int b = 1; b < ile; ++b)
                    {
                        if ((tablica[a, 1] == tablica[b, 1]) && (tablica[a, 0] == tablica[b, 0])) ++suma;
      
                    }
                    textBox1.Text = tablica[a, 0] + "   " + tablica[a, 1] + " - " + suma + " szt";
                    textBox1.Text += "\r\n";
                    suma = 0;

                }
            }
        }
    }

} 

przykład jednej wartości z pliku xml:

<?xml version="1.0" encoding="UTF-8"?>
<Datei Name="SSOFT_20_01_A_14.xml">
  <OptiZuschnittdaten>
    <Optidaten Name="SSOFT_20_01_A_14">
      <Stabdaten StabNr="1" Rohlaenge="6500" Laufbahn="1" Bezeichnung="103.341 B/Złoty dąb" Kommentar="103.341 B/Złoty dąb" ProfilName="1033411K" Farbe="103.341 B/Złoty dąb" Restelaenge="3570" Restekennung="1" ResteTeileNr="0" StahlName="113294175">
        <Teiledaten TeileNr="1" WagenNr="3" FachNr="13" Bezeichnung="GO" Kommision="OBJ. 27749/5" Position="2" Laenge="469" AnGehrung="45" AbGehrung="45" Stahlanwahl="1" StahlLaenge="300" StahlNr="113294175" StahlNummer="113294175" Stahleinschubtiefe="84.5">
          <Werkzeugdaten TeileNr="1">
            <ProfilBearb BNr="1" XPos="127.0"/>
            <ProfilBearb BNr="1" XPos="342.0"/>
            <ProfilBearb BNr="20" XPos="234.5"/>
            <ProfilBearb BNr="251" XPos="104.5"/>
            <ProfilBearb BNr="251" XPos="234.5"/>
            <ProfilBearb BNr="251" XPos="364.5"/>
          </Werkzeugdaten>
        </Teiledaten> 
2

Utwórz sobie listę obiektów klasy, która będzie przechowywała wartości, które chcesz policzyć. Wczytaj do takiej listy wszystkie wartości, które potrzebujesz z pliku, następnie licz powtórzenia za pomocą słownika:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Program {
    class Stahl  {
        public int Laenge { get; set; }
        public int Nr { get; set; }
    };

    class StahlComparer : IComparer<Stahl> {

        public int Compare(Stahl x, Stahl y) {
            if (x.Nr > y.Nr) return 1;
            else if (x.Nr < y.Nr) return -1;
            return 0;
        }
    };

    class Program {
        static void Main(string[] args) {
            var stalhs = new List<Stahl>() {
                new Stahl{ Laenge = 36, Nr = 8 },
                new Stahl{ Laenge = 36, Nr = 12 },
                new Stahl{ Laenge = 36, Nr = 14 },
                new Stahl{ Laenge = 30, Nr = 12 },
                new Stahl{ Laenge = 30, Nr = 4 },
                new Stahl{ Laenge = 36, Nr = 12 },
                new Stahl{ Laenge = 30, Nr = 12 },
                new Stahl{ Laenge = 30, Nr = 12 }
            };

            stalhs.Sort(new StahlComparer());
            Console.WriteLine("Sorted List: ");
            foreach (var stahl in stalhs) Console.WriteLine("{0}, {1}", stahl.Laenge, stahl.Nr);

            var counts = stalhs.GroupBy(obj => new { obj.Laenge, obj.Nr }).ToDictionary(obj => obj.Key, obj => obj.Count());

            Console.WriteLine("\nOccurences: ");
            foreach (var count in counts) Console.WriteLine("{0}, {1}", count.Key, count.Value);
        }
    }
}

Poza tym takie rzeczy można bardzo prosto posortować. Nie trzeba robić takich kombinacji jak u Ciebie (patrz wyżej).

https://ideone.com/3gObyG

1 użytkowników online, w tym zalogowanych: 0, gości: 1