Advertisement
zarkoboyadjiev

C# Metrics Converter

Sep 27th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace metricsConverter
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             double value = double.Parse( Console.ReadLine() );
  12.             string startMetric = Console.ReadLine().ToLower();
  13.             string finalMetric = Console.ReadLine().ToLower();
  14.             //end of reading data;
  15.  
  16.             Dictionary<string, double> metrics = new Dictionary<string, double>();
  17.                 metrics.Add("m", 1);    
  18.                 metrics.Add("mm", 0.001);
  19.                 metrics.Add("cm", 0.01);
  20.                 metrics.Add("mi", 1609.344);
  21.                 metrics.Add("in", 0.0254);
  22.                 metrics.Add("km", 1000);
  23.                 metrics.Add("ft", 0.3048);
  24.                 metrics.Add("yd", 0.9144);
  25.             //end of adding metrics
  26.  
  27.             double startValue = 0;
  28.             double finalValue = 0;
  29.             //creater value for final calculations
  30.  
  31.             foreach (KeyValuePair<string, double> entry in metrics)
  32.                 {
  33.                 //keys and values from metrics dictionary are stored in entry
  34.                     if(startMetric == entry.Key)
  35.                     {
  36.                         startValue = entry.Value;
  37.                     }
  38.  
  39.                     if(finalMetric == entry.Key)
  40.                     {
  41.                         finalValue = entry.Value;
  42.                     }                  
  43.                 }
  44.             double result = (value * startValue) /
  45.             Console.WriteLine("{0:f8}", result);
  46.             //format result to 8-th digit
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement