Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.            
  15.             string plik = "karetki.txt";
  16.             StreamReader r = new StreamReader(plik);
  17.             int n = int.Parse(r.ReadLine());
  18.             string dl=r.ReadLine();
  19.             string[] dlugosc = dl.Split(';');
  20.             int x = dlugosc[1].Length;
  21.             r.Close();
  22.            
  23.             string[] identyfikatory= new string[n];
  24.             int[] stanPocz= new int[x];
  25.             int[] stanKoniec= new int[x];
  26.             WczytajDane(plik,identyfikatory,  stanPocz,  stanKoniec);
  27.             int[] dystans= new int[n];
  28.            
  29.  
  30.              dystans=ObliczDystans(stanPocz,stanKoniec);
  31.              Console.Write("\n");
  32.              for (int i = 0; i < dystans.Length; i++)
  33.              {
  34.                  Console.WriteLine("{0} - {1} = {2}",stanKoniec[i],stanPocz[i],dystans[i]);
  35.              }
  36.  
  37.              int srDystans=ObliczSredniDystans(dystans);
  38.              Console.WriteLine("Średni dystans to: {0}",srDystans);
  39.              string maxDystans = ZnajdzKaretke(identyfikatory, dystans);
  40.              Console.WriteLine("Najwiekszy dzienny dystans pokonała karetka o identyfikatorze: {0}", maxDystans);
  41.              string karetka = WyswielInformacje(maxDystans, identyfikatory, stanPocz, stanKoniec, dystans);
  42.              Console.ReadKey();
  43.  
  44.  
  45.  
  46.  
  47.  
  48.         }static void WczytajDane(string plik, string[] identyfikatory, int[] stanPocz,  int[] stanKoniec)
  49.         {
  50.             StreamReader re = new StreamReader(plik);
  51.             int a = int.Parse(re.ReadLine());
  52.             int ind = 0;
  53.             int pocz = 1;
  54.             int kon = 2;
  55.             for (int i = 0; i < a; i++)
  56.             {
  57.                 string nap = re.ReadLine();
  58.                 string[] all = nap.Split(';');
  59.                
  60.                
  61.                    
  62.                         identyfikatory[i] = all[ind];
  63.                         Console.WriteLine(identyfikatory[i]);
  64.  
  65.                         stanPocz[i] = int.Parse(all[pocz]);
  66.                         Console.WriteLine(stanPocz[i]);
  67.  
  68.                         stanKoniec[i] = int.Parse(all[kon]);
  69.                         Console.WriteLine(stanKoniec[i]);
  70.                    
  71.                
  72.  
  73.                
  74.             }
  75.  
  76.                 re.Close();
  77.                
  78.        
  79.         }
  80.  
  81.         static int[] ObliczDystans(int[] stanPocz, int[] stanKoniec)
  82.         {
  83.             int[] tab = new int[stanPocz.Length];
  84.             for (int i = 0; i < stanPocz.Length; i++)
  85.             {
  86.                
  87.                 tab[i] = stanKoniec[i]  - stanPocz[i];                
  88.             }
  89.             return tab;
  90.         }
  91.  
  92.         static int ObliczSredniDystans(int[] dystans)
  93.         {
  94.             int suma = 0;
  95.             for (int i = 0; i < dystans.Length; i++)
  96.             {
  97.                 suma += dystans[i];
  98.             }
  99.             int wynik = suma / (dystans.Length);
  100.             return wynik;
  101.         }
  102.  
  103.         static string ZnajdzKaretke(string [] identyfikatory, int[] dystans)
  104.         {
  105.             int Licznik = 0;
  106.             int max = dystans[0];
  107.             for (int i = 0; i < dystans.Length;i++)
  108.             {
  109.                 if (max < dystans[i])
  110.                 {
  111.                     max = dystans[i];
  112.                     Licznik = i;
  113.                 }
  114.             }
  115.             return identyfikatory[Licznik];
  116.         }
  117.  
  118.         static string WyswielInformacje(string maxDystans,string[] identyfikatory, int[] stanPocz, int[] stanKoniec, int[] dystans)
  119.         {
  120.             int Licznik = 0;
  121.             int max = dystans[0];
  122.             for (int i = 0; i < dystans.Length; i++)
  123.             {
  124.                 if (max < dystans[i])
  125.                 {
  126.                     max = dystans[i];
  127.                     Licznik = i;
  128.                 }
  129.             }
  130.             string karetka = identyfikatory[Licznik] + " " + stanKoniec[Licznik] + " - " + stanPocz[Licznik] + " Dystans: " + max;
  131.             return karetka;
  132.         }
  133.  
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement