public FormResistorHelper()
        {
            InitializeComponent();

            if (File.Exists("autosave.txt"))
            {
                listBoxResistors.Items.AddRange(File.ReadLines("autosave.txt").Select(Resistor.FromTsv).ToArray());
                Resistor.ResetSearchCache();
            }
        }
 /// <summary>
 /// Enter-Taste, wenn ein neuer Widerstandwert hinzugefügt werden soll
 /// </summary>
 void textBox1_KeyPress(object sender, KeyPressEventArgs e)
 {
     if (e.KeyChar == '\r')
     {
         var p = Resistor.Parse(textBoxValue.Text);
         if (p != null)
         {
             p.ident           = textBoxIdent.Text;
             textBoxIdent.Text = Increment(textBoxIdent.Text);
             int insertPos = 0;
             while (insertPos < listBoxResistors.Items.Count && ((Resistor)listBoxResistors.Items[insertPos]).valueMilliOhm < p.valueMilliOhm)
             {
                 insertPos++;
             }
             listBoxResistors.Items.Insert(insertPos, p);
             AutoSave(listBoxResistors.Items.Cast <Resistor>().ToArray());
         }
     }
 }
        /// <summary>
        /// Änderung der Sucheingabe
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            var val = Resistor.Parse(textBoxSearch.Text);

            if (val == null)
            {
                return;
            }

            int searchType = radioButtonSearchType1.Checked ? 1 : radioButtonSearchType2.Checked ? 2 : radioButtonSearchType3.Checked ? 3 : radioButtonSearchType4.Checked ? 4 : radioButtonSearchType5.Checked ? 5 : radioButtonSearchType6.Checked ? 6 : 0;

            if (searchType == 0)
            {
                throw new ArgumentException("searchType");
            }

            const int TargetLimit = 1000;

            var time = Stopwatch.StartNew();

            ResistorResult[] results = null;
            foreach (double searchLevel in new[] { 1.0001, 1.0002, 1.0005, 1.001, 1.002, 1.005, 1.01, 1.02, 1.05, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 9999999.9 })
            {
                results = Resistor.Search(listBoxResistors.Items.Cast <Resistor>().ToArray(), val, searchType, searchLevel).Take(TargetLimit).ToArray();
                if (results.Length >= TargetLimit / 2)
                {
                    break;                            // mindestens die Hälfte an Ergebnissen erreicht?
                }
            }

            Array.Sort(results, (x, y) => x.errorMilliOhm.CompareTo(y.errorMilliOhm));

            time.Stop();
            Text = "Search-Time: " + time.ElapsedMilliseconds.ToString("N0", CultureInfo.InvariantCulture) + " ms";

            listBoxSearchResults.BeginUpdate();
            listBoxSearchResults.Items.Clear();
            listBoxSearchResults.Items.AddRange(results);
            listBoxSearchResults.EndUpdate();
        }
Example #4
0
        /// <summary>
        /// Suchmethode, um passende Widerstände bzw. deren Kombinationen zu finden
        /// </summary>
        /// <param name="allResistors">Alle Widerstände, welche zur Verfügung stehen</param>
        /// <param name="searchValue">gesuchter Widerstandswert</param>
        /// <param name="maxResistors">optional: gibt die maximale Anzahl der kombinierbaren Widerstände an, welche verwendet werden dürfen (default: 2)</param>
        /// <param name="maxError">optional: maximale Abweichung des Ergebnisses (default: 1.10 = 10 % Abweichung)</param>
        /// <returns>Enumerable der gefundenen Ergebnisse</returns>
        public static IEnumerable <ResistorResult> Search(Resistor[] allResistors, Resistor searchValue, int maxResistors = 2, double maxError = 1.10)
        {
            if (allResistors.Length < 2)
            {
                yield break;
            }

            long search = searchValue.valueMilliOhm;
            long max    = (long)(search * maxError) - search;

            foreach (var r in allResistors)
            {
                long err = Math.Abs(r.valueMilliOhm - search);
                if (err <= max)
                {
                    yield return(new ResistorResult(r, err));
                }
            }

            var fullList    = GetAllDoubles(allResistors);
            var fullRevList = GetAllRevDoubles(allResistors);

            if (maxResistors >= 2) // 2-teilige Suche
            {
                int p = BinarySearch(fullList, search - max);

                while (p < fullList.Length && fullList[p].Key - search < max)
                {
                    yield return(new ResistorResult(fullList[p].Value, Math.Abs(search - fullList[p].Key)));

                    p++;
                }
            }

            if (maxResistors >= 3)
            {
                // --- 3er Kombination - seriell ---
                for (int z = 0; z < allResistors.Length - 2; z++)
                {
                    for (int y = z + 1; y < allResistors.Length - 1; y++)
                    {
                        for (int x = y + 1; x < allResistors.Length; x++)
                        {
                            long err = Math.Abs(allResistors[x].valueMilliOhm + allResistors[y].valueMilliOhm + allResistors[z].valueMilliOhm - search);
                            if (err <= max)
                            {
                                yield return(new ResistorResult(new ResistorCombined(true, allResistors[x], allResistors[y], allResistors[z]), err));
                            }
                        }
                    }
                }

                // --- 3er Kombination - parallel ---
                for (int z = 0; z < allResistors.Length - 2; z++)
                {
                    for (int y = z + 1; y < allResistors.Length - 1; y++)
                    {
                        for (int x = y + 1; x < allResistors.Length; x++)
                        {
                            var  c   = new ResistorCombined(false, allResistors[x], allResistors[y], allResistors[z]);
                            long err = Math.Abs(c.valueMilliOhm - search);
                            if (err <= max)
                            {
                                yield return(new ResistorResult(c, err));
                            }
                        }
                    }
                }
            }
        }
Example #5
0
 /// <summary>
 /// Konstruktor
 /// </summary>
 /// <param name="resultResistor">Widerstand oder eine Kombination, welcher als Ergebnis gefunden wurde</param>
 /// <param name="errorMilliOhm">Fehler-Abstand in milli-Ohm</param>
 public ResistorResult(Resistor resultResistor, long errorMilliOhm)
 {
     this.resultResistor = resultResistor;
     resultMilliOhm      = resultResistor.valueMilliOhm;
     this.errorMilliOhm  = errorMilliOhm;
 }
 /// <summary>
 /// speichert automatisch alle Widerstände
 /// </summary>
 /// <param name="resistors">Widerstände, welche gespeichert werden sollen</param>
 static void AutoSave(Resistor[] resistors)
 {
     File.WriteAllLines("autosave.txt", resistors.Select(x => x.ToTsv()));
     Resistor.ResetSearchCache();
 }