Jak w c# przekonwertować typ generyczny na int?

0

Ktoś wie? Np.

 
Klasa_Generyczna<t,d> gen = new Klasa_Generyczna<t,d>();

I teraz drugi parametr w metodzie pierwszej jest typu t. Jednak w momencie wywołania tej metody chcę użyć parametru typu int. Czy to się da w ogóle?

0

Bezpośrednio (t to instancja generyka):

(int)(object)t

Trochę naokoło:

int.Parse(t.ToString())

Pisane bez uruchamiania.

0

A nie lepiej zrobić po prostu osobny szablon dla typu int jako parametr dla klasy Generic<int,T>??

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

public class Program {
    class Generic<T> where T : struct {
        public Generic() {
            if (typeof(T).Equals(typeof(Int32))) Console.WriteLine("Int32");
        }
    }

    public static void Main() {
        var test = new Generic<float>();
    }
}

Jak sobie zamienisz w main float na int to zobaczysz, że typ jest rozpoznawalny.

0

To jest mój kod:

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

namespace Bum
{
    public partial class Form1 : Form
    {
        public static int i,j;

        public static int dolna_gr;

        public static int gorna_gr;
        public int iloscw_int;
        public static string duplikat;
        public static int porownanie = 0;
        public static int wartosc_elementu_int;
        //public static int wartosc_elementu;
        
        
       DrzewoBinarne<int> drzewo;
        public static int szukana_wartosc_int;
        Random element_losowy = new Random();
        public Form1()
        {
            InitializeComponent();
        }
        
        public class WezelDrzewa<BT>  where BT:struct,IComparable<BT>
        {
            public BT Dane { get; set; }
            public int wartosc { get; set; }
            public bs_WezelDrzewa<BT> Lewa_Galaz{get;set;}
            public bs_WezelDrzewa<BT> Prawa_Galaz { get; set; }
            
            public bs_WezelDrzewa() { }
            public bs_WezelDrzewa(int dane)
            {
                
                if (typeof(BT).Equals(typeof(Int32)))
                {
                    Dane = dane;
                }

            }
            
            
            
           
        }
        public class DrzewoBinarne<BT> where BT : struct,IComparable<BT>
        {
            private WezelDrzewa<BT> korzen_drzewa { get; set; }
            public bs_DrzewoBinarne()
            {
               korzen_drzewa = null;
            }
            public DrzewoBinarne(BT bs_wartosc_korzenia)
            {
                korzen_drzewa = utworz_wezel(wartosc_korzenia);
                
            }
            
            private static WezelDrzewa<BT> utworz_wezel(BT dane)
            {
                WezelDrzewa<BT> wezel = new WezelDrzewa<BT>();
                
                wezel.bs_Dane =dane;
                return wezel;
            }
            public void insert(BT dane)
            {
                WezelDrzewa<BT> bierzacy = korzen_drzewa;
                WezelDrzewa<BT> bs_nastepny =korzen_drzewa;
                
               
                
                while (!(bierzacy == null))
                {
                    nastepny=bierzacy;
                    porownanie = bierzacy.bs_Dane.CompareTo(dane);
                    bierzacy = bs_porownanie < 0 ? bierzacy.Prawa_Galaz : bierzacy.bs_Lewa_Galaz;

                   
                }

                if (porownanie == 0)
                {
                    duplikat = "Wystapil duplikat, ktory nie bedzie dodany";
                }
                else 
                if (porownanie < 0)
                    {
                        nastepny.Prawa_Galaz = utworz_wezel(dane);
                    }
                    else
                    {
                        nastepny.Lewa_Galaz = utworz_wezel(dane);
                    }

                
            }
            private string wypisz_drzewo_infix(WezelDrzewa<BT> wezel)
            {
                string wynik = "";
                if (wezel != null)
                {
                    wynik += wypisz_drzewo_infix(wezel.Lewa_Galaz);
                    
                    wynik += Convert.ToString(wezel.Dane);
                    
                    
                    wynik += wypisz_drzewo_infix(wezel.Prawa_Galaz);
                    

                }
                return bs_wynik;
            }
            public string wypisz_drzewo_binarne_infixowo()
            {
                string wynik = "";
                bs_wynik += wypisz_drzewo_infix(korzen_drzewa);
                return wynik;
            }
            private string wypisz_drzewo_pref(WezelDrzewa<BT> wezel)
            {
                string bs_wynik = "";
                if ((wezel != null))
                {
                    wynik += Convert.ToString(wezel.Dane);
                    wynik += wypisz_drzewo_pref(wezel.Lewa_Galaz);
                    

                    
                   wynik += wypisz_drzewo_pref(wezel.Prawa_Galaz);
                }
                return bs_wynik;
            
            }
            public string wypisz_drzewo_prefixowo()
            {
                string wynik = "";
                wynik += wypisz_drzewo_pref(korzen_drzewa);
                return wynik;
            }
            private string wypisz_drzewo_postfix(WezelDrzewa<BT> wezel)
            {
                string wynik = "";
                if ((wezel != null))
                {
                    wynik += wypisz_drzewo_postfix(wezel.Lewa_Galaz);
                    
                    wynik += wypisz_drzewo_postfix(wezel.Prawa_Galaz);
                    wynik += Convert.ToString(wezel.bs_Dane);
                }
                return wynik;
            }
            public string wypisz_drzewo_postfixowo()
            {
                string wynik = "";
                wynik += wypisz_drzewo_postfix(korzen_drzewa);
                return wynik;
            }
            
            private string odszukaj_element( WezelDrzewa<BT> wezel, BT szukana)
            {
                
                string bs_czy_znaleziono = "";
               
                       
                while (wezel!=null)
                {
                    int porownanie = wezel.Dane.CompareTo(szukana);
                    
                    if (porownanie==0)
                    {
                        czy_znaleziono = "Znaleziono taki element: " + wezel.Dane.ToString();
                    }
                    else if (porownanie<0)
                    {
                        
                        czy_znaleziono = odszukaj_element(wezel.bs_Lewa_Galaz,szukana);
                        
                    }
                    else 
                    {
                        czy_znaleziono = odszukaj_element(wezel.Prawa_Galaz,szukana);
                       
                    }
                    
                }
                return czy_znaleziono;
               
            }
            
            public string wypisz_komunikat()
            {
               
                int otrzymana;
                string czy_znaleziono = "";

              
                
                    czy_znaleziono = odszukaj_element(korzen_drzewa,wartosc_elementu_int);
                
                
                return czy_znaleziono;
            }
           
            

        }

        private void utworz_drzewo_binarne_Click(object sender, EventArgs e)
        {
            dolna_gr = Convert.ToInt32dolna_granica_txb.Text);
            gorna_gr = Convert.ToInt32(gorna_granica_tbx.Text);
            iloscw_int = Convert.ToInt32(ilosc_wezlow_tbx.Text);
            drzewo = new bs_DrzewoBinarne<int>(iloscw_int);
            for (i = 0; i <= 10; i++)
            {
                j = element_losowy.Next(dolna_gr, gorna_gr);
                drzewo.insert(bs_j);

            }

        }

        private void wypisz_infixowo_Click(object sender, EventArgs e)
        {
            wynik_drzewa.Text = drzewo.wypisz_drzewo_binarne_infixowo();
        }

        private void wypisz_drzewo_prefixowo_Click(object sender, EventArgs e)
        {
            wynik_drzewa.Text = drzewo.wypisz_drzewo_prefixowo();
        }

        private void drzewo_postfixowo_Click(object sender, EventArgs e)
        {
            wynik_drzewa.Text =drzewo.wypisz_drzewo_postfixowo();
        }

        private void odszukaj_element_Click(object sender, EventArgs e)
        {
            szukana_wartosc_int = Convert.ToInt32(szukana_wartosc.Text);
            
            znaleziona_wartosc.Text = drzewo.wypisz_komunikat();
        }
        
        
    }
}

0

Jak chcesz pisać BST to robisz to zupełnie od nie tej strony. Napisz generyka BST i jego używaj w formie.

0
grzesiek51114 napisał(a):

Jak chcesz pisać BST to robisz to zupełnie od nie tej strony. Napisz generyka BST i jego używaj w formie.

Wydawało mi się, że to jest właśnie generyk bst:

 public class DrzewoBinarne<BT> where BT : struct,IComparable<BT>
        {
            private WezelDrzewa<BT> korzen_drzewa { get; set; }
            public bs_DrzewoBinarne()
            {
               korzen_drzewa = null;
            }
            public DrzewoBinarne(BT bs_wartosc_korzenia)
            {
                korzen_drzewa = utworz_wezel(wartosc_korzenia);
 
            }
 
            private static WezelDrzewa<BT> utworz_wezel(BT dane)
            {
                WezelDrzewa<BT> wezel = new WezelDrzewa<BT>();
 
                wezel.bs_Dane =dane;
                return wezel;
            }
}

 
2

Wiesz... jak już piszesz drzewo to pisz to w całości generycznie np:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Containers {
    class BinarySearchTree<V, K> {
        private class Element {
            public V Value { get; set; }
            public K Key { get; set; }
            public Element Left { get; set; }
            public Element Right { get; set; }

            public Element(V value, K key) {
                this.Value = value;
                this.Key = key;
                this.Left = this.Right = null;
            }
        }

        private Element _root;

        public BinarySearchTree() {
            this._root = null;
        }

        public void Add(V value, K key, IComparer<K> comparer) {
            var element = new Element(value, key);
            if (this._root != null) {
                var temp = this._root;
                while (temp != null) {
                    if(comparer.Compare(temp.Key,element.Key) > 0) {
                        if (temp.Left != null) temp = temp.Left;
                        else {
                            temp.Left = element;
                            break;
                        }
                    }
                    else if (comparer.Compare(temp.Key, element.Key) < 0) {
                        if (temp.Right != null) temp = temp.Right;
                        else {
                            temp.Right = element;
                            break;
                        }
                    }
                }
            }
            else this._root = element;
        }
    }
}

Później dodając element musisz zrobić sobie klasę, która pomoże Ci porównywać jakoś klucze żeby ustalić czy nowy element ma być prawy czy lewy:

    class Person {
        public string Name { get; set; }
    }

    class PersonNameComparer : IComparer<string> {
        public int Compare(string x, string y) {
            if (x.Length > y.Length) return 1;
            else if (x.Length < y.Length) return -1;
            return 0;
        }
    }

Dodajesz w ten sposób:

            var bst = new BinarySearchTree<Person, string>();
            var first = new Person { Name = "Grzegorz" };
            var second = new Person { Name = "Kamil" };
            var third = new Person { Name = "Magdalena" };
            var fourth = new Person { Name = "Tom" };
            bst.Add(first, first.Name, new PersonNameComparer());
            bst.Add(second, second.Name, new PersonNameComparer());
            bst.Add(third, third.Name, new PersonNameComparer());
            bst.Add(fourth, fourth.Name, new PersonNameComparer());

To drzewo segreguje obiekty klasy Person na podstawie długości imion.

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