Facebook
From Kinga, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 320
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _02TemperatureConverter
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.            
  14.             string zakoncz = "Zakończ";
  15.  
  16.             Console.WriteLine("Konwerter Temperatur");
  17.             Console.WriteLine("Temperatury podaje się w następujących stalach, po liczbie stopni zapisz odpowiednia literę skali:");
  18.             Console.WriteLine("Skala Celsjusza - C");
  19.             Console.WriteLine("Skala Fahrenheita - F");
  20.             Console.WriteLine("Skala Kelvina - K");
  21.             Console.WriteLine("Żeby zakończyć program wpisz: Zakończ");
  22.  
  23.             bool work = true;
  24.  
  25.             while (work)
  26.             {
  27.  
  28.                 string temperature = Console.ReadLine();
  29.                 if (temperature.Length==0)
  30.                 {
  31.                     Console.WriteLine("Wpisz temperature.");
  32.                     continue;
  33.                 }
  34.                
  35.                 char scale = temperature.Last();
  36.  
  37.                 double temperatureValue = 0;
  38.                 Double.TryParse(temperature.Remove(temperature.IndexOf(scale), 1),out temperatureValue);
  39.  
  40.                 if(temperature.Equals(zakoncz))
  41.                 {
  42.                     work = false;
  43.                 }
  44.                 else if (scale == 'C')
  45.                 {
  46.                     double inKelvin = CToK(temperatureValue);
  47.                     double inFarenh = CToF(temperatureValue);
  48.                     Console.WriteLine("Temperatura wynosi:" + String.Format("{0:N2}",inKelvin) + " K");
  49.                     Console.WriteLine("Temperatura wynosi:" + String.Format("{0:N2}",inFarenh) + " F");
  50.                 }
  51.                 else if (scale == 'F')
  52.                 {
  53.                     double inKelvins = FToK(temperatureValue);
  54.                     double inCelcius = FToC(temperatureValue);
  55.                     Console.WriteLine("Temperatura wynosi:" + String.Format("{0:N2}",inKelvins) + " K");
  56.                     Console.WriteLine("Temperatura wynosi:" + String.Format("{0:N2}",inCelcius) + " C");
  57.  
  58.                 }
  59.                 else if (scale == 'K')
  60.                 {
  61.                     double inFarenh = KToF(temperatureValue);
  62.                     double inCelciu = KToC(temperatureValue);
  63.                     Console.WriteLine("Temperatura wynosi:" + String.Format("{0:N2}",inFarenh) + " F");
  64.                     Console.WriteLine("Temperatura wynosi:" + String.Format("{0:N2}",inCelciu) + " C");
  65.  
  66.                 }
  67.                 else
  68.                 {
  69.                     Console.WriteLine("Zły sposób zapisu. Spróbuj jeszcze raz.");
  70.                 }
  71.             }
  72.            
  73.             Console.WriteLine("Program zostanie zakończony");
  74.             Console.ReadKey();
  75.  
  76.         }
  77.  
  78.         public static double CToK(double celc)
  79.         {
  80.             return celc + 273.15;
  81.         }
  82.    
  83.         public static double CToF(double celc)
  84.         {
  85.             return (9 / 5.0) * celc + 32;
  86.         }
  87.  
  88.         public static double KToC(double kelv)
  89.         {
  90.             return kelv - 273.15;
  91.         }
  92.  
  93.         public static double KToF(double kelv)
  94.         {
  95.             return kelv * (9 / 5.0) - 459.67;
  96.         }
  97.        
  98.         public static double FToC(double farenh)
  99.         {
  100.             return (5 / 9.0) * (farenh - 32);
  101.         }
  102.  
  103.         public static double FToK(double farenh)
  104.         {
  105.             return (farenh + 459.67) * (5.0 / 9.0);
  106.         }
  107.        
  108.     }
  109. }