Autor Beitrag
Christoph1972
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Fr 03.01.14 23:03 
Hallo zusammen,

Ich habe gerade eine recht langweilige Fleißarbeit hinter mir und möchte diese hier veröffentlichen damit sie dem einen oder anderen vielleicht erspart bleibt. Es ist zwar ehr Copy & Paste als ein Tutorial, aber ich denke hier passt es am Besten.

Hier die Klasse die ein Element abbildet:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
public class Element
{
    public int Number { get; set; }
    public string ShortCut { get; set; }
    public string Name { get; set; }
    public double RelativeAtomicWeight { get; set; }
    
    public Element(int number, string shortCut, string name, double relativeAtomicWeight)
    {
        Number = number;
        ShortCut = shortCut;
        Name = name;
        RelativeAtomicWeight = relativeAtomicWeight;
    }
}

Ich habe den Konstruktor bewusst eingesetzt......


Hier die Klasse die eine Liste mit den Elementen zurückgibt:
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
public class ElementList : List<Element>
{
    public ElementList()
    {
        Add(new Element(1"H""Waserstoff"1.0079));
        Add(new Element(2"He""Helium"4.0026));
        Add(new Element(3"Li""Lithium"6.941));
        Add(new Element(4"Be""Beryllium"9.0120));
        Add(new Element(5"B""Bor"10.811));
        Add(new Element(6"C""Kohlenstoff"12.011));
        Add(new Element(7"N""Stickstoff"14.007));
        Add(new Element(8"O""Sauerstoff"15.999));
        Add(new Element(9"F""Fluor"18.998));
        Add(new Element(10"Ne""Neon"20.18));
        Add(new Element(11"Na""Natrium"22.990));
        Add(new Element(12"Mg""Magnesium"24.305));
        Add(new Element(13"Al""Aluminum"26.982));
        Add(new Element(14"Si""Silizium"28.0855));
        Add(new Element(15"P""Phosphor"30.974));
        Add(new Element(16"S""Schwefel"32.065));
        Add(new Element(17"Cl""Chlor"35.453));
        Add(new Element(18"Ar""Argon"39.948));
        Add(new Element(19"K""Kalium"39.098));
        Add(new Element(20"Ca""Calcium"40.078));
        Add(new Element(21"Sc""Scandium"44.956));
        Add(new Element(22"Ti""Titan"47.867));
        Add(new Element(23"V""Vanadium"50.942));
        Add(new Element(24"Cr""Chrom"51.996));
        Add(new Element(25"Mn""Mangan"54.938));
        Add(new Element(26"Fe""Eisen"55.845));
        Add(new Element(27"Co""Kobalt"58.933));
        Add(new Element(28"Ni""Nickel"58.693));
        Add(new Element(29"Cu""Kupfer"63.546));
        Add(new Element(30"Zn""Zink"65.409));
        Add(new Element(31"Ga""Gallium"69.723));
        Add(new Element(32"Ge""Germanium"72.64));
        Add(new Element(33"As""Arsen"74.922));
        Add(new Element(34"Se""Selen"78.96));
        Add(new Element(35"Br""Brom"79.904));
        Add(new Element(36"Kr""Krypton"83.798));
        Add(new Element(37"Rb""Rubidium"85.468));
        Add(new Element(38"Sr""Strontium"87.62));
        Add(new Element(39"Y""Yttrium"88.906));
        Add(new Element(40"Zr""Zircon"91.224));
        Add(new Element(41"Nb""Niob"92.906));
        Add(new Element(42"Mo""Molybden"95.94));
        Add(new Element(43"Tc""Technetium"98.0));
        Add(new Element(44"Ru""Ruthenium"101.07));
        Add(new Element(45"Rh""Rhodium"102.91));
        Add(new Element(46"Pd""Palladium"106.42));
        Add(new Element(47"Ag""Silber"107.87));
        Add(new Element(48"Cd""Kadmium"112.41));
        Add(new Element(49"In""Indium"114.82));
        Add(new Element(50"Sn""Zinn"118.71));
        Add(new Element(51"Sb""Antimon"121.76));
        Add(new Element(52"Te""Tellur"127.60));
        Add(new Element(53"I""Iod"126.90));
        Add(new Element(54"Xe""Xenon"131.29));
        Add(new Element(55"Cs""Cäsium"132.91));
        Add(new Element(56"Ba""Barium"137.33));
        Add(new Element(57"La""Lanthaniden"138.91));
        Add(new Element(58"Ce""Cerium"140.12));
        Add(new Element(59"Pr""Praseodym"140.91));
        Add(new Element(60"Nd""Neodym"144.24));
        Add(new Element(61"Pm""Promethium"145.0));
        Add(new Element(62"Sm""Samarium"150.36));
        Add(new Element(63"Eu""Europium"151.96));
        Add(new Element(64"Gd""Gadolinium"157.25));
        Add(new Element(65"Tb""Terbium"158.93));
        Add(new Element(66"Dy""Dysprosium"162.50));
        Add(new Element(67"Ho""Holmium"164.93));
        Add(new Element(68"Er""Erbium"167.26));
        Add(new Element(69"Tm""Thulium"168.93));
        Add(new Element(70"Yb""Ytterbium"173.04));
        Add(new Element(71"Lu""Lutetium"174.97));
        Add(new Element(72"Hf""Hafnium"178.49));
        Add(new Element(73"Ta""Tantal"180.95));
        Add(new Element(74"W""Wolfram"183.84));
        Add(new Element(75"Re""Rhenium"186.21));
        Add(new Element(76"Os""Osmium"190.23));
        Add(new Element(77"Ir""Iridium"192.22));
        Add(new Element(78"Pt""Platin"195.08));
        Add(new Element(79"Au""Gold"196.97));
        Add(new Element(80"Hg""Quecksilber"200.59));
        Add(new Element(81"Tl""Thallium"204.38));
        Add(new Element(82"Pb""Blei"207.2));
        Add(new Element(83"Bi""Bismut"208.98));
        Add(new Element(84"Po""Polonium"209.0));
        Add(new Element(85"At""Astat"210.0));
        Add(new Element(86"Rn""Radon"222.0));
        Add(new Element(87"Fr""Francium"233.0));
        Add(new Element(88"Ra""Radium"226.0));
        Add(new Element(89"Ac""Actinium"227.0));
        Add(new Element(90"Th""Thorium"232.04));
        Add(new Element(91"Pa""Protactinium"231.04));
        Add(new Element(92"U""Uran"238.03));
        Add(new Element(93"Np""Neptunium"237.0));
        Add(new Element(94"Pu""Plutonium"244.0));
        Add(new Element(95"Am""Americium"243.0));
        Add(new Element(96"Cm""Curium"247.0));
        Add(new Element(97"Bk""Berkelium"247.0));
        Add(new Element(98"Cf""Californium"251.0));
        Add(new Element(99"Es""Einsteinium"252.0));
        Add(new Element(100"Fm""Fermium"257.0));
        Add(new Element(101"Md""Mendelevium"258.0));
        Add(new Element(102"No""Nobelium"259.0));
        Add(new Element(103"Lr""Lawrencium"262.0));
        Add(new Element(104"Rf""Rutherfordium"267.0));
        Add(new Element(105"Db""Dubnium"268.0));
        Add(new Element(106"Sg""Seaborgium"271.0));
        Add(new Element(107"Bh""Bohrium"272.0));
        Add(new Element(108"Hs""Hassium"277.0));
        Add(new Element(109"Mt""Meitnerium"276.0));
        Add(new Element(110"Ds""Darmstadtium"281.0));
        Add(new Element(111"Rg""Roentgenium"280.0));
    }
}

Ich denke jetzt sollte klar sein warum ich auf "Auto Initialize" der Klasse verzichtet habe :D


Und hier eine Klasse die eine Methode bereitstellt die den vollen Name eines Elements zurück gibt:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
public class PeriodicElements
{
    public static string GetFullElementName(string shortCut)
    {
        ElementList peList = new ElementList();
        string shortC = shortCut.ToLower();

        var t = from el in peList
                where el.ShortCut.ToLower() == shortC
                select el.Name;

        string name = shortCut;

        if (t.FirstOrDefault() != null)
            name = t.First();
        
        return name;
    }
}

Es sollte wohl kein Problem sein die Atommasse aus der Liste zu fischen ;-)


Hier ein Beispiel wie man den vollen Namen eins Elements erhält:
ausblenden C#-Quelltext
1:
2:
sting fullName = PeriodicElements.GetFullElementName("AL");
//fullName = Aluminium


Wer Fehler findet, diese bitte hier posten!!

Viel Spass damit!

_________________
Gruß
Christoph
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 03.01.14 23:25 
Hallo!

Wäre es nicht sinnvoll, die Setter der Properties private zu machen?

Viele Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 03.01.14 23:29 
Und die Liste gleich statisch? Die ständig neu zu erzeugen ist jetzt nicht wirklich schonend.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Fr 03.01.14 23:32 
Ja, in meinem Periodensystem habe ich dafür gleich ganz statische Arrays genutzt. Ebenso für die Elementpositionen usw. und für die Grafiken habe ich eine externe Datendatei benutzt.

Bei einer so kleinen Liste ist das aber eigentlich egal, das geht ja so oder so sehr schnell.
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Sa 04.01.14 00:45 
Ich würde die Methode GetFullElementName anders schreiben:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
public static string GetFullElementName(string shortCut)
{
    ElementList peList = new ElementList();
    string shortC = shortCut.ToLower();

    var element = peList.FirstOrDefault(el => el.ShortCut.ToLower() == shortC);

    return element == null ? shortCut : element.Name;
}


Die LINQ-Querries sehen zwar toll aus (meine Meinung), aber manchmal sind die Erweiterungsmethoden sinnvoller, finde ich ^^