Advertisement
Spocoman

Metric Converter(Simple Conditions)

Oct 8th, 2023 (edited)
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     double distance;
  7.     cin >> distance;
  8.     cin.ignore();
  9.  
  10.     string inputMetric, outputMetric;
  11.     cin >> inputMetric >> outputMetric;
  12.  
  13.     if (inputMetric == "m") {
  14.         distance /= 1000;
  15.     }
  16.     else if (inputMetric == "cm") {
  17.         distance /= 100000;
  18.     }
  19.     else if (inputMetric == "mm") {
  20.         distance /= 1000000;
  21.     }
  22.     else if (inputMetric == "mi") {
  23.         distance /= 0.621371192;
  24.     }
  25.     else if (inputMetric == "in") {
  26.         distance /= 39370.0787;
  27.     }
  28.     else if (inputMetric == "ft") {
  29.         distance /= 3280.8399;
  30.     }
  31.     else if (inputMetric == "yd") {
  32.         distance /= 1093.6133;
  33.     }
  34.  
  35.     if (outputMetric == "m") {
  36.         distance *= 1000;
  37.     }
  38.     else if (outputMetric == "cm") {
  39.         distance *= 100000;
  40.     }
  41.     else if (outputMetric == "mm") {
  42.         distance *= 1000000;
  43.     }
  44.     else if (outputMetric == "mi") {
  45.         distance *= 0.621371192;
  46.     }
  47.     else if (outputMetric == "in") {
  48.         distance *= 39370.0787;
  49.     }
  50.     else if (outputMetric == "ft") {
  51.         distance *= 3280.8399;
  52.     }
  53.     else if (outputMetric == "yd") {
  54.         distance *= 1093.6133;
  55.     }
  56.  
  57.     cout << distance << endl;
  58.     return 0;
  59. }
  60.  
  61. Решение с тернарен оператор:
  62.  
  63. #include <iostream>
  64.  
  65. using namespace std;
  66.  
  67. int main() {
  68.     double distance;
  69.     cin >> distance;
  70.     cin.ignore();
  71.  
  72.     string inputMetric, outputMetric;
  73.     cin >> inputMetric >> outputMetric;
  74.  
  75.     distance /=
  76.         inputMetric == "m" ? 1000 :
  77.         inputMetric == "cm" ? 100000 :
  78.         inputMetric == "mm" ? 1000000 :
  79.         inputMetric == "mi" ? 0.621371192 :
  80.         inputMetric == "in" ? 39370.0787 :
  81.         inputMetric == "ft" ? 3280.8399 :
  82.         inputMetric == "yd" ? 1093.6133 : 1;
  83.  
  84.     distance *=
  85.         outputMetric == "m" ? 1000 :
  86.         outputMetric == "cm" ? 100000 :
  87.         outputMetric == "mm" ? 1000000 :
  88.         outputMetric == "mi" ? 0.621371192 :
  89.         outputMetric == "in" ? 39370.0787 :
  90.         outputMetric == "ft" ? 3280.8399 :
  91.         outputMetric == "yd" ? 1093.6133 : 1;
  92.  
  93.     cout << distance << endl;
  94.     return 0;
  95. }
  96.  
  97. Решение с колекция:
  98.  
  99. #include <iostream>
  100. #include <map>
  101.  
  102. using namespace std;
  103.  
  104. int main() {
  105.     double distance;
  106.     cin >> distance;
  107.     cin.ignore();
  108.  
  109.     string inputMetric, outputMetric;
  110.     cin >> inputMetric >> outputMetric;
  111.  
  112.     map <string, double> metricsToKM = {
  113.         {"km", 1},
  114.         {"m", 1000},
  115.         {"cm", 100000},
  116.         {"mm", 1000000},
  117.         {"mi", 0.621371192},
  118.         {"in", 39370.0787},
  119.         {"ft", 3280.8399},
  120.         {"yd", 1093.6133}
  121.     };
  122.  
  123.     cout << distance / metricsToKM[inputMetric] * metricsToKM[outputMetric] << endl;
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement