Facebook
From kaja, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 257
  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. #endif
  65.                 //koniec
  66.                 suma = suma + oc;
  67.                 suma2 = (oc * oc)+suma2;
  68.                 if (oc == 2) ile2++;
  69.                 if (oc == 5) ile5++;
  70.                 if (oc != 5 && oc != 2)
  71.                    sk = oc + sk;
  72.                 j++;
  73.  
  74.                 }
  75. #endregion
  76.             sr = suma / n;
  77.            foreach(var oc in tabOcen)
  78.                 if (oc < sr)
  79.                 iluGorszych++;
  80.  
  81.             war = (suma2 / n) - (sr * sr);
  82.  
  83.                    
  84.             Console.WriteLine("Średnia ocen to: {0}, a wariancja= {1:F1}", sr, war);
  85.             Console.WriteLine(" Ilość dwójek to {0}, ilość piątek to {1}, a ocen poniżej średniej: {2}", ile2, ile5, iluGorszych);
  86.             if (j != 0)
  87.             Console.WriteLine("Średnia srednich to: {0:F2}", sk/j);
  88.        
  89.  
  90.             Console.WriteLine("\n *** Lista z oceniami ndst *** ");
  91.             for (int i = 0; i < n; i++)
  92.                 if (tabOcen[i]==2)
  93.                 Console.WriteLine(" nazwisko: {0} ocena: {1} ", tabNazwisk[i], tabOcen[i]);
  94.            
  95.             Console.WriteLine("\n *** Lista z oceniami bdb *** ");
  96.             for (int i = 0; i < n; i++)
  97.                 if (tabOcen[i] == 5)
  98.                     Console.WriteLine(" nazwisko: {0} ocena: {1} ", tabNazwisk[i], tabOcen[i]);
  99.  
  100.  
  101.             Console.WriteLine("\n *** Lista z oceniami poniżej sredniej *** ");
  102.             for (int i = 0; i < n; i++)
  103.                 if (tabOcen[i] < sr)
  104.                     Console.WriteLine(" nazwisko: {0} ocena: {1} ", tabNazwisk[i], tabOcen[i]);
  105.  
  106.  
  107.             Console.WriteLine("Wciśnij dowolny klawisz...");
  108.             Console.ReadKey();
  109.          
  110.         }
  111.     }
  112. }
  113.