using System; using System.Windows.Forms; namespace _1112Sitzzuteilung { public partial class Sitzzuteilung : Form { public Sitzzuteilung() { InitializeComponent(); } private void cmdBerechnung_Click(object sender, EventArgs e) { double Max; int MaxIndex; double[] Stimmen = new double[5]; double[] Urstimme = new double[5]; lblSitze.Text = ""; lblPartei1.Text = ""; lblPartei2.Text = ""; lblPartei3.Text = ""; lblPartei4.Text = ""; lblPartei5.Text = ""; try { Stimmen[0] = Urstimme[0] = Convert.ToDouble(textBox1.Text); } catch (FormatException) { lblPartei1.Text = "fehlerhafte Eingabe bei Partei 1"; } try { Stimmen[1] = Urstimme[1] = Convert.ToDouble(textBox2.Text); } catch (FormatException) { lblPartei2.Text = "fehlerhafte Eingabe bei Partei 2"; } try { Stimmen[2] = Urstimme[2] = Convert.ToDouble(textBox3.Text); } catch (FormatException) { lblPartei3.Text = "fehlerhafte Eingabe bei Partei 3"; } try { Stimmen[3] = Urstimme[3] = Convert.ToDouble(textBox4.Text); } catch (FormatException) { lblPartei4.Text = "fehlerhafte Eingabe bei Partei 4"; } try { Stimmen[4] = Urstimme[4] = Convert.ToDouble(textBox5.Text); } catch (FormatException) { lblPartei5.Text = "fehlerhafte Eingabe bei Partei 5"; } int[] Sitze = new int[5]; int Parlament=0; try { Parlament = Convert.ToInt32(txtSitze.Text); } catch(FormatException) { lblSitze.Text = "Geben sie die Anzahl der insgesamt zu vergebenden Sitze ein!"; } Sitze[0] = Sitze[1] = Sitze[2] = Sitze[3] = Sitze[4] = 0; for(int j =0; jMax) { Max = Stimmen[i]; MaxIndex = i; } } Sitze[MaxIndex]++; Stimmen[MaxIndex] = Urstimme[MaxIndex] / (Sitze[MaxIndex] + 1); } lbl1h.Text = ""+Sitze[0]; lbl2h.Text = ""+Sitze[1]; lbl3h.Text = ""+Sitze[2]; lbl4h.Text = ""+Sitze[3]; lbl5h.Text = ""+Sitze[4]; ///////// Idealanspruch double Gesamtstimmen = 0; for (int k = 0; k < 5; k++) Gesamtstimmen = Gesamtstimmen + Urstimme[k]; double[] Ideal = new double[5]; for (int i = 0; i < 5; i++) Ideal[i] = Urstimme[i] / Gesamtstimmen * Parlament; lbl1i.Text = "" + Ideal[0]; lbl2i.Text = "" + Ideal[1]; lbl3i.Text = "" + Ideal[2]; lbl4i.Text = "" + Ideal[3]; lbl5i.Text = "" + Ideal[4]; ///////// Hare/Niemeyer int[] PNr = new int[5]; //Parteinummer for (int k = 0; k < 5; k++) PNr[k] = k; int Fehlen = 0; double Floorsumme = 0; double Merk = 0; int Merkp = 0; // Merken der Parteinummer // Ermitteln der Gesamtstimmenzahl for (int k = 0; k < 5; k++) Floorsumme = Floorsumme + Math.Floor(Ideal[k]); Fehlen = Parlament - (int)Floorsumme; double[] Rest = new double[5]; for (int i = 0; i < 5; i++) Rest[i] = Ideal[i] - Math.Floor(Ideal[i]); // Bubble-Sort-Algorithmus für Rest[] for(int k=4; k>0; k--) { for (int i = 4; i > 4 - k; i--) { if (Rest[i] > Rest[i - 1]) { Merk = Rest[i - 1]; Rest[i - 1] = Rest[i]; Rest[i] = Merk; Merkp = PNr[i - 1]; PNr[i - 1] = PNr[i]; PNr[i] = Merkp; } } } int[] SitzeHN = new int[5]; for (int k=0; k<5; k++) SitzeHN[k] = (int)Math.Floor(Ideal[k]); for (int k = 0; k < Fehlen; k++) SitzeHN[PNr[k]]++; lbl1hn.Text = "" + SitzeHN[0]; lbl2hn.Text = "" + SitzeHN[1]; lbl3hn.Text = "" + SitzeHN[2]; lbl4hn.Text = "" + SitzeHN[3]; lbl5hn.Text = "" + SitzeHN[4]; /////////////// Sainte-Lague ////////////////// double[] SitzeSL = new double[5]; for (int j = 0; j < 5; j++) Stimmen[j] = 2 * Urstimme[j]; for (int j = 0; j < Parlament; j++) { Max = Stimmen[0]; MaxIndex = 0; for (int i = 1; i < 5; i++) { if (Stimmen[i] > Max) { Max = Stimmen[i]; MaxIndex = i; } } SitzeSL[MaxIndex]++; Stimmen[MaxIndex] = Urstimme[MaxIndex] / (SitzeSL[MaxIndex] + 0.5); } lbl1sl.Text = "" + SitzeSL[0]; lbl2sl.Text = "" + SitzeSL[1]; lbl3sl.Text = "" + SitzeSL[2]; lbl4sl.Text = "" + SitzeSL[3]; lbl5sl.Text = "" + SitzeSL[4]; } private void Sitzzuteilung_Load(object sender, EventArgs e) { } } }