Facebook
From kaja, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 269
  1. #define daneTestowe
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Program
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int n, ile2 = 0, ile5 = 0, j = 0, iluGorszych = 0;
  15.             double sr, sk = 0, suma2 = 0, war;
  16.             string[] tabNazwisk;
  17.             double[] tabOcen;
  18.  
  19.             Console.WriteLine("Program liczy średnią ocen");
  20.             //dane testowe
  21. #if (daneTestowe)
  22.             tabOcen = new double[] { 2.0, 4.5, 5.0, 2.0, 3.5, 4.5, 2.0, 4.0, 5.0, 3.0 };
  23.             tabNazwisk = new string[] { "Stachowiak", "Kot", "Kabat", "Dolata", "Krajewska", "Pawłowska", "Lis", "Krawczyk", "Nowak", "Berkut" };
  24.             n = tabNazwisk.Length;
  25.             if (n != tabOcen.Length)
  26.                 Console.WriteLine("źle!");
  27. #endif
  28.             //dane testowe
  29.  
  30.             //dane z konsoli
  31.  
  32. #if (!daneTestowe)
  33.             Console.WriteLine("Wpisz ile uczniów:");
  34.             while (!int.TryParse(Console.ReadLine(), out n) && n < 1)
  35.                 Console.WriteLine("Błędna wartość podaj jeszcze raz:");
  36.  
  37.             tabOcen = new double[n];
  38.             tabNazwisk= new string[n];
  39. #endif
  40.             double suma = 0;
  41.  
  42.             #region
  43.             for (int i = 1; i <= n; i++)
  44.             {
  45.                 //dane z konsoli
  46. #if (!daneTestowe)
  47.     Console.Write("Podaj nazwisko: ");
  48.                 tabNazwisk[i - 1] = Console.ReadLine();
  49.  
  50.                 Console.WriteLine("Podaj ocene ucznia");
  51.                 double oc;
  52.                 Console.Write("Ocena: ", i);
  53.                 while (!double.TryParse(Console.ReadLine(), out oc) ||
  54.                    //oc < 2 || oc > 5)
  55.                    // /* jest źle*/  (oc !=2 && oc !=3 && oc !=3.5 && oc !=4 && oc !=4.5 && oc !=5)
  56.                    /*jest dobrze*/ !(oc == 2 || oc == 3 || oc == 3.5 || oc == 4 || oc == 4.5 || oc ==5)
  57.                        )
  58.                        Console.WriteLine("Błędna wartość, podaj jeszcze raz: ");
  59.                 tabOcen[i - 1] = oc;
  60. #endif
  61.                 //dane testowe
  62. #if (daneTestowe)
  63.                 double oc = tabOcen[i - 1];
  64.  
  65. #endif
  66.                 //koniec
  67.                 suma = suma + oc;
  68.                 suma2 = (oc * oc) + suma2;
  69.                 if (oc == 2) ile2++;
  70.                 if (oc == 5) ile5++;
  71.                 if (oc != 5 && oc != 2)
  72.                     sk = oc + sk;
  73.                 j++;
  74.  
  75.             }
  76.             #endregion
  77.             sr = suma / n;
  78.             foreach (var oc in tabOcen)
  79.                 if (oc < sr)
  80.                     iluGorszych++;
  81.  
  82.             war = (suma2 / n) - (sr * sr);
  83.  
  84.  
  85.             Console.WriteLine("Średnia ocen to: {0}, a wariancja= {1:F1}", sr, war);
  86.             Console.WriteLine(" Ilość dwójek to {0}, ilość piątek to {1}, a ocen poniżej średniej: {2}", ile2, ile5, iluGorszych);
  87.             if (j != 0)
  88.                 Console.WriteLine("Średnia srednich to: {0:F2}", sk / j);
  89.             //poczatek formatowania
  90.             string formatUO = " |{0,2}|{1,-22}|{2,5:F1}| ";
  91.             string naglUO = string.Format(formatUO, "Nr", "Nazwisko i imię", "Ocena");
  92.             string kreskaUO = new string('=', naglUO.Length);
  93.             /*
  94.              for ( int a=0; a<naglUO.Length/2; a++)
  95.              Console.WriteLine("/");
  96.               */
  97.             //koniec
  98.  
  99.             Console.WriteLine("n *** Lista z oceniami ndst *** ");
  100.             Console.WriteLine(naglUO + "n" + kreskaUO);
  101.                 for (int i = 0; i < n; i++)
  102.                 if (tabOcen[i] == 2)
  103.                     Console.WriteLine(" |{0,2}|{1,-22}|{2,5:F1}| ", i+1, tabNazwisk[i], tabOcen[i]);
  104.             Console.WriteLine(kreskaUO);
  105.  
  106.  
  107.             Console.WriteLine("n *** Lista z oceniami bdb *** ");
  108.             Console.WriteLine(naglUO + "n" + kreskaUO);
  109.             for (int i = 0; i < n; i++)
  110.                 if (tabOcen[i] == 5)
  111.                     Console.WriteLine(" |{0,2}|{1,-22}|{2,5:F1}| ", i + 1, tabNazwisk[i], tabOcen[i]);
  112.             Console.WriteLine(kreskaUO);
  113.  
  114.  
  115.             Console.WriteLine("n *** Lista z oceniami poniżej sredniej *** ");
  116.             Console.WriteLine(naglUO + "n" + kreskaUO);
  117.             for (int i = 0; i < n; i++)
  118.                 if (tabOcen[i] < sr)
  119.             Console.WriteLine(" |{0,2}|{1,-22}|{2,5:F1}| ", i + 1, tabNazwisk[i], tabOcen[i]);
  120.             Console.WriteLine(kreskaUO, "n");
  121.  
  122.  
  123.             Console.WriteLine(" Wciśnij dowolny klawisz...");
  124.             Console.ReadKey();
  125.  
  126.         }
  127.     }
  128. }
  129.