Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace MetricConverter
- {
- class Program
- {
- static void Main(string[] args)
- {
- double distance = double.Parse(Console.ReadLine());
- string inputMetric = Console.ReadLine();
- string outputMetric = Console.ReadLine();
- if (inputMetric == "m")
- {
- distance /= 1000;
- }
- else if (inputMetric == "cm")
- {
- distance /= 100000;
- }
- else if (inputMetric == "mm")
- {
- distance /= 1000000;
- }
- else if (inputMetric == "mi")
- {
- distance /= 0.621371192;
- }
- else if (inputMetric == "in")
- {
- distance /= 39370.0787;
- }
- else if (inputMetric == "ft")
- {
- distance /= 3280.8399;
- }
- else if (inputMetric == "yd")
- {
- distance /= 1093.6133;
- }
- if (outputMetric == "m")
- {
- distance *= 1000;
- }
- else if (outputMetric == "cm")
- {
- distance *= 100000;
- }
- else if (outputMetric == "mm")
- {
- distance *= 1000000;
- }
- else if (outputMetric == "mi")
- {
- distance *= 0.621371192;
- }
- else if (outputMetric == "in")
- {
- distance *= 39370.0787;
- }
- else if (outputMetric == "ft")
- {
- distance *= 3280.8399;
- }
- else if (outputMetric == "yd")
- {
- distance *= 1093.6133;
- }
- Console.WriteLine(distance);
- }
- }
- }
- Решение с тернарен оператор:
- using System;
- namespace MetricConverter
- {
- class Program
- {
- static void Main(string[] args)
- {
- double distance = double.Parse(Console.ReadLine());
- string inputMetric = Console.ReadLine();
- string outputMetric = Console.ReadLine();
- distance /=
- inputMetric == "m" ? 1000 :
- inputMetric == "cm" ? 100000 :
- inputMetric == "mm" ? 1000000 :
- inputMetric == "mi" ? 0.621371192 :
- inputMetric == "in" ? 39370.0787 :
- inputMetric == "ft" ? 3280.8399 :
- inputMetric == "yd" ? 1093.6133 : 1;
- distance *=
- outputMetric == "m" ? 1000 :
- outputMetric == "cm" ? 100000 :
- outputMetric == "mm" ? 1000000 :
- outputMetric == "mi" ? 0.621371192 :
- outputMetric == "in" ? 39370.0787 :
- outputMetric == "ft" ? 3280.8399 :
- outputMetric == "yd" ? 1093.6133 : 1;
- Console.WriteLine(distance);
- }
- }
- }
- Решение с речник:
- using System;
- using System.Collections.Generic;
- namespace MetricConverter
- {
- class Program
- {
- static void Main(string[] args)
- {
- double distance = double.Parse(Console.ReadLine());
- string inputMetric = Console.ReadLine();
- string outputMetric = Console.ReadLine();
- var metricsToKM = new Dictionary<string, double>() {
- { "km", 1 },
- { "m", 1000 },
- { "cm", 100000 },
- { "mm", 1000000 },
- { "mi", 0.621371192 },
- { "in", 39370.0787 },
- { "ft", 3280.8399 },
- { "yd", 1093.6133 }
- };
- Console.WriteLine(distance / metricsToKM[inputMetric] * metricsToKM[outputMetric]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement